eip.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 cloudpods
  15. import (
  16. "time"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. "yunion.io/x/jsonutils"
  20. billing_api "yunion.io/x/onecloud/pkg/apis/billing"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  23. )
  24. type SEip struct {
  25. multicloud.SVirtualResourceBase
  26. multicloud.SBillingBase
  27. CloudpodsTags
  28. region *SRegion
  29. api.ElasticipDetails
  30. }
  31. func (self *SEip) GetName() string {
  32. return self.Name
  33. }
  34. func (self *SEip) GetId() string {
  35. return self.Id
  36. }
  37. func (self *SEip) GetGlobalId() string {
  38. return self.Id
  39. }
  40. func (self *SEip) GetProjectId() string {
  41. return self.TenantId
  42. }
  43. func (self *SEip) GetStatus() string {
  44. return self.Status
  45. }
  46. func (self *SEip) Refresh() error {
  47. eip, err := self.region.GetEip(self.Id)
  48. if err != nil {
  49. return err
  50. }
  51. return jsonutils.Update(self, eip)
  52. }
  53. func (self *SEip) GetBillingType() string {
  54. return self.BillingType
  55. }
  56. func (self *SEip) GetIpAddr() string {
  57. return self.IpAddr
  58. }
  59. func (self *SEip) GetINetworkId() string {
  60. return self.NetworkId
  61. }
  62. func (self *SEip) GetAssociationType() string {
  63. return self.AssociateType
  64. }
  65. func (self *SEip) GetAssociationExternalId() string {
  66. return self.AssociateId
  67. }
  68. func (self *SEip) GetBandwidth() int {
  69. return self.Bandwidth
  70. }
  71. func (self *SEip) GetInternetChargeType() string {
  72. return self.ChargeType
  73. }
  74. func (self *SEip) Delete() error {
  75. return self.region.cli.delete(&modules.Elasticips, self.Id)
  76. }
  77. func (self *SEip) IsAutoRenew() bool {
  78. return self.AutoRenew
  79. }
  80. func (self *SEip) Associate(opts *cloudprovider.AssociateConfig) error {
  81. input := api.ElasticipAssociateInput{}
  82. input.InstanceType = opts.AssociateType
  83. input.InstanceId = opts.InstanceId
  84. switch opts.AssociateType {
  85. case api.EIP_ASSOCIATE_TYPE_SERVER:
  86. default:
  87. return cloudprovider.ErrNotImplemented
  88. }
  89. _, err := self.region.perform(&modules.Elasticips, self.Id, "associate", input)
  90. return err
  91. }
  92. func (self *SEip) Dissociate() error {
  93. _, err := self.region.perform(&modules.Elasticips, self.Id, "dissociate", nil)
  94. return err
  95. }
  96. func (self *SEip) GetCreatedAt() time.Time {
  97. return self.CreatedAt
  98. }
  99. func (self *SEip) GetExpiredAt() time.Time {
  100. return self.ExpiredAt
  101. }
  102. func (self *SEip) GetMode() string {
  103. return self.Mode
  104. }
  105. func (self *SEip) ChangeBandwidth(bw int) error {
  106. return cloudprovider.ErrNotImplemented
  107. }
  108. func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
  109. eip, err := self.GetEip(id)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return eip, nil
  114. }
  115. func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  116. eips, err := self.GetEips("")
  117. if err != nil {
  118. return nil, err
  119. }
  120. ret := []cloudprovider.ICloudEIP{}
  121. for i := range eips {
  122. eips[i].region = self
  123. ret = append(ret, &eips[i])
  124. }
  125. return ret, nil
  126. }
  127. func (self *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  128. input := api.SElasticipCreateInput{}
  129. input.Name = opts.Name
  130. input.CloudregionId = self.Id
  131. input.ChargeType = billing_api.TNetChargeType(opts.ChargeType)
  132. input.BandwidthMb = opts.BandwidthMbps
  133. input.ProjectId = opts.ProjectId
  134. input.NetworkId = opts.NetworkExternalId
  135. input.BgpType = opts.BGPType
  136. input.IpAddr = opts.Ip
  137. eip := &SEip{region: self}
  138. return eip, self.create(&modules.Elasticips, input, eip)
  139. }
  140. func (self *SRegion) GetEips(associateId string) ([]SEip, error) {
  141. eips := []SEip{}
  142. params := map[string]interface{}{}
  143. if len(associateId) > 0 {
  144. params["associate_id"] = associateId
  145. }
  146. return eips, self.list(&modules.Elasticips, params, &eips)
  147. }
  148. func (self *SRegion) GetEip(id string) (*SEip, error) {
  149. eip := &SEip{region: self}
  150. return eip, self.cli.get(&modules.Elasticips, id, nil, eip)
  151. }