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
private findTagByName(objVAPIClient: VAPIClient, strTagName: string): com_vmware_cis_tagging_tag__model {
let objVAPITaggingTag: com_vmware_cis_tagging_tag = new com_vmware_cis_tagging_tag(objVAPIClient);
let arrTagID: string[] = objVAPITaggingTag.list();
let arrVAPITagModel: com_vmware_cis_tagging_tag__model[] = arrTagID.map((strTagID: string): com_vmware_cis_tagging_tag__model => {
let objVAPITagModel: com_vmware_cis_tagging_tag__model = objVAPITaggingTag.get(strTagID);
return objVAPITagModel;
});
let objVAPITagModel: com_vmware_cis_tagging_tag__model = arrVAPITagModel.find((objVAPITagModel: com_vmware_cis_tagging_tag__model): boolean => {
return objVAPITagModel.name === strTagName;
});
return objVAPITagModel;
}Function attachTagToVirtualMachine
private attachTagToVirtualMachine(objVAPIClient: VAPIClient, strTagID: string, objVcVirtualMachine: VcVirtualMachine): void {
let objDynamicID: com_vmware_vapi_std_dynamic__ID = this.getDynamicIDFromVirtualMachine(objVcVirtualMachine);
let objVAPITaggingTagAssociation: com_vmware_cis_tagging_tag__association = new com_vmware_cis_tagging_tag__association(objVAPIClient);
objVAPITaggingTagAssociation.attach(strTagID, objDynamicID);
}Function attachTag
public attachTag(objVcVirtualMachine: VcVirtualMachine, strTagName: string): void {
this.objLogger.info(`Start - Attaching Tag '${strTagName}' on Virtual Machine '${objVcVirtualMachine.name}'`);
let objVcSdkConnection: VcSdkConnection = objVcVirtualMachine.sdkConnection;
let strVCenterServerName: string = objVcSdkConnection.id;
let objVAPIClient: VAPIClient;
try {
let objVAPIEndpoint: VAPIEndpoint = VAPIManager.findEndpoint(`https://${strVCenterServerName}/api`);
objVAPIClient = objVAPIEndpoint.client(null, null);
let objVAPITagModel: 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.
