In this recipe, we will see how to create and use custom attributes and tags. We are talking about vCenter tags and not about Orchestrator tags, which have no common ground. This recipe will also use the vAPI plugin.
You need to have a VM to which you can tag or assign a custom attribute.
For tagging, you need to have a vAPI vCenter Endpoint. See the Getting started with vAPI recipe in this chapter.
We will split this recipe into two parts. The first part concerns Custom Attributes, while the second concerns Tags.
Please note that Custom Attributes are only visible in the vSphere Client (also called Fat client or c-Client).
We will now work through the lifecycle of a Custom Attribute:
setOrCreateCustomField
action onto the schema. This action will create a definition and set a value to an object.getCustomField
action onto the schema. This action will read a Custom Attribute from an object.managedEntity
from Any to VC:VirtualMachine.var vimHost = managedEntity.vimHost; var key; var customFieldDefs = vimHost.customFieldsManager.field; for (var i = 0; i < customFieldDefs.length; i++) { if (customFieldDefs[i].name == customFieldName) { key = customFieldDefs[i].key; break; } } vimHost.customFieldsManager.setField(managedEntity, key, "");
var vimHost = managedEntity.vimHost; var customFieldDefs = vimHost.customFieldsManager.field; for (var i = 0; i < customFieldDefs.length; i++) { if (customFieldDefs[i].name == customFieldName) { key = customFieldDefs[i].key; break; } } vimHost.customFieldsManager.removeCustomFieldDef(key);
managedEntity
from Any to VC:VirtualMachine.See also the example workflow 12.02.1 Custom Attribute Lifecycle
.
We will now work with Tags using the vAPI:
Name |
Type |
Section |
Use |
|
|
IN |
The endpoint we want to use |
|
|
IN |
Name of the tag category |
|
|
IN |
Name of the tag |
|
|
IN |
Value of the tag |
|
|
IN |
VM to tag |
|
|
Attribute |
ID of the tag category |
|
|
Attribute |
ID of the tag |
tagCatID
as OUT:var client = endpoint.client(); var tagging = new com_vmware_cis_tagging_category(client); var spec = new com_vmware_cis_tagging_category_create__spec(); spec.name = tagCatName; spec.description = tagDesciption; spec.cardinality = "MULTIPLE"; spec.associable_types = ["VirtualMachine"]; var tagCatID = tagging.create(spec);
tagID
as OUT:var client = endpoint.client(); var tagging = new com_vmware_cis_tagging_tag(client); var spec = new com_vmware_cis_tagging_tag_create__spec(); spec.category_id = tagCatID; spec.description = tagDesciption; spec.name = tagName; var tagID = tagging.create(spec);
var client = endpoint.client(); var tagging = new com_vmware_cis_tagging_tag__association(client); var objectId = new com_vmware_vapi_std_dynamic__ID() ; objectId.id = VM.id; objectId.type = VM.vimType; tagging.attach(tagID, objectId);
var client = endpoint.client(); var tagging = new com_vmware_cis_tagging_tag(client); var tag = tagging.get(tagId); System.log("Name :"+tag.name+" Category :"+tag.category_id+" Deciption :"+tag.description); tagCatID=tag.category_id; var tagging = new com_vmware_cis_tagging_category(client); var tagCat = tagging.get(tagCatID); System.log("Name :"+tagCat.name+" Category :"+tagCat.cardinality+" Deciption :"+tagCat.description+" Types :"+tagCat.associable_types);
var client = endpoint.client(); var tagging = new com_vmware_cis_tagging_tag(client); tagging.delete(tagID);
var client = endpoint.client(); var tagging = new com_vmware_cis_tagging_category(client); tagging.delete(tagCatID);
This recipe is actually more about the changes in vSphere in the last couple of years. Until vSphere 5.5 and the first real workable vSphere Web Client, the vSphere Client using Custom Attributes have been the way to handle things. With vSphere 6, vAPI and a higher value in vSphere Web Client tagging is the way to go.
With the instructions in this recipe, you could create a workflow that transports your Custom Attributes into Tags:
Custom attributes are actually two things. First, we have the custom attribute definition and then we have the custom attribute value. The definition is set once and is then available for all (or almost all) objects inside vCenter. A value is specifically assigned to one object. For instance, you set a Definition called Manager. This field is now available to all objects in vCenter. Then you define a value of Mickey Mouse to one (or more) VM(s).
There are basically only three methods of customFieldsManager
that we are using:
Method |
Description |
|
Adds a definition to vCenter. |
|
Removes a definition from vCenter. |
|
Sets a value to an object. |
Tagging is one of those things that has been missing from Orchestrator since their introduction. The only way around that was to use PowerShell. With the vAPI, things are easier.
Tags in vSphere are a combination of a tag category and the Tag itself. Both items are available for all vCenter objects. The tag category can be limited to specific types of Objects, for example, virtual machines. In addition to this, a tag category can be set to either one Tag per object or multiple Tags per object. The Tag itself has only a name and description, however, it's the item that gets assigned to an object.
To get some more information about the vAPI documentation, here is what I used:
If you get the hang of it, it's actually quite easy, and I'm looking forward to more possibilities.