yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/networkinterface.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 qcloud 16 17 import ( 18 "fmt" 19 "strings" 20 "time" 21 22 "github.com/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 type SPrivateIpAddress struct { 30 nic *SNetworkInterface 31 Description string 32 Primary bool 33 PrivateIpAddress string 34 PublicIpAddress string 35 IsWanIpBlocked bool 36 State string 37 } 38 39 func (ip *SPrivateIpAddress) GetGlobalId() string { 40 return ip.PrivateIpAddress 41 } 42 43 func (ip *SPrivateIpAddress) GetIP() string { 44 return ip.PrivateIpAddress 45 } 46 47 func (ip *SPrivateIpAddress) GetINetworkId() string { 48 return ip.nic.SubnetId 49 } 50 51 func (ip *SPrivateIpAddress) IsPrimary() bool { 52 return ip.Primary 53 } 54 55 type SNetworkInterfaceAttachment struct { 56 InstanceId string 57 DeviceIndex int 58 InstanceAccountId string 59 AttachTime string 60 } 61 62 type SNetworkInterface struct { 63 multicloud.SNetworkInterfaceBase 64 QcloudTags 65 region *SRegion 66 VpcId string 67 SubnetId string 68 NetworkInterfaceId string 69 NetworkInterfaceName string 70 NetworkInterfaceDescription string 71 GroupSet []string 72 Primary bool 73 MacAddress string 74 State string 75 CreatedTime time.Time 76 Attachment SNetworkInterfaceAttachment 77 Zone string 78 PrivateIpAddressSet []SPrivateIpAddress 79 } 80 81 func (nic *SNetworkInterface) GetName() string { 82 return nic.NetworkInterfaceName 83 } 84 85 func (nic *SNetworkInterface) GetId() string { 86 return nic.NetworkInterfaceId 87 } 88 89 func (nic *SNetworkInterface) GetGlobalId() string { 90 return nic.NetworkInterfaceId 91 } 92 93 func (nic *SNetworkInterface) GetMacAddress() string { 94 return nic.MacAddress 95 } 96 97 func (nic *SNetworkInterface) GetAssociateType() string { 98 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_SERVER 99 } 100 101 func (nic *SNetworkInterface) GetAssociateId() string { 102 return nic.Attachment.InstanceId 103 } 104 105 func (nic *SNetworkInterface) GetStatus() string { 106 switch nic.State { 107 case "PENDING": 108 return api.NETWORK_INTERFACE_STATUS_CREATING 109 case "AVAILABLE": 110 return api.NETWORK_INTERFACE_STATUS_AVAILABLE 111 case "ATTACHING": 112 return api.NETWORK_INTERFACE_STATUS_ATTACHING 113 case "DETACHING": 114 return api.NETWORK_INTERFACE_STATUS_DETACHING 115 case "DELETING": 116 return api.NETWORK_INTERFACE_STATUS_DELETING 117 } 118 return nic.State 119 } 120 121 func (nic *SNetworkInterface) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) { 122 address := []cloudprovider.ICloudInterfaceAddress{} 123 for i := 0; i < len(nic.PrivateIpAddressSet); i++ { 124 nic.PrivateIpAddressSet[i].nic = nic 125 address = append(address, &nic.PrivateIpAddressSet[i]) 126 } 127 return address, nil 128 } 129 130 func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) { 131 interfaces := []SNetworkInterface{} 132 for { 133 parts, total, err := region.GetNetworkInterfaces([]string{}, "", "", len(interfaces), 50) 134 if err != nil { 135 return nil, err 136 } 137 interfaces = append(interfaces, parts...) 138 if len(interfaces) >= total { 139 break 140 } 141 } 142 ret := []cloudprovider.ICloudNetworkInterface{} 143 for i := 0; i < len(interfaces); i++ { 144 if !strings.HasPrefix(interfaces[i].Attachment.InstanceId, "ins-") { //弹性网卡有可能已绑定资源,若绑定资源则由资源进行同步 145 interfaces[i].region = region 146 ret = append(ret, &interfaces[i]) 147 } 148 } 149 return ret, nil 150 } 151 152 func (region *SRegion) GetNetworkInterfaces(nicIds []string, instanceId, subnetId string, offset int, limit int) ([]SNetworkInterface, int, error) { 153 if limit > 50 || limit <= 0 { 154 limit = 50 155 } 156 params := map[string]string{} 157 params["Limit"] = fmt.Sprintf("%d", limit) 158 params["Offset"] = fmt.Sprintf("%d", offset) 159 160 for idx, interfaceId := range nicIds { 161 params[fmt.Sprintf("NetworkInterfaceIds.%d", idx)] = interfaceId 162 } 163 164 idx := 0 165 if len(subnetId) > 0 { 166 params[fmt.Sprintf("Filters.%d.Name", idx)] = "subnet-id" 167 params[fmt.Sprintf("Filters.%d.Values.0", idx)] = subnetId 168 idx++ 169 } 170 if len(instanceId) > 0 { 171 params[fmt.Sprintf("Filters.%d.Name", idx)] = "attachment.instance-id" 172 params[fmt.Sprintf("Filters.%d.Values.0", idx)] = instanceId 173 idx++ 174 } 175 body, err := region.vpcRequest("DescribeNetworkInterfaces", params) 176 if err != nil { 177 return nil, 0, errors.Wrapf(err, "DescribeNetworkInterfaces") 178 } 179 180 interfaces := []SNetworkInterface{} 181 err = body.Unmarshal(&interfaces, "NetworkInterfaceSet") 182 if err != nil { 183 return nil, 0, errors.Wrapf(err, "Unmarshal.NetworkInterfaceSet") 184 } 185 total, _ := body.Float("TotalCount") 186 return interfaces, int(total), nil 187 } 188 189 func (region *SRegion) DeleteNetworkInterface(interfaceId string) error { 190 params := map[string]string{} 191 params["Region"] = region.Region 192 params["NetworkInterfaceId"] = interfaceId 193 194 _, err := region.vpcRequest("DeleteNetworkInterface", params) 195 if err != nil { 196 return errors.Wrapf(err, "vpcRequest.DeleteNetworkInterface") 197 } 198 return nil 199 }