@finos/legend-studio
Version:
339 lines • 16 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { computed, observable, action, makeObservable } from 'mobx';
import { guaranteeType, uuid, UnsupportedOperationError, } from '@finos/legend-shared';
import { ElementEditorState } from './../ElementEditorState.js';
import { DatabaseBuilderState } from './DatabaseBuilderState.js';
import { PackageableConnection, JsonModelConnection, FlatDataConnection, RelationalDatabaseConnection, DefaultH2AuthenticationStrategy, DelegatedKerberosAuthenticationStrategy, OAuthAuthenticationStrategy, UsernamePasswordAuthenticationStrategy, ApiTokenAuthenticationStrategy, SnowflakePublicAuthenticationStrategy, GCPApplicationDefaultCredentialsAuthenticationStrategy, GCPWorkloadIdentityFederationAuthenticationStrategy, EmbeddedH2DatasourceSpecification, LocalH2DatasourceSpecification, DatabricksDatasourceSpecification, SnowflakeDatasourceSpecification, BigQueryDatasourceSpecification, StaticDatasourceSpecification, RedshiftDatasourceSpecification, createValidationError, isStubbed_PackageableElement, } from '@finos/legend-graph';
import { relationDbConnection_setNewAuthenticationStrategy, relationDbConnection_setDatasourceSpecification, } from '../../../graphModifier/StoreRelational_GraphModifierHelper.js';
export class ConnectionValueState {
editorStore;
connection;
constructor(editorStore, connection) {
this.editorStore = editorStore;
this.connection = connection;
}
}
export var RELATIONAL_DATABASE_TAB_TYPE;
(function (RELATIONAL_DATABASE_TAB_TYPE) {
RELATIONAL_DATABASE_TAB_TYPE["GENERAL"] = "GENERAL";
RELATIONAL_DATABASE_TAB_TYPE["STORE"] = "STORE";
})(RELATIONAL_DATABASE_TAB_TYPE = RELATIONAL_DATABASE_TAB_TYPE || (RELATIONAL_DATABASE_TAB_TYPE = {}));
export var CORE_DATASOURCE_SPEC_TYPE;
(function (CORE_DATASOURCE_SPEC_TYPE) {
CORE_DATASOURCE_SPEC_TYPE["STATIC"] = "STATIC";
CORE_DATASOURCE_SPEC_TYPE["H2_LOCAL"] = "H2_LOCAL";
CORE_DATASOURCE_SPEC_TYPE["H2_EMBEDDED"] = "H2_EMBEDDED";
CORE_DATASOURCE_SPEC_TYPE["DATABRICKS"] = "DATABRICKS";
CORE_DATASOURCE_SPEC_TYPE["SNOWFLAKE"] = "SNOWFLAKE";
CORE_DATASOURCE_SPEC_TYPE["REDSHIFT"] = "REDSHIFT";
CORE_DATASOURCE_SPEC_TYPE["BIGQUERY"] = "BIGQUERY";
})(CORE_DATASOURCE_SPEC_TYPE = CORE_DATASOURCE_SPEC_TYPE || (CORE_DATASOURCE_SPEC_TYPE = {}));
export var CORE_AUTHENTICATION_STRATEGY_TYPE;
(function (CORE_AUTHENTICATION_STRATEGY_TYPE) {
CORE_AUTHENTICATION_STRATEGY_TYPE["DELEGATED_KERBEROS"] = "DELEGATED_KERBEROS";
CORE_AUTHENTICATION_STRATEGY_TYPE["H2_DEFAULT"] = "H2_DEFAULT";
CORE_AUTHENTICATION_STRATEGY_TYPE["SNOWFLAKE_PUBLIC"] = "SNOWFLAKE_PUBLIC";
CORE_AUTHENTICATION_STRATEGY_TYPE["GCP_APPLICATION_DEFAULT_CREDENTIALS"] = "GCP_APPLICATION_DEFAULT_CREDENTIALS";
CORE_AUTHENTICATION_STRATEGY_TYPE["API_TOKEN"] = "API_TOKEN";
CORE_AUTHENTICATION_STRATEGY_TYPE["OAUTH"] = "OAUTH";
CORE_AUTHENTICATION_STRATEGY_TYPE["USERNAME_PASSWORD"] = "USERNAME_PASSWORD";
CORE_AUTHENTICATION_STRATEGY_TYPE["GCP_WORKLOAD_IDENTITY_FEDERATION"] = "GCP_WORKLOAD_IDENTITY_FEDERATION";
})(CORE_AUTHENTICATION_STRATEGY_TYPE = CORE_AUTHENTICATION_STRATEGY_TYPE || (CORE_AUTHENTICATION_STRATEGY_TYPE = {}));
export class RelationalDatabaseConnectionValueState extends ConnectionValueState {
connection;
selectedTab = RELATIONAL_DATABASE_TAB_TYPE.GENERAL;
databaseBuilderState;
constructor(editorStore, connection) {
super(editorStore, connection);
makeObservable(this, {
setSelectedTab: action,
databaseBuilderState: observable,
selectedTab: observable,
});
this.connection = connection;
this.databaseBuilderState = new DatabaseBuilderState(editorStore, connection);
}
get storeValidationResult() {
return isStubbed_PackageableElement(this.connection.store.value)
? createValidationError(['Connection database cannot be empty'])
: undefined;
}
setSelectedTab(val) {
this.selectedTab = val;
}
label() {
return `${this.connection.type} connection`;
}
get selectedDatasourceSpec() {
const spec = this.connection.datasourceSpecification;
if (spec instanceof StaticDatasourceSpecification) {
return CORE_DATASOURCE_SPEC_TYPE.STATIC;
}
else if (spec instanceof EmbeddedH2DatasourceSpecification) {
return CORE_DATASOURCE_SPEC_TYPE.H2_EMBEDDED;
}
else if (spec instanceof DatabricksDatasourceSpecification) {
return CORE_DATASOURCE_SPEC_TYPE.DATABRICKS;
}
else if (spec instanceof SnowflakeDatasourceSpecification) {
return CORE_DATASOURCE_SPEC_TYPE.SNOWFLAKE;
}
else if (spec instanceof BigQueryDatasourceSpecification) {
return CORE_DATASOURCE_SPEC_TYPE.BIGQUERY;
}
else if (spec instanceof LocalH2DatasourceSpecification) {
return CORE_DATASOURCE_SPEC_TYPE.H2_LOCAL;
}
else if (spec instanceof RedshiftDatasourceSpecification) {
return CORE_DATASOURCE_SPEC_TYPE.REDSHIFT;
}
const extraDatasourceSpecificationTypeGetters = this.editorStore.pluginManager
.getApplicationPlugins()
.flatMap((plugin) => plugin.getExtraDatasourceSpecificationTypeGetters?.() ?? []);
for (const typeGetter of extraDatasourceSpecificationTypeGetters) {
const type = typeGetter(spec);
if (type) {
return type;
}
}
throw new UnsupportedOperationError(`Can't classify datasource specification: no compatible classifer available from plugins`, spec);
}
changeDatasourceSpec(type) {
const observerContext = this.editorStore.changeDetectionState.observerContext;
let dataSpec;
switch (type) {
case CORE_DATASOURCE_SPEC_TYPE.STATIC: {
dataSpec = new StaticDatasourceSpecification('', 80, '');
break;
}
case CORE_DATASOURCE_SPEC_TYPE.H2_LOCAL: {
dataSpec = new LocalH2DatasourceSpecification();
break;
}
case CORE_DATASOURCE_SPEC_TYPE.H2_EMBEDDED: {
dataSpec = new EmbeddedH2DatasourceSpecification('', '', false);
break;
}
case CORE_DATASOURCE_SPEC_TYPE.DATABRICKS: {
dataSpec = new DatabricksDatasourceSpecification('', '', '', '');
break;
}
case CORE_DATASOURCE_SPEC_TYPE.SNOWFLAKE: {
dataSpec = new SnowflakeDatasourceSpecification('', '', '', '');
break;
}
case CORE_DATASOURCE_SPEC_TYPE.REDSHIFT: {
dataSpec = new RedshiftDatasourceSpecification('', '', 5439, '', '', '');
break;
}
case CORE_DATASOURCE_SPEC_TYPE.BIGQUERY: {
dataSpec = new BigQueryDatasourceSpecification('', '');
break;
}
default: {
const extraDatasourceSpecificationCreators = this.editorStore.pluginManager
.getApplicationPlugins()
.flatMap((plugin) => plugin.getExtraDatasourceSpecificationCreators?.() ?? []);
for (const creator of extraDatasourceSpecificationCreators) {
const spec = creator(type);
if (spec) {
dataSpec = spec;
break;
}
}
}
}
if (!dataSpec) {
throw new UnsupportedOperationError(`Can't create datasource specification of type '${type}': no compatible creator available from plugins`);
}
relationDbConnection_setDatasourceSpecification(this.connection, dataSpec, observerContext);
}
get selectedAuth() {
const auth = this.connection.authenticationStrategy;
if (auth instanceof DelegatedKerberosAuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.DELEGATED_KERBEROS;
}
else if (auth instanceof DefaultH2AuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.H2_DEFAULT;
}
else if (auth instanceof OAuthAuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.OAUTH;
}
else if (auth instanceof ApiTokenAuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.API_TOKEN;
}
else if (auth instanceof SnowflakePublicAuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.SNOWFLAKE_PUBLIC;
}
else if (auth instanceof UsernamePasswordAuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.USERNAME_PASSWORD;
}
else if (auth instanceof GCPApplicationDefaultCredentialsAuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.GCP_APPLICATION_DEFAULT_CREDENTIALS;
}
else if (auth instanceof GCPWorkloadIdentityFederationAuthenticationStrategy) {
return CORE_AUTHENTICATION_STRATEGY_TYPE.GCP_WORKLOAD_IDENTITY_FEDERATION;
}
const extraAuthenticationStrategyTypeGetters = this.editorStore.pluginManager
.getApplicationPlugins()
.flatMap((plugin) => plugin.getExtraAuthenticationStrategyTypeGetters?.() ?? []);
for (const typeGetter of extraAuthenticationStrategyTypeGetters) {
const type = typeGetter(auth);
if (type) {
return type;
}
}
throw new UnsupportedOperationError(`Can't classify authentication strategy: no compatible classifier available from plugins`, auth);
}
changeAuthenticationStrategy(type) {
const observerContext = this.editorStore.changeDetectionState.observerContext;
let authStrategy;
switch (type) {
case CORE_AUTHENTICATION_STRATEGY_TYPE.DELEGATED_KERBEROS: {
authStrategy = new DelegatedKerberosAuthenticationStrategy();
break;
}
case CORE_AUTHENTICATION_STRATEGY_TYPE.API_TOKEN: {
authStrategy = new ApiTokenAuthenticationStrategy('');
break;
}
case CORE_AUTHENTICATION_STRATEGY_TYPE.SNOWFLAKE_PUBLIC: {
authStrategy = new SnowflakePublicAuthenticationStrategy('', '', '');
break;
}
case CORE_AUTHENTICATION_STRATEGY_TYPE.GCP_APPLICATION_DEFAULT_CREDENTIALS: {
authStrategy =
new GCPApplicationDefaultCredentialsAuthenticationStrategy();
break;
}
case CORE_AUTHENTICATION_STRATEGY_TYPE.GCP_WORKLOAD_IDENTITY_FEDERATION: {
authStrategy = new GCPWorkloadIdentityFederationAuthenticationStrategy('', []);
break;
}
case CORE_AUTHENTICATION_STRATEGY_TYPE.H2_DEFAULT: {
authStrategy = new DefaultH2AuthenticationStrategy();
break;
}
case CORE_AUTHENTICATION_STRATEGY_TYPE.USERNAME_PASSWORD: {
authStrategy = new UsernamePasswordAuthenticationStrategy('', '');
break;
}
case CORE_AUTHENTICATION_STRATEGY_TYPE.OAUTH: {
authStrategy = new OAuthAuthenticationStrategy('', '');
break;
}
default: {
const extraAuthenticationStrategyCreators = this.editorStore.pluginManager
.getApplicationPlugins()
.flatMap((plugin) => plugin.getExtraAuthenticationStrategyCreators?.() ?? []);
for (const creator of extraAuthenticationStrategyCreators) {
const auth = creator(type);
if (auth) {
authStrategy = auth;
break;
}
}
}
}
if (!authStrategy) {
throw new UnsupportedOperationError(`Can't create authentication strategy of type '${type}': no compatible creator available from plugins`);
}
relationDbConnection_setNewAuthenticationStrategy(this.connection, authStrategy, observerContext);
}
}
export class JsonModelConnectionValueState extends ConnectionValueState {
connection;
constructor(editorStore, connection) {
super(editorStore, connection);
this.connection = connection;
}
label() {
return 'Pure model connection';
}
}
export class FlatDataConnectionValueState extends ConnectionValueState {
connection;
constructor(editorStore, connection) {
super(editorStore, connection);
this.connection = connection;
}
label() {
return 'flat-data connection';
}
}
export class UnsupportedConnectionValueState extends ConnectionValueState {
label() {
return 'unsupported connection';
}
}
export class ConnectionEditorState {
/**
* NOTE: used to force component remount on state change
*/
uuid = uuid();
editorStore;
connection;
connectionValueState;
constructor(editorStore, connection) {
this.editorStore = editorStore;
this.connection = connection;
this.connectionValueState = this.buildConnectionValueEditorState();
}
buildConnectionValueEditorState() {
const connection = this.connection;
if (connection instanceof JsonModelConnection) {
return new JsonModelConnectionValueState(this.editorStore, connection);
}
else if (connection instanceof FlatDataConnection) {
return new FlatDataConnectionValueState(this.editorStore, connection);
}
else if (connection instanceof RelationalDatabaseConnection) {
return new RelationalDatabaseConnectionValueState(this.editorStore, connection);
}
else {
const extraConnectionValueEditorStateBuilders = this.editorStore.pluginManager
.getApplicationPlugins()
.flatMap((plugin) => plugin.getExtraConnectionValueEditorStateBuilders?.() ?? []);
for (const stateBuilder of extraConnectionValueEditorStateBuilders) {
const state = stateBuilder(this.editorStore, connection);
if (state) {
return state;
}
}
return new UnsupportedConnectionValueState(this.editorStore, connection);
}
}
}
export class PackageableConnectionEditorState extends ElementEditorState {
connectionState;
constructor(editorStore, element) {
super(editorStore, element);
makeObservable(this, {
connection: computed,
reprocess: action,
});
this.connectionState = new ConnectionEditorState(editorStore, this.connection.connectionValue);
}
get connection() {
return guaranteeType(this.element, PackageableConnection, `Element inside connection editor state must be a packageable connection`);
}
reprocess(newElement, editorStore) {
const editorState = new PackageableConnectionEditorState(editorStore, newElement);
return editorState;
}
}
//# sourceMappingURL=ConnectionEditorState.js.map