@@ -14,6 +14,9 @@ import { getDisabledOrgList, getDisabledTenantList } from "../copilot/utils/copi
1414import { CopilotNotAvailable , CopilotNotAvailableECSConfig } from "../copilot/telemetry/telemetryConstants" ;
1515import path from "path" ;
1616import { oneDSLoggerWrapper } from "../OneDSLoggerTelemetry/oneDSLoggerWrapper" ;
17+ import { bapServiceAuthentication } from "../services/AuthenticationProvider" ;
18+ import { BAP_API_VERSION , BAP_ENVIRONMENT_LIST_URL , BAP_SERVICE_ENDPOINT , ServiceEndpointCategory } from "../services/Constants" ;
19+ import { VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS , VSCODE_EXTENSION_GET_ENV_LIST_FAILED , VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION } from "../services/TelemetryConstants" ;
1720
1821export function getSelectedCode ( editor : vscode . TextEditor ) : string {
1922 if ( ! editor ) {
@@ -320,3 +323,74 @@ export function getECSOrgLocationValue(clusterName: string, clusterNumber: strin
320323
321324 return extractedSubstring ;
322325}
326+
327+ //API call to get env list for an org
328+ export async function getEnvList ( telemetry : ITelemetry , endpointStamp : ServiceEndpointCategory ) : Promise < { envId : string , envDisplayName : string } [ ] > {
329+ const envInfo : { envId : string , envDisplayName : string } [ ] = [ ] ;
330+ try {
331+ const bapAuthToken = await bapServiceAuthentication ( telemetry , true ) ;
332+ const bapEndpoint = getBAPEndpoint ( endpointStamp , telemetry ) ;
333+ const envListEndpoint = `${ bapEndpoint } ${ BAP_ENVIRONMENT_LIST_URL . replace ( '{apiVersion}' , BAP_API_VERSION ) } ` ;
334+
335+ const envListResponse = await fetch ( envListEndpoint , {
336+ method : "GET" ,
337+ headers : {
338+ "Authorization" : `Bearer ${ bapAuthToken } `
339+ }
340+ } ) ;
341+
342+ if ( envListResponse . ok ) {
343+ const envListJson = await envListResponse . json ( ) ;
344+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
345+ envListJson . value . forEach ( ( env : any ) => {
346+ envInfo . push ( {
347+ envId : env . properties . linkedEnvironmentMetadata . instanceUrl ,
348+ envDisplayName : env . properties . displayName
349+ } ) ;
350+ } ) ;
351+ sendTelemetryEvent ( telemetry , { eventName : VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS } ) ;
352+ oneDSLoggerWrapper . getLogger ( ) . traceInfo ( VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS ) ;
353+ } else {
354+ sendTelemetryEvent ( telemetry , {
355+ eventName : VSCODE_EXTENSION_GET_ENV_LIST_FAILED ,
356+ errorMsg : envListResponse . statusText
357+ } ) ;
358+ oneDSLoggerWrapper . getLogger ( ) . traceError ( VSCODE_EXTENSION_GET_ENV_LIST_FAILED , VSCODE_EXTENSION_GET_ENV_LIST_FAILED , new Error ( envListResponse . statusText ) ) ;
359+ }
360+ } catch ( error ) {
361+ sendTelemetryEvent ( telemetry , {
362+ eventName : VSCODE_EXTENSION_GET_ENV_LIST_FAILED ,
363+ errorMsg : ( error as Error ) . message
364+ } ) ;
365+ oneDSLoggerWrapper . getLogger ( ) . traceError ( VSCODE_EXTENSION_GET_ENV_LIST_FAILED , VSCODE_EXTENSION_GET_ENV_LIST_FAILED , error as Error ) ;
366+ }
367+ return envInfo ;
368+ }
369+
370+
371+ export function getBAPEndpoint ( serviceEndpointStamp : ServiceEndpointCategory , telemetry : ITelemetry ) : string {
372+ let bapEndpoint = "" ;
373+
374+ switch ( serviceEndpointStamp ) {
375+ case ServiceEndpointCategory . TEST :
376+ bapEndpoint = "https://test.api.bap.microsoft.com" ;
377+ break ;
378+ case ServiceEndpointCategory . PREPROD :
379+ bapEndpoint = "https://preprod.api.bap.microsoft.com" ;
380+ break ;
381+ case ServiceEndpointCategory . PROD :
382+ bapEndpoint = "https://api.bap.microsoft.com" ;
383+ break ;
384+ // All below endpoints are not supported yet
385+ case ServiceEndpointCategory . DOD :
386+ case ServiceEndpointCategory . GCC :
387+ case ServiceEndpointCategory . HIGH :
388+ case ServiceEndpointCategory . MOONCAKE :
389+ default :
390+ sendTelemetryEvent ( telemetry , { eventName : VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION , data : serviceEndpointStamp } ) ;
391+ oneDSLoggerWrapper . getLogger ( ) . traceInfo ( VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION , { data : serviceEndpointStamp } ) ;
392+ break ;
393+ }
394+
395+ return BAP_SERVICE_ENDPOINT . replace ( '{rootURL}' , bapEndpoint )
396+ }
0 commit comments