Skip to content

Instantly share code, notes, and snippets.

@mkuzmin
Created October 30, 2018 10:42
Show Gist options
  • Save mkuzmin/afea4017b38c47a9b09a76bd2a6242e0 to your computer and use it in GitHub Desktop.
Save mkuzmin/afea4017b38c47a9b09a76bd2a6242e0 to your computer and use it in GitHub Desktop.
Create minimal VM
package main
import (
"context"
"fmt"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/vim25/types"
"net/url"
)
func main() {
ctx := context.TODO()
vcenter := "vcenter.vsphere65.test"
username := "root"
password := "jetbrains"
datacenter := "dc1"
folder := ""
host := "esxi-1.vsphere65.test"
pool := ""
datastore := "datastore1"
hw_version := 9
vcenter_url, err := url.Parse(fmt.Sprintf("https://%v/sdk", vcenter))
if err != nil {
panic(err)
}
vcenter_url.User = url.UserPassword(username, password)
client, err := govmomi.NewClient(ctx, vcenter_url, true)
if err != nil {
panic(err)
}
finder := find.NewFinder(client.Client, false)
dc, err := finder.DatacenterOrDefault(ctx, datacenter)
if err != nil {
panic(err)
}
finder.SetDatacenter(dc)
////////////////////////
f, err := finder.Folder(ctx, fmt.Sprintf("/%v/vm/%v", datacenter, folder))
if err != nil {
panic(err)
}
rp, err := finder.ResourcePool(ctx, fmt.Sprintf("%v/Resources/%v", host, pool))
if err != nil {
panic(err)
}
createSpec := types.VirtualMachineConfigSpec{
Name: "mk-hw-version",
}
createSpec.Version = fmt.Sprintf("%s%d", "vmx-", hw_version)
createSpec.Files = &types.VirtualMachineFileInfo{
VmPathName: fmt.Sprintf("[%s]", datastore),
}
task, err := f.CreateVM(ctx, createSpec, rp, nil)
if err != nil {
panic(err)
}
_, err = task.WaitForResult(ctx, nil)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment