From db5fc8fa3feac6f67a36cc33a3aef09b291ae789 Mon Sep 17 00:00:00 2001 From: Matt Apperson Date: Tue, 23 Jul 2019 14:45:32 -0400 Subject: [PATCH] Fixed unused variables. Added a few methods to the SO adapter Co-authored-by: Nicolas Chaulet --- .../__integrations_tests__/adapter.test.ts | 5 + .../adapters/configurations/adapter_types.ts | 8 +- .../libs/adapters/configurations/default.ts | 90 ++++++++++++-- .../libs/adapters/so_database/default.ts | 117 +++++++++++++++++- 4 files changed, 202 insertions(+), 18 deletions(-) create mode 100644 x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/__integrations_tests__/adapter.test.ts diff --git a/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/__integrations_tests__/adapter.test.ts b/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/__integrations_tests__/adapter.test.ts new file mode 100644 index 00000000000000..41bc2aa2588073 --- /dev/null +++ b/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/__integrations_tests__/adapter.test.ts @@ -0,0 +1,5 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ diff --git a/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/adapter_types.ts b/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/adapter_types.ts index b4fb6ccbfb851f..c29b4c142c8180 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/adapter_types.ts +++ b/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/adapter_types.ts @@ -5,7 +5,6 @@ */ import * as t from 'io-ts'; -import { DateFromString } from '../../../../common/types/io_ts'; export const RuntimeDatasourceInput = t.interface( { @@ -48,13 +47,14 @@ export const NewRuntimeBackupConfigurationFile = t.interface( 'BackupConfigurationFile' ); -const ExistingDocument = t.partial({ +const ExistingDocument = t.interface({ id: t.string, shared_id: t.string, version: t.number, - updated_at: DateFromString, + active: t.boolean, + updated_at: t.string, created_by: t.union([t.undefined, t.string]), - updated_on: DateFromString, + updated_on: t.string, updated_by: t.union([t.undefined, t.string]), }); diff --git a/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/default.ts b/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/default.ts index 84a352177d2f66..dbbdaa9471f346 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/default.ts +++ b/x-pack/legacy/plugins/ingest/server/libs/adapters/configurations/default.ts @@ -5,13 +5,9 @@ */ import { SODatabaseAdapter } from '../so_database/default'; +import { RuntimeConfigurationFile, NewConfigurationFile } from './adapter_types'; -import { - ConfigurationFile, - NewConfigurationFile, - DatasourceInput, - BackupConfigurationFile, -} from './adapter_types'; +import { ConfigurationFile, DatasourceInput, BackupConfigurationFile } from './adapter_types'; export class ConfigAdapter { constructor(private readonly so: SODatabaseAdapter) {} @@ -19,19 +15,87 @@ export class ConfigAdapter { public async create( configuration: NewConfigurationFile ): Promise<{ id: string; shared_id: string; version: number }> { + const newSo = await this.so.create( + 'configurations', + (configuration as any) as ConfigurationFile + ); + return { - id: 'fsdfsdf', - shared_id: 'wjkhefkjhfkjs', - version: 0, + id: newSo.id, + shared_id: newSo.attributes.shared_id, + version: newSo.attributes.version, }; } - public async get(sharedID: string, version?: number): Promise { - return {} as ConfigurationFile; + public async get(id: string): Promise { + const config = await this.so.get('configurations', id); + + if (config.error) { + throw new Error(config.error.message); + } + + if (!config.attributes) { + throw new Error(`No configuration found with ID of ${id}`); + } + if (RuntimeConfigurationFile.decode(config.attributes).isRight()) { + return config.attributes as ConfigurationFile; + } else { + throw new Error(`Invalid ConfigurationFile data. == ${config.attributes}`); + } } - public async list(sharedID: string, version?: number): Promise { - return [{} as ConfigurationFile]; + public async list(): Promise { + const configs = await this.so.find({ + type: 'configurations', + search: '*', + searchFields: ['shared_id'], + }); + const uniqConfigurationFile = configs.saved_objects + .map(config => { + if (RuntimeConfigurationFile.decode(config.attributes).isRight()) { + return config.attributes; + } else { + throw new Error(`Invalid ConfigurationFile data. == ${config.attributes}`); + } + }) + .reduce((acc, config: ConfigurationFile) => { + if (!acc.has(config.shared_id)) { + acc.set(config.shared_id, config); + } + const prevConfig = acc.get(config.shared_id); + if (prevConfig && prevConfig.version < config.version) { + acc.set(config.shared_id, config); + } + + return acc; + }, new Map()); + + return [...uniqConfigurationFile.values()]; + } + + public async listVersions(sharedID: string, activeOnly = true): Promise { + const configs = (await this.so.find({ + type: 'configurations', + search: sharedID, + searchFields: ['shared_id'], + })).saved_objects; + + if (!activeOnly) { + const backupConfigs = await this.so.find({ + type: 'backup_configurations', + search: sharedID, + searchFields: ['shared_id'], + }); + configs.concat(backupConfigs.saved_objects); + } + + return configs.map(config => { + if (RuntimeConfigurationFile.decode(config.attributes).isRight()) { + return config.attributes; + } else { + throw new Error(`Invalid ConfigurationFile data. == ${config.attributes}`); + } + }); } public async update( diff --git a/x-pack/legacy/plugins/ingest/server/libs/adapters/so_database/default.ts b/x-pack/legacy/plugins/ingest/server/libs/adapters/so_database/default.ts index e11c8b76967483..b70d2d2c5e823e 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/adapters/so_database/default.ts +++ b/x-pack/legacy/plugins/ingest/server/libs/adapters/so_database/default.ts @@ -4,8 +4,24 @@ * you may not use this file except in compliance with the Elastic License. */ -import { SavedObjectsService, SavedObjectsClient as SavedObjectsClientType } from 'src/core/server'; +import { + SavedObjectsService, + SavedObjectsClient as SavedObjectsClientType, + SavedObjectAttributes, + SavedObjectsBulkCreateObject, + SavedObjectsBaseOptions, + SavedObjectsFindOptions, + SavedObjectsFindResponse, + SavedObjectsBulkResponse, + SavedObject, + SavedObjectsUpdateOptions, +} from 'src/core/server'; import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch'; +import { + SavedObjectsCreateOptions, + SavedObjectsBulkGetObject, + SavedObjectsUpdateResponse, +} from 'target/types/server'; export class SODatabaseAdapter { private client: SavedObjectsClientType; @@ -16,4 +32,103 @@ export class SODatabaseAdapter { this.client = new SavedObjectsClient(internalRepository); } + + /** + * Persists a SavedObject + * + * @param type + * @param attributes + * @param options + */ + async create( + type: string, + data: T, + options?: SavedObjectsCreateOptions + ) { + return await this.client.create(type, data, options); + } + + /** + * Persists multiple documents batched together as a single request + * + * @param objects + * @param options + */ + async bulkCreate( + objects: Array>, + options?: SavedObjectsCreateOptions + ) { + return await this.client.bulkCreate(objects, options); + } + + /** + * Deletes a SavedObject + * + * @param type + * @param id + * @param options + */ + async delete(type: string, id: string, options: SavedObjectsBaseOptions = {}) { + return await this.client.delete(type, id, options); + } + + /** + * Find all SavedObjects matching the search query + * + * @param options + */ + async find( + options: SavedObjectsFindOptions + ): Promise> { + return await this.client.find(options); + } + + /** + * Returns an array of objects by id + * + * @param objects - an array of ids, or an array of objects containing id, type and optionally fields + * @example + * + * bulkGet([ + * { id: 'one', type: 'config' }, + * { id: 'foo', type: 'index-pattern' } + * ]) + */ + async bulkGet( + objects: SavedObjectsBulkGetObject[] = [], + options: SavedObjectsBaseOptions = {} + ): Promise> { + return await this.client.bulkGet(objects, options); + } + + /** + * Retrieves a single object + * + * @param type - The type of SavedObject to retrieve + * @param id - The ID of the SavedObject to retrieve + * @param options + */ + async get( + type: string, + id: string, + options: SavedObjectsBaseOptions = {} + ): Promise> { + return await this.client.get(type, id, options); + } + + /** + * Updates an SavedObject + * + * @param type + * @param id + * @param options + */ + async update( + type: string, + id: string, + attributes: Partial, + options: SavedObjectsUpdateOptions = {} + ): Promise> { + return await this.client.update(type, id, attributes, options); + } }