Skip to content

Commit

Permalink
Add environment list retrieval and refactor BAP endpoint logic
Browse files Browse the repository at this point in the history
  • Loading branch information
amitjoshi committed Nov 20, 2024
1 parent 9dd77cd commit 9c1bbcd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
29 changes: 5 additions & 24 deletions src/common/services/BAPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

import { ITelemetry } from "../OneDSLoggerTelemetry/telemetry/ITelemetry";
import { bapServiceAuthentication, getCommonHeaders } from "./AuthenticationProvider";
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";
import { VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_COMPLETED, VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_FAILED } from "./TelemetryConstants";
import { ServiceEndpointCategory, BAP_API_VERSION, BAP_SERVICE_COPILOT_CROSS_GEO_FLAG_RELATIVE_URL, BAP_SERVICE_ENDPOINT } from "./Constants";
import { sendTelemetryEvent } from "../copilot/telemetry/copilotTelemetry";
import { getBAPEndpoint } from "../utilities/Utils";

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

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

const response = await fetch(await BAPService.getBAPEndpoint(serviceEndpointStamp, telemetry, environmentId), {
const response = await fetch(await BAPService.getBAPCopilotCrossGeoFlagEndpoint(serviceEndpointStamp, telemetry, environmentId), {
method: 'GET',
headers: getCommonHeaders(accessToken)
});
Expand All @@ -33,29 +34,9 @@ export class BAPService {
return false;
}

static async getBAPEndpoint(serviceEndpointStamp: ServiceEndpointCategory, telemetry: ITelemetry, environmentId: string): Promise<string> {
static async getBAPCopilotCrossGeoFlagEndpoint(serviceEndpointStamp: ServiceEndpointCategory, telemetry: ITelemetry, environmentId: string): Promise<string> {

let bapEndpoint = "";

switch (serviceEndpointStamp) {
case ServiceEndpointCategory.TEST:
bapEndpoint = "https://test.api.bap.microsoft.com";
break;
case ServiceEndpointCategory.PREPROD:
bapEndpoint = "https://preprod.api.bap.microsoft.com";
break;
case ServiceEndpointCategory.PROD:
bapEndpoint = "https://api.bap.microsoft.com";
break;
// All below endpoints are not supported yet
case ServiceEndpointCategory.DOD:
case ServiceEndpointCategory.GCC:
case ServiceEndpointCategory.HIGH:
case ServiceEndpointCategory.MOONCAKE:
default:
sendTelemetryEvent(telemetry, { eventName: VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION, data: serviceEndpointStamp });
break;
}
const bapEndpoint = await getBAPEndpoint(serviceEndpointStamp, telemetry);

return BAP_SERVICE_ENDPOINT.replace('{rootURL}', bapEndpoint) +
BAP_SERVICE_COPILOT_CROSS_GEO_FLAG_RELATIVE_URL.replace('{environmentID}', environmentId).replace('{apiVersion}', BAP_API_VERSION);
Expand Down
1 change: 1 addition & 0 deletions src/common/services/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const BAP_API_VERSION = '2021-04-01';
export const BAP_SERVICE_SCOPE_DEFAULT = "https://api.bap.microsoft.com/.default";
export const BAP_SERVICE_ENDPOINT = `{rootURL}/providers/Microsoft.BusinessAppPlatform/`;
export const BAP_SERVICE_COPILOT_CROSS_GEO_FLAG_RELATIVE_URL = `scopes/admin/environments/{environmentID}?$expand=properties/copilotPolicies&api-version={apiVersion}`;
export const BAP_ENVIRONMENT_LIST_URL = `scopes/admin/environments?api-version=2021-04-01&select=name,properties.displayName,properties.linkedEnvironmentMetadata`;

// PPAPI constants
export const PPAPI_WEBSITES_API_VERSION = '2022-03-01-preview';
Expand Down
2 changes: 2 additions & 0 deletions src/common/services/TelemetryConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export const VSCODE_EXTENSION_GET_PPAPI_WEBSITES_ENDPOINT_UNSUPPORTED_REGION = "
export const VSCODE_EXTENSION_DECODE_JWT_TOKEN_FAILED = "VSCodeExtensionDecodeJWTTokenFailed";
export const VSCODE_EXTENSION_PPAPI_GET_WEBSITE_BY_ID_COMPLETED = "VSCodeExtensionPPAPIGetWebsiteByIdCompleted";
export const VSCODE_EXTENSION_PPAPI_GET_WEBSITE_BY_ID_FAILED = "VSCodeExtensionPPAPIGetWebsiteByIdFailed";
export const VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS = "VSCodeExtensionGetEnvListSuccess";
export const VSCODE_EXTENSION_GET_ENV_LIST_FAILED = "VSCodeExtensionGetEnvListFailed";
73 changes: 73 additions & 0 deletions src/common/utilities/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { getDisabledOrgList, getDisabledTenantList } from "../copilot/utils/copi
import { CopilotNotAvailable, CopilotNotAvailableECSConfig } from "../copilot/telemetry/telemetryConstants";
import path from "path";
import { oneDSLoggerWrapper } from "../OneDSLoggerTelemetry/oneDSLoggerWrapper";
import { bapServiceAuthentication } from "../services/AuthenticationProvider";
import { BAP_ENVIRONMENT_LIST_URL, BAP_SERVICE_ENDPOINT, ServiceEndpointCategory } from "../services/Constants";
import { VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS, VSCODE_EXTENSION_GET_ENV_LIST_FAILED, VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION } from "../services/TelemetryConstants";

export function getSelectedCode(editor: vscode.TextEditor): string {
if (!editor) {
Expand Down Expand Up @@ -320,3 +323,73 @@ export function getECSOrgLocationValue(clusterName: string, clusterNumber: strin

return extractedSubstring;
}

//API call to get env list for a org
export async function getEnvList(telemetry: ITelemetry, endpointStamp: ServiceEndpointCategory): Promise<{ envId: string, envDisplayName: string }[]> {
const envInfo: { envId: string, envDisplayName: string }[] = [];
try {
const bapAuthToken = await bapServiceAuthentication(telemetry, true);
const bapEndpoint = getBAPEndpoint(endpointStamp, telemetry);
const envListEndpoint = `${bapEndpoint}${BAP_ENVIRONMENT_LIST_URL}`;

const envListResponse = await fetch(envListEndpoint, {
method: "GET",
headers: {
"Authorization": `Bearer ${bapAuthToken}`
}
});

if (envListResponse.ok) {
const envListJson = await envListResponse.json();
envListJson.value.forEach((env: any) => {

Check warning on line 344 in src/common/utilities/Utils.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Unexpected any. Specify a different type

Check warning on line 344 in src/common/utilities/Utils.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Unexpected any. Specify a different type

Check warning on line 344 in src/common/utilities/Utils.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Unexpected any. Specify a different type
envInfo.push({
envId: env.properties.linkedEnvironmentMetadata.instanceUrl,
envDisplayName: env.properties.displayName
});
});
sendTelemetryEvent(telemetry, { eventName: VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS });
oneDSLoggerWrapper.getLogger().traceInfo(VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS);
} else {
sendTelemetryEvent(telemetry, {
eventName: VSCODE_EXTENSION_GET_ENV_LIST_FAILED,
errorMsg: envListResponse.statusText
});
oneDSLoggerWrapper.getLogger().traceError(VSCODE_EXTENSION_GET_ENV_LIST_FAILED, VSCODE_EXTENSION_GET_ENV_LIST_FAILED, new Error(envListResponse.statusText));
}
} catch (error) {
sendTelemetryEvent(telemetry, {
eventName: VSCODE_EXTENSION_GET_ENV_LIST_FAILED,
errorMsg: (error as Error).message
});
oneDSLoggerWrapper.getLogger().traceError(VSCODE_EXTENSION_GET_ENV_LIST_FAILED, VSCODE_EXTENSION_GET_ENV_LIST_FAILED, error as Error);
}
return envInfo;
}


export function getBAPEndpoint(serviceEndpointStamp: ServiceEndpointCategory, telemetry: ITelemetry): string {
let bapEndpoint = "";

switch (serviceEndpointStamp) {
case ServiceEndpointCategory.TEST:
bapEndpoint = "https://test.api.bap.microsoft.com";
break;
case ServiceEndpointCategory.PREPROD:
bapEndpoint = "https://preprod.api.bap.microsoft.com";
break;
case ServiceEndpointCategory.PROD:
bapEndpoint = "https://api.bap.microsoft.com";
break;
// All below endpoints are not supported yet
case ServiceEndpointCategory.DOD:
case ServiceEndpointCategory.GCC:
case ServiceEndpointCategory.HIGH:
case ServiceEndpointCategory.MOONCAKE:
default:
sendTelemetryEvent(telemetry, { eventName: VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION, data: serviceEndpointStamp });
oneDSLoggerWrapper.getLogger().traceInfo(VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION, { data: serviceEndpointStamp });
break;
}

return BAP_SERVICE_ENDPOINT.replace('{rootURL}', bapEndpoint)
}

0 comments on commit 9c1bbcd

Please sign in to comment.