Skip to content

Commit

Permalink
Reads configuration from opensearch_dashboards.yml
Browse files Browse the repository at this point in the history
Signed-off-by: Bandini Bhopi <bandinib@amazon.com>
  • Loading branch information
bandinib-amzn committed Apr 11, 2023
1 parent b466cd6 commit 89213eb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
7 changes: 4 additions & 3 deletions opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "1.0.0",
"opensearchDashboardsVersion": "opensearchDashboards",
"server": true,
"ui": true,
"requiredPlugins": ["navigation"],
"optionalPlugins": []
"ui": false,
"requiredPlugins": [],
"optionalPlugins": [],
"configPath": ["metaStorage"]
}
42 changes: 39 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,47 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { PluginInitializerContext } from '../../../src/core/server';
import { schema, TypeOf } from "@osd/config-schema";
import { PluginInitializerContext, PluginConfigDescriptor } from '../../../src/core/server';
import { MetadataPlugin } from './plugin';

// This exports static code and TypeScript types,
// as well as, OpenSearch Dashboards Platform `plugin()` initializer.
export type MetaStorageConfigType = TypeOf<typeof metaStorageConfig.schema>;

export const metaStorageConfig = {
schema: schema.object({
enabled: schema.boolean({ defaultValue: false }),
config: schema.object({
type: schema.string({
defaultValue: 'opensearch',
validate(value: string) {
const supportedDbType = ['postgres', 'opensearch'];

if (!supportedDbType.includes(value.toLowerCase())) {
throw new Error(
`Unsupported database type: ${value}. Allowed database types are: ${supportedDbType}.`
);
}
},
}),
database: schema.string({ defaultValue: 'opensearch_dashboards' }),
table: schema.string({ defaultValue: 'MetadataStore' }),
hostName: schema.string({ defaultValue: '' }),
userName: schema.string({ defaultValue: '' }),
password: schema.string({ defaultValue: '' }),
port: schema.number({ defaultValue: 0 }),
max: schema.number({ defaultValue: 100 }),
idleTimeoutMillis: schema.number({ defaultValue: 10000 }),
}),
}),
};

export const config: PluginConfigDescriptor<MetaStorageConfigType> = {
exposeToBrowser: {
enabled: true,
config: true,
},
schema: metaStorageConfig.schema,
};

export function plugin(initializerContext: PluginInitializerContext) {
return new MetadataPlugin(initializerContext);
Expand Down
12 changes: 9 additions & 3 deletions server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { first } from 'rxjs/operators';
import {
PluginInitializerContext,
CoreSetup,
Expand All @@ -12,26 +13,30 @@ import {
import { MetadataPluginSetup, MetadataPluginStart } from './types';
import { defineRoutes } from './routes';
import { PostgresClientWrapper } from './saved_object_wrapper';
import { MetaStorageConfigType } from '.';

export class MetadataPlugin implements Plugin<MetadataPluginSetup, MetadataPluginStart> {
private readonly logger: Logger;
private savedObjectClientWrapper: PostgresClientWrapper;
private readonly config$;
private config: MetaStorageConfigType;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
this.savedObjectClientWrapper = new PostgresClientWrapper();
this.config$ = initializerContext.config.create<MetaStorageConfigType>();
}

public setup(core: CoreSetup) {
public async setup(core: CoreSetup) {
this.logger.info('metadata-plugin: Setup');

this.config = await this.config$.pipe(first()).toPromise();

const router = core.http.createRouter();

// Register server side APIs
defineRoutes(router);

// ToDo: Add condition to check If metadata enabled
core.savedObjects.addClientWrapper(
0,
'postgres-saved-object-client-wrapper',
Expand All @@ -41,11 +46,12 @@ export class MetadataPlugin implements Plugin<MetadataPluginSetup, MetadataPlugi
return {};
}

public start(core: CoreStart) {
public async start(core: CoreStart) {
this.logger.info('metadata-plugin: Started');

this.savedObjectClientWrapper.typeRegistry = core.savedObjects.getTypeRegistry();
console.log(`this.savedObjectClientWrapper.typeRegistry : ${this.savedObjectClientWrapper.typeRegistry}`);
this.savedObjectClientWrapper.metaSrorageConfig = this.config;
this.savedObjectClientWrapper.setup();
return {};
}
Expand Down
30 changes: 21 additions & 9 deletions server/saved_object_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
SavedObjectsFindResult,
} from "../../../src/core/server";
import { IOpenSearchDashboardsMigrator } from 'src/core/server/saved_objects/migrations';
import { MetaStorageConfigType } from '.';

export const ALL_NAMESPACES_STRING = '*';

Expand All @@ -45,21 +46,17 @@ export class PostgresClientWrapper {
private serializer: SavedObjectsSerializer;
private allowedTypes: string[];
private _migrator: IOpenSearchDashboardsMigrator;
public metaSrorageConfig?: MetaStorageConfigType;


constructor() {
this.initializeDBConnection();
}

private initializeDBConnection() {
public setup() {
const pg = require('pg');
const dbName = 'opensearch_dashboards';
// const connUrl = `postgres://${config.dbUserName}:${config.dbPassword}@${config.dbHostName}:${config.dbPort}`;
const connUrl = `postgres://${'postgres'}:${'Kibana123!'}@${'neo-postgres-demo.ctuks8gzcnbe.us-east-1.rds.amazonaws.com'}:${5432}`;
this.postgresClient = new pg.Client(`${connUrl}/${dbName}`);
this.postgresClient = new pg.Pool(this.getMetaStorageInitConfig());
this.postgresClient.connect();
}

public setup() {
this.serializer = new SavedObjectsSerializer(this.typeRegistry!);

const allTypes = this.typeRegistry!.getAllTypes().map((t) => t.name);
Expand All @@ -72,6 +69,21 @@ export class PostgresClientWrapper {
// ToDO: How to get migrator here.
}

private getMetaStorageInitConfig() {
const metaConfig = this.metaSrorageConfig!.config;
const configSchema = {
user: metaConfig.userName,
password: metaConfig.password,
database: metaConfig.database,
host: metaConfig.hostName,
port: metaConfig.port,
max: metaConfig.max,
idleTimeoutMillis: metaConfig.idleTimeoutMillis,
};

return configSchema;
}

public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
const create = async <T = unknown>(
type: string,
Expand Down Expand Up @@ -148,7 +160,7 @@ export class PostgresClientWrapper {
const find = async <T = unknown>(
options: SavedObjectsFindOptions
): Promise<SavedObjectsFindResponse<T>> => {
console.log(`Inside find`);
console.log(`Inside metadata plugin find`);
const {
search,
searchFields,
Expand Down

0 comments on commit 89213eb

Please sign in to comment.