mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-23 02:41:39 +00:00
Prepare for Fabric native (#2050)
* Implement fabric native path * Fix default values to work with current fabric clients * Fix Fabric native mode * Fix unit test * export Fabric context * Dynamically close Home tab for Mirrored databases in Fabric rather than conditional init (which doesn't work for Native) * For Fabric native, don't show "Delete Database" in context menu and reading databases should return the database from the context. * Update to V3 messaging * For data plane operations, skip ARM for Fabric native. Refine the tests for fabric to make the distinction between mirrored key, mirrored AAD and native. Fix FabricUtil to strict compile. * Add support for refreshing access tokens * Buf fix: don't wait for refresh is async * Fix format * Fix strict compile issue --------- Co-authored-by: Laurent Nguyen <languye@microsoft.com>
This commit is contained in:
@@ -2,11 +2,19 @@ import * as Constants from "Common/Constants";
|
||||
import { createUri } from "Common/UrlUtility";
|
||||
import { DATA_EXPLORER_RPC_VERSION } from "Contracts/DataExplorerMessagesContract";
|
||||
import { FabricMessageTypes } from "Contracts/FabricMessageTypes";
|
||||
import { FABRIC_RPC_VERSION, FabricMessageV2 } from "Contracts/FabricMessagesContract";
|
||||
import {
|
||||
ArtifactConnectionInfo,
|
||||
CosmosDbArtifactType,
|
||||
FABRIC_RPC_VERSION,
|
||||
FabricMessageV2,
|
||||
FabricMessageV3,
|
||||
InitializeMessageV3,
|
||||
} from "Contracts/FabricMessagesContract";
|
||||
import { useDialog } from "Explorer/Controls/Dialog";
|
||||
import Explorer from "Explorer/Explorer";
|
||||
import { useDataPlaneRbac } from "Explorer/Panes/SettingsPane/SettingsPane";
|
||||
import { useSelectedNode } from "Explorer/useSelectedNode";
|
||||
import { scheduleRefreshDatabaseResourceToken } from "Platform/Fabric/FabricUtil";
|
||||
import { isFabricMirroredKey, scheduleRefreshFabricToken } from "Platform/Fabric/FabricUtil";
|
||||
import {
|
||||
AppStateComponentNames,
|
||||
OPEN_TABS_SUBCOMPONENT_NAME,
|
||||
@@ -23,7 +31,7 @@ import { AccountKind, Flights } from "../Common/Constants";
|
||||
import { normalizeArmEndpoint } from "../Common/EnvironmentUtility";
|
||||
import * as Logger from "../Common/Logger";
|
||||
import { handleCachedDataMessage, sendMessage, sendReadyMessage } from "../Common/MessageHandler";
|
||||
import { Platform, configContext, updateConfigContext } from "../ConfigContext";
|
||||
import { configContext, Platform, updateConfigContext } from "../ConfigContext";
|
||||
import { ActionType, DataExplorerAction, TabKind } from "../Contracts/ActionContracts";
|
||||
import { MessageTypes } from "../Contracts/ExplorerContracts";
|
||||
import { DataExplorerInputsFrame } from "../Contracts/ViewModels";
|
||||
@@ -44,7 +52,7 @@ import {
|
||||
} from "../Platform/Hosted/HostedUtils";
|
||||
import { extractFeatures } from "../Platform/Hosted/extractFeatures";
|
||||
import { DefaultExperienceUtility } from "../Shared/DefaultExperienceUtility";
|
||||
import { Node, PortalEnv, updateUserContext, userContext } from "../UserContext";
|
||||
import { FabricArtifactInfo, Node, PortalEnv, updateUserContext, userContext } from "../UserContext";
|
||||
import {
|
||||
acquireMsalTokenForAccount,
|
||||
acquireTokenWithMsal,
|
||||
@@ -104,7 +112,7 @@ export function useKnockoutExplorer(platform: Platform): Explorer {
|
||||
|
||||
async function configureFabric(): Promise<Explorer> {
|
||||
// These are the versions of Fabric that Data Explorer supports.
|
||||
const SUPPORTED_FABRIC_VERSIONS = [FABRIC_RPC_VERSION];
|
||||
const SUPPORTED_FABRIC_VERSIONS = ["2", FABRIC_RPC_VERSION];
|
||||
|
||||
let firstContainerOpened = false;
|
||||
let explorer: Explorer;
|
||||
@@ -120,7 +128,7 @@ async function configureFabric(): Promise<Explorer> {
|
||||
return;
|
||||
}
|
||||
|
||||
const data: FabricMessageV2 = event.data?.data;
|
||||
const data: FabricMessageV2 | FabricMessageV3 = event.data?.data;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
@@ -129,38 +137,77 @@ async function configureFabric(): Promise<Explorer> {
|
||||
case "initialize": {
|
||||
const fabricVersion = data.version;
|
||||
if (!SUPPORTED_FABRIC_VERSIONS.includes(fabricVersion)) {
|
||||
// TODO Surface error to user
|
||||
// TODO Surface error to user and log to telemetry
|
||||
useDialog
|
||||
.getState()
|
||||
.showOkModalDialog("Unsupported Fabric version", `Unsupported Fabric version: ${fabricVersion}`);
|
||||
Logger.logError(`Unsupported Fabric version: ${fabricVersion}`, "Explorer/configureFabric");
|
||||
console.error(`Unsupported Fabric version: ${fabricVersion}`);
|
||||
return;
|
||||
}
|
||||
|
||||
explorer = createExplorerFabric(data.message);
|
||||
await scheduleRefreshDatabaseResourceToken(true);
|
||||
resolve(explorer);
|
||||
await explorer.refreshAllDatabases();
|
||||
if (userContext.fabricContext.isVisible) {
|
||||
firstContainerOpened = true;
|
||||
openFirstContainer(explorer, userContext.fabricContext.databaseConnectionInfo.databaseId);
|
||||
if (fabricVersion === "2") {
|
||||
// ----------------- TODO: Remove this when FabricMessageV2 is deprecated -----------------
|
||||
const initializationMessage = data.message as {
|
||||
connectionId: string;
|
||||
isVisible: boolean;
|
||||
};
|
||||
|
||||
explorer = createExplorerFabricLegacy(initializationMessage, data.version);
|
||||
await scheduleRefreshFabricToken(true);
|
||||
resolve(explorer);
|
||||
await explorer.refreshAllDatabases();
|
||||
if (userContext.fabricContext.isVisible) {
|
||||
firstContainerOpened = true;
|
||||
openFirstContainer(explorer, userContext.fabricContext.databaseName);
|
||||
}
|
||||
// -----------------------------------------------------------------------------------------
|
||||
} else if (fabricVersion === FABRIC_RPC_VERSION) {
|
||||
const initializationMessage = data.message as InitializeMessageV3<CosmosDbArtifactType>;
|
||||
explorer = createExplorerFabric(initializationMessage, data.version);
|
||||
|
||||
if (initializationMessage.artifactType === CosmosDbArtifactType.MIRRORED_KEY) {
|
||||
// Do not show Home tab for Mirrored
|
||||
useTabs.getState().closeReactTab(ReactTabKind.Home);
|
||||
}
|
||||
|
||||
// All tokens used in fabric expire, so schedule a refresh
|
||||
// For Mirrored key, we need the token right away to get the database and containers list.
|
||||
if (isFabricMirroredKey()) {
|
||||
await scheduleRefreshFabricToken(true);
|
||||
} else {
|
||||
scheduleRefreshFabricToken(false);
|
||||
}
|
||||
|
||||
resolve(explorer);
|
||||
await explorer.refreshAllDatabases();
|
||||
|
||||
const { databaseName } = userContext.fabricContext;
|
||||
if (userContext.fabricContext.isVisible && databaseName) {
|
||||
firstContainerOpened = true;
|
||||
openFirstContainer(explorer, databaseName);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "newContainer":
|
||||
explorer.onNewCollectionClicked();
|
||||
break;
|
||||
case "authorizationToken":
|
||||
case "allResourceTokens_v2": {
|
||||
case "allResourceTokens_v2":
|
||||
case "accessToken": {
|
||||
handleCachedDataMessage(data);
|
||||
break;
|
||||
}
|
||||
case "explorerVisible": {
|
||||
userContext.fabricContext.isVisible = data.message.visible;
|
||||
if (
|
||||
userContext.fabricContext.isVisible &&
|
||||
!firstContainerOpened &&
|
||||
userContext?.fabricContext?.databaseConnectionInfo?.databaseId !== undefined
|
||||
) {
|
||||
firstContainerOpened = true;
|
||||
openFirstContainer(explorer, userContext.fabricContext.databaseConnectionInfo.databaseId);
|
||||
if (userContext.fabricContext.isVisible && !firstContainerOpened) {
|
||||
const { databaseName } = userContext.fabricContext;
|
||||
if (databaseName !== undefined) {
|
||||
firstContainerOpened = true;
|
||||
openFirstContainer(explorer, databaseName);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -420,13 +467,29 @@ function configureHostedWithResourceToken(config: ResourceToken): Explorer {
|
||||
return explorer;
|
||||
}
|
||||
|
||||
function createExplorerFabric(params: { connectionId: string; isVisible: boolean }): Explorer {
|
||||
/**
|
||||
* Initialization for FabricMessageV2
|
||||
* TODO: delete when FabricMessageV2 is deprecated
|
||||
* @param params
|
||||
* @returns
|
||||
*/
|
||||
function createExplorerFabricLegacy(
|
||||
params: { connectionId: string; isVisible: boolean },
|
||||
fabricClientRpcVersion: string,
|
||||
): Explorer {
|
||||
const artifactInfo: FabricArtifactInfo[CosmosDbArtifactType.MIRRORED_KEY] = {
|
||||
connectionId: params.connectionId,
|
||||
resourceTokenInfo: undefined,
|
||||
};
|
||||
|
||||
updateUserContext({
|
||||
fabricContext: {
|
||||
connectionId: params.connectionId,
|
||||
databaseConnectionInfo: undefined,
|
||||
fabricClientRpcVersion,
|
||||
isReadOnly: true,
|
||||
isVisible: params.isVisible ?? true,
|
||||
databaseName: undefined,
|
||||
artifactType: CosmosDbArtifactType.MIRRORED_KEY,
|
||||
artifactInfo,
|
||||
},
|
||||
authType: AuthType.ConnectionString,
|
||||
databaseAccount: {
|
||||
@@ -440,11 +503,102 @@ function createExplorerFabric(params: { connectionId: string; isVisible: boolean
|
||||
},
|
||||
},
|
||||
});
|
||||
useTabs.getState().closeAllTabs();
|
||||
const explorer = new Explorer();
|
||||
return explorer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialization for FabricMessageV3 and above
|
||||
* @param params
|
||||
* @returns
|
||||
*/
|
||||
const createExplorerFabric = (
|
||||
params: InitializeMessageV3<CosmosDbArtifactType>,
|
||||
fabricClientRpcVersion: string,
|
||||
): Explorer => {
|
||||
updateUserContext({
|
||||
fabricContext: {
|
||||
fabricClientRpcVersion,
|
||||
databaseName: undefined,
|
||||
isVisible: params.isVisible,
|
||||
isReadOnly: params.isReadOnly,
|
||||
artifactType: params.artifactType,
|
||||
artifactInfo: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
if (params.artifactType === CosmosDbArtifactType.MIRRORED_KEY) {
|
||||
updateUserContext({
|
||||
authType: AuthType.ConnectionString, // TODO: will need its own type
|
||||
databaseAccount: {
|
||||
id: "",
|
||||
location: "",
|
||||
type: "",
|
||||
name: "Mounted", // TODO: not used?
|
||||
kind: AccountKind.Default,
|
||||
properties: {
|
||||
documentEndpoint: undefined,
|
||||
},
|
||||
},
|
||||
fabricContext: {
|
||||
...userContext.fabricContext,
|
||||
artifactInfo: {
|
||||
connectionId: (params.artifactConnectionInfo as ArtifactConnectionInfo[CosmosDbArtifactType.MIRRORED_KEY])
|
||||
.connectionId,
|
||||
resourceTokenInfo: undefined,
|
||||
},
|
||||
},
|
||||
});
|
||||
} else if (params.artifactType === CosmosDbArtifactType.MIRRORED_AAD) {
|
||||
updateUserContext({
|
||||
databaseAccount: {
|
||||
id: "",
|
||||
location: "",
|
||||
type: "",
|
||||
name: "Mounted", // TODO: not used?
|
||||
kind: AccountKind.Default,
|
||||
properties: {
|
||||
documentEndpoint: undefined,
|
||||
},
|
||||
},
|
||||
authType: AuthType.AAD,
|
||||
dataPlaneRbacEnabled: true,
|
||||
aadToken: undefined,
|
||||
masterKey: undefined,
|
||||
fabricContext: {
|
||||
...userContext.fabricContext,
|
||||
artifactInfo: undefined,
|
||||
},
|
||||
});
|
||||
} else if (params.artifactType === CosmosDbArtifactType.NATIVE) {
|
||||
const nativeParams = params as InitializeMessageV3<CosmosDbArtifactType.NATIVE>;
|
||||
// Make it behave like Hosted/AAD/RBAC
|
||||
updateUserContext({
|
||||
databaseAccount: {
|
||||
id: "",
|
||||
location: "",
|
||||
type: "",
|
||||
name: "Native", // TODO: not used?
|
||||
kind: AccountKind.Default,
|
||||
properties: {
|
||||
documentEndpoint: nativeParams.artifactConnectionInfo.accountEndpoint,
|
||||
},
|
||||
},
|
||||
authType: AuthType.AAD,
|
||||
dataPlaneRbacEnabled: true,
|
||||
aadToken: nativeParams.artifactConnectionInfo.accessToken,
|
||||
masterKey: undefined,
|
||||
fabricContext: {
|
||||
...userContext.fabricContext,
|
||||
databaseName: nativeParams.artifactConnectionInfo.databaseName,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const explorer = new Explorer();
|
||||
return explorer;
|
||||
};
|
||||
|
||||
function configureWithEncryptedToken(config: EncryptedToken): Explorer {
|
||||
const apiExperience = DefaultExperienceUtility.getDefaultExperienceFromApiKind(config.encryptedTokenMetadata.apiKind);
|
||||
updateUserContext({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { clamp } from "@fluentui/react";
|
||||
import { OpenTab } from "Contracts/ActionContracts";
|
||||
import { useSelectedNode } from "Explorer/useSelectedNode";
|
||||
import { isFabricMirrored } from "Platform/Fabric/FabricUtil";
|
||||
import {
|
||||
AppStateComponentNames,
|
||||
OPEN_TABS_SUBCOMPONENT_NAME,
|
||||
@@ -11,7 +12,6 @@ import * as ViewModels from "../Contracts/ViewModels";
|
||||
import { CollectionTabKind } from "../Contracts/ViewModels";
|
||||
import NotebookTabV2 from "../Explorer/Tabs/NotebookV2Tab";
|
||||
import TabsBase from "../Explorer/Tabs/TabsBase";
|
||||
import { Platform, configContext } from "./../ConfigContext";
|
||||
|
||||
export interface TabsState {
|
||||
openedTabs: TabsBase[];
|
||||
@@ -51,22 +51,11 @@ export enum ReactTabKind {
|
||||
QueryCopilot,
|
||||
}
|
||||
|
||||
// HACK: using this const when the configuration context is not initialized yet.
|
||||
// Since Fabric is always setting the url param, use that instead of the regular config.
|
||||
const isPlatformFabric = (() => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
if (params.has("platform")) {
|
||||
const platform = params.get("platform");
|
||||
return platform === Platform.Fabric;
|
||||
}
|
||||
return false;
|
||||
})();
|
||||
|
||||
export const useTabs: UseStore<TabsState> = create((set, get) => ({
|
||||
openedTabs: [],
|
||||
openedReactTabs: !isPlatformFabric ? [ReactTabKind.Home] : [],
|
||||
activeTab: undefined,
|
||||
activeReactTab: !isPlatformFabric ? ReactTabKind.Home : undefined,
|
||||
openedTabs: [] as TabsBase[],
|
||||
openedReactTabs: [ReactTabKind.Home],
|
||||
activeTab: undefined as TabsBase,
|
||||
activeReactTab: ReactTabKind.Home,
|
||||
queryCopilotTabInitialInput: "",
|
||||
isTabExecuting: false,
|
||||
isQueryErrorThrown: false,
|
||||
@@ -122,7 +111,7 @@ export const useTabs: UseStore<TabsState> = create((set, get) => ({
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (updatedTabs.length === 0 && configContext.platform !== Platform.Fabric) {
|
||||
if (updatedTabs.length === 0 && !isFabricMirrored()) {
|
||||
set({ activeTab: undefined, activeReactTab: undefined });
|
||||
}
|
||||
|
||||
@@ -162,7 +151,7 @@ export const useTabs: UseStore<TabsState> = create((set, get) => ({
|
||||
}
|
||||
});
|
||||
|
||||
if (get().openedTabs.length === 0 && configContext.platform !== Platform.Fabric) {
|
||||
if (get().openedTabs.length === 0 && !isFabricMirrored()) {
|
||||
set({ activeTab: undefined, activeReactTab: undefined });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user