2020-05-25 21:30:55 -05:00
|
|
|
import * as _ from "underscore";
|
|
|
|
import * as Constants from "../Common/Constants";
|
|
|
|
import * as DataModels from "../Contracts/DataModels";
|
2020-08-11 18:36:42 -07:00
|
|
|
import { DefaultAccountExperienceType } from "../DefaultAccountExperienceType";
|
2020-05-25 21:30:55 -05:00
|
|
|
|
|
|
|
export class DefaultExperienceUtility {
|
2020-07-27 16:05:25 -05:00
|
|
|
public static getDefaultExperienceFromDatabaseAccount(databaseAccount: DataModels.DatabaseAccount): string {
|
2020-05-25 21:30:55 -05:00
|
|
|
if (!databaseAccount) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const kind: string =
|
|
|
|
databaseAccount && databaseAccount.kind && databaseAccount.kind && databaseAccount.kind.toLowerCase();
|
2020-07-27 16:05:25 -05:00
|
|
|
const capabilities = (databaseAccount.properties && databaseAccount.properties.capabilities) || [];
|
2020-05-25 21:30:55 -05:00
|
|
|
|
|
|
|
return DefaultExperienceUtility._getDefaultExperience(kind, capabilities);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static getApiKindFromDefaultExperience(defaultExperience: string): DataModels.ApiKind {
|
|
|
|
if (!defaultExperience) {
|
|
|
|
return DataModels.ApiKind.SQL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (defaultExperience) {
|
|
|
|
case Constants.DefaultAccountExperience.DocumentDB:
|
|
|
|
return DataModels.ApiKind.SQL;
|
|
|
|
case Constants.DefaultAccountExperience.MongoDB:
|
|
|
|
case Constants.DefaultAccountExperience.ApiForMongoDB:
|
|
|
|
return DataModels.ApiKind.MongoDB;
|
|
|
|
case Constants.DefaultAccountExperience.Table:
|
|
|
|
return DataModels.ApiKind.Table;
|
|
|
|
case Constants.DefaultAccountExperience.Cassandra:
|
|
|
|
return DataModels.ApiKind.Cassandra;
|
|
|
|
case Constants.DefaultAccountExperience.Graph:
|
|
|
|
return DataModels.ApiKind.Graph;
|
|
|
|
default:
|
|
|
|
return DataModels.ApiKind.SQL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static getDefaultExperienceFromApiKind(apiKind: DataModels.ApiKind): string {
|
|
|
|
if (apiKind == null) {
|
|
|
|
return Constants.DefaultAccountExperience.Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (apiKind) {
|
|
|
|
case DataModels.ApiKind.SQL:
|
|
|
|
return Constants.DefaultAccountExperience.DocumentDB;
|
|
|
|
case DataModels.ApiKind.MongoDB:
|
|
|
|
case DataModels.ApiKind.MongoDBCompute:
|
|
|
|
return Constants.DefaultAccountExperience.MongoDB;
|
|
|
|
case DataModels.ApiKind.Table:
|
|
|
|
return Constants.DefaultAccountExperience.Table;
|
|
|
|
case DataModels.ApiKind.Cassandra:
|
|
|
|
return Constants.DefaultAccountExperience.Cassandra;
|
|
|
|
case DataModels.ApiKind.Graph:
|
|
|
|
return Constants.DefaultAccountExperience.Graph;
|
|
|
|
default:
|
|
|
|
return Constants.DefaultAccountExperience.Default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:36:42 -07:00
|
|
|
public static mapDefaultExperienceStringToEnum(defaultExperience: string): DefaultAccountExperienceType {
|
|
|
|
switch (defaultExperience) {
|
|
|
|
case Constants.DefaultAccountExperience.DocumentDB:
|
|
|
|
return DefaultAccountExperienceType.DocumentDB;
|
|
|
|
case Constants.DefaultAccountExperience.Graph:
|
|
|
|
return DefaultAccountExperienceType.Graph;
|
|
|
|
case Constants.DefaultAccountExperience.MongoDB:
|
|
|
|
return DefaultAccountExperienceType.MongoDB;
|
|
|
|
case Constants.DefaultAccountExperience.Table:
|
|
|
|
return DefaultAccountExperienceType.Table;
|
|
|
|
case Constants.DefaultAccountExperience.Cassandra:
|
|
|
|
return DefaultAccountExperienceType.Cassandra;
|
|
|
|
case Constants.DefaultAccountExperience.ApiForMongoDB:
|
|
|
|
return DefaultAccountExperienceType.ApiForMongoDB;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported default experience type: ${defaultExperience}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
private static _getDefaultExperience(kind: string, capabilities: DataModels.Capability[]): string {
|
2020-05-25 21:30:55 -05:00
|
|
|
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 {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
private static _getDefaultExperienceFromAccountCapabilities(capabilities: DataModels.Capability[]): string {
|
2020-05-25 21:30:55 -05:00
|
|
|
if (!capabilities) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(capabilities)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
const enableTable = DefaultExperienceUtility._findCapability(capabilities, Constants.CapabilityNames.EnableTable);
|
2020-05-25 21:30:55 -05:00
|
|
|
if (enableTable) {
|
|
|
|
return Constants.DefaultAccountExperience.Table;
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
const enableGremlin = DefaultExperienceUtility._findCapability(
|
2020-05-25 21:30:55 -05:00
|
|
|
capabilities,
|
|
|
|
Constants.CapabilityNames.EnableGremlin
|
|
|
|
);
|
|
|
|
if (enableGremlin) {
|
|
|
|
return Constants.DefaultAccountExperience.Graph;
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
const enableCassandra = DefaultExperienceUtility._findCapability(
|
2020-05-25 21:30:55 -05:00
|
|
|
capabilities,
|
|
|
|
Constants.CapabilityNames.EnableCassandra
|
|
|
|
);
|
|
|
|
if (enableCassandra) {
|
|
|
|
return Constants.DefaultAccountExperience.Cassandra;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
private static _findCapability(capabilities: DataModels.Capability[], capabilityName: string): DataModels.Capability {
|
|
|
|
return _.find(capabilities, capability => {
|
2020-05-25 21:30:55 -05:00
|
|
|
return capability && capability.name && capability.name.toLowerCase() === capabilityName.toLowerCase();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|