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