Skip to content

Commit

Permalink
server: Skip unneeded networks
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Oct 24, 2024
1 parent 5e76dd9 commit 4306296
Showing 1 changed file with 74 additions and 92 deletions.
166 changes: 74 additions & 92 deletions server/pkg/api/rest/controller/getRefined.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,58 +131,24 @@ func doGetRefinedInfraInfo(connID string) (*inframodel.ServerProperty, error) {
return &refinedInfraInfo, nil
}

// GetInfraInfoRefined godoc
//
// @ID get-infra-info-refined
// @Summary Get Refined Infra Information
// @Description Get the refined infra information of the connection information.
// @Tags [Get] Get refined source info
// @Accept json
// @Produce json
// @Param sgId path string true "ID of the source group."
// @Param connId path string true "ID of the connection info."
// @Success 200 {object} inframodel.OnpremiseInfraModel "Successfully get refined information of the infra."
// @Failure 400 {object} common.ErrorResponse "Sent bad request."
// @Failure 500 {object} common.ErrorResponse "Failed to get refined information of the infra."
// @Router /source_group/{sgId}/connection_info/{connId}/infra/refined [get]
func GetInfraInfoRefined(c echo.Context) error {
sgID := c.Param("sgId")
if sgID == "" {
return common.ReturnErrorMsg(c, "Please provide the sgId.")
}

connID := c.Param("connId")
if connID == "" {
return common.ReturnErrorMsg(c, "Please provide the connId.")
}

_, err := dao.SourceGroupGet(sgID)
if err != nil {
return common.ReturnErrorMsg(c, err.Error())
}

refinedInfraInfo, err := doGetRefinedInfraInfo(connID)
if err != nil {
return common.ReturnErrorMsg(c, err.Error())
}

var onpremiseInfraModel inframodel.OnpremiseInfraModel
var onpremiseInfra inframodel.OnpremInfra
var networkProperty inframodel.NetworkProperty

onpremiseInfra.Servers = append(onpremiseInfra.Servers, *refinedInfraInfo)

for _, iface := range refinedInfraInfo.Interfaces {
func doGetRefinedNetworkInfo(networkProperty *inframodel.NetworkProperty, ifaces *[]inframodel.NetworkInterfaceProperty) {
for _, iface := range *ifaces {
// append IPv4 networks
for _, ipv4cidr := range iface.IPv4CidrBlocks {
_, netNetwork, err := net.ParseCIDR(ipv4cidr)
if err != nil {
continue
}

var found bool
networkCidr := netNetwork.String()

// Skip local networks
if networkCidr == "127.0.0.0/8" {
continue
}

var found bool

for _, cidr := range networkProperty.IPv4Networks {
if cidr == networkCidr {
found = true
Expand All @@ -202,9 +168,30 @@ func GetInfraInfoRefined(c echo.Context) error {
continue
}

var found bool
networkCidr := netNetwork.String()

// Skip local networks
if networkCidr == "::1/128" {
continue
}

// Skip all nodes multicast
if strings.HasPrefix(networkCidr, "ff02::1/") {
continue
}

// Skip all routers multicast
if strings.HasPrefix(networkCidr, "ff02::2/") {
continue
}

// Skip unspecified
if networkCidr == "::/128" {
continue
}

var found bool

for _, cidr := range networkProperty.IPv6Networks {
if cidr == networkCidr {
found = true
Expand All @@ -217,8 +204,49 @@ func GetInfraInfoRefined(c echo.Context) error {
}
}
}
}

// GetInfraInfoRefined godoc
//
// @ID get-infra-info-refined
// @Summary Get Refined Infra Information
// @Description Get the refined infra information of the connection information.
// @Tags [Get] Get refined source info
// @Accept json
// @Produce json
// @Param sgId path string true "ID of the source group."
// @Param connId path string true "ID of the connection info."
// @Success 200 {object} inframodel.OnpremiseInfraModel "Successfully get refined information of the infra."
// @Failure 400 {object} common.ErrorResponse "Sent bad request."
// @Failure 500 {object} common.ErrorResponse "Failed to get refined information of the infra."
// @Router /source_group/{sgId}/connection_info/{connId}/infra/refined [get]
func GetInfraInfoRefined(c echo.Context) error {
sgID := c.Param("sgId")
if sgID == "" {
return common.ReturnErrorMsg(c, "Please provide the sgId.")
}

connID := c.Param("connId")
if connID == "" {
return common.ReturnErrorMsg(c, "Please provide the connId.")
}

_, err := dao.SourceGroupGet(sgID)
if err != nil {
return common.ReturnErrorMsg(c, err.Error())
}

refinedInfraInfo, err := doGetRefinedInfraInfo(connID)
if err != nil {
return common.ReturnErrorMsg(c, err.Error())
}

var onpremiseInfraModel inframodel.OnpremiseInfraModel
var onpremiseInfra inframodel.OnpremInfra

onpremiseInfra.Servers = append(onpremiseInfra.Servers, *refinedInfraInfo)
doGetRefinedNetworkInfo(&onpremiseInfra.Network, &refinedInfraInfo.Interfaces)

onpremiseInfra.Network = networkProperty
onpremiseInfraModel.OnpremiseInfraModel = onpremiseInfra

return c.JSONPretty(http.StatusOK, onpremiseInfraModel, " ")
Expand Down Expand Up @@ -255,62 +283,16 @@ func GetInfraInfoSourceGroupRefined(c echo.Context) error {

var onpremiseInfraModel inframodel.OnpremiseInfraModel
var onpremiseInfra inframodel.OnpremInfra
var networkProperty inframodel.NetworkProperty

for _, conn := range *list {
refinedInfraInfo, err := doGetRefinedInfraInfo(conn.ID)
if err != nil {
return common.ReturnErrorMsg(c, err.Error())
}
onpremiseInfra.Servers = append(onpremiseInfra.Servers, *refinedInfraInfo)
for _, iface := range refinedInfraInfo.Interfaces {
// append IPv4 networks
for _, ipv4cidr := range iface.IPv4CidrBlocks {
_, netNetwork, err := net.ParseCIDR(ipv4cidr)
if err != nil {
continue
}

var found bool
networkCidr := netNetwork.String()

for _, cidr := range networkProperty.IPv4Networks {
if cidr == networkCidr {
found = true
break
}
}

if !found {
networkProperty.IPv4Networks = append(networkProperty.IPv4Networks, networkCidr)
}
}

// append IPv6 networks
for _, ipv6cidr := range iface.IPv6CidrBlocks {
_, netNetwork, err := net.ParseCIDR(ipv6cidr)
if err != nil {
continue
}

var found bool
networkCidr := netNetwork.String()

for _, cidr := range networkProperty.IPv6Networks {
if cidr == networkCidr {
found = true
break
}
}

if !found {
networkProperty.IPv6Networks = append(networkProperty.IPv6Networks, networkCidr)
}
}
}
doGetRefinedNetworkInfo(&onpremiseInfra.Network, &refinedInfraInfo.Interfaces)
}

onpremiseInfra.Network = networkProperty
onpremiseInfraModel.OnpremiseInfraModel = onpremiseInfra

return c.JSONPretty(http.StatusOK, onpremiseInfraModel, " ")
Expand Down

0 comments on commit 4306296

Please sign in to comment.