Home > vSphere API > Tagging: What’s the Alternative?

Tagging: What’s the Alternative?

After my last post Tagging: An invisible feature in vSphere, William Lam quickly followed up with another post in his blog, arguing vSphere Tagging Feature Not So Invisible. He made his case by showing how you can actually add and remove tags using addTag() and removeTag() method via vSphere MOB. You can definitely play with your test environment, but please do not mess up with any existing tags in your production environment.

Although you can do that via MOB, it’s not the officially supported way (BTW, this should not be taken as show stopper. After all, the Client REST API in VI Java API 2.0 is based on MOB). It subjects to change in the future. The ideal way is via vSphere APIs. As I mentioned, there is no official APIs for that. The two methods William mentioned are not formally exposed.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

HTML5 App

Now, what if you want to achieve the same result with official vSphere APIs? Or, is it achievable at all?

The answer is yes. As I covered in my book, there is a managed object type called CustomFieldsManager. What it does is to add and remove custom fields to managed entities. The custom fields are essentially key/value pairs that are attached to managed entities. They are not properties per se but can be accessed via the customValue property (type: CustomFieldValue[]) of a ManagedEntity object. The CustomFieldValue is a key/value pair. As of vSphere 4.1, the value is only string type.

With these basic understanding, you can start to implement tagging with CustomFieldsManager. First, you want to call addCustomFieldDef() method to create a custom field whose name is tag. Then you can call setField() method to set the value of the tag. Remember, you can have only one string for the tag. For more keywords, you can just use “,” or “;” as delimiter, for example, “development;pilot 101.” Unlike the tag property, which is array of Tag object, you have to parse them out while reading and compile them while writing. But it’s pretty trivial task for a programmer.

When you add tag with CustomFieldsManager, it’s really something for yourself unless tagging is explicitly displayed or made searchable via vSphere Client GUI. So far there is no standard tag keyword that we all agree upon.

Categories: vSphere API Tags: , ,
  1. June 13th, 2011 at 00:23 | #1

    Steve,

    afaik, CustomFieldsManager only allows you to set a custom field for a Host or VM, it’s not available to all entities such as datacenter, datastore, network, etc. which the actual “tagging” feature supports. Though not a complete replacement/substitute, if you needed to tag either a VM and/or Host, then it’s definitely an option. Hopefully VMware will expose these methods in the future as they can be very useful.

    –William

  2. June 13th, 2011 at 01:07 | #2

    William,
    According to API reference (see the AddCustomFieldDef() method of CustomFieldsManager):

    Creates a new custom field. If the moType is specified, the field will only be available for that type of managed object.

    It means by default, a field definition will be available to all entities including datacenter, folder. Only with and after 2.5 can you specify what exact type of entity to define a new field.
    I agree with you that these methods can be very useful.
    -Steve

  3. June 13th, 2011 at 21:44 | #3

    @Steve Jin
    Steve,

    I noticed that earlier while looking at the API reference but I thought from the way it was exposed via the vSphere Client that it was limited to just VM & Hosts. I just gave it a try and it in fact you’re right, any of the managed objects will work but just won’t be visible via the client which is too bad. Definitely another option if someone is looking to tag custom attributes without resorting to a separate DB.

    Great post

    –William

  4. June 14th, 2011 at 01:00 | #4

    William,

    Thanks a lot for taking time to validate it and sharing with us the result!

    Steve

  1. June 29th, 2011 at 11:46 | #1