eip.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 ecloud
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SEip struct {
  25. region *SRegion
  26. multicloud.SEipBase
  27. EcloudTags
  28. BandWidthMbSize int `json:"bandwidthMbSize,omitempty"`
  29. BandWidthType string `json:"bandwidthType,omitempty"`
  30. BindType string `json:"bindType,omitempty"`
  31. // 使用,未使用
  32. Bound bool `json:"bound,omitempty"`
  33. // bandwidthCharge, trafficCharge
  34. ChargeModeEnum string `json:"chargeModeEnum,omitempty"`
  35. CreateTime time.Time `json:"-"`
  36. // OpenAPI 返回的创建时间字符串
  37. CreatedTimeStr string `json:"createdTime,omitempty"`
  38. Frozen bool `json:"frozen,omitempty"`
  39. // 备案状态
  40. IcpStatus string `json:"icpStatus,omitempty"`
  41. Id string `json:"id,omitempty"`
  42. IpType string `json:"ipType,omitempty"`
  43. Ipv6 string `json:"ipv6,omitempty"`
  44. Name string `json:"name,omitempty"` // 公网IPv4地址
  45. NicName string `json:"nicName,omitempty"`
  46. PortNetworkId string `json:"portNetworkId,omitempty"`
  47. Region string `json:"region,omitempty"`
  48. ResourceId string `json:"resourceId,omitempty"`
  49. RouterId string `json:"routerId,omitempty"`
  50. // BINDING, UNBOUND, FROZEN
  51. Status string `json:"status,omitempty"`
  52. }
  53. func (e *SEip) GetId() string {
  54. return e.Id
  55. }
  56. func (e *SEip) GetName() string {
  57. return e.Name
  58. }
  59. func (e *SEip) GetGlobalId() string {
  60. return e.Id
  61. }
  62. func (e *SEip) GetStatus() string {
  63. switch e.Status {
  64. case "BINDING", "UNBOUND":
  65. return api.EIP_STATUS_READY
  66. default:
  67. return api.EIP_STATUS_UNKNOWN
  68. }
  69. }
  70. func (e *SEip) Refresh() error {
  71. return cloudprovider.ErrNotImplemented
  72. }
  73. func (e *SEip) IsEmulated() bool {
  74. return false
  75. }
  76. func (e *SEip) GetIpAddr() string {
  77. return e.Name
  78. }
  79. func (e *SEip) GetMode() string {
  80. return api.EIP_MODE_STANDALONE_EIP
  81. }
  82. func (e *SEip) GetAssociationType() string {
  83. switch e.BindType {
  84. case "vm":
  85. return api.EIP_ASSOCIATE_TYPE_SERVER
  86. case "snat", "dnat":
  87. return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
  88. case "elb":
  89. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  90. default:
  91. return e.BindType
  92. }
  93. }
  94. func (e *SEip) GetAssociationExternalId() string {
  95. return e.ResourceId
  96. }
  97. func (e *SEip) GetBillingType() string {
  98. return billing_api.BILLING_TYPE_POSTPAID
  99. }
  100. func (e *SEip) GetCreatedAt() time.Time {
  101. if len(e.CreatedTimeStr) > 0 {
  102. if t, err := time.Parse("2006-01-02 15:04:05", e.CreatedTimeStr); err == nil {
  103. return t
  104. }
  105. if t, err := time.Parse(time.RFC3339, e.CreatedTimeStr); err == nil {
  106. return t
  107. }
  108. }
  109. return e.CreateTime
  110. }
  111. func (e *SEip) GetExpiredAt() time.Time {
  112. return time.Time{}
  113. }
  114. func (e *SEip) Delete() error {
  115. return cloudprovider.ErrNotImplemented
  116. }
  117. func (e *SEip) GetBandwidth() int {
  118. return e.BandWidthMbSize
  119. }
  120. func (e *SEip) GetINetworkId() string {
  121. return ""
  122. }
  123. func (e *SEip) GetInternetChargeType() string {
  124. switch e.ChargeModeEnum {
  125. // bandwidthCharge, trafficCharge
  126. case "trafficCharge":
  127. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  128. case "bandwidthCharge":
  129. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  130. default:
  131. return "unknown"
  132. }
  133. }
  134. func (e *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
  135. return cloudprovider.ErrNotImplemented
  136. }
  137. func (e *SEip) Dissociate() error {
  138. return cloudprovider.ErrNotImplemented
  139. }
  140. func (e *SEip) ChangeBandwidth(bw int) error {
  141. return cloudprovider.ErrNotImplemented
  142. }
  143. func (e *SEip) GetProjectId() string {
  144. return ""
  145. }
  146. func (r *SRegion) GetEipById(id string) (*SEip, error) {
  147. var eip SEip
  148. // 使用 OpenAPI EIP 详情:GET /api/openapi-eip/acl/v3/floatingip/getRespWithBw/{ipId}
  149. req := NewOpenApiEbsRequest(r.RegionId, fmt.Sprintf("/api/openapi-eip/acl/v3/floatingip/getRespWithBw/%s", id), nil, nil)
  150. err := r.client.doGet(context.Background(), req.Base(), &eip)
  151. if err != nil {
  152. return nil, err
  153. }
  154. eip.region = r
  155. return &eip, nil
  156. }
  157. // GetEipByAddr 使用 OpenAPI EIP 详情查询(按地址):
  158. // GET /api/openapi-eip/acl/v3/floatingip/apiDetail?ipAddr={addr}
  159. func (r *SRegion) GetEipByAddr(addr string) (*SEip, error) {
  160. var eip SEip
  161. params := map[string]string{
  162. "ipAddress": addr,
  163. }
  164. req := NewOpenApiEbsRequest(r.RegionId, "/api/openapi-eip/acl/v3/floatingip/apiDetail", params, nil)
  165. if err := r.client.doGet(context.Background(), req.Base(), &eip); err != nil {
  166. return nil, err
  167. }
  168. eip.region = r
  169. return &eip, nil
  170. }