Orchestrator Function: vCenter VAPI Plugin Attach Tag To vCenter Virtual Machine

by Simon Sparks · 13 January 2026

To use this function add it to the class library file named vCenterVAPIService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: vCenterVAPIService.ts

Function findTagByName

privatefindTagByName(objVAPIClient:VAPIClient,strTagName:string):com_vmware_cis_tagging_tag__model{

letobjVAPITaggingTag: com_vmware_cis_tagging_tag=newcom_vmware_cis_tagging_tag(objVAPIClient);

letarrTagID:string[]=objVAPITaggingTag.list();

letarrVAPITagModel: com_vmware_cis_tagging_tag__model[]=arrTagID.map((strTagID:string): com_vmware_cis_tagging_tag__model=>{
letobjVAPITagModel: com_vmware_cis_tagging_tag__model=objVAPITaggingTag.get(strTagID);

returnobjVAPITagModel;
});

letobjVAPITagModel: com_vmware_cis_tagging_tag__model=arrVAPITagModel.find((objVAPITagModel: com_vmware_cis_tagging_tag__model):boolean=>{
returnobjVAPITagModel.name===strTagName;
});

returnobjVAPITagModel;
}

Function attachTagToVirtualMachine

privateattachTagToVirtualMachine(objVAPIClient:VAPIClient,strTagID:string,objVcVirtualMachine:VcVirtualMachine):void{
letobjDynamicID: com_vmware_vapi_std_dynamic__ID=this.getDynamicIDFromVirtualMachine(objVcVirtualMachine);

letobjVAPITaggingTagAssociation: com_vmware_cis_tagging_tag__association=newcom_vmware_cis_tagging_tag__association(objVAPIClient);
objVAPITaggingTagAssociation.attach(strTagID,objDynamicID);
}

Function attachTag

publicattachTag(objVcVirtualMachine:VcVirtualMachine,strTagName:string):void{
this.objLogger.info(`Start - Attaching Tag '${strTagName}' on Virtual Machine '${objVcVirtualMachine.name}'`);

letobjVcSdkConnection: VcSdkConnection=objVcVirtualMachine.sdkConnection;

letstrVCenterServerName:string=objVcSdkConnection.id;

letobjVAPIClient: VAPIClient;

try{
letobjVAPIEndpoint: VAPIEndpoint=VAPIManager.findEndpoint(`https://${strVCenterServerName}/api`);

objVAPIClient=objVAPIEndpoint.client(null,null);

letobjVAPITagModel: com_vmware_cis_tagging_tag__model=this.findTagByName(objVAPIClient,strTagName);

this.objLogger.info(`===== Project '${this.strProjectID}' - vCenter Inventory on '${strVCenterServerName}' has a Virtual Machine named '${objVcVirtualMachine.name}' - Attempting to Attach vCenter Tag '${strTagName}'...`);

this.attachTagToVirtualMachine(objVAPIClient,objVAPITagModel.id,objVcVirtualMachine);

this.objLogger.info(`===== Project '${this.strProjectID}' - vCenter Inventory on '${strVCenterServerName}' has a Virtual Machine named '${objVcVirtualMachine.name}' - Successfully Attached vCenter Tag '${strTagName}'.`);
}catch (objException){
this.objLogger.error(`===== Project '${this.strProjectID}' - vCenter Inventory on '${strVCenterServerName}' has a Virtual Machine named '${objVcVirtualMachine.name}' - Failed to Attach vCenter Tag '${strTagName}'.`);
}finally{
objVAPIClient.close();
}

this.objLogger.info(`End - Attaching Tag '${strTagName}' on Virtual Machine '${objVcVirtualMachine.name}'`);
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like