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
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:16:44 +00:00
|
|
|
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{
|
|
|
|
{
|
2022-12-22 13:16:44 +00:00
|
|
|
Key: "guestinfo.dns.domains",
|
2022-12-22 15:00:22 +00:00
|
|
|
Value: "${searchPath:%s}",
|
2022-12-22 12:07:21 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-22 13:16:44 +00:00
|
|
|
Key: "guestinfo.dns.servers",
|
2022-12-22 15:00:22 +00:00
|
|
|
Value: "${dns:%s}",
|
2022-12-22 13:16:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "guestinfo.interface.0.ip.0.address",
|
2022-12-22 15:00:22 +00:00
|
|
|
Value: "${autoIp:%s}",
|
2022-12-22 13:16:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "guestinfo.interface.0.ip.0.netmask",
|
2022-12-22 15:00:22 +00:00
|
|
|
Value: "${netmask:%s}",
|
2022-12-22 13:16:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "guestinfo.interface.0.route.0.gateway",
|
2022-12-22 15:00:22 +00:00
|
|
|
Value: "${gateway:%s}",
|
2022-12-22 12:07:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
vappconfig := &types.VmConfigSpec{
|
2022-12-22 15:00:22 +00:00
|
|
|
OvfEnvironmentTransport: []string{"com.vmware.guestInfo"},
|
2022-12-22 12:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-12-22 15:00:22 +00:00
|
|
|
DefaultValue: fmt.Sprintf(vappproperty.Value, network),
|
2022-12-22 12:07:21 +00:00
|
|
|
Type: "expression",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:00:22 +00:00
|
|
|
// spew.Dump(vappconfig)
|
|
|
|
|
2022-12-22 12:07:21 +00:00
|
|
|
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
|
|
|
|
}
|