UNPKG

armpit

Version:

Another resource manager programming interface toolkit.

160 lines 7.19 kB
import { WebSiteManagementClient } from "@azure/arm-appservice"; import { mergeAbortSignals } from "./tsUtils.js"; import { applySourceToTargetObjectWithTemplate, applySourceToTargetObject, wrapPropObjectApply, createKeyedArrayPropApplyFn, shallowCloneDefinedValues, shallowMergeDefinedValues, applyObjectKeyProperties, } from "./optionsUtils.js"; import { applyManagedServiceIdentity, locationNameOrCodeEquals } from "./azureUtils.js"; import { handleGet } from "./azureSdkUtils.js"; function splitAppServiceOptionsAndDescriptor(optionsDescriptor) { const { groupName, location, subscriptionId, abortSignal, ...rest } = optionsDescriptor; return { options: { groupName, location, subscriptionId, abortSignal }, descriptor: rest, }; } function applyAppServicePlan(target, descriptor, context) { let appliedChanges = false; if (applySourceToTargetObjectWithTemplate(target, descriptor, { sku: { capabilities: createKeyedArrayPropApplyFn("name", applySourceToTargetObject, true, true), }, }, context)) { appliedChanges = true; } return appliedChanges; } function applySiteConfigAzureStorageInfo(target, source, context) { return applyObjectKeyProperties(target, source, (k, t, s) => { if (t[k] == null) { t[k] = {}; } return applySourceToTargetObject(t[k], s[k], context); }, true, (k, t, s) => applySourceToTargetObject(t[k], s[k], context)); } function applyKeyedStrings(target, source) { return applyObjectKeyProperties(target, source, (k, t, s) => { t[k] = s[k]; }, true, (k, t, s) => { if (t[k] === s[k]) { return false; } t[k] = s[k]; return true; }); } function applySite(target, descriptor, context) { return applySourceToTargetObjectWithTemplate(target, descriptor, { cloningInfo: { appSettingsOverrides: wrapPropObjectApply(applyKeyedStrings), }, hostNameSslStates: createKeyedArrayPropApplyFn("name", applySourceToTargetObject, true, true), identity: wrapPropObjectApply(applyManagedServiceIdentity), siteConfig: { appSettings: createKeyedArrayPropApplyFn("name", applySourceToTargetObject, true, true), azureStorageAccounts: wrapPropObjectApply(applySiteConfigAzureStorageInfo), connectionStrings: createKeyedArrayPropApplyFn("name", applySourceToTargetObject, true, true), ipSecurityRestrictions: createKeyedArrayPropApplyFn("name", applySourceToTargetObject, true, true), metadata: createKeyedArrayPropApplyFn("name", applySourceToTargetObject, true, true), scmIpSecurityRestrictions: createKeyedArrayPropApplyFn("name", applySourceToTargetObject, true, true), }, }, context); } export class AppServiceTools { #managementClientFactory; #options; constructor(dependencies, options) { this.#managementClientFactory = dependencies.managementClientFactory; this.#options = shallowCloneDefinedValues(options); } async planGet(name, options) { const { groupName, subscriptionId, abortSignal } = this.#buildMergedOptions(options); if (groupName == null) { throw new Error("A group name is required to perform operations."); } const client = this.getClient(subscriptionId); return await handleGet(client.appServicePlans.get(groupName, name, { abortSignal })); } async planUpsert(name, optionsDescriptor) { const { options, descriptor } = splitAppServiceOptionsAndDescriptor(optionsDescriptor); const { location, groupName, subscriptionId, abortSignal } = this.#buildMergedOptions(options); if (groupName == null) { throw new Error("A group name is required to perform operations."); } let upsertRequired = false; const client = this.getClient(subscriptionId); let plan = await handleGet(client.appServicePlans.get(groupName, name, { abortSignal })); if (plan) { if (location != null && plan.location != null && !locationNameOrCodeEquals(location, plan.location)) { throw new Error(`Specified location ${location} conflicts with existing ${plan.location}.`); } } else { if (location == null) { throw new Error("A location is required"); } upsertRequired = true; plan = { location }; } if (applyAppServicePlan(plan, descriptor)) { upsertRequired = true; } if (upsertRequired) { plan = await client.appServicePlans.beginCreateOrUpdateAndWait(groupName, name, plan, { abortSignal: abortSignal, }); } return plan; } async webAppGet(name, options) { const { groupName, subscriptionId, abortSignal } = this.#buildMergedOptions(options); if (groupName == null) { throw new Error("A group name is required to perform operations."); } const client = this.getClient(subscriptionId); return await handleGet(client.webApps.get(groupName, name, { abortSignal })); } async webAppUpsert(name, optionsDescriptor) { const { options, descriptor } = splitAppServiceOptionsAndDescriptor(optionsDescriptor); const { location, groupName, subscriptionId, abortSignal } = this.#buildMergedOptions(options); if (groupName == null) { throw new Error("A group name is required to perform operations."); } let upsertRequired = false; const client = this.getClient(subscriptionId); let site = await handleGet(client.webApps.get(groupName, name, { abortSignal })); if (site) { if (location != null && site.location != null && !locationNameOrCodeEquals(location, site.location)) { throw new Error(`Specified location ${location} conflicts with existing ${site.location}.`); } } else { if (location == null) { throw new Error("A location is required"); } upsertRequired = true; site = { location }; } if (applySite(site, descriptor)) { upsertRequired = true; } if (upsertRequired) { site = await client.webApps.beginCreateOrUpdateAndWait(groupName, name, site, { abortSignal: abortSignal, }); } return site; } getClient(subscriptionId, options) { return this.#managementClientFactory.get(WebSiteManagementClient, (subscriptionId ?? this.#options.subscriptionId), options); } #buildMergedOptions(options) { if (options == null) { return this.#options; } const merged = shallowMergeDefinedValues(this.#options, options); const abortSignal = mergeAbortSignals(options.abortSignal, this.#options.abortSignal); if (abortSignal) { merged.abortSignal = abortSignal; } return merged; } } //# sourceMappingURL=appServiceTools.js.map