govmomi icon indicating copy to clipboard operation
govmomi copied to clipboard

Getting resource custom attributes/fields should be as easy as setting them

Open rickatnight11 opened this issue 1 year ago • 12 comments

Is your feature request related to a problem? Please describe. I'm developing an app to manage VMs (i.e. stop, start, reboot) in a hybrid environment based on custom attributes (to match similar behavior on AWS and Azure hosts using tags). Setting VM tags on a VirtualMachine object is as easy as:

vm.SetCustomValue(ctx, "some-custom-attribute", "some-value")

....whereas getting the current values requires a complicated lookup from the ManagedObjectReference via a CustomFieldManager like:

m, err := object.GetCustomFieldsManager(client.Client)
paths := make(map[types.ManagedObjectReference]string)
var refs []types.ManagedObjectReference
for _, vm := range vms {
	refs = append(refs, vm.Reference())
	paths[vm.Reference()] = vm.InventoryPath
}
var entities []mo.ManagedEntity
err = property.DefaultCollector(client.Client).Retrieve(ctx, refs, []string{"name", "customValue"}, &entities)
matches := func(key int32) bool {
	return true
}
field, err := m.Field(ctx)
vmtagmap := map[string]map[string]string{}
for _, entity := range entities {
	vmtagmap[entity.Name] = map[string]string{}
	for i := range entity.CustomValue {
		val := entity.CustomValue[i].(*types.CustomFieldStringValue)
		if !matches(val.Key) {
			continue
		}
		vmtagmap[entity.Name][field.ByKey(val.Key).Name] = val.Value
	}
}

Describe the solution you'd like It would be amazingly convenient for the VirtualMachine object (and other similar resource objects), itself, to have a function that returns a list of key-value custom attributes/fields, like so:

vm.CustomValues // returns map[string]string of key/values
vm.GetCustomValue(keyname) // returns string of value

In this way custom attributes would actually behave like attributes of the resource they're supposed to be attached to, rather than a lookup for a totally separate resource.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context My use-case only needs this for VMs, but I imagine all resources (or perhaps generically just Common) would greatly benefit.

rickatnight11 avatar Jul 24 '23 16:07 rickatnight11

Howdy 🖐   rickatnight11 ! Thank you for your interest in this project. We value your feedback and will respond soon.

If you want to contribute to this project, please make yourself familiar with the CONTRIBUTION guidelines.

github-actions[bot] avatar Jul 24 '23 16:07 github-actions[bot]

This issue is stale because it has been open for 90 days with no activity. It will automatically close after 30 more days of inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

github-actions[bot] avatar Oct 23 '23 01:10 github-actions[bot]

/remove-lifecycle stale

rickatnight11 avatar Oct 23 '23 10:10 rickatnight11

/remove-lifecycle stale

rickatnight11 avatar Oct 23 '23 10:10 rickatnight11

This issue is stale because it has been open for 90 days with no activity. It will automatically close after 30 more days of inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

github-actions[bot] avatar Jan 22 '24 01:01 github-actions[bot]

Not stale.

rickatnight11 avatar Jan 22 '24 01:01 rickatnight11

/remove-lifecycle stale

rickatnight11 avatar Jan 22 '24 01:01 rickatnight11

/remove-lifecycle stale

rickatnight11 avatar Jan 22 '24 01:01 rickatnight11

This issue is stale because it has been open for 90 days with no activity. It will automatically close after 30 more days of inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

github-actions[bot] avatar Apr 22 '24 01:04 github-actions[bot]

/remove-lifecycle stale

rickatnight11 avatar Apr 22 '24 14:04 rickatnight11

This issue is stale because it has been open for 90 days with no activity. It will automatically close after 30 more days of inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

github-actions[bot] avatar Jul 23 '24 01:07 github-actions[bot]

/remove-lifecycle stale

/remove-lifecycle stale

rickatnight11 avatar Jul 23 '24 17:07 rickatnight11