diff --git a/pkg/multicloud/openstack/region.go b/pkg/multicloud/openstack/region.go index d33944b39a..0f0b7681cf 100644 --- a/pkg/multicloud/openstack/region.go +++ b/pkg/multicloud/openstack/region.go @@ -40,6 +40,7 @@ type SRegion struct { ivpcs []cloudprovider.ICloudVpc storageCache *SStoragecache + routers []SRouter } func (region *SRegion) GetILoadBalancerBackendGroups() ([]cloudprovider.ICloudLoadbalancerBackendGroup, error) { @@ -542,3 +543,28 @@ func (region *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCre func (region *SRegion) GetCapabilities() []string { return region.client.GetCapabilities() } + +func (region *SRegion) GetRouters() ([]SRouter, error) { + _, resp, err := region.Get("network", "/v2.0/routers", "", nil) + if err != nil { + return nil, errors.Wrap(err, "region.Get routes") + } + routers := []SRouter{} + err = resp.Unmarshal(&routers, "routers") + if err != nil { + return nil, errors.Wrap(err, "resp.Unmarshal") + } + return routers, nil +} + +func (region *SRegion) fetchrouters() error { + if len(region.routers) > 0 { + return nil + } + routers, err := region.GetRouters() + if err != nil { + return errors.Wrap(err, "region.GetRouters()") + } + region.routers = routers + return nil +} diff --git a/pkg/multicloud/openstack/router.go b/pkg/multicloud/openstack/router.go new file mode 100644 index 0000000000..116cd9754d --- /dev/null +++ b/pkg/multicloud/openstack/router.go @@ -0,0 +1,45 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package openstack + +type SExternalFixedIP struct { + IPAddress string `json:"ip_address"` + SubnetID string `json:"subnet_id"` +} +type SExternalGatewayInfo struct { + EnableSnat bool `json:"enable_snat"` + ExtrernalFiedIps []SExternalFixedIP `json:"external_fixed_ips"` + NetworkID string `json:"network_id"` +} + +type SConntrackHelper struct { + Protocol string `json:"protocol"` + Helper string `json:"helper"` + Port int `json:"port"` +} +type SRouter struct { + AdminStateUp bool `json:"admin_state_up"` + Description string `json:"description"` + FlavorID string `json:"flavor_id"` + ID string `json:"id"` + Name string `json:"name"` + Routes []SRouteEntry `json:"routes"` + Status string `json:"status"` + ProjectID string `json:"project_id"` + TenantID string `json:"tenant_id"` + Tags []string `json:"tags"` + ConntrackHelpers []SConntrackHelper `json:"conntrack_helpers"` + ExternalGatewayInfo SExternalGatewayInfo `json:"external_gateway_info"` +} diff --git a/pkg/multicloud/openstack/routetable.go b/pkg/multicloud/openstack/routetable.go new file mode 100644 index 0000000000..e76056ebd8 --- /dev/null +++ b/pkg/multicloud/openstack/routetable.go @@ -0,0 +1,101 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package openstack + +import ( + "yunion.io/x/jsonutils" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/cloudprovider" +) + +type SRouteEntry struct { + Destination string `json:"destination"` + Nexthop string `json:"nexthop"` +} + +func (route *SRouteEntry) GetType() string { + return api.ROUTE_ENTRY_TYPE_CUSTOM +} + +func (route *SRouteEntry) GetCidr() string { + return route.Destination +} + +func (route *SRouteEntry) GetNextHopType() string { + return "" +} + +func (route *SRouteEntry) GetNextHop() string { + return route.Nexthop +} + +type SRouteTable struct { + vpc *SVpc + entries []SRouteEntry + router *SRouter +} + +func (self *SRouteTable) GetDescription() string { + return "" +} + +func (self *SRouteTable) GetId() string { + return self.GetGlobalId() +} + +func (self *SRouteTable) GetGlobalId() string { + return self.router.ID +} + +func (self *SRouteTable) GetName() string { + return self.router.Name +} + +func (self *SRouteTable) GetMetadata() *jsonutils.JSONDict { + return nil +} + +func (self *SRouteTable) GetRegionId() string { + return self.vpc.region.GetId() +} + +func (self *SRouteTable) GetType() string { + return "" +} + +func (self *SRouteTable) GetVpcId() string { + return self.vpc.GetId() +} + +func (self *SRouteTable) GetIRoutes() ([]cloudprovider.ICloudRoute, error) { + ret := []cloudprovider.ICloudRoute{} + for index := range self.entries { + ret = append(ret, &self.entries[index]) + } + return ret, nil +} + +func (self *SRouteTable) GetStatus() string { + return self.router.Status +} + +func (self *SRouteTable) IsEmulated() bool { + return false +} + +func (self *SRouteTable) Refresh() error { + return nil +} diff --git a/pkg/multicloud/openstack/shell/router.go b/pkg/multicloud/openstack/shell/router.go new file mode 100644 index 0000000000..14e5af604a --- /dev/null +++ b/pkg/multicloud/openstack/shell/router.go @@ -0,0 +1,33 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shell + +import ( + "yunion.io/x/onecloud/pkg/multicloud/openstack" + "yunion.io/x/onecloud/pkg/util/shellutils" +) + +func init() { + type RouterListOptions struct { + } + shellutils.R(&RouterListOptions{}, "router-list", "List routers", func(cli *openstack.SRegion, args *RouterListOptions) error { + routers, err := cli.GetRouters() + if err != nil { + return nil + } + printList(routers, 0, 0, 0, nil) + return nil + }) +} diff --git a/pkg/multicloud/openstack/vpc.go b/pkg/multicloud/openstack/vpc.go index 5d6860d2dd..8dc3952696 100644 --- a/pkg/multicloud/openstack/vpc.go +++ b/pkg/multicloud/openstack/vpc.go @@ -133,8 +133,29 @@ func (vpc *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, erro } func (vpc *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) { - rts := []cloudprovider.ICloudRouteTable{} - return rts, 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 + } + routeTable := SRouteTable{} + routeTable.entries = router.Routes + routeTable.router = &vpc.region.routers[index] + routeTable.vpc = vpc + routeTables = append(routeTables, routeTable) + } + } + ret := []cloudprovider.ICloudRouteTable{} + for i := range routeTables { + ret = append(ret, &routeTables[i]) + } + return ret, nil } func (vpc *SVpc) fetchWires() error {