import { HttpMethods , HttpOperationResponse , ParameterValue , ServiceClient } from "@azure/ms-rest-js" ; import { TokenCredentialsBase } from "@azure/ms-rest-nodeauth" ; import { createGenericClient } from "vscode-azureextensionui" ; import * as Constants from "../../constants" ; import { nonNullOrEmptyValue } from "../../utils/nonNull" ; import { IFunctionKeys , IWebAppContract } from "./contracts" ; import { IFunctionContract } from "./contracts" ; export class FunctionAppService { public baseUrl : string ; public credentials : TokenCredentialsBase ; public endPointUrl : string ; public subscriptionId : string ; public resourceGroup : string ; public functionName : string ; constructor ( credentials , endPointUrl , subscriptionId , resourceGroup , functionName ) { this . baseUrl = this . genSiteUrl ( endPointUrl , subscriptionId , resourceGroup , functionName ) ; this . credentials = credentials ; this . endPointUrl = endPointUrl ; this . subscriptionId = subscriptionId ; this . resourceGroup = resourceGroup ; this . functionName = functionName ; } public async listAllFunctions ( ) { const result = await this . request ( `${ this . baseUrl }/functions?api-version=${ Constants . functionAppApiVersion }` , "GET" ) ; return < IFunctionContract [ ] > ( result . parsedBody . value ) ; } public async addFuncHostKeyForApim ( keyName ) { const hostKeys = await this . getFuncHostKeys ( ) ; const funcAppKeyName = `apim-${ keyName }` ; if ( hostKeys !== undefined && hostKeys . functionKeys !== undefined && hostKeys . functionKeys [ funcAppKeyName ] ) { return hostKeys . functionKeys [ funcAppKeyName ] ; } return await this . createFuncHostKey ( funcAppKeyName ) ; } public async getWebAppConfig ( resourceGroup , serviceName , apiName ) { const webAppConfig = await this . getFuncAppConfig ( ) ; const apiConfigId = `/subscriptions/${ this . subscriptionId }/resourceGroups/${ resourceGroup }/providers/Microsoft.ApiManagement/service/${ serviceName }/apis/${ apiName }` ; if ( webAppConfig . properties . apiManagementConfig === null ) { webAppConfig . properties . apiManagementConfig = { id : apiConfigId } ; } else if ( ! webAppConfig . properties . apiManagementConfig . id ) { webAppConfig . properties . apiManagementConfig . id = apiConfigId ; } await this . request ( `${ this . baseUrl }/config/web?api-version=${ Constants . functionAppApiVersion }` , "PUT" , undefined , webAppConfig ) ; } private async getFuncAppConfig ( ) { const webConfigUrl = `${ this . baseUrl }/config/web?api-version=${ Constants . functionAppApiVersion }` ; const result = await this . request ( webConfigUrl , "GET" ) ; return < IWebAppContract > result . parsedBody ; } private async getFuncHostKeys ( ) { const url = `${ this . baseUrl }/host/default/listkeys?api-version=${ Constants . functionAppApiVersion }` ; const result = await this . request ( url , "POST" ) ; return < IFunctionKeys > result . parsedBody ; } private genSiteUrl ( endPointUrl , subscriptionId , resourceGroup , functionName ) { return `${ endPointUrl }/subscriptions/${ subscriptionId }/resourceGroups/${ resourceGroup }/providers/Microsoft.Web/sites/${ functionName }` ; } private async createFuncHostKey ( funcKeyName ) { const funcKeyUrl = `${ this . baseUrl }/host/default/functionkeys/${ funcKeyName }?api-version=${ Constants . functionAppCreateKeyApiVersion }` ; const result = await this . request ( funcKeyUrl , "PUT" , undefined , { properties : { } } ) ; return nonNullOrEmptyValue ( < string > result . parsedBody . properties . value ) ; } private async request ( url , method , queryParameters ? , body ? ) { const client = await createGenericClient ( this . credentials ) ; return await client . sendRequest ( { method : method , url : url , queryParameters : queryParameters , body : body } ) ; } }