"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureImageService = void 0; const arm_compute_1 = require("@azure/arm-compute"); const identity_1 = require("@azure/identity"); const cache_1 = require("./cache"); const version_1 = require("./version"); const CACHE_TTL_MS = 5 * 60 * 1000; class AzureImageService { subscriptionId; credential = new identity_1.DefaultAzureCredential(); computeClient; cache = new cache_1.MemoryCache(); constructor(subscriptionId) { this.subscriptionId = subscriptionId; this.computeClient = new arm_compute_1.ComputeManagementClient(this.credential, subscriptionId); } async getLocations() { const cacheKey = "locations"; const cached = this.cache.get(cacheKey); if (cached) { return cached; } const token = await this.credential.getToken("https://management.azure.com/.default"); const url = `https://management.azure.com/subscriptions/${this.subscriptionId}/locations?api-version=2022-12-01`; const response = await fetch(url, { headers: { Authorization: `Bearer ${token?.token ?? ""}` } }); if (!response.ok) { throw new Error(`Failed to fetch Azure locations: ${response.status} ${response.statusText}`); } const payload = (await response.json()); const allLocations = (payload.value ?? []) .filter((loc) => Boolean(loc.name)) .map((loc) => ({ name: loc.name, displayName: loc.displayName ?? loc.name, regionType: loc.metadata?.regionType })); const physical = allLocations.filter((loc) => loc.regionType?.toLowerCase() === "physical"); const locations = (physical.length > 0 ? physical : allLocations).map((loc) => ({ name: loc.name, displayName: loc.displayName })); locations.sort((a, b) => a.name.localeCompare(b.name)); this.cache.set(cacheKey, locations, CACHE_TTL_MS); return locations; } async getPublishers(location) { const cacheKey = `publishers:${location}`; const cached = this.cache.get(cacheKey); if (cached) { return cached; } const response = await this.computeClient.virtualMachineImages.listPublishers(location); const publishers = this.extractNames(response).sort((a, b) => a.localeCompare(b)); this.cache.set(cacheKey, publishers, CACHE_TTL_MS); return publishers; } async getOffers(location, publisher) { const cacheKey = `offers:${location}:${publisher}`; const cached = this.cache.get(cacheKey); if (cached) { return cached; } const response = await this.computeClient.virtualMachineImages.listOffers(location, publisher); const offers = this.extractNames(response).sort((a, b) => a.localeCompare(b)); this.cache.set(cacheKey, offers, CACHE_TTL_MS); return offers; } async getSkus(location, publisher, offer) { const cacheKey = `skus:${location}:${publisher}:${offer}`; const cached = this.cache.get(cacheKey); if (cached) { return cached; } const response = await this.computeClient.virtualMachineImages.listSkus(location, publisher, offer); const skus = this.extractNames(response).sort((a, b) => a.localeCompare(b)); this.cache.set(cacheKey, skus, CACHE_TTL_MS); return skus; } async getVersions(location, publisher, offer, sku) { const cacheKey = `versions:${location}:${publisher}:${offer}:${sku}`; const cached = this.cache.get(cacheKey); if (cached) { return cached; } const response = await this.computeClient.virtualMachineImages.list(location, publisher, offer, sku); const versions = this.extractNames(response); const sorted = (0, version_1.sortImageVersionsIfSemantic)(versions); this.cache.set(cacheKey, sorted, CACHE_TTL_MS); return sorted; } extractNames(source) { const items = Array.isArray(source) ? source : typeof source === "object" && source !== null && "value" in source && Array.isArray(source.value) ? source.value : []; return items .map((item) => (typeof item === "object" && item !== null && "name" in item ? item.name : undefined)) .filter((value) => Boolean(value)); } } exports.AzureImageService = AzureImageService;