I am working with Azure SDK in Node.js and I want to get Virtual Machines by their prefix because I do not have their complete name.
I tried to use the wildcard but it is not working.
In the arm-resources.d.ts
file that contains the types of the @azure/arm-resources
they specified that I can use the function substringof but it seems it does not work with tags
My VM have the tags object property as follows
tags: {
instancename: "WEB-OCP-0003-1"
}
The thing is that I have only the VM prefix WEB-OCP-0003, If id hardcode just the -1 it works but this part is not always 1
This worked for me but I hardcoded the -1
const filter = `tagName eq 'instancename' and tagValie eq '${instancePrefix.toUpperCase()}-.'`;
/**
* @param {GetInstancesFromProviderParams} params
* @returns {Promise<import("@azure/arm-compute").ResourceSku[]>}
*/
async getInstancesFromProvider({ instancePrefix, region: _region }) {
// I tried all of these
// const filter = `tagName eq 'instancename' and startswith(tagValue, '${instancePrefix}')`;
// const filter = `tagName eq 'instancename' and substringof('${instancePrefix}', tagValue)`;
// const filter = `tagName eq 'instancename' and tagValue eq '${instancePrefix}*'`;
// const filter = `substringof('${instancePrefix}', instancename)`;
// const filter = `tagName eq 'instancename' and tagValue eq substringof('${instancePrefix}', instancename)`;
// const filter = `tagName eq 'instancename' and substringof('${instancePrefix}', instancename)`;
console.log("instancePrefix:", instancePrefix);
console.log("instancePrefix uppercase:", instancePrefix.toUpperCase());
// Filter to get VMs whose tags.instancename starts with the specified prefix
// const filter = `tagName eq 'instancename' and tagValue startsWith(tagValue, '${instancePrefix.toUpperCase()}')`;
const filter = `tagName eq 'instancename' and tagValue eq '${instancePrefix.toUpperCase()}-1'`;
const response = await this.resourcesClient.resources.listByResourceGroup(this.resourceGroupName, {
filter,
});
const vms = [];
for await (const vm of response) {
vms.push(vm);
}
console.log("Filtered VMs:", vms);
return vms;
}