yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/networkinterfaces.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 apsara 16 17 import ( 18 "fmt" 19 "time" 20 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SPrivateIp struct { 29 nic *SNetworkInterface 30 Primary bool 31 PrivateIpAddress string 32 } 33 34 func (ip *SPrivateIp) GetGlobalId() string { 35 return ip.PrivateIpAddress 36 } 37 38 func (ip *SPrivateIp) GetINetworkId() string { 39 return ip.nic.VSwitchId 40 } 41 42 func (ip *SPrivateIp) GetIP() string { 43 return ip.PrivateIpAddress 44 } 45 46 func (ip *SPrivateIp) IsPrimary() bool { 47 return ip.Primary 48 } 49 50 type SPrivateIpSets struct { 51 PrivateIpSet []SPrivateIp 52 } 53 54 type SNetworkInterface struct { 55 multicloud.SNetworkInterfaceBase 56 ApsaraTags 57 region *SRegion 58 59 InstanceId string 60 CreationTime time.Time 61 MacAddress string 62 NetworkInterfaceName string 63 PrivateIpSets SPrivateIpSets 64 SecurityGroupIds SSecurityGroupIds 65 Status string 66 Type string 67 VSwitchId string 68 VpcId string 69 ZoneId string 70 NetworkInterfaceId string 71 PrimaryIpAddress string 72 PrivateIpAddress string 73 DepartmentInfo 74 } 75 76 func (nic *SNetworkInterface) GetName() string { 77 return nic.NetworkInterfaceName 78 } 79 80 func (nic *SNetworkInterface) GetId() string { 81 return nic.NetworkInterfaceId 82 } 83 84 func (nic *SNetworkInterface) GetGlobalId() string { 85 return nic.NetworkInterfaceId 86 } 87 88 func (nic *SNetworkInterface) GetAssociateId() string { 89 return nic.InstanceId 90 } 91 92 func (nic *SNetworkInterface) GetAssociateType() string { 93 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_SERVER 94 } 95 96 func (nic *SNetworkInterface) GetMacAddress() string { 97 return nic.MacAddress 98 } 99 100 func (nic *SNetworkInterface) GetStatus() string { 101 switch nic.Status { 102 case "Available": 103 return api.NETWORK_INTERFACE_STATUS_AVAILABLE 104 } 105 return nic.Status 106 } 107 108 func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) { 109 interfaces := []SNetworkInterface{} 110 for { 111 parts, total, err := region.GetNetworkInterfaces("", len(interfaces), 50) 112 if err != nil { 113 return nil, err 114 } 115 interfaces = append(interfaces, parts...) 116 if len(interfaces) >= total { 117 break 118 } 119 } 120 ret := []cloudprovider.ICloudNetworkInterface{} 121 for i := 0; i < len(interfaces); i++ { 122 // 阿里云实例的弹性网卡已经在guestnetwork同步了 123 if len(interfaces[i].InstanceId) == 0 { 124 interfaces[i].region = region 125 ret = append(ret, &interfaces[i]) 126 } 127 } 128 return ret, nil 129 } 130 131 func (nic *SNetworkInterface) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) { 132 address := []cloudprovider.ICloudInterfaceAddress{} 133 for i := 0; i < len(nic.PrivateIpSets.PrivateIpSet); i++ { 134 nic.PrivateIpSets.PrivateIpSet[i].nic = nic 135 address = append(address, &nic.PrivateIpSets.PrivateIpSet[i]) 136 } 137 return address, nil 138 } 139 140 func (region *SRegion) GetNetworkInterfaces(instanceId string, offset int, limit int) ([]SNetworkInterface, int, error) { 141 if limit > 50 || limit <= 0 { 142 limit = 50 143 } 144 145 params := map[string]string{ 146 "RegionId": region.RegionId, 147 "PageSize": fmt.Sprintf("%d", limit), 148 "PageNumber": fmt.Sprintf("%d", (offset/limit)+1), 149 } 150 151 if len(instanceId) > 0 { 152 params["InstanceId"] = instanceId 153 } 154 155 body, err := region.ecsRequest("DescribeNetworkInterfaces", params) 156 if err != nil { 157 return nil, 0, errors.Wrap(err, "DescribeNetworkInterfaces") 158 } 159 160 interfaces := []SNetworkInterface{} 161 err = body.Unmarshal(&interfaces, "NetworkInterfaceSets", "NetworkInterfaceSet") 162 if err != nil { 163 return nil, 0, errors.Wrap(err, "Unmarshal") 164 } 165 total, _ := body.Int("TotalCount") 166 return interfaces, int(total), nil 167 }