Skip to content

Commit

Permalink
add: region/sync openstack routetables
Browse files Browse the repository at this point in the history
  • Loading branch information
lvyangyang committed Jul 9, 2020
1 parent dd90c7c commit eac0a79
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 2 deletions.
26 changes: 26 additions & 0 deletions pkg/multicloud/openstack/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type SRegion struct {
ivpcs []cloudprovider.ICloudVpc

storageCache *SStoragecache
routers []SRouter
}

func (region *SRegion) GetILoadBalancerBackendGroups() ([]cloudprovider.ICloudLoadbalancerBackendGroup, error) {
Expand Down Expand Up @@ -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
}
45 changes: 45 additions & 0 deletions pkg/multicloud/openstack/router.go
Original file line number Diff line number Diff line change
@@ -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"`
}
101 changes: 101 additions & 0 deletions pkg/multicloud/openstack/routetable.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions pkg/multicloud/openstack/shell/router.go
Original file line number Diff line number Diff line change
@@ -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
})
}
25 changes: 23 additions & 2 deletions pkg/multicloud/openstack/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit eac0a79

Please sign in to comment.