Repository: vmware/govmomi
Pull request #3576 (2024-10-05 15:56:58)

vcsim: support clearing ManagedBy field
govc: add vm.change -managed-by flag


Patch:
diff --git a/govc/USAGE.md b/govc/USAGE.md
index 30ff0800..2dbb8f36 100644
--- a/govc/USAGE.md
+++ b/govc/USAGE.md
@@ -6477,6 +6477,7 @@ Options:
   -iommu-enabled=<nil>           Enable IOMMU
   -latency=                      Latency sensitivity (low|normal|medium|high|custom)
   -m=0                           Size in MB of memory
+  -managed-by=                   Set or clear managed by VC Extension
   -mem.limit=<nil>               Memory limit in MB
   -mem.reservation=<nil>         Memory reservation in MB
   -mem.shares=                   Memory shares level or number
diff --git a/govc/vm/change.go b/govc/vm/change.go
index a5a73b53..245e8186 100644
--- a/govc/vm/change.go
+++ b/govc/vm/change.go
@@ -78,6 +78,7 @@ type change struct {
 	extraConfigFile extraConfigFile
 	Latency         string
 	hwUpgradePolicy string
+	managedBy       string
 }
 
 func init() {
@@ -160,6 +161,7 @@ func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
 	f.StringVar(&cmd.Latency, "latency", "", fmt.Sprintf("Latency sensitivity (%s)", strings.Join(latencyLevels, "|")))
 	f.StringVar(&cmd.Annotation, "annotation", "", "VM description")
 	f.StringVar(&cmd.Uuid, "uuid", "", "BIOS UUID")
+	f.StringVar(&cmd.managedBy, "managed-by", "", "Set or clear managed by VC Extension")
 	f.Var(&cmd.extraConfig, "e", "ExtraConfig. <key>=<value>")
 	f.Var(&cmd.extraConfigFile, "f", "ExtraConfig. <key>=<absolute path to file>")
 
@@ -234,6 +236,18 @@ func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
 		return err
 	}
 
+	if cmd.managedBy != "" {
+		// From the VirtualMachineConfigSpec doc:
+		//   To unset this field pass a ManagedByInfo object with an empty extensionKey
+		if cmd.managedBy == "-" {
+			cmd.managedBy = ""
+		}
+		cmd.ManagedBy = &types.ManagedByInfo{
+			Type:         vm.Reference().Type,
+			ExtensionKey: cmd.managedBy,
+		}
+	}
+
 	task, err := vm.Reconfigure(ctx, cmd.VirtualMachineConfigSpec)
 	if err != nil {
 		return err
diff --git a/simulator/virtual_machine.go b/simulator/virtual_machine.go
index b40a99b0..a58354cb 100644
--- a/simulator/virtual_machine.go
+++ b/simulator/virtual_machine.go
@@ -277,7 +277,11 @@ func (vm *VirtualMachine) apply(spec *types.VirtualMachineConfigSpec) {
 	}
 
 	if spec.ManagedBy != nil {
+		if spec.ManagedBy.ExtensionKey == "" {
+			spec.ManagedBy = nil
+		}
 		vm.Config.ManagedBy = spec.ManagedBy
+		vm.Summary.Config.ManagedBy = spec.ManagedBy
 	}
 
 	if spec.BootOptions != nil {


Test patch:
diff --git a/govc/test/vm.bats b/govc/test/vm.bats
index 7628a247..f84bbef7 100755
--- a/govc/test/vm.bats
+++ b/govc/test/vm.bats
@@ -197,6 +197,21 @@ load test_helper
   run govc object.collect -s "vm/$id" config.uuid
   assert_success "$uuid"
 
+  run govc vm.change -vm $id -managed-by com.vmware.govmomi.simulator
+  assert_success
+
+  run govc vm.info -json $id
+  assert_success
+
+  run govc object.collect -s "vm/$id" config.managedBy.extensionKey
+  assert_success com.vmware.govmomi.simulator
+
+  run govc vm.change -vm $id -managed-by -
+  assert_success
+
+  run govc object.collect -s "vm/$id" config.managedBy
+  assert_success ""
+
   nid=$(new_id)
   run govc vm.change -name $nid -vm $id
   assert_success