yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/cloud_enterprise_network_route.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 aliyun
    16  
    17  import (
    18  	"strconv"
    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/multicloud"
    25  )
    26  
    27  type SCenRouteEntries struct {
    28  	PageNumber      int             `json:"PageNumber"`
    29  	TotalCount      int             `json:"TotalCount"`
    30  	PageSize        int             `json:"PageSize"`
    31  	RequestID       string          `json:"RequestId"`
    32  	CenRouteEntries CenRouteEntries `json:"CenRouteEntries"`
    33  }
    34  type CenRouteMapRecord struct {
    35  	RouteMapID string `json:"RouteMapId"`
    36  	RegionID   string `json:"RegionId"`
    37  }
    38  type CenRouteMapRecords struct {
    39  	CenRouteMapRecord []CenRouteMapRecord `json:"CenRouteMapRecord"`
    40  }
    41  type AsPaths struct {
    42  	AsPath []string `json:"AsPath"`
    43  }
    44  type Communities struct {
    45  	Community []string `json:"Community"`
    46  }
    47  type Conflicts struct {
    48  	Conflict []Conflict `json:"Conflict"`
    49  }
    50  
    51  type Conflict struct {
    52  	DestinationCidrBlock string `json:"DestinationCidrBlock"`
    53  	InstanceId           string `json:"InstanceId"`
    54  	InstanceType         string `json:"InstanceType"`
    55  	RegionId             string `json:"RegionId"`
    56  	Status               string `json:"Status"`
    57  }
    58  
    59  type SCenRouteEntry struct {
    60  	multicloud.SResourceBase
    61  	AliyunTags
    62  	ChildInstance        *SCenChildInstance
    63  	NextHopInstanceID    string             `json:"NextHopInstanceId,omitempty"`
    64  	Status               string             `json:"Status"`
    65  	OperationalMode      bool               `json:"OperationalMode"`
    66  	CenRouteMapRecords   CenRouteMapRecords `json:"CenRouteMapRecords"`
    67  	AsPaths              AsPaths            `json:"AsPaths"`
    68  	Communities          Communities        `json:"Communities"`
    69  	Type                 string             `json:"Type"`
    70  	NextHopType          string             `json:"NextHopType"`
    71  	NextHopRegionID      string             `json:"NextHopRegionId,omitempty"`
    72  	RouteTableID         string             `json:"RouteTableId"`
    73  	DestinationCidrBlock string             `json:"DestinationCidrBlock"`
    74  	Conflicts            Conflicts          `json:"Conflicts"`
    75  	PublishStatus        string             `json:"PublishStatus,omitempty"`
    76  }
    77  type CenRouteEntries struct {
    78  	CenRouteEntry []SCenRouteEntry `json:"CenRouteEntry"`
    79  }
    80  
    81  func (client *SAliyunClient) DescribeCenChildInstanceRouteEntries(cenId string, childInstanceId string, childInstanceRegion string, childInstanceType string, pageNumber int, pageSize int) (SCenRouteEntries, error) {
    82  	routeEntries := SCenRouteEntries{}
    83  	params := map[string]string{}
    84  	params["CenId"] = cenId
    85  	params["ChildInstanceId"] = childInstanceId
    86  	params["ChildInstanceRegionId"] = childInstanceRegion
    87  	params["ChildInstanceType"] = childInstanceType
    88  	params["Action"] = "DescribeCenChildInstanceRouteEntries"
    89  	params["PageNumber"] = strconv.Itoa(pageNumber)
    90  	params["PageSize"] = strconv.Itoa(pageSize)
    91  	resp, err := client.cbnRequest("DescribeCenChildInstanceRouteEntries", params)
    92  	if err != nil {
    93  		return routeEntries, errors.Wrapf(err, `client.cbnRequest("DescribeCenChildInstanceRouteEntries", %s)`, jsonutils.Marshal(params).String())
    94  	}
    95  	err = resp.Unmarshal(&routeEntries)
    96  	if err != nil {
    97  		return routeEntries, errors.Wrapf(err, "[%s].Unmarshal(&routeEntries)", resp.String())
    98  	}
    99  	return routeEntries, nil
   100  }
   101  
   102  func (client *SAliyunClient) GetAllCenChildInstanceRouteEntries(cenId, childInstanceId, childInstanceRegion, childInstanceType string) ([]SCenRouteEntry, error) {
   103  	pageNumber := 0
   104  	srouteEntries := []SCenRouteEntry{}
   105  	for {
   106  		pageNumber++
   107  		routeEntries, err := client.DescribeCenChildInstanceRouteEntries(cenId, childInstanceId, childInstanceRegion, childInstanceType, pageNumber, 20)
   108  		if err != nil {
   109  			return nil, errors.Wrap(err, "client.DescribeCenChildInstanceRouteEntries(cenId, childInstanceId, pageNumber, 20)")
   110  		}
   111  		srouteEntries = append(srouteEntries, routeEntries.CenRouteEntries.CenRouteEntry...)
   112  		if len(srouteEntries) >= routeEntries.TotalCount {
   113  			break
   114  		}
   115  	}
   116  	return srouteEntries, nil
   117  }
   118  
   119  func (client *SAliyunClient) PublishRouteEntries(cenId, childInstanceId, routeTableId, childInstanceRegion, childInstanceType, cidr string) error {
   120  	params := map[string]string{}
   121  	params["CenId"] = cenId
   122  	params["ChildInstanceId"] = childInstanceId
   123  	params["ChildInstanceRouteTableId"] = routeTableId
   124  	params["ChildInstanceRegionId"] = childInstanceRegion
   125  	params["ChildInstanceType"] = childInstanceType
   126  	params["DestinationCidrBlock"] = cidr
   127  	params["Action"] = "PublishRouteEntries"
   128  	_, err := client.cbnRequest("PublishRouteEntries", params)
   129  	if err != nil {
   130  		return errors.Wrapf(err, `client.cbnRequest("PublishRouteEntries", %s)`, jsonutils.Marshal(params).String())
   131  	}
   132  	return nil
   133  }
   134  
   135  func (client *SAliyunClient) WithdrawPublishedRouteEntries(cenId, childInstanceId, routeTableId, childInstanceRegion, childInstanceType, cidr string) error {
   136  	params := map[string]string{}
   137  	params["CenId"] = cenId
   138  	params["ChildInstanceId"] = childInstanceId
   139  	params["ChildInstanceRouteTableId"] = routeTableId
   140  	params["ChildInstanceRegionId"] = childInstanceRegion
   141  	params["ChildInstanceType"] = childInstanceType
   142  	params["DestinationCidrBlock"] = cidr
   143  	params["Action"] = "WithdrawPublishedRouteEntries"
   144  	_, err := client.cbnRequest("WithdrawPublishedRouteEntries", params)
   145  	if err != nil {
   146  		return errors.Wrapf(err, `client.cbnRequest("WithdrawPublishedRouteEntries", %s)`, jsonutils.Marshal(params).String())
   147  	}
   148  	return nil
   149  }
   150  
   151  func (self *SCenRouteEntry) GetId() string {
   152  	return self.RouteTableID + ":" + self.DestinationCidrBlock
   153  }
   154  
   155  func (self *SCenRouteEntry) GetName() string {
   156  	return self.GetId()
   157  }
   158  
   159  func (self *SCenRouteEntry) GetGlobalId() string {
   160  	return self.GetId()
   161  }
   162  
   163  func (self *SCenRouteEntry) GetStatus() string {
   164  	if len(self.Conflicts.Conflict) > 0 {
   165  		return api.ROUTE_ENTRY_STATUS_CONFLICT
   166  	}
   167  	return api.ROUTE_ENTRY_STATUS_AVAILIABLE
   168  }
   169  
   170  func (self *SCenRouteEntry) Refresh() error {
   171  	return nil
   172  }
   173  
   174  func (self *SCenRouteEntry) IsEmulated() bool {
   175  	return false
   176  }
   177  
   178  func (self *SCenRouteEntry) GetCidr() string {
   179  	return self.DestinationCidrBlock
   180  }
   181  
   182  func (self *SCenRouteEntry) GetNextHopType() string {
   183  	switch self.NextHopType {
   184  	case "VPC":
   185  		return api.NEXT_HOP_TYPE_VPC
   186  	case "VBR":
   187  		return api.NEXT_HOP_TYPE_VBR
   188  	default:
   189  		return ""
   190  	}
   191  }
   192  
   193  func (self *SCenRouteEntry) GetNextHop() string {
   194  	return self.NextHopInstanceID
   195  }
   196  
   197  func (self *SCenRouteEntry) GetNextHopRegion() string {
   198  	return self.NextHopRegionID
   199  }
   200  
   201  func (self *SCenRouteEntry) GetEnabled() bool {
   202  	if self.PublishStatus == "Published" {
   203  		return true
   204  	}
   205  	return false
   206  }
   207  
   208  func (self *SCenRouteEntry) GetRouteTableId() string {
   209  	return self.RouteTableID
   210  }
   211  
   212  func (self *SCenRouteEntry) GetInstanceId() string {
   213  	return self.ChildInstance.ChildInstanceID
   214  }
   215  
   216  func (self *SCenRouteEntry) GetInstanceType() string {
   217  	return self.ChildInstance.ChildInstanceType
   218  }
   219  
   220  func (self *SCenRouteEntry) GetInstanceRegionId() string {
   221  	return self.ChildInstance.ChildInstanceRegionID
   222  }