Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment list retrieval and refactor BAP endpoint logic #1060

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`;
amitjoshi438 marked this conversation as resolved.
Show resolved Hide resolved
amitjoshi438 marked this conversation as resolved.
Show resolved Hide resolved

// 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";
74 changes: 74 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,74 @@ export function getECSOrgLocationValue(clusterName: string, clusterNumber: strin

return extractedSubstring;
}

//API call to get env list for a org
amitjoshi438 marked this conversation as resolved.
Show resolved Hide resolved
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();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
envListJson.value.forEach((env: any) => {
envInfo.push({
envId: env.properties.linkedEnvironmentMetadata.instanceUrl,
priyanshu92 marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Loading