yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/vnic.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 esxi
    16  
    17  import (
    18  	"github.com/vmware/govmomi/vim25/types"
    19  
    20  	"yunion.io/x/log"
    21  	"yunion.io/x/pkg/util/netutils"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  )
    25  
    26  type SVirtualNIC struct {
    27  	SVirtualDevice
    28  
    29  	cloudprovider.DummyICloudNic
    30  }
    31  
    32  func NewVirtualNIC(vm *SVirtualMachine, dev types.BaseVirtualDevice, index int) SVirtualNIC {
    33  	return SVirtualNIC{
    34  		SVirtualDevice: NewVirtualDevice(vm, dev, index),
    35  	}
    36  }
    37  
    38  func (nic *SVirtualNIC) getVirtualEthernetCard() *types.VirtualEthernetCard {
    39  	card := types.VirtualEthernetCard{}
    40  	if FetchAnonymousFieldValue(nic.dev, &card) {
    41  		return &card
    42  	}
    43  	return nil
    44  }
    45  
    46  func (nic *SVirtualNIC) GetId() string {
    47  	return ""
    48  }
    49  
    50  func (nic *SVirtualNIC) GetIP() string {
    51  	guestIps := nic.vm.getGuestIps()
    52  	if ip, ok := guestIps[nic.GetMAC()]; ok {
    53  		return ip
    54  	}
    55  	log.Warningf("cannot find ip for mac %s", nic.GetMAC())
    56  	return ""
    57  }
    58  
    59  func (nic *SVirtualNIC) GetDriver() string {
    60  	return nic.SVirtualDevice.GetDriver()
    61  }
    62  
    63  func (nic *SVirtualNIC) GetMAC() string {
    64  	return netutils.FormatMacAddr(nic.getVirtualEthernetCard().MacAddress)
    65  }
    66  
    67  func (nic *SVirtualNIC) InClassicNetwork() bool {
    68  	return false
    69  }
    70  
    71  func (nic *SVirtualNIC) GetINetworkId() string {
    72  	return ""
    73  }