Add referencing functions;Add required flags;Populate and associate network protocol profile
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
e269632125
commit
502c361c21
@ -3,13 +3,13 @@ type: kubernetes
|
||||
name: 'Golang Build'
|
||||
|
||||
environment:
|
||||
BINARY_VERSION: v0.4.2
|
||||
BINARY_VERSION: v0.4.3
|
||||
|
||||
clone:
|
||||
disable: true
|
||||
|
||||
steps:
|
||||
- name: Clone vappproperty-manager source repository
|
||||
- name: Clone npp-prepper source repository
|
||||
image: bv11-cr01.bessems.eu/proxy/alpine/git
|
||||
commands:
|
||||
- git clone https://code.spamasaurus.com/djpbessems/Go.NPP-Prepper.git .
|
||||
|
@ -19,12 +19,20 @@ 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"`
|
||||
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"`
|
||||
} // `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"`
|
||||
GuestOS struct {
|
||||
} // `command:"guestos" alias:"os" description:"Configure guest OS network with allocated IP address"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -33,6 +41,8 @@ func main() {
|
||||
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)
|
||||
parser.AddCommand("guestos", "Configure guest OS network with allocated IP address", "", &Commands.GuestOS)
|
||||
parser.AddCommand("os", "", "", &Commands.GuestOS)
|
||||
_, err := parser.Parse()
|
||||
if err != nil {
|
||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||
@ -52,11 +62,13 @@ func main() {
|
||||
|
||||
switch parser.Active.Name {
|
||||
case "datacenter", "dc":
|
||||
if err := hypervisor.CreateNetworkProtocolProfile(ctx, clt, Commands.Datacenter.Name, Commands.Datacenter.Network); err != nil {
|
||||
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); err != nil {
|
||||
log.Fatalf("[ERROR] Could not create network protocol profile: %s", err)
|
||||
}
|
||||
|
||||
log.Printf("[SUCCESS] New network protocol profile created within datacenter '%s' (associated with network '%s')", Commands.Datacenter.Name, Commands.Datacenter.Network)
|
||||
case "guestos", "os":
|
||||
// TODO
|
||||
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)
|
||||
|
6
go.mod
6
go.mod
@ -5,7 +5,11 @@ go 1.19
|
||||
require (
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jessevdk/go-flags v1.5.0
|
||||
github.com/netdata/go.d.plugin v0.47.0
|
||||
github.com/vmware/govmomi v0.30.0
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
|
||||
require (
|
||||
github.com/apparentlymart/go-cidr v1.1.0 // indirect
|
||||
golang.org/x/sys v0.3.0 // indirect
|
||||
)
|
||||
|
11
go.sum
11
go.sum
@ -1,8 +1,17 @@
|
||||
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
|
||||
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
|
||||
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
|
||||
github.com/netdata/go.d.plugin v0.47.0 h1:cy3edan4Sxo4EzVWw9GSeyMBMtyjCdlUvarBl3oxQDs=
|
||||
github.com/netdata/go.d.plugin v0.47.0/go.mod h1:3HPJ19CAzwPi2J5An20E+xAfTcTNGTpPGdWoOiTzfkg=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/vmware/govmomi v0.30.0 h1:Fm8ugPnnlMSTSceDKY9goGvjmqc6eQLPUSUeNXdpeXA=
|
||||
github.com/vmware/govmomi v0.30.0/go.mod h1:F7adsVewLNHsW/IIm7ziFURaXDaHEwcc+ym4r3INMdY=
|
||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
|
||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
@ -2,41 +2,57 @@ package hypervisor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/netdata/go.d.plugin/pkg/iprange"
|
||||
"github.com/vmware/govmomi/find"
|
||||
"github.com/vmware/govmomi/vim25"
|
||||
"github.com/vmware/govmomi/vim25/methods"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
"spamasaurus.com/m/pkg/utils"
|
||||
)
|
||||
|
||||
func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datacenter, network string) error {
|
||||
func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datacenter, network, startaddress, endaddress, netmask, dnsdomain, gateway string, dnsserver []string) error {
|
||||
finder := find.NewFinder(clt, true)
|
||||
dc, err := finder.Datacenter(ctx, datacenter)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Unable to determine datacenter: %s", err)
|
||||
}
|
||||
finder.SetDatacenter(dc)
|
||||
// nw, err := finder.Network(ctx, network)
|
||||
// if err != nil {
|
||||
// log.Fatalf("[ERROR] Unable to determine network: %s", err)
|
||||
// }
|
||||
nw, err := finder.Network(ctx, network)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Unable to determine network: %s", err)
|
||||
}
|
||||
|
||||
iprange, err := iprange.ParseRange(fmt.Sprintf("%s-%s", startaddress, endaddress))
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Foo: %s", err)
|
||||
}
|
||||
ipnetwork := net.ParseIP(startaddress).Mask(net.IPMask(net.ParseIP(netmask).To4()))
|
||||
|
||||
request := &types.CreateIpPool{
|
||||
This: *clt.ServiceContent.IpPoolManager,
|
||||
Dc: dc.Reference(),
|
||||
Pool: types.IpPool{
|
||||
Name: "ippool-" + (uuid.New().String()),
|
||||
DnsDomain: "meta.k8s.cluster",
|
||||
DnsSearchPath: "meta.k8s.cluster",
|
||||
// NetworkAssociation: []types.IpPoolAssociation{{
|
||||
// // This generates the error 'cannot use nw.Reference() (value of type types.ManagedObjectReference) as type *types.ManagedObjectReference in struct literal'
|
||||
// Network: nw.Reference(),
|
||||
// NetworkName: network,
|
||||
// },
|
||||
// },
|
||||
Name: "ippool-" + uuid.New().String()[0:5],
|
||||
DnsDomain: dnsdomain,
|
||||
DnsSearchPath: dnsdomain,
|
||||
NetworkAssociation: []types.IpPoolAssociation{{
|
||||
Network: utils.MoRefAddr(nw.Reference()),
|
||||
NetworkName: network,
|
||||
}},
|
||||
Ipv4Config: &types.IpPoolIpPoolConfigInfo{
|
||||
SubnetAddress: ipnetwork.String(),
|
||||
Netmask: netmask,
|
||||
Gateway: gateway,
|
||||
Range: fmt.Sprintf("%s#%d", startaddress, iprange.Size()),
|
||||
Dns: dnsserver,
|
||||
DhcpServerAvailable: utils.BoolAddr(false),
|
||||
IpPoolEnabled: utils.BoolAddr(true),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
13
pkg/utils/utils.go
Normal file
13
pkg/utils/utils.go
Normal file
@ -0,0 +1,13 @@
|
||||
package utils
|
||||
|
||||
import "github.com/vmware/govmomi/vim25/types"
|
||||
|
||||
func BoolAddr(b bool) *bool {
|
||||
boolVar := b
|
||||
return &boolVar
|
||||
}
|
||||
|
||||
func MoRefAddr(mo types.ManagedObjectReference) *types.ManagedObjectReference {
|
||||
morefVar := mo
|
||||
return &morefVar
|
||||
}
|
Loading…
Reference in New Issue
Block a user