import type { WebSiteManagementClient , WebSiteManagementModels } from '@azure/arm-appservice' ; import { AzExtLocation , AzureWizardPromptStep , IAzureQuickPickItem , IAzureQuickPickOptions , IWizardOptions , LocationListStep , ResourceGroupListStep } from 'vscode-azureextensionui' ; import { webProvider } from '../constants' ; import { localize } from '../localize' ; import { tryGetAppServicePlan } from '../tryGetSiteResource' ; import { createWebSiteClient } from '../utils/azureClients' ; import { nonNullProp } from '../utils/nonNull' ; import { uiUtils } from '../utils/uiUtils' ; import { AppKind , getWebsiteOSDisplayName , WebsiteOS } from './AppKind' ; import { AppServicePlanCreateStep } from './AppServicePlanCreateStep' ; import { AppServicePlanNameStep } from './AppServicePlanNameStep' ; import { AppServicePlanSkuStep } from './AppServicePlanSkuStep' ; import { IAppServiceWizardContext } from './IAppServiceWizardContext' ; export class AppServicePlanListStep extends AzureWizardPromptStep < IAppServiceWizardContext > { private _suppressCreate : boolean | undefined ; public constructor ( suppressCreate ? ) { super ( ) ; this . _suppressCreate = suppressCreate ; } public static async getPlans ( context ) { if ( context . plansTask === undefined ) { const client = await createWebSiteClient ( context ) ; context . plansTask = uiUtils . listAll ( client . appServicePlans , client . appServicePlans . list ( ) ) ; } return await context . plansTask ; } public static async isNameAvailable ( context , name , resourceGroupName ) { const plans = await AppServicePlanListStep . getPlans ( context ) ; return ! plans . some ( plan => nonNullProp ( plan , 'resourceGroup' ) . toLowerCase ( ) === resourceGroupName . toLowerCase ( ) && nonNullProp ( plan , 'name' ) . toLowerCase ( ) === name . toLowerCase ( ) ) ; } public async prompt ( context ) { const options = { placeHolder : context . newSiteKind ?. includes ( AppKind . workflowapp ) && context . planSkuFamilyFilter ?. test ( 'IV2' ) ? localize ( 'selectV3Plan' , 'Select an App Service Environment (v3) Plan' ) : localize ( 'selectPlan' , 'Select a {0} App Service plan.' , getWebsiteOSDisplayName ( nonNullProp ( context , 'newSiteOS' ) ) ) , id : `AppServicePlanListStep/${ context . subscriptionId }` } ; context . plan = ( await context . ui . showQuickPick ( this . getQuickPicks ( context ) , options ) ) . data ; context . telemetry . properties . newPlan = String ( ! context . plan ) ; if ( context . plan ) { await LocationListStep . setLocation ( context , context . plan . location ) ; } } public async getSubWizard ( context ) { if ( ! context . plan ) { const promptSteps = [ new AppServicePlanNameStep ( ) , new AppServicePlanSkuStep ( ) , new ResourceGroupListStep ( ) ] ; LocationListStep . addStep ( context , promptSteps ) ; return { promptSteps : promptSteps , executeSteps : [ new AppServicePlanCreateStep ( ) ] } ; } else { context . valuesToMask . push ( nonNullProp ( context . plan , 'name' ) ) ; return undefined ; } } public shouldPrompt ( context ) { return ! context . plan && ! context . newPlanName ; } private async getQuickPicks ( context ) { const picks = ! this . _suppressCreate ? [ { label : localize ( 'CreateNewAppServicePlan' , '$(plus) Create new App Service plan' ) , description : '' , data : undefined } ] : [ ] ; let plans = await AppServicePlanListStep . getPlans ( context ) ; const famFilter = context . planSkuFamilyFilter ; if ( famFilter ) { plans = plans . filter ( plan => ! plan . sku || ! plan . sku . family || famFilter . test ( plan . sku . family ) ) ; } let location ; if ( LocationListStep . hasLocation ( context ) ) { location = await LocationListStep . getLocation ( context , webProvider ) ; } let hasFilteredLocations = false ; for ( const plan of plans ) { const isNewSiteLinux = context . newSiteOS === WebsiteOS . linux ; let isPlanLinux = nonNullProp ( plan , 'kind' ) . toLowerCase ( ) . includes ( WebsiteOS . linux ) ; if ( plan . sku && ( plan . sku . family === 'EP' || plan . sku . family === 'WS' ) ) { const client = await createWebSiteClient ( context ) ; const epPlan = await tryGetAppServicePlan ( client , nonNullProp ( plan , 'resourceGroup' ) , nonNullProp ( plan , 'name' ) ) ; isPlanLinux = ! ! epPlan ?. reserved ; } if ( isNewSiteLinux === isPlanLinux ) { if ( location && ! LocationListStep . locationMatchesName ( location , plan . location ) ) { hasFilteredLocations = true ; } else { picks . push ( { id : plan . id , label : nonNullProp ( plan , 'name' ) , description : plan . sku ?. name , data : plan } ) ; } } } if ( hasFilteredLocations && location ) { picks . push ( { label : localize ( 'hasFilteredLocations' , '$(warning) Only plans in the region "{0}" are shown.' , location . displayName ) , onPicked : ( ) => { } , data : undefined } ) ; } return picks ; } }