eip.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 ucloud
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. )
  26. const (
  27. EIP_CHARGE_TYPE_BY_TRAFFIC = "traffic"
  28. EIP_CHARGE_TYPE_BY_BANDWIDTH = "bandwidth"
  29. )
  30. // https://docs.ucloud.cn/api/unet-api/describe_eip
  31. type SEip struct {
  32. region *SRegion
  33. multicloud.SEipBase
  34. UcloudTags
  35. BandwidthMb int `json:"Bandwidth"`
  36. BandwidthType int `json:"BandwidthType"`
  37. ChargeType string `json:"ChargeType"`
  38. CreateTime int64 `json:"CreateTime"`
  39. EIPAddr []EIPAddr `json:"EIPAddr"`
  40. EIPID string `json:"EIPId"`
  41. Expire bool `json:"Expire"`
  42. ExpireTime int64 `json:"ExpireTime"`
  43. Name string `json:"Name"`
  44. PayMode string `json:"PayMode"`
  45. Remark string `json:"Remark"`
  46. Resource Resource `json:"Resource"`
  47. ShareBandwidthSet ShareBandwidthSet `json:"ShareBandwidthSet"`
  48. Status string `json:"Status"`
  49. Tag string `json:"Tag"`
  50. Weight int `json:"Weight"`
  51. }
  52. func (self *SEip) GetProjectId() string {
  53. return self.region.client.projectId
  54. }
  55. type EIPAddr struct {
  56. IP string `json:"IP"`
  57. OperatorName string `json:"OperatorName"`
  58. }
  59. type Resource struct {
  60. ResourceID string `json:"ResourceID"`
  61. ResourceName string `json:"ResourceName"`
  62. ResourceType string `json:"ResourceType"`
  63. Zone string `json:"Zone"`
  64. }
  65. type ShareBandwidthSet struct {
  66. ShareBandwidth int `json:"ShareBandwidth"`
  67. ShareBandwidthID string `json:"ShareBandwidthId"`
  68. ShareBandwidthName string `json:"ShareBandwidthName"`
  69. }
  70. func (self *SEip) GetId() string {
  71. return self.EIPID
  72. }
  73. func (self *SEip) GetName() string {
  74. if len(self.Name) == 0 {
  75. return self.GetId()
  76. }
  77. return self.Name
  78. }
  79. func (self *SEip) GetGlobalId() string {
  80. return self.GetId()
  81. }
  82. // 弹性IP的资源绑定状态, 枚举值为: used: 已绑定, free: 未绑定, freeze: 已冻结
  83. func (self *SEip) GetStatus() string {
  84. switch self.Status {
  85. case "used":
  86. return api.EIP_STATUS_ASSOCIATE // ?
  87. case "free":
  88. return api.EIP_STATUS_READY
  89. case "freeze":
  90. return api.EIP_STATUS_UNKNOWN
  91. default:
  92. return api.EIP_STATUS_UNKNOWN
  93. }
  94. }
  95. func (self *SEip) Refresh() error {
  96. if self.IsEmulated() {
  97. return nil
  98. }
  99. new, err := self.region.GetEipById(self.GetId())
  100. if err != nil {
  101. return err
  102. }
  103. return jsonutils.Update(self, new)
  104. }
  105. func (self *SEip) IsEmulated() bool {
  106. return false
  107. }
  108. // 付费方式, 枚举值为: Year, 按年付费; Month, 按月付费; Dynamic, 按小时付费; Trial, 试用. 按小时付费和试用这两种付费模式需要开通权限.
  109. func (self *SEip) GetBillingType() string {
  110. switch self.ChargeType {
  111. case "Year", "Month":
  112. return billing_api.BILLING_TYPE_PREPAID
  113. default:
  114. return billing_api.BILLING_TYPE_POSTPAID
  115. }
  116. }
  117. func (self *SEip) GetCreatedAt() time.Time {
  118. return time.Unix(self.CreateTime, 0)
  119. }
  120. func (self *SEip) GetExpiredAt() time.Time {
  121. return time.Unix(self.ExpireTime, 0)
  122. }
  123. func (self *SEip) GetIpAddr() string {
  124. if len(self.EIPAddr) > 1 {
  125. log.Warningf("GetIpAddr %d eip addr found", len(self.EIPAddr))
  126. } else if len(self.EIPAddr) == 0 {
  127. return ""
  128. }
  129. return self.EIPAddr[0].IP
  130. }
  131. func (self *SEip) GetMode() string {
  132. return api.EIP_MODE_STANDALONE_EIP
  133. }
  134. func (self *SEip) GetAssociationType() string {
  135. switch self.Resource.ResourceType {
  136. case "uhost":
  137. return api.EIP_ASSOCIATE_TYPE_SERVER
  138. case "natgw":
  139. return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
  140. case "ulb":
  141. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  142. default:
  143. return self.Resource.ResourceType
  144. }
  145. }
  146. // 已绑定的资源类型, 枚举值为: uhost, 云主机;natgw:NAT网关;ulb:负载均衡器;upm: 物理机; hadoophost: 大数据集群;fortresshost:堡垒机;udockhost:容器;udhost:私有专区主机;vpngw:IPSec VPN;ucdr:云灾备;dbaudit:数据库审计。
  147. func (self *SEip) GetAssociationExternalId() string {
  148. return self.Resource.ResourceID
  149. }
  150. func (self *SEip) GetBandwidth() int {
  151. return self.BandwidthMb
  152. }
  153. func (self *SEip) GetINetworkId() string {
  154. return ""
  155. }
  156. // 弹性IP的计费模式, 枚举值为: "Bandwidth", 带宽计费; "Traffic", 流量计费; "ShareBandwidth",共享带宽模式. 默认为 "Bandwidth".
  157. func (self *SEip) GetInternetChargeType() string {
  158. switch self.PayMode {
  159. case "Bandwidth":
  160. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  161. case "Traffic":
  162. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  163. default:
  164. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  165. }
  166. }
  167. func (self *SEip) Delete() error {
  168. return self.region.DeallocateEIP(self.GetId())
  169. }
  170. func (self *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
  171. return self.region.AssociateEip(self.GetId(), conf.InstanceId)
  172. }
  173. func (self *SEip) Dissociate() error {
  174. return self.region.DissociateEip(self.GetId(), self.Resource.ResourceID)
  175. }
  176. func (self *SEip) ChangeBandwidth(bw int) error {
  177. return self.region.UpdateEipBandwidth(self.GetId(), bw)
  178. }
  179. // https://docs.ucloud.cn/api/unet-api/allocate_eip
  180. // 增加共享带宽模式ShareBandwidth
  181. func (self *SRegion) CreateEIP(eip *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  182. if len(eip.BGPType) == 0 {
  183. if strings.HasPrefix(self.GetId(), "cn-") {
  184. eip.BGPType = "Bgp"
  185. } else {
  186. eip.BGPType = "International"
  187. }
  188. }
  189. params := NewUcloudParams()
  190. params.Set("OperatorName", eip.BGPType)
  191. params.Set("Bandwidth", eip.BandwidthMbps)
  192. params.Set("Name", eip.Name)
  193. var payMode string
  194. switch eip.ChargeType {
  195. case api.EIP_CHARGE_TYPE_BY_TRAFFIC:
  196. payMode = "Traffic"
  197. case api.EIP_CHARGE_TYPE_BY_BANDWIDTH:
  198. payMode = "Bandwidth"
  199. }
  200. params.Set("PayMode", payMode)
  201. params.Set("ChargeType", "Dynamic") // 按需付费
  202. eips := make([]SEip, 0)
  203. err := self.DoAction("AllocateEIP", params, &eips)
  204. if err != nil {
  205. return nil, err
  206. }
  207. if len(eips) == 1 {
  208. eip := eips[0]
  209. eip.region = self
  210. eip.Refresh()
  211. return &eip, nil
  212. } else {
  213. return nil, fmt.Errorf("CreateEIP %d eip created", len(eips))
  214. }
  215. }
  216. // https://docs.ucloud.cn/api/unet-api/release_eip
  217. func (self *SRegion) DeallocateEIP(eipId string) error {
  218. params := NewUcloudParams()
  219. params.Set("EIPId", eipId)
  220. return self.DoAction("ReleaseEIP", params, nil)
  221. }
  222. // https://docs.ucloud.cn/api/unet-api/bind_eip
  223. func (self *SRegion) AssociateEip(eipId string, instanceId string) error {
  224. params := NewUcloudParams()
  225. params.Set("EIPId", eipId)
  226. params.Set("ResourceType", "uhost")
  227. params.Set("ResourceId", instanceId)
  228. return self.DoAction("BindEIP", params, nil)
  229. }
  230. // https://docs.ucloud.cn/api/unet-api/unbind_eip
  231. func (self *SRegion) DissociateEip(eipId string, instanceId string) error {
  232. params := NewUcloudParams()
  233. params.Set("EIPId", eipId)
  234. params.Set("ResourceType", "uhost")
  235. params.Set("ResourceId", instanceId)
  236. return self.DoAction("UnBindEIP", params, nil)
  237. }
  238. // https://docs.ucloud.cn/api/unet-api/modify_eip_bandwidth
  239. func (self *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
  240. params := NewUcloudParams()
  241. params.Set("EIPId", eipId)
  242. params.Set("Bandwidth", bw)
  243. return self.DoAction("ModifyEIPBandwidth", params, nil)
  244. }