eip.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 aws
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/cloudmux/pkg/apis/billing"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. const (
  26. EIP_STATUS_INUSE = "InUse"
  27. EIP_STATUS_AVAILABLE = "Available"
  28. )
  29. type SEipAddress struct {
  30. region *SRegion
  31. multicloud.SEipBase
  32. AwsTags
  33. AllocationId string `xml:"allocationId"`
  34. AssociationId string `xml:"associationId"`
  35. CarrierIp string `xml:"carrierIp"`
  36. CustomerOwnedIp string `xml:"customerOwnedIp"`
  37. CustomerOwnedIpv4Pool string `xml:"customerOwnedIpv4Pool"`
  38. Domain string `xml:"domain"`
  39. InstanceId string `xml:"instanceId"`
  40. NetworkBorderGroup string `xml:"networkBorderGroup"`
  41. NetworkInterfaceId string `xml:"networkInterfaceId"`
  42. NetworkInterfaceOwnerId string `xml:"networkInterfaceOwnerId"`
  43. PrivateIpAddress string `xml:"privateIpAddress"`
  44. PublicIp string `xml:"publicIp"`
  45. PublicIpv4Pool string `xml:"publicIpv4Pool"`
  46. }
  47. func (self *SEipAddress) GetId() string {
  48. return self.AllocationId
  49. }
  50. func (self *SEipAddress) GetName() string {
  51. name := self.AwsTags.GetName()
  52. if len(name) > 0 {
  53. return name
  54. }
  55. return self.AllocationId
  56. }
  57. func (self *SEipAddress) GetGlobalId() string {
  58. return self.AllocationId
  59. }
  60. func (self *SEipAddress) GetStatus() string {
  61. return api.EIP_STATUS_READY
  62. }
  63. func (self *SEipAddress) Refresh() error {
  64. if self.IsEmulated() {
  65. return nil
  66. }
  67. new, err := self.region.GetEip(self.AllocationId)
  68. if err != nil {
  69. return err
  70. }
  71. return jsonutils.Update(self, new)
  72. }
  73. func (self *SEipAddress) IsEmulated() bool {
  74. if self.AllocationId == self.InstanceId {
  75. return true
  76. }
  77. return false
  78. }
  79. func (self *SEipAddress) GetIpAddr() string {
  80. return self.PublicIp
  81. }
  82. func (self *SEipAddress) GetMode() string {
  83. if self.InstanceId == self.AllocationId {
  84. return api.EIP_MODE_INSTANCE_PUBLICIP
  85. }
  86. return api.EIP_MODE_STANDALONE_EIP
  87. }
  88. func (self *SEipAddress) GetAssociationType() string {
  89. if len(self.InstanceId) > 0 {
  90. return api.EIP_ASSOCIATE_TYPE_SERVER
  91. }
  92. if len(self.NetworkInterfaceId) > 0 {
  93. net, err := self.region.GetNetworkInterface(self.NetworkInterfaceId)
  94. if err != nil {
  95. return ""
  96. }
  97. switch net.InterfaceType {
  98. case "nat_gateway":
  99. return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
  100. case "network_load_balancer":
  101. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  102. default:
  103. return net.InterfaceType
  104. }
  105. }
  106. return ""
  107. }
  108. func (self *SEipAddress) GetAssociationExternalId() string {
  109. if len(self.InstanceId) > 0 {
  110. return self.InstanceId
  111. }
  112. if len(self.NetworkInterfaceId) > 0 {
  113. net, err := self.region.GetNetworkInterface(self.NetworkInterfaceId)
  114. if err != nil {
  115. return ""
  116. }
  117. switch net.InterfaceType {
  118. case "nat_gateway":
  119. nats, err := self.region.GetNatGateways(nil, net.VpcId, net.SubnetId)
  120. if err != nil {
  121. return ""
  122. }
  123. for i := range nats {
  124. for _, addr := range nats[i].NatGatewayAddresses {
  125. if addr.PublicIp == self.PublicIp {
  126. return nats[i].GetGlobalId()
  127. }
  128. }
  129. }
  130. return ""
  131. }
  132. }
  133. return self.InstanceId
  134. }
  135. func (self *SEipAddress) GetBandwidth() int {
  136. return 0
  137. }
  138. func (self *SEipAddress) GetINetworkId() string {
  139. return ""
  140. }
  141. func (self *SEipAddress) GetInternetChargeType() string {
  142. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  143. }
  144. func (self *SEipAddress) Delete() error {
  145. return self.region.DeallocateEIP(self.AllocationId)
  146. }
  147. func (self *SEipAddress) Associate(conf *cloudprovider.AssociateConfig) error {
  148. for i := 0; i < 3; i++ {
  149. err := self.region.AssociateEip(self.AllocationId, conf.InstanceId)
  150. if err == nil {
  151. return nil
  152. }
  153. if e, ok := err.(*sAwsError); ok && e.Errors.Code == "InvalidAllocationID.NotFound" {
  154. time.Sleep(time.Second * 30)
  155. continue
  156. }
  157. return errors.Wrapf(err, "AssociateEip")
  158. }
  159. return nil
  160. }
  161. func (self *SEipAddress) Dissociate() error {
  162. return self.region.DissociateEip(self.AssociationId)
  163. }
  164. func (self *SEipAddress) ChangeBandwidth(bw int) error {
  165. return self.region.UpdateEipBandwidth(self.AllocationId, bw)
  166. }
  167. func (self *SRegion) GetEips(id, ip, associateId string) ([]SEipAddress, error) {
  168. params := map[string]string{}
  169. if len(id) > 0 {
  170. params["AllocationId.1"] = id
  171. }
  172. if len(ip) > 0 {
  173. params["PublicIp.1"] = ip
  174. }
  175. idx := 1
  176. if len(associateId) > 0 {
  177. params[fmt.Sprintf("Filter.%d.Name", idx)] = "association-id"
  178. params[fmt.Sprintf("Filter.%d.Value.1", idx)] = associateId
  179. idx++
  180. }
  181. result := struct {
  182. AddressesSet []SEipAddress `xml:"addressesSet>item"`
  183. }{}
  184. for i := 0; i < 3; i++ {
  185. err := self.ec2Request("DescribeAddresses", params, &result)
  186. if err == nil {
  187. return result.AddressesSet, nil
  188. }
  189. if errors.Cause(err) == cloudprovider.ErrNotFound {
  190. time.Sleep(time.Second * 10)
  191. continue
  192. }
  193. return nil, errors.Wrapf(err, "DescribeAddresses")
  194. }
  195. return result.AddressesSet, nil
  196. }
  197. func (self *SRegion) GetEip(id string) (*SEipAddress, error) {
  198. eips, err := self.GetEips(id, "", "")
  199. if err != nil {
  200. return nil, errors.Wrapf(err, "GetEips")
  201. }
  202. for i := range eips {
  203. if eips[i].GetGlobalId() == id {
  204. eips[i].region = self
  205. return &eips[i], nil
  206. }
  207. }
  208. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  209. }
  210. func (self *SRegion) GetEipByIpAddress(eipAddress string) (*SEipAddress, error) {
  211. eips, err := self.GetEips("", eipAddress, "")
  212. if err != nil {
  213. return nil, errors.Wrapf(err, "GetEips")
  214. }
  215. for i := range eips {
  216. if eips[i].GetIpAddr() == eipAddress {
  217. eips[i].region = self
  218. return &eips[i], nil
  219. }
  220. }
  221. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", eipAddress)
  222. }
  223. func (self *SRegion) AllocateEIP(opts *cloudprovider.SEip) (*SEipAddress, error) {
  224. params := map[string]string{
  225. "Domain": "vpc",
  226. }
  227. if len(opts.Name) > 0 {
  228. params["TagSpecification.1.ResourceType"] = "elastic-ip"
  229. params["TagSpecification.1.Tag.1.Key"] = "Name"
  230. params["TagSpecification.1.Tag.1.Value"] = opts.Name
  231. }
  232. ret := SEipAddress{region: self}
  233. err := self.ec2Request("AllocateAddress", params, &ret)
  234. if err != nil {
  235. return nil, errors.Wrapf(err, "AllocateAddress")
  236. }
  237. return &ret, nil
  238. }
  239. func (self *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  240. eip, err := self.AllocateEIP(opts)
  241. if err != nil {
  242. return nil, errors.Wrapf(err, "AllocateEIP")
  243. }
  244. return eip, nil
  245. }
  246. func (self *SRegion) DeallocateEIP(eipId string) error {
  247. params := map[string]string{
  248. "AllocationId": eipId,
  249. }
  250. return self.ec2Request("ReleaseAddress", params, nil)
  251. }
  252. func (self *SRegion) AssociateEip(eipId string, instanceId string) error {
  253. params := map[string]string{
  254. "AllocationId": eipId,
  255. "InstanceId": instanceId,
  256. }
  257. return self.ec2Request("AssociateAddress", params, nil)
  258. }
  259. func (self *SRegion) DissociateEip(insId string) error {
  260. params := map[string]string{
  261. "AssociationId": insId,
  262. }
  263. return self.ec2Request("DisassociateAddress", params, nil)
  264. }
  265. func (self *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
  266. return cloudprovider.ErrNotSupported
  267. }
  268. func (self *SEipAddress) GetBillingType() string {
  269. return billing.BILLING_TYPE_POSTPAID
  270. }
  271. func (self *SEipAddress) GetCreatedAt() time.Time {
  272. return time.Time{}
  273. }
  274. func (self *SEipAddress) GetExpiredAt() time.Time {
  275. return time.Time{}
  276. }
  277. func (self *SEipAddress) GetProjectId() string {
  278. return ""
  279. }
  280. func (self *SEipAddress) SetTags(tags map[string]string, replace bool) error {
  281. return self.region.setTags("elastic-ip", self.AllocationId, tags, replace)
  282. }