vpc.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 ucloud
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SVPC struct {
  24. multicloud.SVpc
  25. UcloudTags
  26. region *SRegion
  27. iwires []cloudprovider.ICloudWire
  28. CreateTime int64 `json:"CreateTime"`
  29. Name string `json:"Name"`
  30. Network []string `json:"Network"`
  31. NetworkInfo []NetworkInfo `json:"NetworkInfo"`
  32. SubnetCount int `json:"SubnetCount"`
  33. Tag string `json:"Tag"`
  34. UpdateTime int64 `json:"UpdateTime"`
  35. VPCID string `json:"VPCId"`
  36. }
  37. type NetworkInfo struct {
  38. Network string `json:"Network"`
  39. SubnetCount int `json:"SubnetCount"`
  40. }
  41. func (self *SVPC) addWire(wire *SWire) {
  42. if self.iwires == nil {
  43. self.iwires = make([]cloudprovider.ICloudWire, 0)
  44. }
  45. self.iwires = append(self.iwires, wire)
  46. }
  47. func (self *SVPC) GetId() string {
  48. return self.VPCID
  49. }
  50. func (self *SVPC) GetName() string {
  51. if len(self.Name) > 0 {
  52. return self.Name
  53. }
  54. return self.VPCID
  55. }
  56. func (self *SVPC) GetGlobalId() string {
  57. return self.GetId()
  58. }
  59. func (self *SVPC) GetStatus() string {
  60. return api.VPC_STATUS_AVAILABLE
  61. }
  62. func (self *SVPC) Refresh() error {
  63. new, err := self.region.getVpc(self.GetId())
  64. if err != nil {
  65. return err
  66. }
  67. return jsonutils.Update(self, new)
  68. }
  69. func (self *SVPC) IsEmulated() bool {
  70. return false
  71. }
  72. func (self *SVPC) GetRegion() cloudprovider.ICloudRegion {
  73. return self.region
  74. }
  75. func (self *SVPC) GetIsDefault() bool {
  76. return false
  77. }
  78. func (self *SVPC) GetCidrBlock() string {
  79. return strings.Join(self.Network, ",")
  80. }
  81. func (self *SVPC) GetIWires() ([]cloudprovider.ICloudWire, error) {
  82. if self.iwires == nil {
  83. err := self.fetchNetworks()
  84. if err != nil {
  85. return nil, err
  86. }
  87. }
  88. return self.iwires, nil
  89. }
  90. // 由于Ucloud 安全组和vpc没有直接关联,这里是返回同一个项目下的防火墙列表,会导致重复同步的问题。
  91. // https://docs.ucloud.cn/api/unet-api/grant_firewall
  92. func (self *SVPC) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  93. return []cloudprovider.ICloudSecurityGroup{}, nil
  94. }
  95. func (self *SVPC) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  96. rts := []cloudprovider.ICloudRouteTable{}
  97. return rts, nil
  98. }
  99. func (self *SVPC) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  100. return nil, cloudprovider.ErrNotSupported
  101. }
  102. func (self *SVPC) Delete() error {
  103. return self.region.DeleteVpc(self.GetId())
  104. }
  105. func (self *SVPC) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  106. if self.iwires == nil {
  107. err := self.fetchNetworks()
  108. if err != nil {
  109. return nil, err
  110. }
  111. }
  112. for i := 0; i < len(self.iwires); i += 1 {
  113. if self.iwires[i].GetGlobalId() == wireId {
  114. return self.iwires[i], nil
  115. }
  116. }
  117. return nil, cloudprovider.ErrNotFound
  118. }
  119. func (self *SVPC) fetchNetworks() error {
  120. networks, err := self.region.GetNetworks(self.GetId())
  121. if err != nil {
  122. return err
  123. }
  124. for i := 0; i < len(networks); i += 1 {
  125. wire := self.getWireByRegionId(self.region.GetId())
  126. networks[i].wire = wire
  127. wire.addNetwork(&networks[i])
  128. }
  129. return nil
  130. }
  131. func (self *SVPC) getWireByRegionId(regionId string) *SWire {
  132. if len(regionId) == 0 {
  133. return nil
  134. }
  135. for i := 0; i < len(self.iwires); i++ {
  136. wire := self.iwires[i].(*SWire)
  137. if wire.region.GetId() == regionId {
  138. return wire
  139. }
  140. }
  141. return nil
  142. }
  143. func (self *SRegion) getVpc(vpcId string) (*SVPC, error) {
  144. vpcs, err := self.GetVpcs(vpcId)
  145. if err != nil {
  146. return nil, err
  147. }
  148. if len(vpcs) == 1 {
  149. return &vpcs[0], nil
  150. } else if len(vpcs) == 0 {
  151. return nil, cloudprovider.ErrNotFound
  152. } else {
  153. return nil, fmt.Errorf("getVpc %s %d found", vpcId, len(vpcs))
  154. }
  155. }
  156. // https://docs.ucloud.cn/api/vpc2.0-api/delete_vpc
  157. func (self *SRegion) DeleteVpc(vpcId string) error {
  158. params := NewUcloudParams()
  159. params.Set("VPCId", vpcId)
  160. return self.DoAction("DeleteVPC", params, nil)
  161. }
  162. // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090625.html
  163. func (self *SRegion) GetVpcs(vpcId string) ([]SVPC, error) {
  164. vpcs := make([]SVPC, 0)
  165. params := NewUcloudParams()
  166. if len(vpcId) > 0 {
  167. params.Set("VPCIds.0", vpcId)
  168. }
  169. err := self.DoListAll("DescribeVPC", params, &vpcs)
  170. return vpcs, err
  171. }
  172. func (self *SRegion) GetNetworks(vpcId string) ([]SNetwork, error) {
  173. params := NewUcloudParams()
  174. if len(vpcId) > 0 {
  175. params.Set("VPCId", vpcId)
  176. }
  177. networks := make([]SNetwork, 0)
  178. err := self.DoAction("DescribeSubnet", params, &networks)
  179. return networks, err
  180. }