Skip to content

Commit c7d4ff9

Browse files
amitjoshi438amitjoshipriyanshu92
authored
Add environment list retrieval and refactor BAP endpoint logic (#1060)
* Add environment list retrieval and refactor BAP endpoint logic * Add type annotation for environment list retrieval in Utils.ts * Update src/common/utilities/Utils.ts Co-authored-by: Priyanshu Agrawal <[email protected]> * Refactor BAP environment list URL to use a variable for API version and update imports in Utils.ts --------- Co-authored-by: amitjoshi <[email protected]> Co-authored-by: Priyanshu Agrawal <[email protected]>
1 parent 9dd77cd commit c7d4ff9

File tree

4 files changed

+82
-24
lines changed

4 files changed

+82
-24
lines changed

src/common/services/BAPService.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55

66
import { ITelemetry } from "../OneDSLoggerTelemetry/telemetry/ITelemetry";
77
import { bapServiceAuthentication, getCommonHeaders } from "./AuthenticationProvider";
8-
import { VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION, VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_COMPLETED, VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_FAILED } from "./TelemetryConstants";
8+
import { VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_COMPLETED, VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_FAILED } from "./TelemetryConstants";
99
import { ServiceEndpointCategory, BAP_API_VERSION, BAP_SERVICE_COPILOT_CROSS_GEO_FLAG_RELATIVE_URL, BAP_SERVICE_ENDPOINT } from "./Constants";
1010
import { sendTelemetryEvent } from "../copilot/telemetry/copilotTelemetry";
11+
import { getBAPEndpoint } from "../utilities/Utils";
1112

1213
export class BAPService {
1314
public static async getCrossGeoCopilotDataMovementEnabledFlag(serviceEndpointStamp: ServiceEndpointCategory, telemetry: ITelemetry, environmentId: string): Promise<boolean> {
1415

1516
try {
1617
const accessToken = await bapServiceAuthentication(telemetry, true);
1718

18-
const response = await fetch(await BAPService.getBAPEndpoint(serviceEndpointStamp, telemetry, environmentId), {
19+
const response = await fetch(await BAPService.getBAPCopilotCrossGeoFlagEndpoint(serviceEndpointStamp, telemetry, environmentId), {
1920
method: 'GET',
2021
headers: getCommonHeaders(accessToken)
2122
});
@@ -33,29 +34,9 @@ export class BAPService {
3334
return false;
3435
}
3536

36-
static async getBAPEndpoint(serviceEndpointStamp: ServiceEndpointCategory, telemetry: ITelemetry, environmentId: string): Promise<string> {
37+
static async getBAPCopilotCrossGeoFlagEndpoint(serviceEndpointStamp: ServiceEndpointCategory, telemetry: ITelemetry, environmentId: string): Promise<string> {
3738

38-
let bapEndpoint = "";
39-
40-
switch (serviceEndpointStamp) {
41-
case ServiceEndpointCategory.TEST:
42-
bapEndpoint = "https://test.api.bap.microsoft.com";
43-
break;
44-
case ServiceEndpointCategory.PREPROD:
45-
bapEndpoint = "https://preprod.api.bap.microsoft.com";
46-
break;
47-
case ServiceEndpointCategory.PROD:
48-
bapEndpoint = "https://api.bap.microsoft.com";
49-
break;
50-
// All below endpoints are not supported yet
51-
case ServiceEndpointCategory.DOD:
52-
case ServiceEndpointCategory.GCC:
53-
case ServiceEndpointCategory.HIGH:
54-
case ServiceEndpointCategory.MOONCAKE:
55-
default:
56-
sendTelemetryEvent(telemetry, { eventName: VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION, data: serviceEndpointStamp });
57-
break;
58-
}
39+
const bapEndpoint = await getBAPEndpoint(serviceEndpointStamp, telemetry);
5940

6041
return BAP_SERVICE_ENDPOINT.replace('{rootURL}', bapEndpoint) +
6142
BAP_SERVICE_COPILOT_CROSS_GEO_FLAG_RELATIVE_URL.replace('{environmentID}', environmentId).replace('{apiVersion}', BAP_API_VERSION);

src/common/services/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const BAP_API_VERSION = '2021-04-01';
1717
export const BAP_SERVICE_SCOPE_DEFAULT = "https://api.bap.microsoft.com/.default";
1818
export const BAP_SERVICE_ENDPOINT = `{rootURL}/providers/Microsoft.BusinessAppPlatform/`;
1919
export const BAP_SERVICE_COPILOT_CROSS_GEO_FLAG_RELATIVE_URL = `scopes/admin/environments/{environmentID}?$expand=properties/copilotPolicies&api-version={apiVersion}`;
20+
export const BAP_ENVIRONMENT_LIST_URL = `scopes/admin/environments?api-version={apiVersion}&select=name,properties.displayName,properties.linkedEnvironmentMetadata`;
2021

2122
// PPAPI constants
2223
export const PPAPI_WEBSITES_API_VERSION = '2022-03-01-preview';

src/common/services/TelemetryConstants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ export const VSCODE_EXTENSION_GET_PPAPI_WEBSITES_ENDPOINT_UNSUPPORTED_REGION = "
2424
export const VSCODE_EXTENSION_DECODE_JWT_TOKEN_FAILED = "VSCodeExtensionDecodeJWTTokenFailed";
2525
export const VSCODE_EXTENSION_PPAPI_GET_WEBSITE_BY_ID_COMPLETED = "VSCodeExtensionPPAPIGetWebsiteByIdCompleted";
2626
export const VSCODE_EXTENSION_PPAPI_GET_WEBSITE_BY_ID_FAILED = "VSCodeExtensionPPAPIGetWebsiteByIdFailed";
27+
export const VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS = "VSCodeExtensionGetEnvListSuccess";
28+
export const VSCODE_EXTENSION_GET_ENV_LIST_FAILED = "VSCodeExtensionGetEnvListFailed";

src/common/utilities/Utils.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { getDisabledOrgList, getDisabledTenantList } from "../copilot/utils/copi
1414
import { CopilotNotAvailable, CopilotNotAvailableECSConfig } from "../copilot/telemetry/telemetryConstants";
1515
import path from "path";
1616
import { 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

1821
export 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

Comments
 (0)