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

75 lines
1.6 KiB
Go
Raw Normal View History

2022-12-22 12:07:21 +00:00
package hypervisor
import (
"context"
"fmt"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/vim25/types"
)
type vAPPProperty struct {
Key string
Value string
}
func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualmachine string, network string) error {
vm, err := fnd.VirtualMachine(ctx, virtualmachine)
2022-12-22 12:07:21 +00:00
if err != nil {
return err
}
vappproperties := []vAPPProperty{
{
Key: "guestinfo.dns.domains",
Value: fmt.Sprintf("${searchPath:%s}", network),
2022-12-22 12:07:21 +00:00
},
{
Key: "guestinfo.dns.servers",
Value: fmt.Sprintf("${dns:%s}", network),
},
{
Key: "guestinfo.interface.0.ip.0.address",
Value: fmt.Sprintf("${autoIp:%s}", network),
},
{
Key: "guestinfo.interface.0.ip.0.netmask",
Value: fmt.Sprintf("${netmask:%s}", network),
},
{
Key: "guestinfo.interface.0.route.0.gateway",
Value: fmt.Sprintf("${gateway:%s}", network),
2022-12-22 12:07:21 +00:00
},
}
vappconfig := &types.VmConfigSpec{
// OvfEnvironmentTransport: []string{"com.vmware.guestinfo"},
}
for i, vappproperty := range vappproperties {
vappconfig.Property = append(vappconfig.Property, types.VAppPropertySpec{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationAdd,
},
Info: &types.VAppPropertyInfo{
Key: int32(i),
Id: vappproperty.Key,
DefaultValue: vappproperty.Value,
2022-12-22 12:07:21 +00:00
Type: "expression",
},
})
}
task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
VAppConfig: vappconfig,
})
if err != nil {
return err
}
if err := task.Wait(ctx); err != nil {
return err
}
return nil
}