yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/port.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 hcs 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 22 "yunion.io/x/pkg/utils" 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 "yunion.io/x/cloudmux/pkg/multicloud/huawei" 28 ) 29 30 type SFixedIP struct { 31 IpAddress string 32 SubnetId string 33 NetworkId string 34 } 35 36 func (fixip *SFixedIP) GetGlobalId() string { 37 return fixip.IpAddress 38 } 39 40 func (fixip *SFixedIP) GetIP() string { 41 return fixip.IpAddress 42 } 43 44 func (fixip *SFixedIP) GetINetworkId() string { 45 return fixip.NetworkId 46 } 47 48 func (fixip *SFixedIP) IsPrimary() bool { 49 return true 50 } 51 52 type Port struct { 53 multicloud.SNetworkInterfaceBase 54 huawei.HuaweiTags 55 region *SRegion 56 Id string `json:"id"` 57 Name string `json:"name"` 58 Status string `json:"status"` 59 AdminStateUp string `json:"admin_state_up"` 60 DNSName string `json:"dns_name"` 61 MACAddress string `json:"mac_address"` 62 NetworkId string `json:"network_id"` 63 TenantId string `json:"tenant_id"` 64 DeviceId string `json:"device_id"` 65 DeviceOwner string `json:"device_owner"` 66 BindingVnicType string `json:"binding:vnic_type"` 67 FixedIps []SFixedIP 68 } 69 70 func (port *Port) GetName() string { 71 if len(port.Name) > 0 { 72 return port.Name 73 } 74 return port.Id 75 } 76 77 func (port *Port) GetId() string { 78 return port.Id 79 } 80 81 func (port *Port) GetGlobalId() string { 82 return port.Id 83 } 84 85 func (port *Port) GetMacAddress() string { 86 return port.MACAddress 87 } 88 89 // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0133195888.html 90 func (port *Port) GetAssociateType() string { 91 switch port.DeviceOwner { 92 case "compute:nova": 93 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_SERVER 94 case "network:router_gateway", "network:router_interface", "network:router_interface_distributed": 95 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_RESERVED 96 case "network:dhcp": 97 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_DHCP 98 case "neutron:LOADBALANCERV2": 99 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_LOADBALANCER 100 case "neutron:VIP_PORT": 101 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_VIP 102 default: 103 if strings.HasPrefix(port.DeviceOwner, "compute:") { 104 return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_SERVER 105 } 106 } 107 return port.DeviceOwner 108 } 109 110 func (port *Port) GetAssociateId() string { 111 return port.DeviceId 112 } 113 114 func (port *Port) GetStatus() string { 115 switch port.Status { 116 case "ACTIVE", "DOWN": 117 return api.NETWORK_INTERFACE_STATUS_AVAILABLE 118 case "BUILD": 119 return api.NETWORK_INTERFACE_STATUS_CREATING 120 } 121 return port.Status 122 } 123 124 func (port *Port) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) { 125 address := []cloudprovider.ICloudInterfaceAddress{} 126 for i := 0; i < len(port.FixedIps); i++ { 127 port.FixedIps[i].NetworkId = port.NetworkId 128 address = append(address, &port.FixedIps[i]) 129 } 130 return address, nil 131 } 132 133 func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) { 134 ports, err := region.GetPorts("") 135 if err != nil { 136 return nil, err 137 } 138 ret := []cloudprovider.ICloudNetworkInterface{} 139 for i := 0; i < len(ports); i++ { 140 if len(ports[i].DeviceId) == 0 || !utils.IsInStringArray(ports[i].DeviceOwner, []string{"compute:CCI", "compute:nova", "neutron:LOADBALANCERV2"}) { 141 ports[i].region = region 142 ret = append(ret, &ports[i]) 143 } 144 } 145 return ret, nil 146 } 147 148 func (self *SRegion) GetPort(id string) (*Port, error) { 149 port := &Port{} 150 res := fmt.Sprintf("ports/%s", id) 151 return port, self.vpcGet(res, port) 152 } 153 154 // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0133195888.html 155 func (self *SRegion) GetPorts(instanceId string) ([]Port, error) { 156 ports := []Port{} 157 params := url.Values{} 158 if len(instanceId) > 0 { 159 params.Set("device_id", instanceId) 160 } 161 return ports, self.vpcList("ports", params, &ports) 162 }