Move routines to subcommands;Add initial logic for datacenter configuration; Move code to package
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
103e355b8e
commit
d8c361101e
@ -3,7 +3,7 @@ type: kubernetes
|
|||||||
name: 'Golang Build'
|
name: 'Golang Build'
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
BINARY_VERSION: v0.3.2
|
BINARY_VERSION: v0.4.0
|
||||||
|
|
||||||
clone:
|
clone:
|
||||||
disable: true
|
disable: true
|
||||||
|
@ -10,43 +10,58 @@ import (
|
|||||||
"spamasaurus.com/m/pkg/hypervisor"
|
"spamasaurus.com/m/pkg/hypervisor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Global 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"`
|
||||||
|
}
|
||||||
|
var Commands struct {
|
||||||
|
Datacenter struct {
|
||||||
|
Name string `short:"n" long:"name" description:"Name of datacenter" required:"true"`
|
||||||
|
Network string `short:"p" long:"portgroup" description:"Name of network portgroup" required:"true"`
|
||||||
|
} // `command:"datacenter" alias:"dc" description:"Define a Network Protocol Profile within a datacenter"`
|
||||||
|
VirtualMachine struct {
|
||||||
|
Datacenter string `short:"d" long:"datacenter" description:"Name of datacenter" required:"true"`
|
||||||
|
Name string `short:"n" long:"name" description:"Name of virtual machine" required:"true"`
|
||||||
|
Network string `short:"p" long:"portgroup" description:"Name of network portgroup" required:"true"`
|
||||||
|
} // `command:"virtualmachine" alias:"vm" description:"Configure a virtual machine for usage of Network Protocol Profiles"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var opts struct {
|
parser := flags.NewParser(&Global, flags.Default)
|
||||||
FQDN string `short:"s" long:"server" description:"FQDN of the vCenter appliance" required:"true"`
|
parser.AddCommand("datacenter", "Define a Network Protocol Profile within a datacenter", "", &Commands.Datacenter)
|
||||||
Username string `short:"u" long:"username" description:"Username to authenticate with" required:"true"`
|
parser.AddCommand("dc", "", "", &Commands.Datacenter)
|
||||||
Password string `short:"p" long:"password" description:"Password to authenticate with" required:"true"`
|
parser.AddCommand("virtualmachine", "Configure a virtual machine for usage of Network Protocol Profiles", "", &Commands.VirtualMachine)
|
||||||
|
parser.AddCommand("vm", "", "", &Commands.VirtualMachine)
|
||||||
Datacenter string `short:"d" long:"datacenter" description:"Name of datacenter" required:"true"`
|
_, err := parser.Parse()
|
||||||
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"`
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := flags.Parse(&opts)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if e, ok := err.(*flags.Error); ok {
|
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||||
if e.Type == flags.ErrHelp {
|
os.Exit(0)
|
||||||
os.Exit(0)
|
} else {
|
||||||
}
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
clt, err := hypervisor.NewClient(ctx, opts.FQDN, opts.Username, opts.Password, true)
|
clt, err := hypervisor.NewClient(ctx, Global.FQDN, Global.Username, Global.Password, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("[ERROR] Login failed: %s", err)
|
log.Fatalf("[ERROR] Login failed: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fnd, err := hypervisor.DatacenterFinder(ctx, clt, opts.Datacenter)
|
switch parser.Active.Name {
|
||||||
if err != nil {
|
case "datacenter", "dc":
|
||||||
log.Fatalf("[ERROR] Unable to determine datacenter: %s", err)
|
if err := hypervisor.CreateNetworkProtocolProfile(ctx, clt, Commands.Datacenter.Name, Commands.Datacenter.Network); err != nil {
|
||||||
}
|
log.Fatalf("[ERROR] Could not create network protocol profile: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, opts.VirtualMachine, opts.Network); err != nil {
|
log.Printf("[SUCCESS] Hurray! We did it!")
|
||||||
log.Fatalf("[ERROR] Could not apply vApp properties: %s", err)
|
case "virtualmachine", "vm":
|
||||||
}
|
if err := hypervisor.SetVirtualMachineProperties(ctx, clt, Commands.VirtualMachine.Datacenter, Commands.VirtualMachine.Name, Commands.VirtualMachine.Network); err != nil {
|
||||||
|
log.Fatalf("[ERROR] Could not apply vApp properties: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("[SUCCESS] Network protocol profile properties added to virtual machine '%s' and configured for network '%s'", opts.VirtualMachine, opts.Network)
|
log.Printf("[SUCCESS] Network protocol profile properties added to virtual machine '%s' and configured for network '%s'", Commands.VirtualMachine.Name, Commands.VirtualMachine.Network)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
6
go.mod
6
go.mod
@ -3,11 +3,9 @@ module spamasaurus.com/m
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/jessevdk/go-flags v1.5.0
|
github.com/jessevdk/go-flags v1.5.0
|
||||||
github.com/vmware/govmomi v0.30.0
|
github.com/vmware/govmomi v0.30.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
|
||||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
|
|
||||||
)
|
|
||||||
|
45
pkg/hypervisor/dc.go
Normal file
45
pkg/hypervisor/dc.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package hypervisor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/vmware/govmomi/find"
|
||||||
|
"github.com/vmware/govmomi/object"
|
||||||
|
"github.com/vmware/govmomi/vim25"
|
||||||
|
"github.com/vmware/govmomi/vim25/methods"
|
||||||
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datacenter, network string) error {
|
||||||
|
dc, err := find.NewFinder(clt, true).Datacenter(ctx, datacenter)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[ERROR] Unable to determine datacenter: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
object.NewCommon(clt, dc.Reference())
|
||||||
|
request := &types.CreateIpPool{
|
||||||
|
This: types.ManagedObjectReference{
|
||||||
|
Type: "",
|
||||||
|
Value: "",
|
||||||
|
},
|
||||||
|
Pool: types.IpPool{
|
||||||
|
Name: "Test",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
response, err := methods.CreateIpPool(ctx, clt.RoundTripper, request)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Bar: %s", err)
|
||||||
|
}
|
||||||
|
spew.Dump(response)
|
||||||
|
|
||||||
|
// task := object.NewTask(clt)
|
||||||
|
// err = task.Wait(ctx)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatalf("Foo: %s", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -3,8 +3,10 @@ package hypervisor
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/vmware/govmomi/find"
|
"github.com/vmware/govmomi/find"
|
||||||
|
"github.com/vmware/govmomi/vim25"
|
||||||
"github.com/vmware/govmomi/vim25/mo"
|
"github.com/vmware/govmomi/vim25/mo"
|
||||||
"github.com/vmware/govmomi/vim25/types"
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
)
|
)
|
||||||
@ -14,10 +16,18 @@ type vAPPProperty struct {
|
|||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualmachine string, network string) error {
|
func SetVirtualMachineProperties(ctx context.Context, clt *vim25.Client, datacenter, virtualmachine, network string) error {
|
||||||
vm, err := fnd.VirtualMachine(ctx, virtualmachine)
|
finder := find.NewFinder(clt, true)
|
||||||
|
|
||||||
|
dc, err := finder.Datacenter(ctx, datacenter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Fatalf("[ERROR] Unable to determine datacenter: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
finder.SetDatacenter(dc)
|
||||||
|
vm, err := finder.VirtualMachine(ctx, virtualmachine)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[ERROR] Unable to determine virtual machine: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var moref mo.VirtualMachine
|
var moref mo.VirtualMachine
|
||||||
|
Loading…
Reference in New Issue
Block a user