Added capability to find a host if -hostname isn't specified

Will return the list of all hosts that are part of a vCenter DataCenter
and choose one at random to use.

Signed-off-by: Dan Finneran <daniel.finneran@gmail.com>
This commit is contained in:
thebsdbox 2018-01-10 19:44:18 +00:00
parent c2b534245b
commit 8b9eddf6f6

View File

@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"math/rand"
"net/url"
"os"
"path"
@ -208,10 +209,21 @@ func vCenterConnect(ctx context.Context, newVM vmConfig) (*govmomi.Client, *obje
}
}
// Set the host that the VM will be created on
hs, err := f.HostSystemOrDefault(ctx, *newVM.vSphereHost)
if err != nil {
log.Fatalf("vSphere host [%s], could not be found", *newVM.vSphereHost)
// Check to see if a host has been specified
var hs *object.HostSystem
if *newVM.vSphereHost != "" {
// Find the selected host
hs, err = f.HostSystem(ctx, *newVM.vSphereHost)
if err != nil {
log.Fatalf("vSphere host [%s], could not be found", *newVM.vSphereHost)
}
} else {
allHosts, err := f.HostSystemList(ctx, "*/*")
if err != nil || len(allHosts) == 0 {
log.Fatalf("No vSphere hosts could be found on vCenter server")
}
// Select a host from the list off all available hosts at random
hs = allHosts[rand.Int()%len(allHosts)]
}
var rp *object.ResourcePool