本文整理汇总了Golang中github.com/vmware/vic/pkg/vsphere/vm.VirtualMachine.String方法的典型用法代码示例。如果您正苦于以下问题:Golang VirtualMachine.String方法的具体用法?Golang VirtualMachine.String怎么用?Golang VirtualMachine.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/vmware/vic/pkg/vsphere/vm.VirtualMachine
的用法示例。
在下文中一共展示了VirtualMachine.String方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: deleteVM
func (d *Dispatcher) deleteVM(vm *vm.VirtualMachine, force bool) error {
defer trace.End(trace.Begin(fmt.Sprintf("vm %q, force %t", vm.String(), force)))
var err error
power, err := vm.PowerState(d.ctx)
if err != nil || power != types.VirtualMachinePowerStatePoweredOff {
if err != nil {
log.Warnf("Failed to get vm power status %q: %s", vm.Reference(), err)
}
if !force {
if err != nil {
return err
}
name := d.getName(vm)
if name != "" {
err = errors.Errorf("VM %q is powered on", name)
} else {
err = errors.Errorf("VM %q is powered on", vm.Reference())
}
return err
}
if _, err = vm.WaitForResult(d.ctx, func(ctx context.Context) (tasks.Task, error) {
return vm.PowerOff(ctx)
}); err != nil {
log.Debugf("Failed to power off existing appliance for %s, try to remove anyway", err)
}
}
// get the actual folder name before we delete it
folder, err := vm.FolderName(d.ctx)
if err != nil {
// failed to get folder name, might not be able to remove files for this VM
name := d.getName(vm)
if name == "" {
log.Errorf("Unable to automatically remove all files in datastore for VM %q", vm.Reference())
} else {
// try to use the vm name in place of folder
log.Infof("Delete will attempt to remove datastore files for VM %q", name)
folder = name
}
}
_, err = vm.WaitForResult(d.ctx, func(ctx context.Context) (tasks.Task, error) {
return vm.DeleteExceptDisks(ctx)
})
if err != nil {
err = errors.Errorf("Failed to destroy VM %q: %s", vm.Reference(), err)
err2 := vm.Unregister(d.ctx)
if err2 != nil {
return errors.Errorf("%s then failed to unregister VM: %s", err, err2)
}
log.Infof("Unregistered VM to cleanup after failed destroy: %q", vm.Reference())
}
if _, err = d.deleteDatastoreFiles(d.session.Datastore, folder, true); err != nil {
log.Warnf("Failed to remove datastore files for VM path %q: %s", folder, err)
}
return nil
}
示例2: findDisk
// Find the disk by name attached to the given vm.
func findDisk(op trace.Operation, vm *vm.VirtualMachine, name string) (*types.VirtualDisk, error) {
defer trace.End(trace.Begin(vm.String()))
log.Debugf("Looking for attached disk matching filename %s", name)
devices, err := vm.Device(op)
if err != nil {
return nil, fmt.Errorf("Failed to refresh devices for vm: %s", errors.ErrorStack(err))
}
candidates := devices.Select(func(device types.BaseVirtualDevice) bool {
db := device.GetVirtualDevice().Backing
if db == nil {
return false
}
backing, ok := device.GetVirtualDevice().Backing.(*types.VirtualDiskFlatVer2BackingInfo)
if !ok {
return false
}
log.Debugf("backing file name %s", backing.VirtualDeviceFileBackingInfo.FileName)
match := strings.HasSuffix(backing.VirtualDeviceFileBackingInfo.FileName, name)
if match {
log.Debugf("Found candidate disk for %s at %s", name, backing.VirtualDeviceFileBackingInfo.FileName)
}
return match
})
if len(candidates) == 0 {
log.Warnf("No disks match name: %s", name)
return nil, os.ErrNotExist
}
if len(candidates) > 1 {
return nil, errors.Errorf("Too many disks match name: %s", name)
}
return candidates[0].(*types.VirtualDisk), nil
}