eip.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/utils"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. )
  27. type TInternetChargeType string
  28. const (
  29. InternetChargeByTraffic = TInternetChargeType("PayByTraffic")
  30. InternetChargeByBandwidth = TInternetChargeType("PayByBandwidth")
  31. )
  32. const (
  33. EIP_STATUS_ASSOCIATING = "Associating"
  34. EIP_STATUS_UNASSOCIATING = "Unassociating"
  35. EIP_STATUS_INUSE = "InUse"
  36. EIP_STATUS_AVAILABLE = "Available"
  37. EIP_OPERATION_LOCK_FINANCIAL = "financial"
  38. EIP_OPERATION_LOCK_SECURITY = "security"
  39. EIP_INSTANCE_TYPE_ECS = "EcsInstance" // (默认值):VPC类型的ECS实例
  40. EIP_INTANNCE_TYPE_SLB = "SlbInstance" // :VPC类型的SLB实例
  41. EIP_INSTANCE_TYPE_NAT = "Nat" // :NAT网关
  42. EIP_INSTANCE_TYPE_HAVIP = "HaVip" // :HAVIP
  43. )
  44. /*
  45. {
  46. "AllocationId":"eip-2zeddtan63ou44dtyt9s3",
  47. "AllocationTime":"2019-02-23T06:48:36Z",
  48. "Bandwidth":"100",
  49. "ChargeType":"PostPaid",
  50. "ExpiredTime":"",
  51. "InstanceId":"",
  52. "InstanceType":"",
  53. "InternetChargeType":"PayByTraffic",
  54. "IpAddress":"39.105.131.32",
  55. "OperationLocks":{"LockReason":[]},
  56. "RegionId":"cn-beijing",
  57. "Status":"Available"
  58. }
  59. */
  60. type SEipAddress struct {
  61. region *SRegion
  62. multicloud.SEipBase
  63. ApsaraTags
  64. AllocationId string
  65. InternetChargeType TInternetChargeType
  66. IpAddress string
  67. Status string
  68. InstanceType string
  69. InstanceId string
  70. Bandwidth int /* Mbps */
  71. AllocationTime time.Time
  72. OperationLocks string
  73. ChargeType TChargeType
  74. ExpiredTime time.Time
  75. DepartmentInfo
  76. }
  77. func (self *SEipAddress) GetId() string {
  78. return self.AllocationId
  79. }
  80. func (self *SEipAddress) GetName() string {
  81. return self.IpAddress
  82. }
  83. func (self *SEipAddress) GetGlobalId() string {
  84. return self.AllocationId
  85. }
  86. func (self *SEipAddress) GetStatus() string {
  87. switch self.Status {
  88. case EIP_STATUS_AVAILABLE, EIP_STATUS_INUSE:
  89. return api.EIP_STATUS_READY
  90. case EIP_STATUS_ASSOCIATING:
  91. return api.EIP_STATUS_ASSOCIATE
  92. case EIP_STATUS_UNASSOCIATING:
  93. return api.EIP_STATUS_DISSOCIATE
  94. default:
  95. return api.EIP_STATUS_UNKNOWN
  96. }
  97. }
  98. func (self *SEipAddress) Refresh() error {
  99. if self.IsEmulated() {
  100. return nil
  101. }
  102. new, err := self.region.GetEip(self.AllocationId)
  103. if err != nil {
  104. return err
  105. }
  106. return jsonutils.Update(self, new)
  107. }
  108. func (self *SEipAddress) IsEmulated() bool {
  109. if self.AllocationId == self.InstanceId {
  110. // fixed Public IP
  111. return true
  112. } else {
  113. return false
  114. }
  115. }
  116. func (self *SEipAddress) GetIpAddr() string {
  117. return self.IpAddress
  118. }
  119. func (self *SEipAddress) GetMode() string {
  120. if self.InstanceId == self.AllocationId {
  121. return api.EIP_MODE_INSTANCE_PUBLICIP
  122. } else {
  123. return api.EIP_MODE_STANDALONE_EIP
  124. }
  125. }
  126. func (self *SEipAddress) GetAssociationType() string {
  127. switch self.InstanceType {
  128. case EIP_INSTANCE_TYPE_ECS, "NetworkInterface":
  129. return api.EIP_ASSOCIATE_TYPE_SERVER
  130. case EIP_INSTANCE_TYPE_NAT:
  131. return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
  132. case EIP_INTANNCE_TYPE_SLB:
  133. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  134. default:
  135. return self.InstanceType
  136. }
  137. }
  138. func (self *SEipAddress) GetAssociationExternalId() string {
  139. return self.InstanceId
  140. }
  141. func (self *SEipAddress) GetBillingType() string {
  142. return convertChargeType(self.ChargeType)
  143. }
  144. func (self *SEipAddress) GetCreatedAt() time.Time {
  145. return self.AllocationTime
  146. }
  147. func (self *SEipAddress) Delete() error {
  148. return self.region.DeallocateEIP(self.AllocationId)
  149. }
  150. func (self *SEipAddress) GetBandwidth() int {
  151. return self.Bandwidth
  152. }
  153. func (self *SEipAddress) GetINetworkId() string {
  154. return ""
  155. }
  156. func (self *SEipAddress) GetInternetChargeType() string {
  157. switch self.InternetChargeType {
  158. case InternetChargeByTraffic:
  159. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  160. case InternetChargeByBandwidth:
  161. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  162. default:
  163. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  164. }
  165. }
  166. func (self *SEipAddress) Associate(conf *cloudprovider.AssociateConfig) error {
  167. err := cloudprovider.Wait(20*time.Second, 60*time.Second, func() (bool, error) {
  168. err := self.region.AssociateEip(self.AllocationId, conf.InstanceId)
  169. if err != nil {
  170. if isError(err, "IncorrectInstanceStatus") {
  171. return false, nil
  172. }
  173. return false, errors.Wrap(err, "region.AssociateEip")
  174. }
  175. return true, nil
  176. })
  177. err = cloudprovider.WaitStatus(self, api.EIP_STATUS_READY, 10*time.Second, 180*time.Second)
  178. return err
  179. }
  180. func (self *SEipAddress) Dissociate() error {
  181. err := self.region.DissociateEip(self.AllocationId, self.InstanceId)
  182. if err != nil {
  183. return err
  184. }
  185. err = cloudprovider.WaitStatus(self, api.EIP_STATUS_READY, 10*time.Second, 180*time.Second)
  186. return err
  187. }
  188. func (self *SEipAddress) ChangeBandwidth(bw int) error {
  189. return self.region.UpdateEipBandwidth(self.AllocationId, bw)
  190. }
  191. func (region *SRegion) GetEips(eipId string, associatedId string, offset int, limit int) ([]SEipAddress, int, error) {
  192. if limit > 50 || limit <= 0 {
  193. limit = 50
  194. }
  195. params := make(map[string]string)
  196. params["RegionId"] = region.RegionId
  197. params["PageSize"] = fmt.Sprintf("%d", limit)
  198. params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
  199. if len(eipId) > 0 {
  200. params["AllocationId"] = eipId
  201. }
  202. if len(associatedId) > 0 {
  203. params["AssociatedInstanceId"] = associatedId
  204. for prefix, instanceType := range map[string]string{"i-": "EcsInstance", "ngw-": "Nat", "lb-": "SlbInstance"} {
  205. if strings.HasPrefix(associatedId, prefix) {
  206. params["AssociatedInstanceType"] = instanceType
  207. }
  208. }
  209. }
  210. body, err := region.vpcRequest("DescribeEipAddresses", params)
  211. if err != nil {
  212. log.Errorf("DescribeEipAddresses fail %s", err)
  213. return nil, 0, err
  214. }
  215. eips := make([]SEipAddress, 0)
  216. err = body.Unmarshal(&eips, "EipAddresses", "EipAddress")
  217. if err != nil {
  218. log.Errorf("Unmarshal EipAddress details fail %s", err)
  219. return nil, 0, err
  220. }
  221. total, _ := body.Int("TotalCount")
  222. for i := 0; i < len(eips); i += 1 {
  223. eips[i].region = region
  224. }
  225. return eips, int(total), nil
  226. }
  227. func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
  228. eips, total, err := region.GetEips(eipId, "", 0, 1)
  229. if err != nil {
  230. return nil, err
  231. }
  232. if total != 1 {
  233. return nil, cloudprovider.ErrNotFound
  234. }
  235. return &eips[0], nil
  236. }
  237. func (region *SRegion) AllocateEIP(bwMbps int, chargeType TInternetChargeType, projectId string) (*SEipAddress, error) {
  238. params := make(map[string]string)
  239. params["Bandwidth"] = fmt.Sprintf("%d", bwMbps)
  240. params["InternetChargeType"] = string(chargeType)
  241. params["InstanceChargeType"] = "PostPaid"
  242. params["ClientToken"] = utils.GenRequestId(20)
  243. if len(projectId) > 0 {
  244. params["ResourceGroup"] = projectId
  245. }
  246. body, err := region.vpcRequest("AllocateEipAddress", params)
  247. if err != nil {
  248. log.Errorf("AllocateEipAddress fail %s", err)
  249. return nil, err
  250. }
  251. eipId, err := body.GetString("AllocationId")
  252. if err != nil {
  253. log.Errorf("fail to get AllocationId after EIP allocation??? %s", err)
  254. return nil, err
  255. }
  256. return region.GetEip(eipId)
  257. }
  258. func (region *SRegion) CreateEIP(eip *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  259. var ctype TInternetChargeType
  260. switch eip.ChargeType {
  261. case api.EIP_CHARGE_TYPE_BY_TRAFFIC:
  262. ctype = InternetChargeByTraffic
  263. case api.EIP_CHARGE_TYPE_BY_BANDWIDTH:
  264. ctype = InternetChargeByBandwidth
  265. }
  266. return region.AllocateEIP(eip.BandwidthMbps, ctype, eip.ProjectId)
  267. }
  268. func (region *SRegion) DeallocateEIP(eipId string) error {
  269. params := make(map[string]string)
  270. params["AllocationId"] = eipId
  271. _, err := region.ecsRequest("ReleaseEipAddress", params)
  272. if err != nil {
  273. log.Errorf("ReleaseEipAddress fail %s", err)
  274. }
  275. return err
  276. }
  277. func (region *SRegion) AssociateEip(eipId string, instanceId string) error {
  278. params := make(map[string]string)
  279. params["AllocationId"] = eipId
  280. params["InstanceId"] = instanceId
  281. for prefix, instanceType := range map[string]string{"i-": "EcsInstance", "lb-": "SlbInstance", "ngw-": "Nat"} {
  282. if strings.HasPrefix(instanceId, prefix) {
  283. params["InstanceType"] = instanceType
  284. }
  285. }
  286. _, err := region.ecsRequest("AssociateEipAddress", params)
  287. if err != nil {
  288. log.Errorf("AssociateEipAddress fail %s", err)
  289. }
  290. return err
  291. }
  292. func (region *SRegion) DissociateEip(eipId string, instanceId string) error {
  293. params := make(map[string]string)
  294. params["AllocationId"] = eipId
  295. params["InstanceId"] = instanceId
  296. for prefix, instanceType := range map[string]string{"i-": "EcsInstance", "lb-": "SlbInstance", "ngw-": "Nat"} {
  297. if strings.HasPrefix(instanceId, prefix) {
  298. params["InstanceType"] = instanceType
  299. }
  300. }
  301. _, err := region.ecsRequest("UnassociateEipAddress", params)
  302. if err != nil {
  303. log.Errorf("UnassociateEipAddress fail %s", err)
  304. }
  305. return err
  306. }
  307. func (region *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
  308. params := make(map[string]string)
  309. params["AllocationId"] = eipId
  310. params["Bandwidth"] = fmt.Sprintf("%d", bw)
  311. _, err := region.ecsRequest("ModifyEipAddressAttribute", params)
  312. if err != nil {
  313. log.Errorf("ModifyEipAddressAttribute fail %s", err)
  314. }
  315. return err
  316. }