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.
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.