vpc.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 apsara
  15. import (
  16. "strings"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. const (
  23. VpcAvailable = "Available"
  24. VpcPending = "Pending"
  25. )
  26. // "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":""
  27. type SUserCIDRs struct {
  28. UserCidr []string
  29. }
  30. type SVSwitchIds struct {
  31. VSwitchId []string
  32. }
  33. type SVpc struct {
  34. multicloud.SVpc
  35. ApsaraTags
  36. region *SRegion
  37. iwires []cloudprovider.ICloudWire
  38. secgroups []cloudprovider.ICloudSecurityGroup
  39. routeTables []cloudprovider.ICloudRouteTable
  40. CidrBlock string
  41. CreationTime time.Time
  42. Description string
  43. IsDefault bool
  44. RegionId string
  45. Status string
  46. UserCidrs SUserCIDRs
  47. VRouterId string
  48. VSwitchIds SVSwitchIds
  49. VpcId string
  50. VpcName string
  51. DepartmentInfo
  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) GetStatus() string {
  75. return strings.ToLower(self.Status)
  76. }
  77. func (self *SVpc) GetCreatedAt() time.Time {
  78. return self.CreationTime
  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) GetSysTags() map[string]string {
  91. tags := self.ApsaraTags.GetSysTags()
  92. if len(self.ResourceGroup) > 0 {
  93. tags["ResourceGroup"] = self.ResourceGroup
  94. }
  95. if len(self.ResourceGroupId) > 0 {
  96. tags["ResourceGroupId"] = self.ResourceGroupId
  97. }
  98. if len(self.Department) > 0 {
  99. tags["Department"] = self.Department
  100. }
  101. if len(self.DepartmentName) > 0 {
  102. tags["DepartmentName"] = self.DepartmentName
  103. }
  104. if len(self.ResourceGroupName) > 0 {
  105. groupName := strings.TrimPrefix(self.ResourceGroupName, "ResourceSet(")
  106. groupName = strings.TrimSuffix(groupName, ")")
  107. tags["ResourceGroupName"] = groupName
  108. }
  109. return tags
  110. }
  111. func (self *SVpc) addWire(wire *SWire) {
  112. if self.iwires == nil {
  113. self.iwires = make([]cloudprovider.ICloudWire, 0)
  114. }
  115. self.iwires = append(self.iwires, wire)
  116. }
  117. func (self *SVpc) getWireByZoneId(zoneId string) *SWire {
  118. for i := 0; i < len(self.iwires); i += 1 {
  119. wire := self.iwires[i].(*SWire)
  120. if wire.zone.ZoneId == zoneId {
  121. return wire
  122. }
  123. }
  124. return nil
  125. }
  126. func (self *SVpc) fetchVSwitches() error {
  127. switches, total, err := self.region.GetVSwitches(nil, self.VpcId, 0, 50)
  128. if err != nil {
  129. return err
  130. }
  131. if total > len(switches) {
  132. switches, _, err = self.region.GetVSwitches(nil, self.VpcId, 0, total)
  133. if err != nil {
  134. return err
  135. }
  136. }
  137. for i := 0; i < len(switches); i += 1 {
  138. wire := self.getWireByZoneId(switches[i].ZoneId)
  139. switches[i].wire = wire
  140. wire.addNetwork(&switches[i])
  141. }
  142. return nil
  143. }
  144. func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  145. if self.iwires == nil {
  146. err := self.fetchVSwitches()
  147. if err != nil {
  148. return nil, err
  149. }
  150. }
  151. return self.iwires, nil
  152. }
  153. func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  154. if self.iwires == nil {
  155. err := self.fetchVSwitches()
  156. if err != nil {
  157. return nil, err
  158. }
  159. }
  160. for i := 0; i < len(self.iwires); i += 1 {
  161. if self.iwires[i].GetGlobalId() == wireId {
  162. return self.iwires[i], nil
  163. }
  164. }
  165. return nil, cloudprovider.ErrNotFound
  166. }
  167. func (self *SVpc) fetchSecurityGroups() error {
  168. secgroups := make([]SSecurityGroup, 0)
  169. for {
  170. parts, total, err := self.region.GetSecurityGroups(self.VpcId, "", []string{}, len(secgroups), 50)
  171. if err != nil {
  172. return err
  173. }
  174. secgroups = append(secgroups, parts...)
  175. if len(secgroups) >= total || len(parts) == 0 {
  176. break
  177. }
  178. }
  179. self.secgroups = make([]cloudprovider.ICloudSecurityGroup, len(secgroups))
  180. for i := 0; i < len(secgroups); i++ {
  181. secgroups[i].region = self.region
  182. self.secgroups[i] = &secgroups[i]
  183. }
  184. return nil
  185. }
  186. func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  187. if self.secgroups == nil {
  188. err := self.fetchSecurityGroups()
  189. if err != nil {
  190. return nil, err
  191. }
  192. }
  193. return self.secgroups, nil
  194. }
  195. func (self *SVpc) fetchRouteTables() error {
  196. routeTables := make([]*SRouteTable, 0)
  197. for {
  198. parts, total, err := self.RemoteGetRouteTableList(len(routeTables), 50)
  199. if err != nil {
  200. return err
  201. }
  202. routeTables = append(routeTables, parts...)
  203. if len(routeTables) >= total || len(parts) == 0 {
  204. break
  205. }
  206. }
  207. self.routeTables = make([]cloudprovider.ICloudRouteTable, len(routeTables))
  208. for i := 0; i < len(routeTables); i++ {
  209. routeTables[i].vpc = self
  210. self.routeTables[i] = routeTables[i]
  211. }
  212. return nil
  213. }
  214. func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  215. if self.routeTables == nil {
  216. err := self.fetchRouteTables()
  217. if err != nil {
  218. return nil, err
  219. }
  220. }
  221. return self.routeTables, nil
  222. }
  223. func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  224. return nil, cloudprovider.ErrNotSupported
  225. }
  226. func (self *SVpc) Delete() error {
  227. return self.region.DeleteVpc(self.VpcId)
  228. }
  229. func (self *SVpc) getNatGateways() ([]SNatGetway, error) {
  230. natgatways := make([]SNatGetway, 0)
  231. gwTotal := -1
  232. for gwTotal < 0 || len(natgatways) < gwTotal {
  233. parts, total, err := self.region.GetNatGateways(self.VpcId, "", len(natgatways), 50)
  234. if err != nil {
  235. return nil, err
  236. }
  237. if len(parts) > 0 {
  238. natgatways = append(natgatways, parts...)
  239. }
  240. gwTotal = total
  241. }
  242. for i := 0; i < len(natgatways); i += 1 {
  243. natgatways[i].vpc = self
  244. }
  245. return natgatways, nil
  246. }
  247. func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) {
  248. nats := []SNatGetway{}
  249. for {
  250. parts, total, err := self.region.GetNatGateways(self.VpcId, "", len(nats), 50)
  251. if err != nil {
  252. return nil, err
  253. }
  254. nats = append(nats, parts...)
  255. if len(nats) >= total || len(parts) == 0 {
  256. break
  257. }
  258. }
  259. inats := []cloudprovider.ICloudNatGateway{}
  260. for i := 0; i < len(nats); i++ {
  261. nats[i].vpc = self
  262. inats = append(inats, &nats[i])
  263. }
  264. return inats, nil
  265. }