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