routetables.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. package huawei
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. var hoptypes = map[string]string{
  25. api.NEXT_HOP_TYPE_VPCPEERING: "peering",
  26. api.NEXT_HOP_TYPE_INSTANCE: "ecs",
  27. api.NEXT_HOP_TYPE_NAT: "nat",
  28. api.NEXT_HOP_TYPE_HAVIP: "vip",
  29. api.NEXT_HOP_TYPE_NETWORK: "eni",
  30. api.NEXT_HOP_TYPE_INTERVPCNETWORK: "cc",
  31. }
  32. type SRoute struct {
  33. multicloud.SResourceBase
  34. HuaweiTags
  35. Type string `json:"type"`
  36. Destination string `json:"destination"`
  37. Nexthop string `json:"nexthop"`
  38. Description string `json:"description,omitempty"`
  39. }
  40. func (self *SRoute) GetCidr() string {
  41. return self.Destination
  42. }
  43. func (self *SRoute) GetGlobalId() string {
  44. return self.GetId()
  45. }
  46. func (self *SRoute) GetId() string {
  47. return fmt.Sprintf("%s:%s:%s", self.Type, self.Nexthop, self.Destination)
  48. }
  49. func (self *SRoute) GetName() string {
  50. return ""
  51. }
  52. func (route *SRoute) GetStatus() string {
  53. return api.ROUTE_ENTRY_STATUS_AVAILIABLE
  54. }
  55. func (route *SRoute) GetType() string {
  56. if route.Type != "local" {
  57. return api.ROUTE_ENTRY_TYPE_CUSTOM
  58. }
  59. return api.ROUTE_ENTRY_TYPE_SYSTEM
  60. }
  61. func (self *SRoute) GetNextHopType() string {
  62. for k, v := range hoptypes {
  63. if v == self.Type {
  64. return k
  65. }
  66. }
  67. return self.Type
  68. }
  69. type Subnet struct {
  70. Id string `json:"id"`
  71. }
  72. type SRouteTable struct {
  73. multicloud.SResourceBase
  74. HuaweiTags
  75. vpc *SVpc
  76. Id string `json:"id"`
  77. Name string `json:"name"`
  78. Routes []SRoute `json:"routes"`
  79. Subnets []Subnet `json:"subnets"`
  80. VpcId string `json:"vpc_id"`
  81. Default bool `json:"default"`
  82. TenantId string `json:"tenant_id"`
  83. }
  84. func (self *SRouteTable) GetDescription() string {
  85. return ""
  86. }
  87. func (self *SRouteTable) GetGlobalId() string {
  88. return self.Id
  89. }
  90. func (self *SRouteTable) GetId() string {
  91. return self.Id
  92. }
  93. func (self *SRouteTable) GetName() string {
  94. return self.Name
  95. }
  96. func (self *SRouteTable) GetRegionId() string {
  97. return self.vpc.region.GetId()
  98. }
  99. func (self *SRouteTable) GetVpcId() string {
  100. return self.VpcId
  101. }
  102. func (self *SRouteTable) GetType() cloudprovider.RouteTableType {
  103. return cloudprovider.RouteTableTypeSystem
  104. }
  105. func (self *SRouteTable) GetStatus() string {
  106. return api.ROUTE_TABLE_AVAILABLE
  107. }
  108. func (self *SRouteTable) Refresh() error {
  109. rtb, err := self.vpc.region.GetRouteTable(self.Id)
  110. if err != nil {
  111. return err
  112. }
  113. return jsonutils.Update(self, rtb)
  114. }
  115. func (self *SRouteTable) GetIRoutes() ([]cloudprovider.ICloudRoute, error) {
  116. if len(self.Routes) == 0 {
  117. err := self.Refresh()
  118. if err != nil {
  119. return nil, err
  120. }
  121. }
  122. ret := []cloudprovider.ICloudRoute{}
  123. for i := range self.Routes {
  124. ret = append(ret, &self.Routes[i])
  125. }
  126. return ret, nil
  127. }
  128. func (self *SRoute) GetNextHop() string {
  129. return self.Nexthop
  130. }
  131. func (self *SRegion) GetRouteTables(vpcId string) ([]SRouteTable, error) {
  132. params := url.Values{}
  133. if len(vpcId) > 0 {
  134. params.Set("vpc_id", vpcId)
  135. }
  136. rtbs := make([]SRouteTable, 0)
  137. for {
  138. resp, err := self.list(SERVICE_VPC, "routetables", params)
  139. if err != nil {
  140. return nil, errors.Wrapf(err, "list routetables")
  141. }
  142. part := struct {
  143. Routetables []SRouteTable
  144. }{}
  145. err = resp.Unmarshal(&part)
  146. if err != nil {
  147. return nil, errors.Wrapf(err, "Unmarshal")
  148. }
  149. rtbs = append(rtbs, part.Routetables...)
  150. if len(part.Routetables) == 0 {
  151. break
  152. }
  153. params.Set("marker", part.Routetables[len(part.Routetables)-1].Id)
  154. }
  155. return rtbs, nil
  156. }
  157. // https://console.huaweicloud.com/apiexplorer/#/openapi/VPC/doc?version=v2&api=ShowRouteTable
  158. func (self *SRegion) GetRouteTable(id string) (*SRouteTable, error) {
  159. resp, err := self.list(SERVICE_VPC, "routetables/"+id, nil)
  160. if err != nil {
  161. return nil, errors.Wrapf(err, "show routetable")
  162. }
  163. ret := &SRouteTable{}
  164. err = resp.Unmarshal(&ret, "routetable")
  165. if err != nil {
  166. return nil, err
  167. }
  168. return ret, nil
  169. }
  170. // https://console.huaweicloud.com/apiexplorer/#/openapi/VPC/doc?version=v2&api=CreateRouteTable
  171. func (self *SRouteTable) CreateRoute(route cloudprovider.RouteSet) error {
  172. routeType, ok := hoptypes[route.NextHopType]
  173. if !ok {
  174. return errors.Wrapf(cloudprovider.ErrNotSupported, "%s", route.NextHopType)
  175. }
  176. params := map[string]interface{}{
  177. "route": map[string]interface{}{
  178. "type": routeType,
  179. "destination": route.Destination,
  180. "nexthop": route.NextHop,
  181. "vpc_id": self.vpc.ID,
  182. },
  183. }
  184. _, err := self.vpc.region.post(SERVICE_VPC, "vpc/routes", params)
  185. return err
  186. }
  187. func (self *SRouteTable) RemoveRoute(route cloudprovider.RouteSet) error {
  188. _, ok := hoptypes[route.NextHopType]
  189. if !ok {
  190. return errors.Wrapf(cloudprovider.ErrNotSupported, "%s", route.NextHopType)
  191. }
  192. _, err := self.vpc.region.delete(SERVICE_VPC, "vpc/routes/"+route.RouteId)
  193. return err
  194. }
  195. func (self *SRouteTable) UpdateRoute(route cloudprovider.RouteSet) error {
  196. err := self.RemoveRoute(route)
  197. if err != nil {
  198. return errors.Wrap(err, "self.RemoveRoute(route)")
  199. }
  200. err = self.CreateRoute(route)
  201. if err != nil {
  202. return errors.Wrap(err, "self.CreateRoute(route)")
  203. }
  204. return nil
  205. }
  206. func (self *SRouteTable) GetAssociations() []cloudprovider.RouteTableAssociation {
  207. return []cloudprovider.RouteTableAssociation{}
  208. }