yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/device.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  	"reflect"
    19  	"strings"
    20  
    21  	"github.com/vmware/govmomi/vim25/types"
    22  )
    23  
    24  type SVirtualDevice struct {
    25  	vm    *SVirtualMachine
    26  	dev   types.BaseVirtualDevice
    27  	index int
    28  }
    29  
    30  func NewVirtualDevice(vm *SVirtualMachine, dev types.BaseVirtualDevice, index int) SVirtualDevice {
    31  	return SVirtualDevice{
    32  		vm:    vm,
    33  		dev:   dev,
    34  		index: index,
    35  	}
    36  }
    37  
    38  func (dev *SVirtualDevice) getKey() int32 {
    39  	return dev.dev.GetVirtualDevice().Key
    40  }
    41  
    42  func (dev *SVirtualDevice) getControllerKey() int32 {
    43  	return dev.dev.GetVirtualDevice().ControllerKey
    44  }
    45  
    46  func (dev *SVirtualDevice) GetIndex() int {
    47  	return dev.index
    48  }
    49  
    50  func (dev *SVirtualDevice) getLabel() string {
    51  	return dev.dev.GetVirtualDevice().DeviceInfo.GetDescription().Label
    52  }
    53  
    54  func (dev *SVirtualDevice) GetDriver() string {
    55  	return getDevName(dev.dev)
    56  }
    57  
    58  func getDevName(device types.BaseVirtualDevice) string {
    59  	val := reflect.Indirect(reflect.ValueOf(device))
    60  	driver := strings.ToLower(val.Type().Name())
    61  	if strings.Contains(driver, "virtualmachine") {
    62  		return strings.Replace(driver, "virtualmachine", "", -1)
    63  	} else if strings.Contains(driver, "virtual") {
    64  		return strings.Replace(driver, "virtual", "", -1)
    65  	} else {
    66  		return driver
    67  	}
    68  }
    69  
    70  type SVirtualVGA struct {
    71  	SVirtualDevice
    72  }
    73  
    74  func NewVirtualVGA(vm *SVirtualMachine, dev types.BaseVirtualDevice, index int) SVirtualVGA {
    75  	return SVirtualVGA{
    76  		NewVirtualDevice(vm, dev, index),
    77  	}
    78  }
    79  
    80  func (vga *SVirtualVGA) getVirtualMachineVideoCard() *types.VirtualMachineVideoCard {
    81  	return vga.dev.(*types.VirtualMachineVideoCard)
    82  }
    83  
    84  func (vga *SVirtualVGA) GetEnable3D() bool {
    85  	p3d := vga.getVirtualMachineVideoCard().Enable3DSupport
    86  	return p3d != nil && *p3d
    87  }
    88  
    89  func (vga *SVirtualVGA) GetRamSizeMB() int {
    90  	return int(vga.getVirtualMachineVideoCard().VideoRamSizeInKB / 1024)
    91  }
    92  
    93  func (vga *SVirtualVGA) String() string {
    94  	return vga.getVirtualMachineVideoCard().DeviceInfo.GetDescription().Summary
    95  }
    96  
    97  type SVirtualCdrom struct {
    98  	SVirtualDevice
    99  }
   100  
   101  func NewVirtualCdrom(vm *SVirtualMachine, dev types.BaseVirtualDevice, index int) SVirtualCdrom {
   102  	return SVirtualCdrom{
   103  		NewVirtualDevice(vm, dev, index),
   104  	}
   105  }
   106  
   107  func (cdrom *SVirtualCdrom) getVirtualCdrom() *types.VirtualCdrom {
   108  	return cdrom.dev.(*types.VirtualCdrom)
   109  }