diff --git a/src/FunctionAppResolver.ts b/src/FunctionAppResolver.ts index 14a3f887b..7f5031c51 100644 --- a/src/FunctionAppResolver.ts +++ b/src/FunctionAppResolver.ts @@ -1,18 +1,28 @@ -import { getResourceGroupFromId } from "@microsoft/vscode-azext-azureutils"; -import { callWithTelemetryAndErrorHandling, IActionContext, ISubscriptionContext, nonNullProp } from "@microsoft/vscode-azext-utils"; +import { Site } from "@azure/arm-appservice"; +import { uiUtils } from "@microsoft/vscode-azext-azureutils"; +import { IActionContext, ISubscriptionContext, callWithTelemetryAndErrorHandling, nonNullProp, nonNullValue } from "@microsoft/vscode-azext-utils"; import { AppResource, AppResourceResolver } from "@microsoft/vscode-azext-utils/hostapi"; import { ResolvedFunctionAppResource } from "./tree/ResolvedFunctionAppResource"; import { createWebSiteClient } from "./utils/azureClients"; export class FunctionAppResolver implements AppResourceResolver { + + private siteCacheLastUpdated = 0; + private siteCache: Map = new Map(); + public async resolveResource(subContext: ISubscriptionContext, resource: AppResource): Promise { return await callWithTelemetryAndErrorHandling('resolveResource', async (context: IActionContext) => { const client = await createWebSiteClient({ ...context, ...subContext }); - const rg = getResourceGroupFromId(nonNullProp(resource, 'id')); - const name = nonNullProp(resource, 'name'); - const site = await client.webApps.get(rg, name); - return ResolvedFunctionAppResource.createResolvedFunctionAppResource(context, subContext, site); + if (this.siteCacheLastUpdated < Date.now() - 1000 * 3) { + this.siteCache.clear(); + const sites = await uiUtils.listAllIterator(client.webApps.list()); + sites.forEach(site => this.siteCache.set(nonNullProp(site, 'id').toLowerCase(), site)); + this.siteCacheLastUpdated = Date.now(); + } + + const site = this.siteCache.get(nonNullProp(resource, 'id').toLowerCase()); + return ResolvedFunctionAppResource.createResolvedFunctionAppResource(context, subContext, nonNullValue(site)); }); }