const { ComputeManagementClient } = require('@azure/arm-compute');
const azureConfig = require('../config/azure');
/**
* Serviço para monitorar Virtual Machine Scale Sets (VMSS) da Azure
*/
class VMSSService {
constructor() {
this.client = null;
}
getClient() {
if (!this.client) {
this.client = new ComputeManagementClient(
azureConfig.getCredential(),
azureConfig.getSubscriptionId()
);
}
return this.client;
}
/**
* Lista todos os VMSS em um grupo de recursos
*/
async listVMSSByResourceGroup(resourceGroupName) {
try {
const client = this.getClient();
const vmssList = [];
for await (const vmss of client.virtualMachineScaleSets.list(resourceGroupName)) {
vmssList.push(vmss);
}
return vmssList;
} catch (error) {
throw new Error(`Erro ao listar VMSS: ${error.message}`);
}
}
/**
* Lista todos os VMSS na subscription
*/
async listAllVMSS() {
try {
const client = this.getClient();
const vmssList = [];
for await (const vmss of client.virtualMachineScaleSets.listAll()) {
vmssList.push(vmss);
}
return vmssList;
} catch (error) {
throw new Error(`Erro ao listar todos os VMSS: ${error.message}`);
}
}
/**
* Obtém detalhes de um VMSS específico
*/
async getVMSS(resourceGroupName, vmssName) {
try {
const client = this.getClient();
const vmss = await client.virtualMachineScaleSets.get(
resourceGroupName,
vmssName
);
return vmss;
} catch (error) {
throw new Error(`Erro ao obter VMSS ${vmssName}: ${error.message}`);
}
}
/**
* Lista instâncias de um VMSS
*/
async listVMSSInstances(resourceGroupName, vmssName) {
try {
const client = this.getClient();
const instances = [];
for await (const instance of client.virtualMachineScaleSetVMs.list(
resourceGroupName,
vmssName
)) {
instances.push(instance);
}
return instances;
} catch (error) {
throw new Error(`Erro ao listar instâncias do VMSS ${vmssName}: ${error.message}`);
}
}
/**
* Obtém status detalhado de um VMSS
*/
async getVMSSStatus(resourceGroupName, vmssName) {
try {
const vmss = await this.getVMSS(resourceGroupName, vmssName);
const instances = await this.listVMSSInstances(resourceGroupName, vmssName);
return {
name: vmss.name,
location: vmss.location,
sku: {
name: vmss.sku?.name,
tier: vmss.sku?.tier,
capacity: vmss.sku?.capacity
},
provisioningState: vmss.provisioningState,
upgradePolicy: vmss.upgradePolicy?.mode,
instanceCount: instances.length,
instances: instances.map(inst => ({
id: inst.instanceId,
name: inst.name,
provisioningState: inst.provisioningState,
vmId: inst.vmId
}))
};
} catch (error) {
throw new Error(`Erro ao obter status do VMSS ${vmssName}: ${error.message}`);
}
}
/**
* Obtém view de instância de uma VM específica no VMSS
*/
async getVMSSInstanceView(resourceGroupName, vmssName, instanceId) {
try {
const client = this.getClient();
const instanceView = await client.virtualMachineScaleSetVMs.getInstanceView(
resourceGroupName,
vmssName,
instanceId
);
return instanceView;
} catch (error) {
throw new Error(`Erro ao obter instance view: ${error.message}`);
}
}
}
module.exports = new VMSSService();