eip.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 bingocloud
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type SEip struct {
  23. multicloud.SEipBase
  24. BingoTags
  25. region *SRegion
  26. AddressId string
  27. AddressType string
  28. Bandtype string
  29. Bandwidth int
  30. CanAssociate bool
  31. InstanceId string
  32. Owner string
  33. PublicIp string
  34. SubnetId string
  35. VpcId string
  36. }
  37. func (self *SEip) GetId() string {
  38. return self.PublicIp
  39. }
  40. func (self *SEip) GetGlobalId() string {
  41. return self.PublicIp
  42. }
  43. func (self *SEip) GetName() string {
  44. return self.PublicIp
  45. }
  46. func (self *SEip) GetIpAddr() string {
  47. return self.PublicIp
  48. }
  49. func (self *SEip) GetMode() string {
  50. return api.EIP_MODE_STANDALONE_EIP
  51. }
  52. func (self *SEip) GetINetworkId() string {
  53. return self.SubnetId
  54. }
  55. func (self *SEip) GetAssociationType() string {
  56. if len(self.InstanceId) > 0 {
  57. return api.EIP_ASSOCIATE_TYPE_SERVER
  58. }
  59. return ""
  60. }
  61. func (self *SEip) GetAssociationExternalId() string {
  62. return self.InstanceId
  63. }
  64. func (self *SEip) GetBandwidth() int {
  65. return self.Bandwidth
  66. }
  67. func (self *SEip) GetInternetChargeType() string {
  68. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  69. }
  70. func (self *SEip) Delete() error {
  71. return cloudprovider.ErrNotImplemented
  72. }
  73. func (self *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
  74. params := map[string]string{}
  75. params["PublicIp"] = self.PublicIp
  76. params["InstanceId"] = conf.InstanceId
  77. _, err := self.region.invoke("AssociateAddress", params)
  78. return err
  79. }
  80. func (self *SEip) Dissociate() error {
  81. params := map[string]string{}
  82. params["PublicIp"] = self.PublicIp
  83. _, err := self.region.invoke("DisassociateAddress", params)
  84. return err
  85. }
  86. func (self *SEip) ChangeBandwidth(bw int) error {
  87. return cloudprovider.ErrNotImplemented
  88. }
  89. func (self *SEip) GetProjectId() string {
  90. return ""
  91. }
  92. func (self *SEip) GetStatus() string {
  93. return api.EIP_STATUS_READY
  94. }
  95. func (self *SRegion) GetEips(ip, instanceId, nextToken string) ([]SEip, string, error) {
  96. params := map[string]string{}
  97. if len(ip) > 0 {
  98. params["PublicIp"] = ip
  99. }
  100. if len(nextToken) > 0 {
  101. params["NextToken"] = nextToken
  102. }
  103. idx := 1
  104. if len(instanceId) > 0 {
  105. params[fmt.Sprintf("Filter.%d.Name", idx)] = "instance-id"
  106. params[fmt.Sprintf("Filter.%d.Value.1", idx)] = instanceId
  107. idx++
  108. }
  109. resp, err := self.invoke("DescribeAddresses", params)
  110. if err != nil {
  111. return nil, "", errors.Wrapf(err, "DescribeAddresses")
  112. }
  113. ret := struct {
  114. AddressesSet []SEip
  115. NextToken string
  116. }{}
  117. _ = resp.Unmarshal(&ret)
  118. return ret.AddressesSet, ret.NextToken, nil
  119. }
  120. func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  121. part, nextToken, err := self.GetEips("", "", "")
  122. if err != nil {
  123. return nil, err
  124. }
  125. var eips []SEip
  126. eips = append(eips, part...)
  127. for len(nextToken) > 0 {
  128. part, nextToken, err = self.GetEips("", "", nextToken)
  129. if err != nil {
  130. return nil, err
  131. }
  132. eips = append(eips, part...)
  133. }
  134. var ret []cloudprovider.ICloudEIP
  135. for i := range eips {
  136. eips[i].region = self
  137. ret = append(ret, &eips[i])
  138. }
  139. return ret, nil
  140. }
  141. func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
  142. eips, _, err := self.GetEips(id, "", "")
  143. if err != nil {
  144. return nil, err
  145. }
  146. for i := range eips {
  147. if eips[i].GetGlobalId() == id {
  148. eips[i].region = self
  149. return &eips[i], nil
  150. }
  151. }
  152. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  153. }