import { ApiManagementModels } from "@azure/arm-apimanagement" ; import { ApiContract , ApiCreateOrUpdateParameter } from "@azure/arm-apimanagement/src/models" ; import { AzExtTreeItem , AzureParentTreeItem , ICreateChildImplContext } from "vscode-azureextensionui" ; import { topItemCount } from "../constants" ; import { localize } from "../localize" ; import { IOpenApiImportObject } from "../openApi/OpenApiImportObject" ; import { apiUtil } from "../utils/apiUtil" ; import { processError } from "../utils/errorUtil" ; import { treeUtils } from "../utils/treeUtils" ; import { ApiTreeItem } from "./ApiTreeItem" ; import { ApiVersionSetTreeItem } from "./ApiVersionSetTreeItem" ; import { IServiceTreeRoot } from "./IServiceTreeRoot" ; export interface IApiTreeItemContext extends ICreateChildImplContext { apiName : string ; document ? : IOpenApiImportObject ; apiContract ? : ApiContract ; } export class ApisTreeItem extends AzureParentTreeItem < IServiceTreeRoot > { public get iconPath ( ) : { light : string ; dark : string ; } { return treeUtils . getThemedIconPath ( 'list' ) ; } public static contextValue : string = 'azureApiManagementApis' ; public label : string = "APIs" ; public contextValue : string = ApisTreeItem . contextValue ; public selectedApis : ApiContract [ ] = [ ] ; public readonly childTypeLabel : string = localize ( 'azureApiManagement.Api' , 'API' ) ; private _nextLink : string | undefined ; public hasMoreChildrenImpl ( ) { return this . _nextLink !== undefined ; } public async loadMoreChildrenImpl ( clearCache ) { if ( clearCache ) { this . _nextLink = undefined ; } let apisToLoad = this . selectedApis ; if ( this . selectedApis . length === 0 ) { const apiCollection = this . _nextLink === undefined ? await this . root . client . api . listByService ( this . root . resourceGroupName , this . root . serviceName , { expandApiVersionSet : true , top : topItemCount } ) : await this . root . client . api . listByServiceNext ( this . _nextLink ) ; this . _nextLink = apiCollection . nextLink ; apisToLoad = apiCollection . map ( ( s ) => s ) . filter ( s => apiUtil . isNotApiRevision ( s ) ) ; } const versionSetMap = new Map < string , ApiVersionSetTreeItem > ( ) ; return await this . createTreeItemsWithErrorHandling ( apisToLoad , "invalidApiManagementApi" , async ( api ) => { if ( api . apiVersionSetId ) { let apiVersionSetTreeItem = versionSetMap . get ( api . apiVersionSetId ) ; if ( ! apiVersionSetTreeItem ) { apiVersionSetTreeItem = new ApiVersionSetTreeItem ( this , api ) ; versionSetMap . set ( api . apiVersionSetId , apiVersionSetTreeItem ! ) ; return apiVersionSetTreeItem ; } else { if ( apiUtil . isNotApiRevision ( api ) ) { apiVersionSetTreeItem . addApiToSet ( api ) ; } return undefined ; } } else if ( apiUtil . isNotApiRevision ( api ) ) { return new ApiTreeItem ( this , api ) ; } return undefined ; } , ( api ) => { return api . name ; } ) ; } public async createChildImpl ( context ) { if ( context . document ) { return await this . createApiFromOpenApi ( context . showCreatingTreeItem , context . apiName , context . document ) ; } else if ( context . apiContract ) { return await this . createApiWithApiContract ( context . showCreatingTreeItem , context . apiName , context . apiContract ) ; } throw Error ( localize ( "" , "Missing one or more userOptions when creating new Api" ) ) ; } private async createApiFromOpenApi ( showCreatingTreeItem , apiName , document ? ) { if ( document && apiName ) { showCreatingTreeItem ( apiName ) ; try { const api = await apiUtil . createOrUpdateApiWithSwaggerObject ( this , apiName , document ) ; return new ApiTreeItem ( this , api ) ; } catch ( error ) { throw new Error ( processError ( error , localize ( "createAPIFailed" , `Failed to create the API ${ apiName }` ) ) ) ; } } else { throw Error ( localize ( "" , "Expected either OpenAPI document or link." ) ) ; } } private async createApiWithApiContract ( showCreatingTreeItem , apiName , apiContract ? ) { if ( apiContract && apiName ) { showCreatingTreeItem ( apiName ) ; try { await apiUtil . checkApiExist ( this , apiName ) ; const apiPayload = { displayName : apiName , path : apiName , description : apiContract . description , protocols : apiContract . protocols } ; const api = await this . root . client . api . createOrUpdate ( this . root . resourceGroupName , this . root . serviceName , apiName , apiPayload ) ; return new ApiTreeItem ( this , api ) ; } catch ( error ) { throw new Error ( processError ( error , localize ( "createAPIFailed" , `Failed to create the API ${ apiName }` ) ) ) ; } } else { throw Error ( localize ( "" , "Something went wrong when creating this new api." ) ) ; } } }