eip.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 baidu
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "time"
  20. billing_api "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. "yunion.io/x/jsonutils"
  25. "yunion.io/x/pkg/utils"
  26. )
  27. type SEip struct {
  28. multicloud.SEipBase
  29. SBaiduTag
  30. region *SRegion
  31. Name string
  32. Eip string
  33. EipId string
  34. Status string
  35. InstanceType string
  36. InstanceId string
  37. ShareGroupId string
  38. EipInstanceType string
  39. BandwidthInMbps int
  40. PaymentTiming string
  41. BilingMethod string
  42. CreateTime time.Time
  43. ExpireTime time.Time
  44. Region string
  45. RouteTypt string
  46. }
  47. func (self *SEip) GetId() string {
  48. return self.Eip
  49. }
  50. func (self *SEip) GetName() string {
  51. return self.Name
  52. }
  53. func (self *SEip) GetGlobalId() string {
  54. return self.Eip
  55. }
  56. func (self *SEip) GetStatus() string {
  57. switch self.Status {
  58. case "creating":
  59. return api.EIP_STATUS_ALLOCATE
  60. case "available", "binded", "updating", "paused":
  61. return api.EIP_STATUS_READY
  62. case "binding":
  63. return api.EIP_STATUS_ASSOCIATE
  64. case "unbinding":
  65. return api.EIP_STATUS_DISSOCIATE
  66. case "unavailable":
  67. return api.EIP_STATUS_UNKNOWN
  68. default:
  69. return api.EIP_STATUS_UNKNOWN
  70. }
  71. }
  72. func (self *SEip) Refresh() error {
  73. eip, err := self.region.GetEip(self.Eip)
  74. if err != nil {
  75. return err
  76. }
  77. return jsonutils.Update(self, eip)
  78. }
  79. func (self *SEip) GetIpAddr() string {
  80. return self.Eip
  81. }
  82. func (self *SEip) GetMode() string {
  83. return api.EIP_MODE_STANDALONE_EIP
  84. }
  85. func (self *SEip) GetAssociationType() string {
  86. switch self.InstanceType {
  87. case "BCC", "BBC", "DCC", "ENI":
  88. return api.EIP_ASSOCIATE_TYPE_SERVER
  89. case "NAT":
  90. return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
  91. case "BLB":
  92. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  93. default:
  94. return strings.ToLower(self.InstanceType)
  95. }
  96. }
  97. func (self *SEip) GetAssociationExternalId() string {
  98. return self.InstanceId
  99. }
  100. func (self *SEip) GetBillingType() string {
  101. if strings.EqualFold(self.PaymentTiming, "prepaid") {
  102. return billing_api.BILLING_TYPE_PREPAID
  103. }
  104. return billing_api.BILLING_TYPE_POSTPAID
  105. }
  106. func (self *SEip) GetCreatedAt() time.Time {
  107. return self.CreateTime
  108. }
  109. func (self *SEip) GetExpiredAt() time.Time {
  110. return self.ExpireTime
  111. }
  112. func (self *SEip) Delete() error {
  113. return self.region.DeleteEip(self.Eip)
  114. }
  115. func (self *SEip) GetBandwidth() int {
  116. return self.BandwidthInMbps
  117. }
  118. func (self *SEip) GetInternetChargeType() string {
  119. if strings.EqualFold(self.BilingMethod, "ByTraffic") {
  120. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  121. }
  122. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  123. }
  124. func (self *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
  125. return self.region.AssociateEip(self.Eip, conf.InstanceId)
  126. }
  127. func (region *SRegion) AssociateEip(id string, instanceId string) error {
  128. params := url.Values{}
  129. params.Set("clientToken", utils.GenRequestId(20))
  130. params.Set("bind", "")
  131. body := map[string]interface{}{
  132. "instanceType": "BCC",
  133. "instanceId": instanceId,
  134. }
  135. _, err := region.eipUpdate(fmt.Sprintf("v1/eip/%s", id), params, body)
  136. return err
  137. }
  138. func (self *SEip) Dissociate() error {
  139. return self.region.DissociateEip(self.Eip)
  140. }
  141. func (region *SRegion) DissociateEip(id string) error {
  142. params := url.Values{}
  143. params.Set("clientToken", utils.GenRequestId(20))
  144. params.Set("unbind", "")
  145. _, err := region.eipUpdate(fmt.Sprintf("v1/eip/%s", id), params, nil)
  146. return err
  147. }
  148. func (self *SEip) ChangeBandwidth(bw int) error {
  149. return self.region.UpdateEipBandwidth(self.Eip, bw)
  150. }
  151. func (self *SEip) GetProjectId() string {
  152. return ""
  153. }
  154. func (region *SRegion) GetEips(instanceId string) ([]SEip, error) {
  155. params := url.Values{}
  156. if len(instanceId) > 0 {
  157. params.Set("instanceId", instanceId)
  158. params.Set("instanceType", "BCC")
  159. }
  160. ret := []SEip{}
  161. for {
  162. resp, err := region.eipList("v1/eip", params)
  163. if err != nil {
  164. return nil, err
  165. }
  166. part := struct {
  167. NextMarker string
  168. EipList []SEip
  169. }{}
  170. err = resp.Unmarshal(&part)
  171. if err != nil {
  172. return nil, err
  173. }
  174. ret = append(ret, part.EipList...)
  175. if len(part.NextMarker) == 0 {
  176. break
  177. }
  178. params.Set("marker", part.NextMarker)
  179. }
  180. return ret, nil
  181. }
  182. func (region *SRegion) GetEip(id string) (*SEip, error) {
  183. resp, err := region.eipList(fmt.Sprintf("v1/eip/%s", id), nil)
  184. if err != nil {
  185. return nil, err
  186. }
  187. ret := &SEip{region: region}
  188. err = resp.Unmarshal(ret)
  189. if err != nil {
  190. return nil, err
  191. }
  192. return ret, nil
  193. }
  194. func (region *SRegion) DeleteEip(id string) error {
  195. _, err := region.eipDelete(fmt.Sprintf("v1/eip/%s", id), nil)
  196. return err
  197. }
  198. func (region *SRegion) UpdateEipBandwidth(id string, bw int) error {
  199. params := url.Values{}
  200. params.Set("clientToken", utils.GenRequestId(20))
  201. params.Set("resize", "")
  202. body := map[string]interface{}{
  203. "newBandwidthInMbps": bw,
  204. }
  205. _, err := region.eipUpdate(fmt.Sprintf("v1/eip/%s", id), params, body)
  206. return err
  207. }
  208. func (region *SRegion) CreateEip(opts *cloudprovider.SEip) (*SEip, error) {
  209. params := url.Values{}
  210. params.Set("clientToken", utils.GenRequestId(20))
  211. tags := []BaiduTag{}
  212. for k, v := range opts.Tags {
  213. tags = append(tags, BaiduTag{
  214. TagKey: k,
  215. TagValue: v,
  216. })
  217. }
  218. billing := map[string]interface{}{
  219. "paymentTiming": "Postpaid",
  220. "billingMethod": "ByTraffic",
  221. }
  222. if opts.ChargeType == api.EIP_CHARGE_TYPE_BY_BANDWIDTH {
  223. billing["billingMethod"] = "ByBandwidth"
  224. }
  225. body := map[string]interface{}{
  226. "name": opts.Name,
  227. "bandwidthInMbps": opts.BandwidthMbps,
  228. "tags": tags,
  229. "billing": billing,
  230. }
  231. if len(opts.BGPType) > 0 {
  232. body["routeType"] = opts.BGPType
  233. }
  234. resp, err := region.eipPost("v1/eip", params, body)
  235. if err != nil {
  236. return nil, err
  237. }
  238. eipId, err := resp.GetString("eip")
  239. if err != nil {
  240. return nil, err
  241. }
  242. return region.GetEip(eipId)
  243. }