53 lines
1.6 KiB
Go
Raw Normal View History

2022-12-22 13:07:21 +01:00
package main
import (
"context"
"log"
2022-12-23 12:45:12 +01:00
"os"
"github.com/jessevdk/go-flags"
2022-12-22 13:07:21 +01:00
"spamasaurus.com/m/pkg/hypervisor"
)
func main() {
2022-12-23 12:45:12 +01:00
var opts struct {
FQDN string `short:"s" long:"server" description:"FQDN of the vCenter appliance" required:"true"`
Username string `short:"u" long:"username" description:"Username to authenticate with" required:"true"`
Password string `short:"p" long:"password" description:"Password to authenticate with" required:"true"`
Datacenter string `short:"d" long:"datacenter" description:"Name of datacenter" required:"true"`
VirtualMachine string `short:"v" long:"virtualmachine" description:"Name of virtual machine" required:"true"`
Network string `short:"n" long:"network" description:"Name of network portgroup" required:"true"`
}
2022-12-22 13:07:21 +01:00
2022-12-23 12:45:12 +01:00
_, err := flags.Parse(&opts)
if err != nil {
if e, ok := err.(*flags.Error); ok {
if e.Type == flags.ErrHelp {
os.Exit(0)
}
}
os.Exit(1)
}
2022-12-22 13:07:21 +01:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2022-12-23 12:45:12 +01:00
clt, err := hypervisor.NewClient(ctx, opts.FQDN, opts.Username, opts.Password, true)
2022-12-22 13:07:21 +01:00
if err != nil {
2022-12-23 12:45:12 +01:00
log.Fatalf("[ERROR] Login failed: %s", err)
2022-12-22 13:07:21 +01:00
}
2022-12-23 12:45:12 +01:00
fnd, err := hypervisor.DatacenterFinder(ctx, clt, opts.Datacenter)
2022-12-22 13:07:21 +01:00
if err != nil {
2022-12-23 12:45:12 +01:00
log.Fatalf("[ERROR] Unable to determine datacenter: %s", err)
2022-12-22 13:07:21 +01:00
}
2022-12-23 12:45:12 +01:00
if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, opts.VirtualMachine, opts.Network); err != nil {
log.Fatalf("[ERROR] Could not apply vApp properties: %s", err)
2022-12-22 13:07:21 +01:00
}
2022-12-23 12:50:22 +01:00
log.Printf("[SUCCESS] Network protocol profile properties added to virtual machine '%s' and configured for network '%s'", opts.VirtualMachine, opts.Network)
2022-12-22 13:07:21 +01:00
}