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"
)
2022-12-28 14:41:03 +01:00
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 {
2022-12-29 11:34:51 +01:00
Name string ` short:"n" long:"name" description:"Name of datacenter" required:"true" `
2023-01-18 12:16:51 +01:00
Network string ` long:"portgroup" description:"Name of network portgroup" required:"true" `
2022-12-29 11:34:51 +01:00
StartAddress string ` long:"startaddress" required:"true" `
EndAddress string ` long:"endaddress" required:"true" `
Netmask string ` long:"netmask" required:"true" `
DnsServer [ ] string ` long:"dnsserver" required:"true" `
DnsDomain string ` long:"dnsdomain" required:"true" `
Gateway string ` long:"gateway" required:"true" `
2022-12-29 21:34:41 +01:00
Force bool ` short:"f" long:"force" `
2022-12-28 14:41:03 +01:00
} // `command:"datacenter" alias:"dc" description:"Define a Network Protocol Profile within a datacenter"`
VirtualMachine struct {
Name string ` short:"n" long:"name" description:"Name of virtual machine" required:"true" `
2023-01-18 12:16:51 +01:00
Datacenter string ` long:"datacenter" description:"Name of datacenter" required:"true" `
Network string ` long:"portgroup" description:"Name of network portgroup" required:"true" `
2022-12-28 14:41:03 +01:00
} // `command:"virtualmachine" alias:"vm" description:"Configure a virtual machine for usage of Network Protocol Profiles"`
2022-12-29 11:34:51 +01:00
GuestOS struct {
} // `command:"guestos" alias:"os" description:"Configure guest OS network with allocated IP address"`
2022-12-28 14:41:03 +01:00
}
2022-12-22 13:07:21 +01:00
2022-12-28 14:41:03 +01:00
func main ( ) {
parser := flags . NewParser ( & Global , flags . Default )
parser . AddCommand ( "datacenter" , "Define a Network Protocol Profile within a datacenter" , "" , & Commands . Datacenter )
parser . AddCommand ( "dc" , "" , "" , & Commands . Datacenter )
parser . AddCommand ( "virtualmachine" , "Configure a virtual machine for usage of Network Protocol Profiles" , "" , & Commands . VirtualMachine )
parser . AddCommand ( "vm" , "" , "" , & Commands . VirtualMachine )
2022-12-29 11:34:51 +01:00
parser . AddCommand ( "guestos" , "Configure guest OS network with allocated IP address" , "" , & Commands . GuestOS )
parser . AddCommand ( "os" , "" , "" , & Commands . GuestOS )
2022-12-28 14:41:03 +01:00
_ , err := parser . Parse ( )
2022-12-23 12:45:12 +01:00
if err != nil {
2022-12-28 14:41:03 +01:00
if flagsErr , ok := err . ( * flags . Error ) ; ok && flagsErr . Type == flags . ErrHelp {
os . Exit ( 0 )
} else {
os . Exit ( 1 )
2022-12-23 12:45:12 +01:00
}
}
2022-12-22 13:07:21 +01:00
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
2022-12-28 14:41:03 +01:00
clt , err := hypervisor . NewClient ( ctx , Global . FQDN , Global . Username , Global . 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-28 14:41:03 +01:00
switch parser . Active . Name {
case "datacenter" , "dc" :
2022-12-29 21:34:41 +01:00
if err := hypervisor . CreateNetworkProtocolProfile ( ctx , clt , Commands . Datacenter . Name , Commands . Datacenter . Network , Commands . Datacenter . StartAddress , Commands . Datacenter . EndAddress , Commands . Datacenter . Netmask , Commands . Datacenter . DnsDomain , Commands . Datacenter . Gateway , Commands . Datacenter . DnsServer , Commands . Datacenter . Force ) ; err != nil {
2022-12-28 14:41:03 +01:00
log . Fatalf ( "[ERROR] Could not create network protocol profile: %s" , err )
}
2022-12-22 13:07:21 +01:00
2022-12-28 16:29:25 +01:00
log . Printf ( "[SUCCESS] New network protocol profile created within datacenter '%s' (associated with network '%s')" , Commands . Datacenter . Name , Commands . Datacenter . Network )
2022-12-29 11:34:51 +01:00
case "guestos" , "os" :
// TODO
2022-12-28 14:41:03 +01:00
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 )
}
2022-12-23 12:50:22 +01:00
2022-12-28 16:29:25 +01:00
log . Printf ( "[SUCCESS] Network protocol profile properties added to virtual machine '%s' (configured for network '%s')" , Commands . VirtualMachine . Name , Commands . VirtualMachine . Network )
2022-12-28 14:41:03 +01:00
}
2022-12-22 13:07:21 +01:00
}