import { ApiManagementModels } from "@azure/arm-apimanagement" ; import { ApiContract } from "@azure/arm-apimanagement/src/models" ; import { ProgressLocation , window } from "vscode" ; import { AzureParentTreeItem , AzureTreeItem , DialogResponses , ISubscriptionContext , UserCancelledError } from "vscode-azureextensionui" ; import { localize } from "../localize" ; import { nonNullProp } from "../utils/nonNull" ; import { treeUtils } from "../utils/treeUtils" ; import { ApiOperationsTreeItem } from "./ApiOperationsTreeItem" ; import { ApiOperationTreeItem } from "./ApiOperationTreeItem" ; import { ApiPolicyTreeItem } from "./ApiPolicyTreeItem" ; import { IApiTreeRoot } from "./IApiTreeRoot" ; import { IServiceTreeRoot } from "./IServiceTreeRoot" ; import { OperationPolicyTreeItem } from "./OperationPolicyTreeItem" ; export class ApiTreeItem extends AzureParentTreeItem < IApiTreeRoot > { public static contextValue : string = 'azureApiManagementApi' ; public contextValue : string = ApiTreeItem . contextValue ; public readonly commandId : string = 'azureApiManagement.showArmApi' ; public policyTreeItem : ApiPolicyTreeItem ; private _name : string ; private _label : string ; private _root : IApiTreeRoot ; private _operationsTreeItem : ApiOperationsTreeItem ; constructor ( parent , public apiContract , apiVersion ? ) { super ( parent ) ; if ( ! apiVersion ) { this . _label = nonNullProp ( this . apiContract , 'displayName' ) ; } else { this . _label = apiVersion ; } this . _name = nonNullProp ( this . apiContract , 'name' ) ; this . _root = this . createRoot ( parent . root , this . _name ) ; this . _operationsTreeItem = new ApiOperationsTreeItem ( this ) ; this . policyTreeItem = new ApiPolicyTreeItem ( this ) ; } public get id ( ) : string { return this . _name ; } public get label ( ) : string { return this . _label ; } public get root ( ) : IApiTreeRoot { return this . _root ; } public get iconPath ( ) : { light : string ; dark : string ; } { return treeUtils . getThemedIconPath ( 'api' ) ; } public async loadMoreChildrenImpl ( ) { return [ this . _operationsTreeItem , this . policyTreeItem ] ; } public hasMoreChildrenImpl ( ) { return false ; } public async deleteTreeItemImpl ( ) { const message = localize ( "confirmDeleteApi" , `Are you sure you want to delete API '${ this . root . apiName }' and its contents?` ) ; const result = await window . showWarningMessage ( message , { modal : true } , DialogResponses . deleteResponse , DialogResponses . cancel ) ; if ( result === DialogResponses . deleteResponse ) { const deletingMessage = localize ( "deletingApi" , `Deleting API "${ this . root . apiName }"...` ) ; await window . withProgress ( { location : ProgressLocation . Notification , title : deletingMessage } , async ( ) => { await this . root . client . api . deleteMethod ( this . root . resourceGroupName , this . root . serviceName , this . root . apiName , '*' ) ; } ) ; window . showInformationMessage ( localize ( "deletedApi" , `Successfully deleted API "${ this . root . apiName }".` ) ) ; } else { throw new UserCancelledError ( ) ; } } public pickTreeItemImpl ( expectedContextValues ) { for ( const expectedContextValue of expectedContextValues ) { switch ( expectedContextValue ) { case OperationPolicyTreeItem . contextValue : case ApiOperationTreeItem . contextValue : return this . _operationsTreeItem ; default : } } return undefined ; } public async reloadApi ( api ) { this . apiContract = api ; this . _name = nonNullProp ( api , 'name' ) ; this . _label = this . getRevisionDisplayName ( api ) ; this . _root = this . createRoot ( this . root , this . _name ) ; this . _operationsTreeItem = new ApiOperationsTreeItem ( this ) ; this . policyTreeItem = new ApiPolicyTreeItem ( this ) ; } private getRevisionDisplayName ( api ) { if ( api . isCurrent !== undefined && api . isCurrent === true ) { return api . displayName ! ; } else { const revNumber = api . name ! . split ( ';rev=' ) [ 1 ] ; return api . displayName ! . concat ( ';rev=' , revNumber ) ; } } private createRoot ( subRoot , apiName ) { return Object . assign ( { } , < IServiceTreeRoot > subRoot , { apiName : apiName } ) ; } }