yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    16  
    17  import (
    18  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/models"
    19  
    20  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  	"yunion.io/x/cloudmux/pkg/multicloud"
    23  )
    24  
    25  type SNetworkInterface struct {
    26  	multicloud.SNetworkInterfaceBase
    27  	JdcloudTags
    28  	region *SRegion
    29  
    30  	models.NetworkInterface
    31  }
    32  
    33  func (nic *SNetworkInterface) GetId() string {
    34  	return nic.NetworkInterfaceId
    35  }
    36  
    37  func (nic *SNetworkInterface) GetName() string {
    38  	return nic.NetworkInterfaceName
    39  }
    40  
    41  func (nic *SNetworkInterface) GetGlobalId() string {
    42  	return nic.GetId()
    43  }
    44  
    45  func (nic *SNetworkInterface) GetAssociateId() string {
    46  	return nic.InstanceId
    47  }
    48  
    49  func (nic *SNetworkInterface) GetAssociateType() string {
    50  	if nic.InstanceType == "vm" {
    51  		return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_SERVER
    52  	}
    53  	return ""
    54  }
    55  
    56  func (nic *SNetworkInterface) GetMacAddress() string {
    57  	return nic.MacAddress
    58  }
    59  
    60  func (nic *SNetworkInterface) GetStatus() string {
    61  	switch nic.NetworkInterfaceStatus {
    62  	case "enabled":
    63  		return api.NETWORK_INTERFACE_STATUS_AVAILABLE
    64  	case "disabled":
    65  		return api.NETWORK_INTERFACE_STATUS_DISABLED
    66  	}
    67  	return api.NETWORK_INTERFACE_STATUS_UNKNOWN
    68  }
    69  
    70  type SNetworkInterfacePrivateIp struct {
    71  	subnetId  string
    72  	isPrimary bool
    73  	models.NetworkInterfacePrivateIp
    74  }
    75  
    76  func (ip *SNetworkInterfacePrivateIp) GetGlobalId() string {
    77  	return ip.PrivateIpAddress
    78  }
    79  
    80  func (ip *SNetworkInterfacePrivateIp) GetINetworkId() string {
    81  	return ip.subnetId
    82  }
    83  
    84  func (ip *SNetworkInterfacePrivateIp) GetIP() string {
    85  	return ip.PrivateIpAddress
    86  }
    87  
    88  func (ip *SNetworkInterfacePrivateIp) IsPrimary() bool {
    89  	return ip.isPrimary
    90  }
    91  
    92  func (nic *SNetworkInterface) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) {
    93  	address := make([]cloudprovider.ICloudInterfaceAddress, 0, len(nic.SecondaryIps)+1)
    94  	address = append(address, &SNetworkInterfacePrivateIp{
    95  		subnetId:                  nic.SubnetId,
    96  		isPrimary:                 true,
    97  		NetworkInterfacePrivateIp: nic.PrimaryIp,
    98  	})
    99  	for i := range nic.SecondaryIps {
   100  		address = append(address, &SNetworkInterfacePrivateIp{
   101  			subnetId:                  nic.SubnetId,
   102  			isPrimary:                 false,
   103  			NetworkInterfacePrivateIp: nic.SecondaryIps[i],
   104  		})
   105  	}
   106  	return address, nil
   107  }