yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/route_table.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package qcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SRouteSet struct {
    29  	multicloud.SResourceBase
    30  	QcloudTags
    31  	RouteID                  int    `json:"RouteId"`
    32  	RouteItemID              string `json:"RouteItemId"`
    33  	DestinationIpv6CidrBlock string `json:"DestinationIpv6CidrBlock,omitempty"`
    34  	GatewayType              string `json:"GatewayType"`
    35  	GatewayID                string `json:"GatewayId"`
    36  	RouteDescription         string `json:"RouteDescription"`
    37  	DestinationCidrBlock     string `json:"DestinationCidrBlock,omitempty"`
    38  	RouteType                string `json:"RouteType"`
    39  }
    40  type SRouteTableSet struct {
    41  	multicloud.SResourceBase
    42  	QcloudTags
    43  	vpc            *SVpc
    44  	VpcID          string                     `json:"VpcId"`
    45  	RouteTableID   string                     `json:"RouteTableId"`
    46  	RouteTableName string                     `json:"RouteTableName"`
    47  	AssociationSet []RouteTableAssociationSet `json:"AssociationSet"`
    48  	RouteSet       []SRouteSet                `json:"RouteSet"`
    49  	Main           bool                       `json:"Main"`
    50  	CreatedTime    string                     `json:"CreatedTime"`
    51  }
    52  
    53  type RouteTableAssociationSet struct {
    54  	SubnetId     string
    55  	RouteTableId string
    56  }
    57  
    58  func (self *SRegion) DescribeRouteTables(vpcId string, routetables []string, offset int, limit int) ([]SRouteTableSet, int, error) {
    59  	if limit > 50 || limit <= 0 {
    60  		limit = 50
    61  	}
    62  	params := make(map[string]string)
    63  	params["Limit"] = fmt.Sprintf("%d", limit)
    64  	params["Offset"] = fmt.Sprintf("%d", offset)
    65  
    66  	for i := range routetables {
    67  		params[fmt.Sprintf("RouteTableIds.%d", i)] = routetables[i]
    68  	}
    69  
    70  	if len(vpcId) > 0 {
    71  		params["Filters.0.Name"] = "vpc-id"
    72  		params["Filters.0.Values.0"] = vpcId
    73  	}
    74  	body, err := self.vpcRequest("DescribeRouteTables", params)
    75  	if err != nil {
    76  		return nil, 0, errors.Wrapf(err, ` self.vpcRequest("DescribeRouteTables", %s)`, jsonutils.Marshal(params))
    77  	}
    78  	routeTables := make([]SRouteTableSet, 0)
    79  	err = body.Unmarshal(&routeTables, "RouteTableSet")
    80  	if err != nil {
    81  		return nil, 0, errors.Wrapf(err, "body.Unmarshal(RouteTableSet)[%s]", body.String())
    82  	}
    83  	total, _ := body.Float("TotalCount")
    84  	return routeTables, int(total), nil
    85  }
    86  
    87  func (self *SRegion) GetAllRouteTables(vpcId string, routetables []string) ([]SRouteTableSet, error) {
    88  	routeTables := make([]SRouteTableSet, 0)
    89  	for {
    90  		part, total, err := self.DescribeRouteTables(vpcId, routetables, len(routeTables), 50)
    91  		if err != nil {
    92  			return nil, errors.Wrap(err, "self.DescribeRouteTables(vpcId, routetables, offset, limit)")
    93  		}
    94  		routeTables = append(routeTables, part...)
    95  		if len(routeTables) >= total {
    96  			break
    97  		}
    98  	}
    99  	return routeTables, nil
   100  }
   101  
   102  func (self *SRouteTableSet) GetId() string {
   103  	return self.RouteTableID
   104  }
   105  
   106  func (self *SRouteTableSet) GetName() string {
   107  	return self.RouteTableName
   108  }
   109  
   110  func (self *SRouteTableSet) GetGlobalId() string {
   111  	return self.RouteTableID
   112  }
   113  
   114  func (self *SRouteTableSet) GetStatus() string {
   115  	return api.ROUTE_TABLE_AVAILABLE
   116  }
   117  
   118  func (self *SRouteTableSet) Refresh() error {
   119  	return nil
   120  }
   121  
   122  func (self *SRouteTableSet) IsEmulated() bool {
   123  	return false
   124  }
   125  
   126  func (self *SRouteTableSet) GetAssociations() []cloudprovider.RouteTableAssociation {
   127  	result := []cloudprovider.RouteTableAssociation{}
   128  	for i := range self.AssociationSet {
   129  		association := cloudprovider.RouteTableAssociation{
   130  			AssociationType:      cloudprovider.RouteTableAssociaToSubnet,
   131  			AssociatedResourceId: self.AssociationSet[i].SubnetId,
   132  		}
   133  		result = append(result, association)
   134  	}
   135  	return result
   136  }
   137  
   138  func (self *SRouteTableSet) GetDescription() string {
   139  	return ""
   140  }
   141  
   142  func (self *SRouteTableSet) GetRegionId() string {
   143  	return self.vpc.GetRegion().GetId()
   144  }
   145  
   146  func (self *SRouteTableSet) GetVpcId() string {
   147  	return self.vpc.GetId()
   148  }
   149  
   150  func (self *SRouteTableSet) GetType() cloudprovider.RouteTableType {
   151  	if self.Main {
   152  		return cloudprovider.RouteTableTypeSystem
   153  	}
   154  	return cloudprovider.RouteTableTypeCustom
   155  }
   156  
   157  func (self *SRouteTableSet) GetIRoutes() ([]cloudprovider.ICloudRoute, error) {
   158  	result := []cloudprovider.ICloudRoute{}
   159  	for i := range self.RouteSet {
   160  		result = append(result, &self.RouteSet[i])
   161  	}
   162  	return result, nil
   163  }
   164  
   165  func (self *SRouteTableSet) CreateRoute(route cloudprovider.RouteSet) error {
   166  	return cloudprovider.ErrNotSupported
   167  }
   168  
   169  func (self *SRouteTableSet) UpdateRoute(route cloudprovider.RouteSet) error {
   170  	return cloudprovider.ErrNotSupported
   171  }
   172  
   173  func (self *SRouteTableSet) RemoveRoute(route cloudprovider.RouteSet) error {
   174  	return cloudprovider.ErrNotSupported
   175  }
   176  
   177  func (self *SRouteSet) GetId() string {
   178  	return self.RouteItemID
   179  }
   180  
   181  func (self *SRouteSet) GetName() string {
   182  	return ""
   183  }
   184  
   185  func (self *SRouteSet) GetGlobalId() string {
   186  	return self.RouteItemID
   187  }
   188  
   189  func (self *SRouteSet) GetStatus() string {
   190  	return api.ROUTE_ENTRY_STATUS_AVAILIABLE
   191  }
   192  
   193  func (self *SRouteSet) Refresh() error {
   194  	return nil
   195  }
   196  
   197  func (self *SRouteSet) IsEmulated() bool {
   198  	return false
   199  }
   200  
   201  func (self *SRouteSet) GetType() string {
   202  	switch self.RouteType {
   203  	case "USER":
   204  		return api.ROUTE_ENTRY_TYPE_CUSTOM
   205  	case "NETD":
   206  		return api.ROUTE_ENTRY_TYPE_SYSTEM
   207  	case "CCN":
   208  		return api.ROUTE_ENTRY_TYPE_PROPAGATE
   209  	default:
   210  		return api.ROUTE_ENTRY_TYPE_SYSTEM
   211  	}
   212  }
   213  
   214  func (self *SRouteSet) GetCidr() string {
   215  	return self.DestinationCidrBlock
   216  }
   217  
   218  func (self *SRouteSet) GetNextHopType() string {
   219  	switch self.GatewayType {
   220  	case "CVM":
   221  		return api.NEXT_HOP_TYPE_INSTANCE
   222  	case "VPN":
   223  		return api.NEXT_HOP_TYPE_VPN
   224  	case "DIRECTCONNECT":
   225  		return api.NEXT_HOP_TYPE_DIRECTCONNECTION
   226  	case "PEERCONNECTION":
   227  		return api.NEXT_HOP_TYPE_VPCPEERING
   228  	case "SSLVPN":
   229  		return api.NEXT_HOP_TYPE_VPN
   230  	case "NAT":
   231  		return api.NEXT_HOP_TYPE_NAT
   232  	case "NORMAL_CVM":
   233  		return api.NEXT_HOP_TYPE_INSTANCE
   234  	case "EIP":
   235  		return api.NEXT_HOP_TYPE_EIP
   236  	case "CCN":
   237  		return api.NEXT_HOP_TYPE_INTERVPCNETWORK
   238  	default:
   239  		return ""
   240  	}
   241  }
   242  
   243  func (self *SRouteSet) GetNextHop() string {
   244  	return self.GatewayID
   245  }