yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/cloud_connect_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 qcloud 16 17 import ( 18 "fmt" 19 "strconv" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SCcnRouteSet struct { 29 multicloud.SResourceBase 30 QcloudTags 31 32 RouteID string `json:"RouteId"` 33 DestinationCidrBlock string `json:"DestinationCidrBlock"` 34 InstanceType string `json:"InstanceType"` 35 InstanceID string `json:"InstanceId"` 36 InstanceName string `json:"InstanceName"` 37 InstanceRegion string `json:"InstanceRegion"` 38 InstanceUin string `json:"InstanceUin"` 39 UpdateTime string `json:"UpdateTime"` 40 Enabled bool `json:"Enabled"` 41 ExtraState string `json:"ExtraState"` 42 } 43 type SCcnRouteSets struct { 44 RouteSet []SCcnRouteSet `json:"RouteSet"` 45 TotalCount int `json:"TotalCount"` 46 RequestID string `json:"RequestId"` 47 } 48 49 func (self *SRegion) DescribeCcnRoutes(ccnId string, offset int, limit int) ([]SCcnRouteSet, int, error) { 50 params := map[string]string{} 51 params["Offset"] = strconv.Itoa(offset) 52 params["Limit"] = strconv.Itoa(limit) 53 params["CcnId"] = ccnId 54 resp, err := self.vpcRequest("DescribeCcnRoutes", params) 55 if err != nil { 56 return nil, 0, errors.Wrapf(err, `self.vpcRequest("DescribeCcnRoutes", %s)`, jsonutils.Marshal(params).String()) 57 } 58 routes := []SCcnRouteSet{} 59 err = resp.Unmarshal(&routes, "RouteSet") 60 if err != nil { 61 return nil, 0, errors.Wrapf(err, `(%s).Unmarshal(&routes,"RouteSet")`, jsonutils.Marshal(resp).String()) 62 } 63 total, _ := resp.Float("TotalCount") 64 return routes, int(total), nil 65 } 66 67 func (self *SRegion) GetAllCcnRouteSets(ccnId string) ([]SCcnRouteSet, error) { 68 routes := []SCcnRouteSet{} 69 for { 70 part, total, err := self.DescribeCcnRoutes(ccnId, len(routes), 50) 71 if err != nil { 72 return nil, errors.Wrapf(err, "self.DescribeCcns(nil, %d, 50)", len(routes)) 73 } 74 routes = append(routes, part...) 75 if len(routes) >= total { 76 break 77 } 78 } 79 return routes, nil 80 } 81 82 func (self *SRegion) EnableCcnRoutes(ccnId string, routeIds []string) error { 83 params := map[string]string{} 84 params["CcnId"] = ccnId 85 for i := range routeIds { 86 params[fmt.Sprintf("RouteIds.%d", i)] = routeIds[i] 87 } 88 _, err := self.vpcRequest("EnableCcnRoutes", params) 89 if err != nil { 90 return errors.Wrapf(err, `self.vpcRequest("EnableCcnRoutes", %s)`, jsonutils.Marshal(params).String()) 91 } 92 return nil 93 } 94 95 func (self *SRegion) DisableCcnRoutes(ccnId string, routeIds []string) error { 96 params := map[string]string{} 97 params["CcnId"] = ccnId 98 for i := range routeIds { 99 params[fmt.Sprintf("RouteIds.%d", i)] = routeIds[i] 100 } 101 _, err := self.vpcRequest("DisableCcnRoutes", params) 102 if err != nil { 103 return errors.Wrapf(err, `self.vpcRequest("DisableCcnRoutes", %s)`, jsonutils.Marshal(params).String()) 104 } 105 return nil 106 } 107 108 func (self *SCcnRouteSet) GetId() string { 109 return self.RouteID 110 } 111 112 func (self *SCcnRouteSet) GetName() string { 113 return "" 114 } 115 116 func (self *SCcnRouteSet) GetGlobalId() string { 117 return self.RouteID 118 } 119 120 func (self *SCcnRouteSet) GetStatus() string { 121 switch self.ExtraState { 122 case "Disable": 123 return api.ROUTE_ENTRY_STATUS_DISABLED 124 case "Running": 125 return api.ROUTE_ENTRY_STATUS_AVAILIABLE 126 default: 127 return api.ROUTE_ENTRY_STATUS_UNKNOWN 128 } 129 } 130 131 func (self *SCcnRouteSet) Refresh() error { 132 return nil 133 } 134 135 func (self *SCcnRouteSet) IsEmulated() bool { 136 return false 137 } 138 139 func (self *SCcnRouteSet) GetInstanceId() string { 140 return self.InstanceID 141 } 142 143 func (self *SCcnRouteSet) GetInstanceType() string { 144 switch self.InstanceType { 145 case "VPC": 146 return api.NEXT_HOP_TYPE_VPC 147 case "DIRECTCONNECT": 148 return api.NEXT_HOP_TYPE_VBR 149 default: 150 return "" 151 } 152 } 153 154 func (self *SCcnRouteSet) GetInstanceRegionId() string { 155 return self.InstanceRegion 156 } 157 158 func (self *SCcnRouteSet) GetEnabled() bool { 159 return self.Enabled 160 } 161 162 func (self *SCcnRouteSet) GetCidr() string { 163 return self.DestinationCidrBlock 164 }