import { WebSiteManagementClient , WebSiteManagementMappers , WebSiteManagementModels } from '@azure/arm-appservice' ; import { MessageItem , Progress } from 'vscode' ; import { AzExtLocation , AzureWizardExecuteStep , LocationListStep , parseError } from 'vscode-azureextensionui' ; import { webProvider } from '../constants' ; import { ext } from '../extensionVariables' ; import { localize } from '../localize' ; import { tryGetAppServicePlan } from '../tryGetSiteResource' ; import { createWebSiteClient } from '../utils/azureClients' ; import { nonNullProp , nonNullValueAndProp } from '../utils/nonNull' ; import { AppKind , WebsiteOS } from './AppKind' ; import { AppServicePlanListStep } from './AppServicePlanListStep' ; import { CustomLocation , IAppServiceWizardContext } from './IAppServiceWizardContext' ; export class AppServicePlanCreateStep extends AzureWizardExecuteStep < IAppServiceWizardContext > { public priority : number = 120 ; public async execute ( context , progress ) { const newPlanName = nonNullProp ( context , 'newPlanName' ) ; const rgName = nonNullValueAndProp ( context . resourceGroup , 'name' ) ; const findingAppServicePlan = localize ( 'FindingAppServicePlan' , 'Ensuring App Service plan "{0}" exists...' , newPlanName ) ; const creatingAppServicePlan = localize ( 'CreatingAppServicePlan' , 'Creating App Service plan "{0}"...' , newPlanName ) ; const foundAppServicePlan = localize ( 'FoundAppServicePlan' , 'Successfully found App Service plan "{0}".' , newPlanName ) ; const createdAppServicePlan = localize ( 'CreatedAppServicePlan' , 'Successfully created App Service plan "{0}".' , newPlanName ) ; ext . outputChannel . appendLog ( findingAppServicePlan ) ; try { const client = await createWebSiteClient ( context ) ; const existingPlan = await tryGetAppServicePlan ( client , rgName , newPlanName ) ; if ( existingPlan ) { context . plan = existingPlan ; ext . outputChannel . appendLog ( foundAppServicePlan ) ; } else { ext . outputChannel . appendLog ( creatingAppServicePlan ) ; progress . report ( { message : creatingAppServicePlan } ) ; context . plan = await client . appServicePlans . createOrUpdate ( rgName , newPlanName , await getNewPlan ( context ) ) ; ext . outputChannel . appendLog ( createdAppServicePlan ) ; } } catch ( e ) { if ( parseError ( e ) . errorType === 'AuthorizationFailed' ) { await this . selectExistingPrompt ( context ) ; } else { throw e ; } } } public async selectExistingPrompt ( context ) { const message = localize ( 'planForbidden' , 'You do not have permission to create an app service plan in subscription "{0}".' , context . subscriptionDisplayName ) ; const selectExisting = { title : localize ( 'selectExisting' , 'Select Existing' ) } ; await context . ui . showWarningMessage ( message , { modal : true , stepName : 'AspNoPermissions' } , selectExisting ) ; context . telemetry . properties . forbiddenResponse = 'SelectExistingAsp' ; const step = new AppServicePlanListStep ( true ) ; await step . prompt ( context ) ; } public shouldExecute ( context ) { return ! context . plan ; } } async function getNewPlan ( context ) { const location = await LocationListStep . getLocation ( context , webProvider ) ; const plan = { kind : getPlanKind ( context ) , sku : nonNullProp ( context , 'newPlanSku' ) , location : location . name , reserved : context . newSiteOS === WebsiteOS . linux } ; const skuFamily = context . newPlanSku ?. family ? context . newPlanSku ?. family . toLowerCase ( ) : '' ; if ( skuFamily === 'ep' || skuFamily === 'ws' ) { plan . maximumElasticWorkerCount = 20 ; } if ( context . customLocation ) { addCustomLocationProperties ( plan , context . customLocation ) ; } return plan ; } /**
 * Has a few temporary workarounds so that the sdk allows some newer properties on the plan
 */ function addCustomLocationProperties ( plan , customLocation ) { plan . perSiteScaling = true ; WebSiteManagementMappers . AppServicePlan . type . modelProperties ! . kubeEnvironmentProfile = { serializedName : 'properties.kubeEnvironmentProfile' , type : { name : "Composite" , modelProperties : { id : { serializedName : "id" , type : { name : "String" } } } } } ; ( < any > plan ) . kubeEnvironmentProfile = { id : customLocation . kubeEnvironment . id } ; WebSiteManagementMappers . AppServicePlan . type . modelProperties ! . extendedLocation = { serializedName : 'extendedLocation' , type : { name : "Composite" , modelProperties : { name : { serializedName : "name" , type : { name : "String" } } , type : { serializedName : "type" , type : { name : "String" } } } } } ; ( < any > plan ) . extendedLocation = { name : customLocation . id , type : 'customLocation' } ; } function getPlanKind ( context ) { if ( context . customLocation ) { return 'linux,kubernetes' ; } else if ( context . newSiteOS === WebsiteOS . linux ) { return WebsiteOS . linux ; } else { return AppKind . app ; } }