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