本文整理匯總了Golang中github.com/vmware/govmomi/vim25/types.VirtualMachineConfigSpec.Name方法的典型用法代碼示例。如果您正苦於以下問題:Golang VirtualMachineConfigSpec.Name方法的具體用法?Golang VirtualMachineConfigSpec.Name怎麽用?Golang VirtualMachineConfigSpec.Name使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/vmware/govmomi/vim25/types.VirtualMachineConfigSpec
的用法示例。
在下文中一共展示了VirtualMachineConfigSpec.Name方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestCreateVm
func TestCreateVm(t *testing.T) {
ctx := context.Background()
for _, model := range []*Model{ESX(), VPX()} {
defer model.Remove()
err := model.Create()
if err != nil {
t.Fatal(err)
}
s := model.Service.NewServer()
defer s.Close()
c, err := govmomi.NewClient(ctx, s.URL, true)
if err != nil {
t.Fatal(err)
}
spec := types.VirtualMachineConfigSpec{
// Note: real ESX allows the VM to be created without a GuestId,
// but will power on will fail.
GuestId: string(types.VirtualMachineGuestOsIdentifierOtherGuest),
}
steps := []func(){
func() {
spec.Name = "test"
},
func() {
spec.Files = &types.VirtualMachineFileInfo{
VmPathName: fmt.Sprintf("[LocalDS_0] %s/%s.vmx", spec.Name, spec.Name),
}
},
}
finder := find.NewFinder(c.Client, false)
dc, err := finder.DefaultDatacenter(ctx)
if err != nil {
t.Fatal(err)
}
finder.SetDatacenter(dc)
folders, err := dc.Folders(ctx)
if err != nil {
t.Fatal(err)
}
hosts, err := finder.HostSystemList(ctx, "*/*")
if err != nil {
t.Fatal(err)
}
nhosts := len(hosts)
host := hosts[rand.Intn(nhosts)]
pool, err := host.ResourcePool(ctx)
if err != nil {
t.Fatal(err)
}
if nhosts == 1 {
// test the default path against the ESX model
host = nil
}
vmFolder := folders.VmFolder
// expecting CreateVM to fail until all steps are taken
for _, step := range steps {
task, cerr := vmFolder.CreateVM(ctx, spec, pool, host)
if cerr != nil {
t.Fatal(err)
}
_, cerr = task.WaitForResult(ctx, nil)
if cerr == nil {
t.Error("expected error")
}
step()
}
task, err := vmFolder.CreateVM(ctx, spec, pool, host)
if err != nil {
t.Fatal(err)
}
info, err := task.WaitForResult(ctx, nil)
if err != nil {
t.Fatal(err)
}
vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference))
name, err := vm.ObjectName(ctx)
if err != nil {
t.Fatal(err)
}
if name != spec.Name {
//.........這裏部分代碼省略.........