eip.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 zstack
  15. import (
  16. "fmt"
  17. "net/url"
  18. "time"
  19. "yunion.io/x/jsonutils"
  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 SEipAddress struct {
  25. region *SRegion
  26. multicloud.SEipBase
  27. ZStackTags
  28. ZStackBasic
  29. VMNicUUID string `json:"vmNicUuid"`
  30. VipUUID string `json:"vipUuid"`
  31. State string `json:"state"`
  32. VipIP string `json:"vipIp"`
  33. GuestIP string `json:"guestIp"`
  34. ZStackTime
  35. }
  36. func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
  37. eip := &SEipAddress{region: region}
  38. return eip, region.client.getResource("eips", eipId, eip)
  39. }
  40. func (region *SRegion) GetEips(eipId, instanceId string) ([]SEipAddress, error) {
  41. eips := []SEipAddress{}
  42. params := url.Values{}
  43. if len(eipId) > 0 {
  44. params.Add("q", "uuid="+eipId)
  45. }
  46. if len(instanceId) > 0 {
  47. params.Add("q", "vmNic.vmInstanceUuid="+instanceId)
  48. }
  49. err := region.client.listAll("eips", params, &eips)
  50. if err != nil {
  51. return nil, err
  52. }
  53. for i := 0; i < len(eips); i++ {
  54. eips[i].region = region
  55. }
  56. return eips, nil
  57. }
  58. func (eip *SEipAddress) GetId() string {
  59. return eip.UUID
  60. }
  61. func (eip *SEipAddress) GetName() string {
  62. return eip.Name
  63. }
  64. func (eip *SEipAddress) GetGlobalId() string {
  65. return eip.UUID
  66. }
  67. func (eip *SEipAddress) GetStatus() string {
  68. return api.EIP_STATUS_READY
  69. }
  70. func (eip *SEipAddress) Refresh() error {
  71. new, err := eip.region.GetEip(eip.UUID)
  72. if err != nil {
  73. return err
  74. }
  75. return jsonutils.Update(eip, new)
  76. }
  77. func (eip *SEipAddress) IsEmulated() bool {
  78. return false
  79. }
  80. func (eip *SEipAddress) GetIpAddr() string {
  81. return eip.VipIP
  82. }
  83. func (eip *SEipAddress) GetMode() string {
  84. return api.EIP_MODE_STANDALONE_EIP
  85. }
  86. func (eip *SEipAddress) GetAssociationType() string {
  87. if len(eip.GetAssociationExternalId()) > 0 {
  88. return api.EIP_ASSOCIATE_TYPE_SERVER
  89. }
  90. return ""
  91. }
  92. func (eip *SEipAddress) GetAssociationExternalId() string {
  93. if len(eip.VMNicUUID) > 0 {
  94. instances, err := eip.region.GetInstances("", "", eip.VMNicUUID)
  95. if err == nil && len(instances) == 1 {
  96. return instances[0].UUID
  97. }
  98. }
  99. return ""
  100. }
  101. func (eip *SEipAddress) GetBillingType() string {
  102. return ""
  103. }
  104. func (eip *SEipAddress) GetCreatedAt() time.Time {
  105. return eip.CreateDate
  106. }
  107. func (eip *SEipAddress) GetExpiredAt() time.Time {
  108. return time.Time{}
  109. }
  110. func (eip *SEipAddress) Delete() error {
  111. return eip.region.DeleteVirtualIP(eip.VipUUID)
  112. }
  113. func (eip *SEipAddress) GetBandwidth() int {
  114. return 0
  115. }
  116. func (eip *SEipAddress) GetINetworkId() string {
  117. vip, err := eip.region.GetVirtualIP(eip.VipUUID)
  118. if err == nil {
  119. return eip.region.GetNetworkId(vip)
  120. }
  121. return ""
  122. }
  123. func (eip *SEipAddress) GetInternetChargeType() string {
  124. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  125. }
  126. func (eip *SEipAddress) Associate(conf *cloudprovider.AssociateConfig) error {
  127. return eip.region.AssociateEip(conf.InstanceId, eip.UUID)
  128. }
  129. func (eip *SEipAddress) Dissociate() error {
  130. return eip.region.DisassociateEip(eip.UUID)
  131. }
  132. func (eip *SEipAddress) ChangeBandwidth(bw int) error {
  133. return cloudprovider.ErrNotSupported
  134. }
  135. func (eip *SEipAddress) GetProjectId() string {
  136. return ""
  137. }
  138. func (region *SRegion) CreateEip(name string, vipId string, desc string) (*SEipAddress, error) {
  139. params := map[string]map[string]string{
  140. "params": {
  141. "name": name,
  142. "description": desc,
  143. "vipUuid": vipId,
  144. },
  145. }
  146. eip := &SEipAddress{region: region}
  147. resp, err := region.client.post("eips", jsonutils.Marshal(params))
  148. if err != nil {
  149. return nil, err
  150. }
  151. return eip, resp.Unmarshal(eip, "inventory")
  152. }
  153. func (region *SRegion) AssociateEip(instanceId, eipId string) error {
  154. instance, err := region.GetInstance(instanceId)
  155. if err != nil {
  156. return err
  157. }
  158. if len(instance.VMNics) == 0 {
  159. return fmt.Errorf("instance %s does not have any nic", instance.Name)
  160. }
  161. resource := fmt.Sprintf("eips/%s/vm-instances/nics/%s", eipId, instance.VMNics[0].UUID)
  162. params := map[string]interface{}{
  163. "params": jsonutils.NewDict(),
  164. }
  165. _, err = region.client.post(resource, jsonutils.Marshal(params))
  166. return err
  167. }
  168. func (region *SRegion) DisassociateEip(eipId string) error {
  169. resource := fmt.Sprintf("eips/%s/vm-instances/nics", eipId)
  170. return region.client.delete(resource, "", "")
  171. }