eip.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 jdcloud
  15. import (
  16. "fmt"
  17. "time"
  18. "github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/apis"
  19. "github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/client"
  20. "github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/models"
  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. type SEip struct {
  26. region *SRegion
  27. multicloud.SEipBase
  28. JdcloudTags
  29. models.ElasticIp
  30. }
  31. func (e *SEip) GetId() string {
  32. return e.ElasticIpId
  33. }
  34. func (e *SEip) GetName() string {
  35. return e.ElasticIpAddress
  36. }
  37. func (e *SEip) GetGlobalId() string {
  38. return e.GetId()
  39. }
  40. func (e *SEip) GetStatus() string {
  41. return api.EIP_STATUS_READY
  42. }
  43. func (e *SEip) Refresh() error {
  44. return nil
  45. }
  46. func (e *SEip) IsEmulated() bool {
  47. return false
  48. }
  49. func (e *SEip) GetIpAddr() string {
  50. return e.ElasticIpAddress
  51. }
  52. func (e *SEip) GetMode() string {
  53. return api.EIP_MODE_STANDALONE_EIP
  54. }
  55. func (e *SEip) GetAssociationType() string {
  56. switch e.InstanceType {
  57. case "compute":
  58. return api.EIP_ASSOCIATE_TYPE_SERVER
  59. case "lb":
  60. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  61. default:
  62. return e.InstanceType
  63. }
  64. }
  65. func (e *SEip) GetAssociationExternalId() string {
  66. return e.InstanceId
  67. }
  68. func (e *SEip) GetBillingType() string {
  69. return billingType(&e.Charge)
  70. }
  71. func (e *SEip) GetCreatedAt() time.Time {
  72. return parseTime(e.CreatedTime)
  73. }
  74. func (e *SEip) GetExpiredAt() time.Time {
  75. return expireAt(&e.Charge)
  76. }
  77. func (e *SEip) Delete() error {
  78. return nil
  79. }
  80. func (e *SEip) GetBandwidth() int {
  81. return e.BandwidthMbps
  82. }
  83. func (e *SEip) GetINetworkId() string {
  84. return ""
  85. }
  86. func (e *SEip) GetInternetChargeType() string {
  87. switch e.Charge.ChargeMode {
  88. case "postpaid_by_usage":
  89. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  90. case "postpaid_by_duration":
  91. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  92. default:
  93. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  94. }
  95. }
  96. func (e *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
  97. return nil
  98. }
  99. func (e *SEip) Dissociate() error {
  100. return nil
  101. }
  102. func (e *SEip) ChangeBandwidth(bw int) error {
  103. return nil
  104. }
  105. func (e *SEip) GetProjectId() string {
  106. return ""
  107. }
  108. func (r *SRegion) GetEIPById(id string) (*SEip, error) {
  109. req := apis.NewDescribeElasticIpRequest(r.ID, id)
  110. client := client.NewVpcClient(r.getCredential())
  111. client.Logger = Logger{debug: r.client.debug}
  112. resp, err := client.DescribeElasticIp(req)
  113. if err != nil {
  114. return nil, err
  115. }
  116. if resp.Error.Code >= 400 {
  117. return nil, fmt.Errorf("%s", resp.Error.Message)
  118. }
  119. return &SEip{
  120. region: r,
  121. ElasticIp: resp.Result.ElasticIp,
  122. }, nil
  123. }