import { ApiManagementClient , ApiManagementModels } from '@azure/arm-apimanagement' ; import { ApiManagementServiceListResponse } from '@azure/arm-apimanagement/src/models' ; import { MessageItem } from 'vscode' ; import { AzExtTreeItem , AzureParentTreeItem , AzureWizard , AzureWizardExecuteStep , AzureWizardPromptStep , createAzureClient , ICreateChildImplContext , IErrorHandlingContext , LocationListStep , parseError , ResourceGroupCreateStep , ResourceGroupListStep , SubscriptionTreeItemBase } from 'vscode-azureextensionui' ; import { IServiceWizardContext } from '../commands/createService/IServiceWizardContext' ; import { ServiceCreateStep } from '../commands/createService/ServiceCreateStep' ; import { ServiceNameStep } from '../commands/createService/ServiceNameStep' ; import { ServiceSkuStep } from '../commands/createService/ServiceSkuStep' ; import { extensionPrefix } from '../constants' ; import { ext } from '../extensionVariables' ; import { localize } from "../localize" ; import { nonNullProp } from '../utils/nonNull' ; import { getWorkspaceSetting , updateGlobalSetting } from '../vsCodeConfig/settings' ; import { ServiceTreeItem } from './ServiceTreeItem' ; export class ApiManagementProvider extends SubscriptionTreeItemBase { public readonly childTypeLabel : string = localize ( 'azureApiManagement.ApimService' , 'API Management Service' ) ; private _nextLink : string | undefined ; public hasMoreChildrenImpl ( ) { return this . _nextLink !== undefined ; } public async loadMoreChildrenImpl ( clearCache ) { if ( clearCache ) { this . _nextLink = undefined ; } const client = createAzureClient ( this . root , ApiManagementClient ) ; let apiManagementServiceListResponse ; try { apiManagementServiceListResponse = this . _nextLink === undefined ? await client . apiManagementService . list ( ) : await client . apiManagementService . listNext ( this . _nextLink ) ; } catch ( error ) { if ( parseError ( error ) . errorType . toLowerCase ( ) === 'notfound' ) { return [ ] ; } else { throw error ; } } this . _nextLink = apiManagementServiceListResponse . _response . parsedBody . nextLink ; return await this . createTreeItemsWithErrorHandling ( apiManagementServiceListResponse . _response . parsedBody , "invalidApiManagementService" , ( service ) => new ServiceTreeItem ( this , client , service ) , ( service ) => { return service . name ; } ) ; } public async createChildImpl ( context ) { const client = createAzureClient ( this . root , ApiManagementClient ) ; const errorhandler = { issueProperties : { } } ; const wizardContext = { client : client , subscriptionId : this . root . subscriptionId , subscriptionDisplayName : this . root . subscriptionDisplayName , credentials : this . root . credentials , environment : this . root . environment , subscriptionPath : "" , userId : this . root . userId , tenantId : "" , errorHandling : errorhandler , ... context } ; const promptSteps = [ ] ; const executeSteps = [ ] ; promptSteps . push ( new ServiceNameStep ( ) ) ; wizardContext . email = this . root . userId ; const advancedCreationKey = 'advancedCreation' ; const advancedCreation = ! ! getWorkspaceSetting ( advancedCreationKey ) ; if ( ! advancedCreation ) { wizardContext . sku = "Consumption" ; await LocationListStep . setLocation ( wizardContext , 'westus' ) ; executeSteps . push ( new ResourceGroupCreateStep ( ) ) ; } else { promptSteps . push ( new ServiceSkuStep ( ) ) ; promptSteps . push ( new ResourceGroupListStep ( ) ) ; LocationListStep . addStep ( wizardContext , promptSteps ) ; } executeSteps . push ( new ServiceCreateStep ( ) ) ; const title = localize ( 'serviceCreatingTitle' , 'Create new API Management instance in Azure' ) ; const wizard = new AzureWizard ( wizardContext , { promptSteps , executeSteps , title } ) ; await wizard . prompt ( ) ; context . showCreatingTreeItem ( nonNullProp ( wizardContext , 'serviceName' ) ) ; if ( ! advancedCreation ) { const newName = await wizardContext . relatedNameTask ; if ( ! newName ) { throw new Error ( localize ( 'noUniqueName' , 'Failed to generate unique name for resources.' ) ) ; } wizardContext . newResourceGroupName = newName ; } try { await wizard . execute ( ) ; } catch ( error ) { if ( ! parseError ( error ) . isUserCancelledError && ! advancedCreation ) { const message = localize ( 'tryAdvancedCreate' , 'Modify the setting "{0}.{1}" if you want to change the default values when creating a API Management Instance in Azure.' , extensionPrefix , advancedCreationKey ) ; const btn = { title : localize ( 'turnOn' , 'Turn on advanced creation' ) } ; ext . ui . showWarningMessage ( message , btn ) . then ( async ( result ) => { if ( result === btn ) { await updateGlobalSetting ( advancedCreationKey , true ) ; } } ) ; } throw error ; } const service = nonNullProp ( wizardContext , 'service' ) ; return new ServiceTreeItem ( this , client , service ) ; } }