vip.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "github.com/pkg/errors"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/netutils"
  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 SVirtualIP struct {
  26. multicloud.SNetworkInterfaceBase
  27. ZStackTags
  28. region *SRegion
  29. ZStackBasic
  30. IPRangeUUID string `json:"ipRangeUuid"`
  31. L3NetworkUUID string `json:"l3NetworkUuid"`
  32. IP string `json:"ip"`
  33. State string `json:"state"`
  34. Gateway string `json:"gateway"`
  35. Netmask string `json:"netmask"`
  36. PrefixLen int `json:"prefixLen"`
  37. ServiceProvider string `json:"serviceProvider"`
  38. PeerL3NetworkUuids []string `json:"peerL3NetworkUuids"`
  39. UseFor string `json:"useFor"`
  40. UsedIPUUID string `json:"usedIpUuid"`
  41. ZStackTime
  42. }
  43. type SInterfaceIP struct {
  44. IP string
  45. L3NetworkUUID string
  46. IPRangeUUID string
  47. }
  48. func (ip *SInterfaceIP) GetIP() string {
  49. return ip.IP
  50. }
  51. func (ip *SInterfaceIP) GetINetworkId() string {
  52. return fmt.Sprintf("%s/%s", ip.L3NetworkUUID, ip.IPRangeUUID)
  53. }
  54. func (ip *SInterfaceIP) IsPrimary() bool {
  55. return true
  56. }
  57. func (ip *SInterfaceIP) GetGlobalId() string {
  58. return ip.IP
  59. }
  60. func (vip *SVirtualIP) GetName() string {
  61. if len(vip.Name) > 0 {
  62. return vip.Name
  63. }
  64. return vip.UUID
  65. }
  66. func (vip *SVirtualIP) GetId() string {
  67. return vip.UUID
  68. }
  69. func (vip *SVirtualIP) GetGlobalId() string {
  70. return vip.UUID
  71. }
  72. func (vip *SVirtualIP) GetMacAddress() string {
  73. ip, _ := netutils.NewIPV4Addr(vip.IP)
  74. return ip.ToMac("00:16:")
  75. }
  76. func (vip *SVirtualIP) GetAssociateType() string {
  77. switch vip.UseFor {
  78. case "LoadBalancer":
  79. return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_LOADBALANCER
  80. case "Eip":
  81. return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_RESERVED
  82. }
  83. return vip.UseFor
  84. }
  85. func (vip *SVirtualIP) GetAssociateId() string {
  86. return vip.UsedIPUUID
  87. }
  88. func (vip *SVirtualIP) GetStatus() string {
  89. if vip.State == "Enabled" {
  90. return api.NETWORK_INTERFACE_STATUS_AVAILABLE
  91. }
  92. return api.NETWORK_INTERFACE_STATUS_UNKNOWN
  93. }
  94. func (vip *SVirtualIP) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) {
  95. ip := &SInterfaceIP{IP: vip.IP, IPRangeUUID: vip.IPRangeUUID, L3NetworkUUID: vip.L3NetworkUUID}
  96. return []cloudprovider.ICloudInterfaceAddress{ip}, nil
  97. }
  98. func (region *SRegion) GetVirtualIP(vipId string) (*SVirtualIP, error) {
  99. vip := &SVirtualIP{}
  100. return vip, region.client.getResource("vips", vipId, vip)
  101. }
  102. func (region *SRegion) GetNetworkId(vip *SVirtualIP) string {
  103. networks, err := region.GetNetworks("", "", vip.L3NetworkUUID, "")
  104. if err == nil {
  105. for _, network := range networks {
  106. if network.Contains(vip.IP) {
  107. return fmt.Sprintf("%s/%s", vip.L3NetworkUUID, network.UUID)
  108. }
  109. }
  110. }
  111. return ""
  112. }
  113. func (region *SRegion) GetVirtualIPs(vipId string) ([]SVirtualIP, error) {
  114. vips := []SVirtualIP{}
  115. params := url.Values{}
  116. if len(vipId) > 0 {
  117. params.Add("q", "uuid="+vipId)
  118. }
  119. return vips, region.client.listAll("vips", params, &vips)
  120. }
  121. func (region *SRegion) CreateVirtualIP(name, desc, ip string, l3Id string) (*SVirtualIP, error) {
  122. vip := SVirtualIP{}
  123. params := map[string]map[string]string{
  124. "params": {
  125. "name": name,
  126. "description": desc,
  127. "l3NetworkUuid": l3Id,
  128. },
  129. }
  130. if len(ip) > 0 {
  131. params["params"]["requiredIp"] = ip
  132. }
  133. resp, err := region.client.post("vips", jsonutils.Marshal(params))
  134. if err != nil {
  135. return nil, err
  136. }
  137. return &vip, resp.Unmarshal(&vip, "inventory")
  138. }
  139. func (region *SRegion) DeleteVirtualIP(vipId string) error {
  140. return region.client.delete("vips", vipId, "")
  141. }
  142. func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) {
  143. vips, err := region.GetVirtualIPs("")
  144. if err != nil {
  145. return nil, errors.Wrap(err, "region.GetVirtualIPs")
  146. }
  147. ret := []cloudprovider.ICloudNetworkInterface{}
  148. for i := 0; i < len(vips); i++ {
  149. if vips[i].UseFor != "Eip" {
  150. vips[i].region = region
  151. ret = append(ret, &vips[i])
  152. }
  153. }
  154. return ret, nil
  155. }