Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set custom attributes & tags when cloning VM from template #415

Open
voiprodrigo opened this issue Feb 19, 2018 · 10 comments
Open

Comments

@voiprodrigo
Copy link

Hello,

Did not find this capability in the documentation. Is this possible somehow?
I think it's very useful feature, to be able to classify the VM with either tags or custom attributes, either as part of the cloning process, and a separate get/set command for existing VM's.

Thanks.

@swalberg
Copy link
Collaborator

Hi, I'd love to see tags. Unfortunately it's not available in the SOAP API. VMWare does have an API but so far has not made the ruby gem available on rubygems, which means we can't distribute it, and haven't tried to do the code.

Which custom attributes are you talking about? There's an --annotate option when you clone, but no separate command. There's also get/set properties but they've been here longer than I have, and I've never used them.

@voiprodrigo
Copy link
Author

Noted regarding tags.
The annotate option will just add free text to the VM "Notes" field.
vCenter allows your to define key/values as custom attributes, which can be assigned to different vsphere objects, VM's included.

One example of a tool that leverages this is the vagrant-vsphere plugin, which also uses rbvmomi. When provisioning a VM with it, you can define which values to assign to one or more custom attribute keys.

@swalberg
Copy link
Collaborator

Aha, so this is setting extraConfig on the VM. We'd be able to do that with knife vsphere vm config if it could handle data structures as the value rather than only strings.

@voiprodrigo
Copy link
Author

@swalberg
Copy link
Collaborator

We could definitely add that kind of stuff (or take PRs doing it ;) ), how were you thinking it would get used?

@voiprodrigo
Copy link
Author

voiprodrigo commented Feb 19, 2018

Hint noted :)

I see these use cases:

  • Assign values to 1+ custom attributes keys as an option to vm clone command
  • Generically, get all values currently assigned to the target object (VM, datastore, etc)
  • Generically, assign values to the target object

In all cases, any custom attribute key would need to already exist in vCenter, and be assigned to the object type (VM, datastore, etc)

It should be possible to set the option value as a list of k/v pairs (or maybe call the option multiple times in the same command). I don't know how this is usually done in knife-vsphere.

In my particular case, I'm wanting this to facilitate some asset management stuff.

@swalberg
Copy link
Collaborator

Typically we pass JSON, such as --bootstrap-vault-json or --json-attributes, though if it's not nested there's also precedent of comma delimited, such as --myopt k1:v1,k2:v2.

Since I don't use those features (yet? It sounds helpful) I'll have a look around the UI to see how and where I'd use them.

@voiprodrigo
Copy link
Author

I was taking a second look to the VMware link for ExtensibleManagedObject, and as the only method is setCustomValue, it limits this functionality to a (over)write operation, which is unfortunate. No idea if that information can be extracted with a different method.

@swalberg
Copy link
Collaborator

Would it be customValue on the VM object? (not at work today otherwise I'd give it a try).

I also see in the docs there's a AddCustomFieldDef method, so presumably we could make these keys from the command line, or even creating them if they don't exist.

@voiprodrigo
Copy link
Author

voiprodrigo commented Feb 19, 2018

I digged a little bit further, and this is actually doable in a r/w fashion using the customFieldsManager interface. Came up with this snippet of code as proof of concept.

This code finds the VM, gets all custom fields from vcenter and creates a simple hash map of field id > field name. Then iterates over the VM customValue property and outputs field Name and the Value assigned in that particular VM. Then actually changes the value of field with id 41.

Seems to work fine, just tested against one vCenter 6.5.

(disclaimer: Ruby newbie here)

`vim = VIM.connect opts

dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
vm = dc.find_vm(vm_name) or abort "VM not found"

fields = vim.serviceInstance.content.customFieldsManager.field
fpairs = Hash.new

fields.each do |f|
fpairs[f.key] = f.name
end

vm.customValue.each do |x|
puts "#{fpairs[x.key]} (#{x.key}): #{x.value}"
end

vim.serviceInstance.content.customFieldsManager.SetField(entity: vm, key: "41", value: "whatever")`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants