Prevent overwriting existing properties
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-26 16:52:38 +01:00
parent 46ff2a0f99
commit 103e355b8e
4 changed files with 41 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
@ -19,7 +20,37 @@ func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualm
return err
}
vappproperties := []vAPPProperty{
var moref mo.VirtualMachine
vm.Properties(ctx, vm.Reference(), []string{"config.vAppConfig"}, &moref)
vappconfig := &types.VmConfigSpec{
OvfEnvironmentTransport: []string{"com.vmware.guestInfo"},
}
currentvappproperties := moref.Config.VAppConfig.GetVmConfigInfo().Property
for _, vappproperty := range currentvappproperties {
vappconfig.Property = append(vappconfig.Property, types.VAppPropertySpec{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationAdd,
},
Info: &types.VAppPropertyInfo{
Key: vappproperty.Key,
ClassId: vappproperty.ClassId,
InstanceId: vappproperty.InstanceId,
Id: vappproperty.Id,
Category: vappproperty.Category,
Label: vappproperty.Label,
Type: vappproperty.Type,
TypeReference: vappproperty.TypeReference,
UserConfigurable: vappproperty.UserConfigurable,
DefaultValue: vappproperty.DefaultValue,
Value: vappproperty.Value,
Description: vappproperty.Description,
},
})
}
newvappproperties := []vAPPProperty{
{
Key: "guestinfo.dns.domains",
Value: "${searchPath:%s}",
@ -42,17 +73,13 @@ func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualm
},
}
vappconfig := &types.VmConfigSpec{
OvfEnvironmentTransport: []string{"com.vmware.guestInfo"},
}
for i, vappproperty := range vappproperties {
for i, vappproperty := range newvappproperties {
vappconfig.Property = append(vappconfig.Property, types.VAppPropertySpec{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationAdd,
},
Info: &types.VAppPropertyInfo{
Key: int32(i),
Key: int32(i + len(currentvappproperties)),
Id: vappproperty.Key,
DefaultValue: fmt.Sprintf(vappproperty.Value, network),
Type: "expression",
@ -60,8 +87,6 @@ func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualm
})
}
// spew.Dump(vappconfig)
task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
VAppConfig: vappconfig,
})