diff --git a/pkg/multicloud/openstack/region.go b/pkg/multicloud/openstack/region.go index 0f0b7681cf..39f451d749 100644 --- a/pkg/multicloud/openstack/region.go +++ b/pkg/multicloud/openstack/region.go @@ -17,6 +17,7 @@ package openstack import ( "fmt" "net/http" + "net/url" "strings" "yunion.io/x/jsonutils" @@ -554,6 +555,13 @@ func (region *SRegion) GetRouters() ([]SRouter, error) { if err != nil { return nil, errors.Wrap(err, "resp.Unmarshal") } + for i := 0; i < len(routers); i++ { + ports, err := region.GetPortsByDeviceId(routers[i].ID) + if err != nil { + return nil, errors.Wrap(err, "vpc.region.GetPortsByDeviceId") + } + routers[i].ports = ports + } return routers, nil } @@ -568,3 +576,15 @@ func (region *SRegion) fetchrouters() error { region.routers = routers return nil } + +func (region *SRegion) GetPortsByDeviceId(routerId string) ([]SPort, error) { + query := url.Values{} + query.Set("device_id", routerId) + _, resp, err := region.Get("network", "/v2.0/ports?"+query.Encode(), "", nil) + if err != nil { + return nil, errors.Wrap(err, "region.Get routes") + } + ports := []SPort{} + return ports, resp.Unmarshal(&ports, "ports") + +} diff --git a/pkg/multicloud/openstack/router.go b/pkg/multicloud/openstack/router.go index 116cd9754d..a623e002aa 100644 --- a/pkg/multicloud/openstack/router.go +++ b/pkg/multicloud/openstack/router.go @@ -30,6 +30,7 @@ type SConntrackHelper struct { Port int `json:"port"` } type SRouter struct { + ports []SPort AdminStateUp bool `json:"admin_state_up"` Description string `json:"description"` FlavorID string `json:"flavor_id"` diff --git a/pkg/multicloud/openstack/routetable.go b/pkg/multicloud/openstack/routetable.go index e76056ebd8..a4941a1290 100644 --- a/pkg/multicloud/openstack/routetable.go +++ b/pkg/multicloud/openstack/routetable.go @@ -35,7 +35,7 @@ func (route *SRouteEntry) GetCidr() string { } func (route *SRouteEntry) GetNextHopType() string { - return "" + return route.Nexthop } func (route *SRouteEntry) GetNextHop() string { diff --git a/pkg/multicloud/openstack/vpc.go b/pkg/multicloud/openstack/vpc.go index 8dc3952696..7f86895925 100644 --- a/pkg/multicloud/openstack/vpc.go +++ b/pkg/multicloud/openstack/vpc.go @@ -64,6 +64,9 @@ type SVpc struct { VlanTransparent bool Fescription string IsDefault bool + NetworkType string `json:"provider:network_type"` + PhysicalNetwork string `json:"provider:physical_network"` + SegmentationId string `json:"provider:segmentation_id"` } func (vpc *SVpc) GetMetadata() *jsonutils.JSONDict { @@ -133,22 +136,27 @@ func (vpc *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, erro } func (vpc *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) { + if vpc.PhysicalNetwork == "public" { + return []cloudprovider.ICloudRouteTable{}, nil + } err := vpc.region.fetchrouters() if err != nil { return nil, errors.Wrap(err, "vpc.region.fetchrouters()") } routeTables := []SRouteTable{} for index, router := range vpc.region.routers { - if router.ExternalGatewayInfo.NetworkID == vpc.GetId() { - - if len(router.Routes) < 1 { - continue + if len(router.Routes) < 1 { + continue + } + for _, port := range router.ports { + if port.NetworkID == vpc.GetId() { + routeTable := SRouteTable{} + routeTable.entries = router.Routes + routeTable.router = &vpc.region.routers[index] + routeTable.vpc = vpc + routeTables = append(routeTables, routeTable) + break } - routeTable := SRouteTable{} - routeTable.entries = router.Routes - routeTable.router = &vpc.region.routers[index] - routeTable.vpc = vpc - routeTables = append(routeTables, routeTable) } } ret := []cloudprovider.ICloudRouteTable{}