yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/nutanix/instancenic.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 nutanix
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  )
    23  
    24  type SInstanceNic struct {
    25  	cloudprovider.DummyICloudNic
    26  	ins *SInstance
    27  
    28  	MacAddress  string   `json:"mac_address"`
    29  	NetworkUUID string   `json:"network_uuid"`
    30  	NicUUID     string   `json:"nic_uuid"`
    31  	Model       string   `json:"model"`
    32  	IPAddress   string   `json:"ip_address"`
    33  	IPAddresses []string `json:"ip_addresses"`
    34  	VlanMode    string   `json:"vlan_mode"`
    35  	IsConnected bool     `json:"is_connected"`
    36  }
    37  
    38  func (self *SInstanceNic) GetId() string {
    39  	return self.NicUUID
    40  }
    41  
    42  func (self *SInstanceNic) GetIP() string {
    43  	return self.IPAddress
    44  }
    45  
    46  func (self *SInstanceNic) GetMAC() string {
    47  	return self.MacAddress
    48  }
    49  
    50  func (self *SInstanceNic) GetDriver() string {
    51  	return "virtio"
    52  }
    53  
    54  func (self *SInstanceNic) InClassicNetwork() bool {
    55  	return false
    56  }
    57  
    58  func (self *SInstanceNic) GetSubAddress() ([]string, error) {
    59  	ret := []string{}
    60  	for _, addr := range self.IPAddresses {
    61  		if addr != self.IPAddress {
    62  			ret = append(ret, addr)
    63  		}
    64  	}
    65  	return ret, nil
    66  }
    67  
    68  func (self *SInstanceNic) GetINetworkId() string {
    69  	if len(self.IPAddress) == 0 {
    70  		return self.NetworkUUID
    71  	}
    72  	vpc, err := self.ins.host.zone.region.GetVpc(self.NetworkUUID)
    73  	if err != nil {
    74  		return self.NetworkUUID
    75  	}
    76  	wires, err := vpc.GetIWires()
    77  	if err != nil {
    78  		return self.NetworkUUID
    79  	}
    80  	for i := range wires {
    81  		networks, err := wires[i].GetINetworks()
    82  		if err != nil {
    83  			continue
    84  		}
    85  		for j := range networks {
    86  			network := networks[j].(*SNetwork)
    87  			if network.Contains(self.IPAddress) {
    88  				return network.GetGlobalId()
    89  			}
    90  		}
    91  	}
    92  	return self.NetworkUUID
    93  }
    94  
    95  func (self *SInstanceNic) AssignAddress(ipAddrs []string) error {
    96  	return cloudprovider.ErrNotImplemented
    97  }
    98  
    99  func (self *SRegion) GetInstanceNics(id string) ([]SInstanceNic, error) {
   100  	nics := []SInstanceNic{}
   101  	res := fmt.Sprintf("vms/%s/nics", id)
   102  	params := url.Values{}
   103  	params.Set("include_address_assignments", "true")
   104  	_, err := self.list(res, params, &nics)
   105  	return nics, err
   106  }