import * as vscode from 'vscode' ; import { IActionContext } from 'vscode-azureextensionui' ; import { gatewayHostName } from '../../constants' ; import { ApiOperationTreeItem } from '../../explorer/ApiOperationTreeItem' ; import { IOperationTreeRoot } from '../../explorer/IOperationTreeRoot' ; import { ext } from "../../extensionVariables" ; import { localize } from '../../localize' ; import { nameUtil } from '../../utils/nameUtil' ; export async function debugPolicy ( context , node ? ) { if ( ! node ) { node = < ApiOperationTreeItem > await ext . tree . showTreeItemPicker ( ApiOperationTreeItem . contextValue , context ) ; } const operationData = await node . getOperationDebugInfo ( ) ; const debugConfig = { type : "apim-policy" , request : "launch" , name : "Attach to APIM" , stopOnEntry : true , gatewayAddress : await getDebugGatewayAddressUrl ( node ! ) , managementAddress : getManagementUrl ( node . root ) , subscriptionId : node . root . subscriptionId , operationData : operationData , fileName : `${ nameUtil ( node . root ) }.http` } ; if ( ! vscode . debug . activeDebugSession ) { await vscode . debug . startDebugging ( undefined , debugConfig ) ; vscode . debug . onDidTerminateDebugSession ( _ => { const editors = vscode . window . visibleTextEditors ; editors . forEach ( editor => { editor . hide ( ) ; } ) ; } , vscode . debug . activeDebugSession ) ; } } function getManagementUrl ( root ) { return `${ root . environment . resourceManagerEndpointUrl }/subscriptions/${ root . subscriptionId }/resourceGroups/${ root . resourceGroupName }/providers/microsoft.apimanagement/service/${ root . serviceName }` ; } async function getDebugGatewayAddressUrl ( node ) { const gatewayUrl = ext . context . globalState . get ( node . root . serviceName + gatewayHostName ) ; if ( gatewayUrl !== undefined ) { return `wss://${ gatewayUrl }/debug-0123456789abcdef` ; } const service = await node . root . client . apiManagementService . get ( node . root . resourceGroupName , node . root . serviceName ) ; const hostNameConfigs = service . hostnameConfigurations ! ; if ( hostNameConfigs . length !== 0 ) { let gatewayHostNameUl = "" ; if ( hostNameConfigs . length > 1 ) { const allHostNames = hostNameConfigs . filter ( ( s ) => ( s . type === "Proxy" ) ) ; const pick = await ext . ui . showQuickPick ( allHostNames . map ( ( s ) => { return { label : s . hostName , gateway : s } ; } ) , { canPickMany : false } ) ; gatewayHostNameUl = `wss://${ pick . gateway . hostName }/debug-0123456789abcdef` ; } else { gatewayHostNameUl = `wss://${ hostNameConfigs [ 0 ] . hostName }/debug-0123456789abcdef` ; } return gatewayHostNameUl ; } throw new Error ( localize ( "ProxyUrlError" , "Please make sure proxy host url is usable." ) ) ; }