From fdd12b41c49364fd9f064379af0c2ad33d16f360 Mon Sep 17 00:00:00 2001 From: Hardikkumar Nai <80053762+hardiknai-techm@users.noreply.github.com> Date: Wed, 5 May 2021 23:30:01 +0530 Subject: [PATCH] Remove Explorer.defaultExperience (#680) Co-authored-by: Steve Faulkner --- src/Common/Constants.ts | 22 +--- src/Common/CosmosClient.test.ts | 3 +- src/Contracts/DataModels.ts | 1 - .../NotebookTerminalComponent.test.tsx | 4 - .../ScaleComponent.test.tsx | 13 +- .../SettingsComponent.test.tsx.snap | 4 - .../ContainerSampleGenerator.test.ts | 1 - src/Explorer/Explorer.tsx | 16 --- src/Explorer/Panes/AddCollectionPane.test.ts | 11 +- src/Explorer/Panes/AddDatabasePane.test.ts | 2 - .../GitHubReposPanel.test.tsx.snap | 1 - .../StringInputPane.test.tsx.snap | 1 - ...eteDatabaseConfirmationPanel.test.tsx.snap | 1 - src/Explorer/Tabs/DocumentsTab.test.ts | 10 +- src/Explorer/Tabs/NotebookTabBase.ts | 3 +- src/Explorer/Tabs/TabsManager.test.ts | 1 - src/Explorer/Tree/Database.test.ts | 9 +- src/GitHub/GitHubOAuthService.test.ts | 1 - src/Juno/JunoClient.test.ts | 1 - src/Platform/Emulator/emulatorAccount.tsx | 5 +- src/Platform/Hosted/Authorization.ts | 4 +- src/Platform/Hosted/HostedUtils.ts | 21 ++-- src/Shared/DefaultExperienceUtility.test.ts | 94 +++----------- src/Shared/DefaultExperienceUtility.ts | 115 +++--------------- src/UserContext.ts | 2 +- src/hooks/useKnockoutExplorer.ts | 8 +- 26 files changed, 77 insertions(+), 277 deletions(-) diff --git a/src/Common/Constants.ts b/src/Common/Constants.ts index e6b545793..097c4237f 100644 --- a/src/Common/Constants.ts +++ b/src/Common/Constants.ts @@ -65,28 +65,18 @@ export class ClientDefaults { public static readonly arcadiaTokenRefreshIntervalPaddingMs: number = 2000; } -export class AccountKind { - public static DocumentDB: string = "DocumentDB"; - public static MongoDB: string = "MongoDB"; - public static Parse: string = "Parse"; - public static GlobalDocumentDB: string = "GlobalDocumentDB"; - public static Default: string = AccountKind.DocumentDB; +export enum AccountKind { + DocumentDB = "DocumentDB", + MongoDB = "MongoDB", + Parse = "Parse", + GlobalDocumentDB = "GlobalDocumentDB", + Default = "DocumentDB", } export class CorrelationBackend { public static Url: string = "https://aka.ms/cosmosdbanalytics"; } -export class DefaultAccountExperience { - public static DocumentDB: string = "DocumentDB"; - public static Graph: string = "Graph"; - public static MongoDB: string = "MongoDB"; - public static ApiForMongoDB: string = "Azure Cosmos DB for MongoDB API"; - public static Table: string = "Table"; - public static Cassandra: string = "Cassandra"; - public static Default: string = DefaultAccountExperience.DocumentDB; -} - export class CapabilityNames { public static EnableTable: string = "EnableTable"; public static EnableGremlin: string = "EnableGremlin"; diff --git a/src/Common/CosmosClient.test.ts b/src/Common/CosmosClient.test.ts index ea91efa53..96505f208 100644 --- a/src/Common/CosmosClient.test.ts +++ b/src/Common/CosmosClient.test.ts @@ -1,5 +1,5 @@ import { ResourceType } from "@azure/cosmos/dist-esm/common/constants"; -import { configContext, Platform, updateConfigContext, resetConfigContext } from "../ConfigContext"; +import { Platform, resetConfigContext, updateConfigContext } from "../ConfigContext"; import { updateUserContext } from "../UserContext"; import { endpoint, getTokenFromAuthService, requestPlugin, tokenProvider } from "./CosmosClient"; @@ -91,7 +91,6 @@ describe("endpoint", () => { location: "foo", type: "foo", kind: "foo", - tags: [], properties: { documentEndpoint: "bar", gremlinEndpoint: "foo", diff --git a/src/Contracts/DataModels.ts b/src/Contracts/DataModels.ts index 4e3085b45..aece6a04a 100644 --- a/src/Contracts/DataModels.ts +++ b/src/Contracts/DataModels.ts @@ -4,7 +4,6 @@ export interface DatabaseAccount { location: string; type: string; kind: string; - tags: any; properties: DatabaseAccountExtendedProperties; } diff --git a/src/Explorer/Controls/Notebook/NotebookTerminalComponent.test.tsx b/src/Explorer/Controls/Notebook/NotebookTerminalComponent.test.tsx index cca31f60a..627041b38 100644 --- a/src/Explorer/Controls/Notebook/NotebookTerminalComponent.test.tsx +++ b/src/Explorer/Controls/Notebook/NotebookTerminalComponent.test.tsx @@ -13,7 +13,6 @@ const createTestDatabaseAccount = (): DataModels.DatabaseAccount => { gremlinEndpoint: null, tableEndpoint: null, }, - tags: "testTags", type: "testType", }; }; @@ -30,7 +29,6 @@ const createTestMongo32DatabaseAccount = (): DataModels.DatabaseAccount => { gremlinEndpoint: null, tableEndpoint: null, }, - tags: "testTags", type: "testType", }; }; @@ -48,7 +46,6 @@ const createTestMongo36DatabaseAccount = (): DataModels.DatabaseAccount => { tableEndpoint: null, mongoEndpoint: "https://testMongoEndpoint.azure.com/", }, - tags: "testTags", type: "testType", }; }; @@ -65,7 +62,6 @@ const createTestCassandraDatabaseAccount = (): DataModels.DatabaseAccount => { gremlinEndpoint: null, tableEndpoint: null, }, - tags: "testTags", type: "testType", }; }; diff --git a/src/Explorer/Controls/Settings/SettingsSubComponents/ScaleComponent.test.tsx b/src/Explorer/Controls/Settings/SettingsSubComponents/ScaleComponent.test.tsx index de31b2d5f..cead8bdae 100644 --- a/src/Explorer/Controls/Settings/SettingsSubComponents/ScaleComponent.test.tsx +++ b/src/Explorer/Controls/Settings/SettingsSubComponents/ScaleComponent.test.tsx @@ -1,14 +1,14 @@ import { shallow } from "enzyme"; +import ko from "knockout"; import React from "react"; -import { ScaleComponent, ScaleComponentProps } from "./ScaleComponent"; -import { container, collection } from "../TestUtils"; -import { ThroughputInputAutoPilotV3Component } from "./ThroughputInputComponents/ThroughputInputAutoPilotV3Component"; -import Explorer from "../../../Explorer"; import * as Constants from "../../../../Common/Constants"; import * as DataModels from "../../../../Contracts/DataModels"; -import { throughputUnit } from "../SettingsRenderUtils"; import * as SharedConstants from "../../../../Shared/Constants"; -import ko from "knockout"; +import Explorer from "../../../Explorer"; +import { throughputUnit } from "../SettingsRenderUtils"; +import { collection, container } from "../TestUtils"; +import { ScaleComponent, ScaleComponentProps } from "./ScaleComponent"; +import { ThroughputInputAutoPilotV3Component } from "./ThroughputInputComponents/ThroughputInputAutoPilotV3Component"; describe("ScaleComponent", () => { const nonNationalCloudContainer = new Explorer(); @@ -87,7 +87,6 @@ describe("ScaleComponent", () => { location: undefined, type: undefined, kind: "documentdb", - tags: undefined, properties: { documentEndpoint: undefined, tableEndpoint: undefined, diff --git a/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap b/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap index f116d83f7..d7279c245 100644 --- a/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap +++ b/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap @@ -424,7 +424,6 @@ exports[`SettingsComponent renders 1`] = ` }, "databaseAccount": [Function], "databases": [Function], - "defaultExperience": [Function], "deleteCollectionText": [Function], "deleteDatabaseText": [Function], "gitHubClient": GitHubClient { @@ -1710,7 +1709,6 @@ exports[`SettingsComponent renders 1`] = ` }, "databaseAccount": [Function], "databases": [Function], - "defaultExperience": [Function], "deleteCollectionText": [Function], "deleteDatabaseText": [Function], "gitHubClient": GitHubClient { @@ -3009,7 +3007,6 @@ exports[`SettingsComponent renders 1`] = ` }, "databaseAccount": [Function], "databases": [Function], - "defaultExperience": [Function], "deleteCollectionText": [Function], "deleteDatabaseText": [Function], "gitHubClient": GitHubClient { @@ -4295,7 +4292,6 @@ exports[`SettingsComponent renders 1`] = ` }, "databaseAccount": [Function], "databases": [Function], - "defaultExperience": [Function], "deleteCollectionText": [Function], "deleteDatabaseText": [Function], "gitHubClient": GitHubClient { diff --git a/src/Explorer/DataSamples/ContainerSampleGenerator.test.ts b/src/Explorer/DataSamples/ContainerSampleGenerator.test.ts index fdab3a30d..75d059dc3 100644 --- a/src/Explorer/DataSamples/ContainerSampleGenerator.test.ts +++ b/src/Explorer/DataSamples/ContainerSampleGenerator.test.ts @@ -79,7 +79,6 @@ describe("ContainerSampleGenerator", () => { location: "foo", type: "foo", kind: "foo", - tags: [], properties: { documentEndpoint: "bar", gremlinEndpoint: "foo", diff --git a/src/Explorer/Explorer.tsx b/src/Explorer/Explorer.tsx index 5b368781f..2b6292e44 100644 --- a/src/Explorer/Explorer.tsx +++ b/src/Explorer/Explorer.tsx @@ -115,11 +115,6 @@ export default class Explorer { * */ public databaseAccount: ko.Observable; public collectionCreationDefaults: ViewModels.CollectionCreationDefaults = SharedConstants.CollectionCreationDefaults; - /** - * @deprecated - * Use userContext.apiType instead - * */ - public defaultExperience: ko.Observable; public isFixedCollectionWithSharedThroughputSupported: ko.Computed; /** * @deprecated @@ -376,17 +371,6 @@ export default class Explorer { bounds: splitterBounds, direction: SplitterDirection.Vertical, }); - this.defaultExperience = ko.observable(); - // this.databaseAccount.subscribe((databaseAccount) => { - // const defaultExperience: string = DefaultExperienceUtility.getDefaultExperienceFromDatabaseAccount( - // databaseAccount - // ); - // this.defaultExperience(defaultExperience); - // // TODO. Remove this entirely - // updateUserContext({ - // apiType: DefaultExperienceUtility.mapDefaultExperienceStringToEnum(defaultExperience), - // }); - // }); this.isFixedCollectionWithSharedThroughputSupported = ko.computed(() => { if (userContext.features.enableFixedCollectionWithSharedThroughput) { diff --git a/src/Explorer/Panes/AddCollectionPane.test.ts b/src/Explorer/Panes/AddCollectionPane.test.ts index 887f8c5bb..2465074ee 100644 --- a/src/Explorer/Panes/AddCollectionPane.test.ts +++ b/src/Explorer/Panes/AddCollectionPane.test.ts @@ -20,7 +20,6 @@ describe("Add Collection Pane", () => { enableFreeTier: false, }, type: undefined, - tags: [], }; const mockFreeTierDatabaseAccount: DatabaseAccount = { @@ -36,7 +35,6 @@ describe("Add Collection Pane", () => { enableFreeTier: true, }, type: undefined, - tags: [], }; beforeEach(() => { @@ -44,7 +42,13 @@ describe("Add Collection Pane", () => { }); it("should be true if graph API and partition key is not /id nor /label", () => { - explorer.defaultExperience(Constants.DefaultAccountExperience.Graph.toLowerCase()); + updateUserContext({ + databaseAccount: { + properties: { + capabilities: [{ name: "EnableGremlin" }], + }, + } as DatabaseAccount, + }); const addCollectionPane = explorer.addCollectionPane as AddCollectionPane; addCollectionPane.partitionKey("/blah"); expect(addCollectionPane.isValid()).toBe(true); @@ -58,7 +62,6 @@ describe("Add Collection Pane", () => { }, } as DatabaseAccount, }); - const addCollectionPane = explorer.addCollectionPane as AddCollectionPane; addCollectionPane.partitionKey("/id"); expect(addCollectionPane.isValid()).toBe(false); diff --git a/src/Explorer/Panes/AddDatabasePane.test.ts b/src/Explorer/Panes/AddDatabasePane.test.ts index 711a9cfc1..5e8072884 100644 --- a/src/Explorer/Panes/AddDatabasePane.test.ts +++ b/src/Explorer/Panes/AddDatabasePane.test.ts @@ -21,7 +21,6 @@ describe("Add Database Pane", () => { enableFreeTier: false, }, type: undefined, - tags: [], }; const mockFreeTierDatabaseAccount: DatabaseAccount = { @@ -37,7 +36,6 @@ describe("Add Database Pane", () => { enableFreeTier: true, }, type: undefined, - tags: [], }; beforeEach(() => { diff --git a/src/Explorer/Panes/GitHubReposPanel/__snapshots__/GitHubReposPanel.test.tsx.snap b/src/Explorer/Panes/GitHubReposPanel/__snapshots__/GitHubReposPanel.test.tsx.snap index 6b308f110..71b70e0ed 100644 --- a/src/Explorer/Panes/GitHubReposPanel/__snapshots__/GitHubReposPanel.test.tsx.snap +++ b/src/Explorer/Panes/GitHubReposPanel/__snapshots__/GitHubReposPanel.test.tsx.snap @@ -413,7 +413,6 @@ exports[`GitHub Repos Panel should render Default properly 1`] = ` }, "databaseAccount": [Function], "databases": [Function], - "defaultExperience": [Function], "deleteCollectionText": [Function], "deleteDatabaseText": [Function], "gitHubClient": GitHubClient { diff --git a/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap b/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap index 0e664fad9..30051a5d8 100644 --- a/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap +++ b/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap @@ -403,7 +403,6 @@ exports[`StringInput Pane should render Create new directory properly 1`] = ` }, "databaseAccount": [Function], "databases": [Function], - "defaultExperience": [Function], "deleteCollectionText": [Function], "deleteDatabaseText": [Function], "gitHubClient": GitHubClient { diff --git a/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap b/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap index 46710be04..dd8b81d1a 100644 --- a/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap +++ b/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap @@ -401,7 +401,6 @@ exports[`Delete Database Confirmation Pane submit() Should call delete database }, "databaseAccount": [Function], "databases": [Function], - "defaultExperience": [Function], "deleteCollectionText": [Function], "deleteDatabaseText": [Function], "gitHubClient": GitHubClient { diff --git a/src/Explorer/Tabs/DocumentsTab.test.ts b/src/Explorer/Tabs/DocumentsTab.test.ts index 8ebb6a8ad..6321a4afb 100644 --- a/src/Explorer/Tabs/DocumentsTab.test.ts +++ b/src/Explorer/Tabs/DocumentsTab.test.ts @@ -1,5 +1,5 @@ import * as ko from "knockout"; -import * as Constants from "../../Common/Constants"; +import { DatabaseAccount } from "../../Contracts/DataModels"; import * as ViewModels from "../../Contracts/ViewModels"; import { updateUserContext } from "../../UserContext"; import { CommandButtonComponentProps } from "../Controls/CommandButton/CommandButtonComponent"; @@ -27,7 +27,13 @@ describe("Documents tab", () => { describe("showPartitionKey", () => { const explorer = new Explorer(); const mongoExplorer = new Explorer(); - mongoExplorer.defaultExperience(Constants.DefaultAccountExperience.MongoDB); + updateUserContext({ + databaseAccount: { + properties: { + capabilities: [{ name: "EnableGremlin" }], + }, + } as DatabaseAccount, + }); const collectionWithoutPartitionKey = ({ id: ko.observable("foo"), diff --git a/src/Explorer/Tabs/NotebookTabBase.ts b/src/Explorer/Tabs/NotebookTabBase.ts index e74f12b86..096328fda 100644 --- a/src/Explorer/Tabs/NotebookTabBase.ts +++ b/src/Explorer/Tabs/NotebookTabBase.ts @@ -3,6 +3,7 @@ import * as DataModels from "../../Contracts/DataModels"; import * as ViewModels from "../../Contracts/ViewModels"; import { ActionModifiers } from "../../Shared/Telemetry/TelemetryConstants"; import * as TelemetryProcessor from "../../Shared/Telemetry/TelemetryProcessor"; +import { userContext } from "../../UserContext"; import Explorer from "../Explorer"; import { NotebookClientV2 } from "../Notebook/NotebookClientV2"; import TabsBase from "./TabsBase"; @@ -29,7 +30,7 @@ export default class NotebookTabBase extends TabsBase { NotebookTabBase.clientManager = new NotebookClientV2({ connectionInfo: this.container.notebookServerInfo(), databaseAccountName: this.container.databaseAccount().name, - defaultExperience: this.container.defaultExperience(), + defaultExperience: userContext.apiType, contentProvider: this.container.notebookManager?.notebookContentProvider, }); } diff --git a/src/Explorer/Tabs/TabsManager.test.ts b/src/Explorer/Tabs/TabsManager.test.ts index ac0adb5ce..19c192456 100644 --- a/src/Explorer/Tabs/TabsManager.test.ts +++ b/src/Explorer/Tabs/TabsManager.test.ts @@ -23,7 +23,6 @@ describe("Tabs manager tests", () => { location: "", type: "", kind: "", - tags: "", properties: undefined, }); diff --git a/src/Explorer/Tree/Database.test.ts b/src/Explorer/Tree/Database.test.ts index c37e0be94..d364adca5 100644 --- a/src/Explorer/Tree/Database.test.ts +++ b/src/Explorer/Tree/Database.test.ts @@ -1,10 +1,10 @@ -import * as DataModels from "../../Contracts/DataModels"; import * as ko from "knockout"; -import Database from "./Database"; -import Explorer from "../Explorer"; import { HttpStatusCodes } from "../../Common/Constants"; +import * as DataModels from "../../Contracts/DataModels"; import { JunoClient } from "../../Juno/JunoClient"; -import { userContext, updateUserContext } from "../../UserContext"; +import { updateUserContext, userContext } from "../../UserContext"; +import Explorer from "../Explorer"; +import Database from "./Database"; const createMockContainer = (): Explorer => { const mockContainer = new Explorer(); @@ -19,7 +19,6 @@ updateUserContext({ name: "fakeName", location: "fakeLocation", type: "fakeType", - tags: undefined, kind: "fakeKind", properties: { documentEndpoint: "fakeEndpoint", diff --git a/src/GitHub/GitHubOAuthService.test.ts b/src/GitHub/GitHubOAuthService.test.ts index 43f510c16..1e0169153 100644 --- a/src/GitHub/GitHubOAuthService.test.ts +++ b/src/GitHub/GitHubOAuthService.test.ts @@ -14,7 +14,6 @@ const sampleDatabaseAccount: DataModels.DatabaseAccount = { location: "location", type: "type", kind: "kind", - tags: [], properties: { documentEndpoint: "documentEndpoint", gremlinEndpoint: "gremlinEndpoint", diff --git a/src/Juno/JunoClient.test.ts b/src/Juno/JunoClient.test.ts index 73e2a3cf8..2e4e571c4 100644 --- a/src/Juno/JunoClient.test.ts +++ b/src/Juno/JunoClient.test.ts @@ -13,7 +13,6 @@ const sampleDatabaseAccount: DatabaseAccount = { location: "location", type: "type", kind: "kind", - tags: [], properties: { documentEndpoint: "documentEndpoint", gremlinEndpoint: "gremlinEndpoint", diff --git a/src/Platform/Emulator/emulatorAccount.tsx b/src/Platform/Emulator/emulatorAccount.tsx index 5042a53a6..295f9ee6f 100644 --- a/src/Platform/Emulator/emulatorAccount.tsx +++ b/src/Platform/Emulator/emulatorAccount.tsx @@ -1,4 +1,4 @@ -import { AccountKind, DefaultAccountExperience, TagNames } from "../../Common/Constants"; +import { AccountKind } from "../../Common/Constants"; export const emulatorAccount = { name: "", @@ -6,9 +6,6 @@ export const emulatorAccount = { location: "", type: "", kind: AccountKind.DocumentDB, - tags: { - [TagNames.defaultExperience]: DefaultAccountExperience.DocumentDB, - }, properties: { documentEndpoint: "", tableEndpoint: "", diff --git a/src/Platform/Hosted/Authorization.ts b/src/Platform/Hosted/Authorization.ts index 64cd00a72..da9bfb6e1 100644 --- a/src/Platform/Hosted/Authorization.ts +++ b/src/Platform/Hosted/Authorization.ts @@ -1,4 +1,3 @@ -import Q from "q"; import * as Constants from "../../Common/Constants"; import { configContext } from "../../ConfigContext"; import * as DataModels from "../../Contracts/DataModels"; @@ -21,8 +20,7 @@ export default class AuthHeadersUtil { const databaseAccount = userContext.databaseAccount; const subscriptionId: string = userContext.subscriptionId; const resourceGroup = userContext.resourceGroup; - const defaultExperience: string = DefaultExperienceUtility.getDefaultExperienceFromDatabaseAccount(databaseAccount); - const apiKind: DataModels.ApiKind = DefaultExperienceUtility.getApiKindFromDefaultExperience(defaultExperience); + const apiKind: DataModels.ApiKind = DefaultExperienceUtility.getApiKindFromDefaultExperience(userContext.apiType); const accountEndpoint = (databaseAccount && databaseAccount.properties.documentEndpoint) || ""; const sid = subscriptionId || ""; const rg = resourceGroup || ""; diff --git a/src/Platform/Hosted/HostedUtils.ts b/src/Platform/Hosted/HostedUtils.ts index 85ef302f3..8cc59daa4 100644 --- a/src/Platform/Hosted/HostedUtils.ts +++ b/src/Platform/Hosted/HostedUtils.ts @@ -1,27 +1,28 @@ -import { DefaultAccountExperience, CapabilityNames, AccountKind } from "../../Common/Constants"; +import { AccountKind, CapabilityNames } from "../../Common/Constants"; import { AccessInputMetadata, ApiKind } from "../../Contracts/DataModels"; import { DefaultExperienceUtility } from "../../Shared/DefaultExperienceUtility"; +import { userContext } from "../../UserContext"; export function getDatabaseAccountPropertiesFromMetadata(metadata: AccessInputMetadata): unknown { let properties = { documentEndpoint: metadata.documentEndpoint }; - const apiExperience: string = DefaultExperienceUtility.getDefaultExperienceFromApiKind(metadata.apiKind); + const apiExperience = DefaultExperienceUtility.getDefaultExperienceFromApiKind(metadata.apiKind); - if (apiExperience === DefaultAccountExperience.Cassandra) { + if (apiExperience === "Cassandra") { properties = Object.assign(properties, { cassandraEndpoint: metadata.apiEndpoint, capabilities: [{ name: CapabilityNames.EnableCassandra }], }); - } else if (apiExperience === DefaultAccountExperience.Table) { + } else if (apiExperience === "Tables") { properties = Object.assign(properties, { tableEndpoint: metadata.apiEndpoint, capabilities: [{ name: CapabilityNames.EnableTable }], }); - } else if (apiExperience === DefaultAccountExperience.Graph) { + } else if (apiExperience === "Gremlin") { properties = Object.assign(properties, { gremlinEndpoint: metadata.apiEndpoint, capabilities: [{ name: CapabilityNames.EnableGremlin }], }); - } else if (apiExperience === DefaultAccountExperience.MongoDB) { + } else if (apiExperience === "Mongo") { if (metadata.apiKind === ApiKind.MongoDBCompute) { properties = Object.assign(properties, { mongoEndpoint: metadata.mongoEndpoint, @@ -31,12 +32,8 @@ export function getDatabaseAccountPropertiesFromMetadata(metadata: AccessInputMe return properties; } -export function getDatabaseAccountKindFromExperience(apiExperience: string): string { - if (apiExperience === DefaultAccountExperience.MongoDB) { - return AccountKind.MongoDB; - } - - if (apiExperience === DefaultAccountExperience.ApiForMongoDB) { +export function getDatabaseAccountKindFromExperience(apiExperience: typeof userContext.apiType): AccountKind { + if (apiExperience === "Mongo") { return AccountKind.MongoDB; } diff --git a/src/Shared/DefaultExperienceUtility.test.ts b/src/Shared/DefaultExperienceUtility.test.ts index ece1d6e88..c72b46823 100644 --- a/src/Shared/DefaultExperienceUtility.test.ts +++ b/src/Shared/DefaultExperienceUtility.test.ts @@ -1,133 +1,67 @@ -import * as Constants from "../Common/Constants"; import * as DataModels from "../Contracts/DataModels"; -import * as ViewModels from "../Contracts/ViewModels"; +import { userContext } from "../UserContext"; import { DefaultExperienceUtility } from "./DefaultExperienceUtility"; describe("Default Experience Utility", () => { describe("getDefaultExperienceFromApiKind()", () => { - function runScenario(apiKind: number, expectedExperience: string): void { + function runScenario(apiKind: number, expectedExperience: typeof userContext.apiType): void { const resolvedExperience = DefaultExperienceUtility.getDefaultExperienceFromApiKind(apiKind); expect(resolvedExperience).toEqual(expectedExperience); } describe("On SQL", () => { - it("should return SQL", () => runScenario(DataModels.ApiKind.SQL, Constants.DefaultAccountExperience.DocumentDB)); + it("should return SQL", () => runScenario(DataModels.ApiKind.SQL, "SQL")); }); describe("On MongoDB", () => { - it("should return MongoDB", () => - runScenario(DataModels.ApiKind.MongoDB, Constants.DefaultAccountExperience.MongoDB)); + it("should return MongoDB", () => runScenario(DataModels.ApiKind.MongoDB, "Mongo")); }); describe("On Table", () => { - it("should return Table", () => runScenario(DataModels.ApiKind.Table, Constants.DefaultAccountExperience.Table)); + it("should return Table", () => runScenario(DataModels.ApiKind.Table, "Tables")); }); describe("On Cassandra", () => { - it("should return Cassandra", () => - runScenario(DataModels.ApiKind.Cassandra, Constants.DefaultAccountExperience.Cassandra)); + it("should return Cassandra", () => runScenario(DataModels.ApiKind.Cassandra, "Cassandra")); }); describe("On Graph", () => { - it("should return Graph", () => runScenario(DataModels.ApiKind.Graph, Constants.DefaultAccountExperience.Graph)); + it("should return Graph", () => runScenario(DataModels.ApiKind.Graph, "Gremlin")); }); describe("On unknown", () => { - it("should return Default", () => runScenario(-1, Constants.DefaultAccountExperience.Default)); + it("should return Default", () => runScenario(-1, "SQL")); }); }); describe("getApiKindFromDefaultExperience()", () => { - function runScenario(defaultExperience: string, expectedApiKind: number): void { + function runScenario(defaultExperience: typeof userContext.apiType, expectedApiKind: number): void { const resolvedApiKind = DefaultExperienceUtility.getApiKindFromDefaultExperience(defaultExperience); expect(resolvedApiKind).toEqual(expectedApiKind); } describe("On SQL", () => { - it("should return SQL", () => runScenario(Constants.DefaultAccountExperience.DocumentDB, DataModels.ApiKind.SQL)); + it("should return SQL", () => runScenario("SQL", DataModels.ApiKind.SQL)); }); describe("On MongoDB", () => { - it("should return MongoDB", () => - runScenario(Constants.DefaultAccountExperience.MongoDB, DataModels.ApiKind.MongoDB)); + it("should return MongoDB", () => runScenario("Mongo", DataModels.ApiKind.MongoDB)); }); describe("On Table", () => { - it("should return Table", () => runScenario(Constants.DefaultAccountExperience.Table, DataModels.ApiKind.Table)); + it("should return Table", () => runScenario("Tables", DataModels.ApiKind.Table)); }); describe("On Cassandra", () => { - it("should return Cassandra", () => - runScenario(Constants.DefaultAccountExperience.Cassandra, DataModels.ApiKind.Cassandra)); + it("should return Cassandra", () => runScenario("Cassandra", DataModels.ApiKind.Cassandra)); }); describe("On Graph", () => { - it("should return Graph", () => runScenario(Constants.DefaultAccountExperience.Graph, DataModels.ApiKind.Graph)); + it("should return Graph", () => runScenario("Gremlin", DataModels.ApiKind.Graph)); }); describe("On null", () => { it("should return SQL", () => runScenario(null, DataModels.ApiKind.SQL)); }); }); - - describe("getDefaultExperienceFromDatabaseAccount()", () => { - function runScenario(databaseAccount: DataModels.DatabaseAccount, expectedDefaultExperience: string): void { - const resolvedExperience = DefaultExperienceUtility.getDefaultExperienceFromDatabaseAccount(databaseAccount); - expect(resolvedExperience).toEqual(expectedDefaultExperience); - } - - const databaseAccountWithWrongTagsAndCapabilities: DataModels.DatabaseAccount = { - id: "test", - kind: "GlobalDocumentDB", - name: "test", - location: "somewhere", - type: "DocumentDB", - tags: { - defaultExperience: "Gremlin (graph)", - }, - properties: { - documentEndpoint: "", - cassandraEndpoint: "", - gremlinEndpoint: "", - tableEndpoint: "", - capabilities: [ - { - name: Constants.CapabilityNames.EnableGremlin, - description: "something", - }, - ], - }, - }; - - const databaseAccountWithApiKind: DataModels.DatabaseAccount = { - id: "test", - kind: Constants.AccountKind.MongoDB, - name: "test", - location: "somewhere", - type: "DocumentDB", - tags: {}, - properties: { - documentEndpoint: "", - cassandraEndpoint: "", - gremlinEndpoint: "", - tableEndpoint: "", - capabilities: [ - { - name: Constants.CapabilityNames.EnableGremlin, - description: "something", - }, - ], - }, - }; - - describe("Disregard tags", () => { - it("should return Graph", () => - runScenario(databaseAccountWithWrongTagsAndCapabilities, Constants.DefaultAccountExperience.Graph)); - }); - - describe("Respect Kind over capabilities", () => { - it("should return MongoDB", () => - runScenario(databaseAccountWithApiKind, Constants.DefaultAccountExperience.MongoDB)); - }); - }); }); diff --git a/src/Shared/DefaultExperienceUtility.ts b/src/Shared/DefaultExperienceUtility.ts index bee402c1c..27635968e 100644 --- a/src/Shared/DefaultExperienceUtility.ts +++ b/src/Shared/DefaultExperienceUtility.ts @@ -1,132 +1,47 @@ -import * as _ from "underscore"; -import * as Constants from "../Common/Constants"; import * as DataModels from "../Contracts/DataModels"; +import { userContext } from "../UserContext"; export class DefaultExperienceUtility { - public static getDefaultExperienceFromDatabaseAccount(databaseAccount: DataModels.DatabaseAccount): string | null { - if (!databaseAccount) { - return null; - } - - const kind: string = - databaseAccount && databaseAccount.kind && databaseAccount.kind && databaseAccount.kind.toLowerCase(); - const capabilities = (databaseAccount.properties && databaseAccount.properties.capabilities) || []; - - return DefaultExperienceUtility._getDefaultExperience(kind, capabilities); - } - - public static getApiKindFromDefaultExperience(defaultExperience: string): DataModels.ApiKind { + public static getApiKindFromDefaultExperience(defaultExperience: typeof userContext.apiType): DataModels.ApiKind { if (!defaultExperience) { return DataModels.ApiKind.SQL; } switch (defaultExperience) { - case Constants.DefaultAccountExperience.DocumentDB: + case "SQL": return DataModels.ApiKind.SQL; - case Constants.DefaultAccountExperience.MongoDB: - case Constants.DefaultAccountExperience.ApiForMongoDB: + case "Mongo": return DataModels.ApiKind.MongoDB; - case Constants.DefaultAccountExperience.Table: + case "Tables": return DataModels.ApiKind.Table; - case Constants.DefaultAccountExperience.Cassandra: + case "Cassandra": return DataModels.ApiKind.Cassandra; - case Constants.DefaultAccountExperience.Graph: + case "Gremlin": return DataModels.ApiKind.Graph; default: return DataModels.ApiKind.SQL; } } - public static getDefaultExperienceFromApiKind(apiKind: DataModels.ApiKind): string { + public static getDefaultExperienceFromApiKind(apiKind: DataModels.ApiKind): typeof userContext.apiType { if (apiKind == null) { - return Constants.DefaultAccountExperience.Default; + return "SQL"; } switch (apiKind) { case DataModels.ApiKind.SQL: - return Constants.DefaultAccountExperience.DocumentDB; + return "SQL"; case DataModels.ApiKind.MongoDB: case DataModels.ApiKind.MongoDBCompute: - return Constants.DefaultAccountExperience.MongoDB; + return "Mongo"; case DataModels.ApiKind.Table: - return Constants.DefaultAccountExperience.Table; + return "Tables"; case DataModels.ApiKind.Cassandra: - return Constants.DefaultAccountExperience.Cassandra; + return "Cassandra"; case DataModels.ApiKind.Graph: - return Constants.DefaultAccountExperience.Graph; + return "Gremlin"; default: - return Constants.DefaultAccountExperience.Default; + return "SQL"; } } - - private static _getDefaultExperience(kind: string, capabilities: DataModels.Capability[]): string { - const defaultDefaultExperience: string = Constants.DefaultAccountExperience.DocumentDB; - const defaultExperienceFromKind: string = DefaultExperienceUtility._getDefaultExperienceFromAccountKind(kind) || ""; - const defaultExperienceFromCapabilities: string = - DefaultExperienceUtility._getDefaultExperienceFromAccountCapabilities(capabilities) || ""; - if (!!defaultExperienceFromKind) { - return defaultExperienceFromKind; - } - - if (!!defaultExperienceFromCapabilities) { - return defaultExperienceFromCapabilities; - } - - return defaultDefaultExperience; - } - - private static _getDefaultExperienceFromAccountKind(kind: string): string | null { - if (!kind) { - return null; - } - - if (kind.toLowerCase() === Constants.AccountKind.MongoDB.toLowerCase()) { - return Constants.DefaultAccountExperience.MongoDB; - } - - if (kind.toLowerCase() === Constants.AccountKind.Parse.toLowerCase()) { - return Constants.DefaultAccountExperience.MongoDB; - } - - return null; - } - - private static _getDefaultExperienceFromAccountCapabilities(capabilities: DataModels.Capability[]): string | null { - if (!capabilities) { - return null; - } - - if (!Array.isArray(capabilities)) { - return null; - } - - const enableTable = DefaultExperienceUtility._findCapability(capabilities, Constants.CapabilityNames.EnableTable); - if (enableTable) { - return Constants.DefaultAccountExperience.Table; - } - - const enableGremlin = DefaultExperienceUtility._findCapability( - capabilities, - Constants.CapabilityNames.EnableGremlin - ); - if (enableGremlin) { - return Constants.DefaultAccountExperience.Graph; - } - - const enableCassandra = DefaultExperienceUtility._findCapability( - capabilities, - Constants.CapabilityNames.EnableCassandra - ); - if (enableCassandra) { - return Constants.DefaultAccountExperience.Cassandra; - } - - return null; - } - - private static _findCapability(capabilities: DataModels.Capability[], capabilityName: string): DataModels.Capability { - return _.find(capabilities, (capability) => { - return capability && capability.name && capability.name.toLowerCase() === capabilityName.toLowerCase(); - }); - } } diff --git a/src/UserContext.ts b/src/UserContext.ts index 45b1a805e..0cbec9226 100644 --- a/src/UserContext.ts +++ b/src/UserContext.ts @@ -27,7 +27,7 @@ interface UserContext { readonly hasWriteAccess: boolean; } -type ApiType = "SQL" | "Mongo" | "Gremlin" | "Tables" | "Cassandra"; +export type ApiType = "SQL" | "Mongo" | "Gremlin" | "Tables" | "Cassandra"; export type PortalEnv = "localhost" | "blackforest" | "fairfax" | "mooncake" | "prod" | "dev"; const features = extractFeatures(); diff --git a/src/hooks/useKnockoutExplorer.ts b/src/hooks/useKnockoutExplorer.ts index 2905474a9..ceea5557c 100644 --- a/src/hooks/useKnockoutExplorer.ts +++ b/src/hooks/useKnockoutExplorer.ts @@ -1,7 +1,7 @@ import { useEffect, useState } from "react"; import { applyExplorerBindings } from "../applyExplorerBindings"; import { AuthType } from "../AuthType"; -import { AccountKind, DefaultAccountExperience } from "../Common/Constants"; +import { AccountKind } from "../Common/Constants"; import { normalizeArmEndpoint } from "../Common/EnvironmentUtility"; import { sendMessage, sendReadyMessage } from "../Common/MessageHandler"; import { configContext, Platform, updateConfigContext } from "../ConfigContext"; @@ -141,8 +141,6 @@ function configureHostedWithResourceToken(config: ResourceToken, explorerParams: name: parsedResourceToken.accountEndpoint, kind: AccountKind.GlobalDocumentDB, properties: { documentEndpoint: parsedResourceToken.accountEndpoint }, - // Resource tokens can only be used with SQL API - tags: { defaultExperience: DefaultAccountExperience.DocumentDB }, }; updateUserContext({ databaseAccount, @@ -165,9 +163,7 @@ function configureHostedWithEncryptedToken(config: EncryptedToken, explorerParam authType: AuthType.EncryptedToken, accessToken: encodeURIComponent(config.encryptedToken), }); - const apiExperience: string = DefaultExperienceUtility.getDefaultExperienceFromApiKind( - config.encryptedTokenMetadata.apiKind - ); + const apiExperience = DefaultExperienceUtility.getDefaultExperienceFromApiKind(config.encryptedTokenMetadata.apiKind); const explorer = new Explorer(explorerParams); explorer.configure({ databaseAccount: {