Go.NPP-Prepper/pkg/hypervisor/datacenter.go

74 lines
2.4 KiB
Go
Raw Normal View History

package hypervisor
import (
"context"
"fmt"
"log"
"net"
2022-12-28 15:29:25 +00:00
"github.com/google/uuid"
netrange "github.com/netdata/go.d.plugin/pkg/iprange"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"spamasaurus.com/m/pkg/utils"
)
func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datacenter, network, startaddress, endaddress, netmask, dnsdomain, gateway string, dnsserver []string) error {
2022-12-28 15:29:25 +00:00
finder := find.NewFinder(clt, true)
dc, err := finder.Datacenter(ctx, datacenter)
if err != nil {
log.Fatalf("[ERROR] Unable to determine datacenter: %s", err)
}
2022-12-28 15:29:25 +00:00
finder.SetDatacenter(dc)
nw, err := finder.Network(ctx, network)
if err != nil {
log.Fatalf("[ERROR] Unable to determine network: %s", err)
}
var networksummary mo.Network
pc := property.DefaultCollector(clt)
pc.Retrieve(ctx, []types.ManagedObjectReference{nw.Reference()}, []string{"summary"}, &networksummary)
if networksummary.Summary.GetNetworkSummary().IpPoolId != nil {
log.Fatalf("[ERROR] Network '%s' already has an existing protocol profile associated", network)
}
iprange, err := netrange.ParseRange(fmt.Sprintf("%s-%s", startaddress, endaddress))
if err != nil {
log.Fatalf("[ERROR] Invalid IP range: %s", err)
}
ipnetwork := net.ParseIP(startaddress).Mask(net.IPMask(net.ParseIP(netmask).To4()))
request := &types.CreateIpPool{
2022-12-28 15:29:25 +00:00
This: *clt.ServiceContent.IpPoolManager,
Dc: dc.Reference(),
Pool: types.IpPool{
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),
},
},
}
2022-12-28 15:29:25 +00:00
if _, err := methods.CreateIpPool(ctx, clt.RoundTripper, request); err != nil {
log.Fatalf("[ERROR]: Failed creating new network protocol profile (for network '%s' within datacenter '%s'): %s", network, datacenter, err)
}
return nil
}