vpc.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 aliyun
  15. import (
  16. "strings"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. const (
  24. VpcAvailable = "Available"
  25. VpcPending = "Pending"
  26. )
  27. // "CidrBlock":"172.31.0.0/16","CreationTime":"2017-03-19T13:37:40Z","Description":"System created default VPC.","IsDefault":true,"RegionId":"cn-hongkong","Status":"Available","UserCidrs":{"UserCidr":[]},"VRouterId":"vrt-j6c00qrol733dg36iq4qj","VSwitchIds":{"VSwitchId":["vsw-j6c3gig5ub4fmi2veyrus"]},"VpcId":"vpc-j6c86z3sh8ufhgsxwme0q","VpcName":""
  28. type SUserCIDRs struct {
  29. UserCidr []string
  30. }
  31. type SVSwitchIds struct {
  32. VSwitchId []string
  33. }
  34. type SVpc struct {
  35. multicloud.SVpc
  36. AliyunTags
  37. region *SRegion
  38. iwires []cloudprovider.ICloudWire
  39. routeTables []cloudprovider.ICloudRouteTable
  40. CidrBlock string
  41. Ipv6CidrBlock string
  42. CreationTime time.Time
  43. Description string
  44. IsDefault bool
  45. RegionId string
  46. Status string
  47. UserCidrs SUserCIDRs
  48. VRouterId string
  49. VSwitchIds SVSwitchIds
  50. VpcId string
  51. VpcName string
  52. }
  53. func (self *SVpc) GetId() string {
  54. return self.VpcId
  55. }
  56. func (self *SVpc) GetName() string {
  57. if len(self.VpcName) > 0 {
  58. return self.VpcName
  59. }
  60. return self.VpcId
  61. }
  62. func (self *SVpc) GetGlobalId() string {
  63. return self.VpcId
  64. }
  65. func (self *SVpc) IsEmulated() bool {
  66. return false
  67. }
  68. func (self *SVpc) GetIsDefault() bool {
  69. return self.IsDefault
  70. }
  71. func (self *SVpc) GetCidrBlock() string {
  72. return self.CidrBlock
  73. }
  74. func (self *SVpc) GetCidrBlock6() string {
  75. return self.Ipv6CidrBlock
  76. }
  77. func (self *SVpc) GetStatus() string {
  78. return strings.ToLower(self.Status)
  79. }
  80. func (self *SVpc) Refresh() error {
  81. new, err := self.region.getVpc(self.VpcId)
  82. if err != nil {
  83. return err
  84. }
  85. return jsonutils.Update(self, new)
  86. }
  87. func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
  88. return self.region
  89. }
  90. func (self *SVpc) addWire(wire *SWire) {
  91. if self.iwires == nil {
  92. self.iwires = make([]cloudprovider.ICloudWire, 0)
  93. }
  94. self.iwires = append(self.iwires, wire)
  95. }
  96. func (self *SVpc) getWireByZoneId(zoneId string) *SWire {
  97. for i := 0; i < len(self.iwires); i += 1 {
  98. wire := self.iwires[i].(*SWire)
  99. if wire.zone.ZoneId == zoneId {
  100. return wire
  101. }
  102. }
  103. return nil
  104. }
  105. func (self *SVpc) fetchVSwitches() error {
  106. switches, total, err := self.region.GetVSwitches(nil, self.VpcId, 0, 50)
  107. if err != nil {
  108. return err
  109. }
  110. if total > len(switches) {
  111. switches, _, err = self.region.GetVSwitches(nil, self.VpcId, 0, total)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. for i := 0; i < len(switches); i += 1 {
  117. wire := self.getWireByZoneId(switches[i].ZoneId)
  118. if wire != nil {
  119. switches[i].wire = wire
  120. wire.addNetwork(&switches[i])
  121. }
  122. }
  123. return nil
  124. }
  125. func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  126. if self.iwires == nil {
  127. err := self.fetchVSwitches()
  128. if err != nil {
  129. return nil, err
  130. }
  131. }
  132. return self.iwires, nil
  133. }
  134. func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  135. if self.iwires == nil {
  136. err := self.fetchVSwitches()
  137. if err != nil {
  138. return nil, err
  139. }
  140. }
  141. for i := 0; i < len(self.iwires); i += 1 {
  142. if self.iwires[i].GetGlobalId() == wireId {
  143. return self.iwires[i], nil
  144. }
  145. }
  146. return nil, cloudprovider.ErrNotFound
  147. }
  148. func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  149. groups, err := self.region.GetSecurityGroups(self.VpcId, "", nil)
  150. if err != nil {
  151. return nil, err
  152. }
  153. ret := []cloudprovider.ICloudSecurityGroup{}
  154. for i := range groups {
  155. groups[i].region = self.region
  156. ret = append(ret, &groups[i])
  157. }
  158. return ret, nil
  159. }
  160. func (self *SVpc) fetchRouteTables() error {
  161. routeTables := make([]*SRouteTable, 0)
  162. for {
  163. parts, total, err := self.RemoteGetRouteTableList(len(routeTables), 50)
  164. if err != nil {
  165. return err
  166. }
  167. routeTables = append(routeTables, parts...)
  168. if len(routeTables) >= total {
  169. break
  170. }
  171. }
  172. self.routeTables = make([]cloudprovider.ICloudRouteTable, len(routeTables))
  173. for i := 0; i < len(routeTables); i++ {
  174. routeTables[i].vpc = self
  175. self.routeTables[i] = routeTables[i]
  176. }
  177. return nil
  178. }
  179. func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  180. if self.routeTables == nil {
  181. err := self.fetchRouteTables()
  182. if err != nil {
  183. return nil, err
  184. }
  185. }
  186. return self.routeTables, nil
  187. }
  188. func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  189. tables, err := self.GetIRouteTables()
  190. if err != nil {
  191. return nil, errors.Wrapf(err, "GetIRouteTables")
  192. }
  193. for i := range tables {
  194. if tables[i].GetGlobalId() == routeTableId {
  195. return tables[i], nil
  196. }
  197. }
  198. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", routeTableId)
  199. }
  200. func (self *SVpc) Delete() error {
  201. return self.region.DeleteVpc(self.VpcId)
  202. }
  203. func (self *SVpc) getNatGateways() ([]SNatGateway, error) {
  204. natgatways := make([]SNatGateway, 0)
  205. gwTotal := -1
  206. for gwTotal < 0 || len(natgatways) < gwTotal {
  207. parts, total, err := self.region.GetNatGateways(self.VpcId, "", len(natgatways), 50)
  208. if err != nil {
  209. return nil, err
  210. }
  211. if len(parts) > 0 {
  212. natgatways = append(natgatways, parts...)
  213. }
  214. gwTotal = total
  215. }
  216. for i := 0; i < len(natgatways); i += 1 {
  217. natgatways[i].vpc = self
  218. }
  219. return natgatways, nil
  220. }
  221. func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) {
  222. nats := []SNatGateway{}
  223. for {
  224. parts, total, err := self.region.GetNatGateways(self.VpcId, "", len(nats), 50)
  225. if err != nil {
  226. return nil, err
  227. }
  228. nats = append(nats, parts...)
  229. if len(nats) >= total {
  230. break
  231. }
  232. }
  233. inats := []cloudprovider.ICloudNatGateway{}
  234. for i := 0; i < len(nats); i++ {
  235. nats[i].vpc = self
  236. inats = append(inats, &nats[i])
  237. }
  238. return inats, nil
  239. }
  240. func (self *SVpc) GetAuthorityOwnerId() string {
  241. return self.region.client.ownerId
  242. }
  243. func (self *SRegion) GrantInstanceToCen(opts *cloudprovider.SVpcJointInterVpcNetworkOption, instance SCenAttachInstanceInput) error {
  244. params := make(map[string]string)
  245. params["CenId"] = opts.InterVpcNetworkId
  246. params["CenOwnerId"] = opts.NetworkAuthorityOwnerId
  247. params["InstanceId"] = instance.InstanceId
  248. params["InstanceType"] = instance.InstanceType
  249. params["RegionId"] = instance.InstanceRegion
  250. _, err := self.vpcRequest("GrantInstanceToCen", params)
  251. if err != nil {
  252. return errors.Wrapf(err, `self.vpcRequest("GrantInstanceToCen", %s)`, jsonutils.Marshal(params).String())
  253. }
  254. return nil
  255. }
  256. func (self *SVpc) ProposeJoinICloudInterVpcNetwork(opts *cloudprovider.SVpcJointInterVpcNetworkOption) error {
  257. instance := SCenAttachInstanceInput{
  258. InstanceType: "VPC",
  259. InstanceId: self.GetId(),
  260. InstanceRegion: self.region.GetId(),
  261. }
  262. err := self.region.GrantInstanceToCen(opts, instance)
  263. if err != nil {
  264. return errors.Wrapf(err, "self.region.GrantInstanceToCen(%s,%s)", self.GetId(), jsonutils.Marshal(opts).String())
  265. }
  266. return nil
  267. }