vip.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 ucloud
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. "yunion.io/x/pkg/util/netutils"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type SVipAddr struct {
  23. VIP string
  24. SubnetId string
  25. }
  26. func (ip *SVipAddr) GetIP() string {
  27. return ip.VIP
  28. }
  29. func (ip *SVipAddr) GetINetworkId() string {
  30. return ip.SubnetId
  31. }
  32. func (ip *SVipAddr) IsPrimary() bool {
  33. return true
  34. }
  35. func (ip *SVipAddr) GetGlobalId() string {
  36. return ip.VIP
  37. }
  38. type SVip struct {
  39. multicloud.SNetworkInterfaceBase
  40. UcloudTags
  41. region *SRegion
  42. CreateTime int64
  43. Name string
  44. RealIp string
  45. Remark string
  46. SubnetId string
  47. Tag string
  48. VIP string
  49. VIPId string
  50. VPCId string
  51. Zone string
  52. }
  53. func (vip *SVip) GetName() string {
  54. if len(vip.Name) > 0 {
  55. return vip.Name
  56. }
  57. return vip.VIPId
  58. }
  59. func (vip *SVip) GetId() string {
  60. return vip.VIPId
  61. }
  62. func (vip *SVip) GetGlobalId() string {
  63. return vip.VIPId
  64. }
  65. func (vip *SVip) GetMacAddress() string {
  66. ip, _ := netutils.NewIPV4Addr(vip.VIP)
  67. return ip.ToMac("00:16:")
  68. }
  69. func (vip *SVip) GetAssociateType() string {
  70. return ""
  71. }
  72. func (vip *SVip) GetAssociateId() string {
  73. return ""
  74. }
  75. func (vip *SVip) GetStatus() string {
  76. return api.NETWORK_INTERFACE_STATUS_AVAILABLE
  77. }
  78. func (vip *SVip) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) {
  79. ip := &SVipAddr{VIP: vip.VIP, SubnetId: vip.SubnetId}
  80. return []cloudprovider.ICloudInterfaceAddress{ip}, nil
  81. }
  82. func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) {
  83. vips, err := region.GetVips()
  84. if err != nil {
  85. return nil, errors.Wrap(err, "region.GetVips")
  86. }
  87. ret := []cloudprovider.ICloudNetworkInterface{}
  88. for i := 0; i < len(vips); i++ {
  89. vips[i].region = region
  90. ret = append(ret, &vips[i])
  91. }
  92. return ret, nil
  93. }
  94. func (self *SRegion) GetVips() ([]SVip, error) {
  95. vips := []SVip{}
  96. params := NewUcloudParams()
  97. err := self.DoListAll("DescribeVIP", params, &vips)
  98. return vips, err
  99. }