diff --git a/x-pack/plugins/ingest_manager/common/types/models/agent_config.ts b/x-pack/plugins/ingest_manager/common/types/models/agent_config.ts index b19e4a0f4ac27..7547f56237eec 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/agent_config.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/agent_config.ts @@ -33,6 +33,8 @@ export interface AgentConfig extends NewAgentConfig { revision: number; } +export type AgentConfigSOAttributes = Omit; + export type FullAgentConfigDatasource = Pick< Datasource, 'id' | 'name' | 'namespace' | 'enabled' diff --git a/x-pack/plugins/ingest_manager/common/types/models/datasource.ts b/x-pack/plugins/ingest_manager/common/types/models/datasource.ts index 42ce69a5707cb..aa92b90a6caec 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/datasource.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/datasource.ts @@ -63,3 +63,5 @@ export interface Datasource extends Omit { created_at: string; created_by: string; } + +export type DatasourceSOAttributes = Omit; diff --git a/x-pack/plugins/ingest_manager/common/types/models/output.ts b/x-pack/plugins/ingest_manager/common/types/models/output.ts index f4ded50d7d474..f3e76cd167b3f 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/output.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/output.ts @@ -20,6 +20,8 @@ export interface NewOutput { config?: Record; } +export type OutputSOAttributes = NewOutput; + export type Output = NewOutput & { id: string; }; diff --git a/x-pack/plugins/ingest_manager/server/services/agent_config.ts b/x-pack/plugins/ingest_manager/server/services/agent_config.ts index 5d16b4a9a96a4..c90afaf5edb88 100644 --- a/x-pack/plugins/ingest_manager/server/services/agent_config.ts +++ b/x-pack/plugins/ingest_manager/server/services/agent_config.ts @@ -15,6 +15,7 @@ import { Datasource, NewAgentConfig, AgentConfig, + AgentConfigSOAttributes, FullAgentConfig, AgentConfigStatus, ListWithKuery, @@ -39,7 +40,7 @@ class AgentConfigService { private async _update( soClient: SavedObjectsClientContract, id: string, - agentConfig: Partial, + agentConfig: Partial, user?: AuthenticatedUser ): Promise { const oldAgentConfig = await this.get(soClient, id, false); @@ -57,7 +58,7 @@ class AgentConfigService { ); } - await soClient.update(SAVED_OBJECT_TYPE, id, { + await soClient.update(SAVED_OBJECT_TYPE, id, { ...agentConfig, revision: oldAgentConfig.revision + 1, updated_at: new Date().toISOString(), @@ -70,7 +71,7 @@ class AgentConfigService { } public async ensureDefaultAgentConfig(soClient: SavedObjectsClientContract) { - const configs = await soClient.find({ + const configs = await soClient.find({ type: AGENT_CONFIG_SAVED_OBJECT_TYPE, filter: `${AGENT_CONFIG_SAVED_OBJECT_TYPE}.attributes.is_default:true`, }); @@ -83,7 +84,10 @@ class AgentConfigService { return this.create(soClient, newDefaultAgentConfig); } - return configs.saved_objects[0].attributes; + return { + id: configs.saved_objects[0], + ...configs.saved_objects[0].attributes, + }; } public async create( @@ -91,7 +95,7 @@ class AgentConfigService { agentConfig: NewAgentConfig, options?: { id?: string; user?: AuthenticatedUser } ): Promise { - const newSo = await soClient.create( + const newSo = await soClient.create( SAVED_OBJECT_TYPE, { ...agentConfig, @@ -106,7 +110,7 @@ class AgentConfigService { await this.triggerAgentConfigUpdatedEvent(soClient, 'created', newSo.id); } - return newSo.attributes; + return { id: newSo.id, ...newSo.attributes }; } public async get( @@ -114,7 +118,7 @@ class AgentConfigService { id: string, withDatasources: boolean = true ): Promise { - const agentConfigSO = await soClient.get(SAVED_OBJECT_TYPE, id); + const agentConfigSO = await soClient.get(SAVED_OBJECT_TYPE, id); if (!agentConfigSO) { return null; } @@ -123,7 +127,7 @@ class AgentConfigService { throw new Error(agentConfigSO.error.message); } - const agentConfig = agentConfigSO.attributes; + const agentConfig = { id: agentConfigSO.id, ...agentConfigSO.attributes }; if (withDatasources) { agentConfig.datasources = @@ -142,7 +146,7 @@ class AgentConfigService { ): Promise<{ items: AgentConfig[]; total: number; page: number; perPage: number }> { const { page = 1, perPage = 20, kuery } = options; - const agentConfigs = await soClient.find({ + const agentConfigs = await soClient.find({ type: SAVED_OBJECT_TYPE, page, perPage, @@ -156,9 +160,10 @@ class AgentConfigService { }); return { - items: agentConfigs.saved_objects.map( - (agentConfigSO) => agentConfigSO.attributes - ), + items: agentConfigs.saved_objects.map((agentConfigSO) => ({ + id: agentConfigSO.id, + ...agentConfigSO.attributes, + })), total: agentConfigs.total, page, perPage, diff --git a/x-pack/plugins/ingest_manager/server/services/datasource.ts b/x-pack/plugins/ingest_manager/server/services/datasource.ts index 556a64cb9f503..c559dac0c0dcd 100644 --- a/x-pack/plugins/ingest_manager/server/services/datasource.ts +++ b/x-pack/plugins/ingest_manager/server/services/datasource.ts @@ -13,7 +13,7 @@ import { PackageInfo, } from '../../common'; import { DATASOURCE_SAVED_OBJECT_TYPE } from '../constants'; -import { NewDatasource, Datasource, ListWithKuery } from '../types'; +import { NewDatasource, Datasource, ListWithKuery, DatasourceSOAttributes } from '../types'; import { agentConfigService } from './agent_config'; import { getPackageInfo, getInstallation } from './epm/packages'; import { outputService } from './output'; @@ -32,7 +32,7 @@ class DatasourceService { options?: { id?: string; user?: AuthenticatedUser } ): Promise { const isoDate = new Date().toISOString(); - const newSo = await soClient.create>( + const newSo = await soClient.create( SAVED_OBJECT_TYPE, { ...datasource, @@ -57,7 +57,7 @@ class DatasourceService { } public async get(soClient: SavedObjectsClientContract, id: string): Promise { - const datasourceSO = await soClient.get(SAVED_OBJECT_TYPE, id); + const datasourceSO = await soClient.get(SAVED_OBJECT_TYPE, id); if (!datasourceSO) { return null; } @@ -66,14 +66,17 @@ class DatasourceService { throw new Error(datasourceSO.error.message); } - return datasourceSO.attributes; + return { + id: datasourceSO.id, + ...datasourceSO.attributes, + }; } public async getByIDs( soClient: SavedObjectsClientContract, ids: string[] ): Promise { - const datasourceSO = await soClient.bulkGet( + const datasourceSO = await soClient.bulkGet( ids.map((id) => ({ id, type: SAVED_OBJECT_TYPE, @@ -83,7 +86,10 @@ class DatasourceService { return null; } - return datasourceSO.saved_objects.map((so) => so.attributes); + return datasourceSO.saved_objects.map((so) => ({ + id: so.id, + ...so.attributes, + })); } public async list( @@ -92,7 +98,7 @@ class DatasourceService { ): Promise<{ items: Datasource[]; total: number; page: number; perPage: number }> { const { page = 1, perPage = 20, kuery } = options; - const datasources = await soClient.find({ + const datasources = await soClient.find({ type: SAVED_OBJECT_TYPE, page, perPage, @@ -106,7 +112,10 @@ class DatasourceService { }); return { - items: datasources.saved_objects.map((datasourceSO) => datasourceSO.attributes), + items: datasources.saved_objects.map((datasourceSO) => ({ + id: datasourceSO.id, + ...datasourceSO.attributes, + })), total: datasources.total, page, perPage, @@ -125,7 +134,7 @@ class DatasourceService { throw new Error('Datasource not found'); } - await soClient.update(SAVED_OBJECT_TYPE, id, { + await soClient.update(SAVED_OBJECT_TYPE, id, { ...datasource, revision: oldDatasource.revision + 1, updated_at: new Date().toISOString(), diff --git a/x-pack/plugins/ingest_manager/server/services/output.ts b/x-pack/plugins/ingest_manager/server/services/output.ts index b253214a38750..254edf112f1e2 100644 --- a/x-pack/plugins/ingest_manager/server/services/output.ts +++ b/x-pack/plugins/ingest_manager/server/services/output.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { SavedObjectsClientContract } from 'src/core/server'; -import { NewOutput, Output } from '../types'; +import { NewOutput, Output, OutputSOAttributes } from '../types'; import { DEFAULT_OUTPUT, OUTPUT_SAVED_OBJECT_TYPE } from '../constants'; import { appContextService } from './app_context'; import { decodeCloudId } from '../../common'; @@ -13,7 +13,7 @@ const SAVED_OBJECT_TYPE = OUTPUT_SAVED_OBJECT_TYPE; class OutputService { public async ensureDefaultOutput(soClient: SavedObjectsClientContract) { - const outputs = await soClient.find({ + const outputs = await soClient.find({ type: OUTPUT_SAVED_OBJECT_TYPE, filter: `${OUTPUT_SAVED_OBJECT_TYPE}.attributes.is_default:true`, }); @@ -44,7 +44,7 @@ class OutputService { id: string, data: Partial ) { - await soClient.update(SAVED_OBJECT_TYPE, id, data); + await soClient.update(SAVED_OBJECT_TYPE, id, data); } public async getDefaultOutputId(soClient: SavedObjectsClientContract) { @@ -67,7 +67,7 @@ class OutputService { } const so = await appContextService .getEncryptedSavedObjects() - ?.getDecryptedAsInternalUser(OUTPUT_SAVED_OBJECT_TYPE, defaultOutputId); + ?.getDecryptedAsInternalUser(OUTPUT_SAVED_OBJECT_TYPE, defaultOutputId); if (!so || !so.attributes.fleet_enroll_username || !so.attributes.fleet_enroll_password) { return null; @@ -84,27 +84,33 @@ class OutputService { output: NewOutput, options?: { id?: string } ): Promise { - const newSo = await soClient.create(SAVED_OBJECT_TYPE, output as Output, options); + const newSo = await soClient.create( + SAVED_OBJECT_TYPE, + output as Output, + options + ); return { + id: newSo.id, ...newSo.attributes, }; } public async get(soClient: SavedObjectsClientContract, id: string): Promise { - const outputSO = await soClient.get(SAVED_OBJECT_TYPE, id); + const outputSO = await soClient.get(SAVED_OBJECT_TYPE, id); if (outputSO.error) { throw new Error(outputSO.error.message); } return { + id: outputSO.id, ...outputSO.attributes, }; } public async update(soClient: SavedObjectsClientContract, id: string, data: Partial) { - const outputSO = await soClient.update(SAVED_OBJECT_TYPE, id, data); + const outputSO = await soClient.update(SAVED_OBJECT_TYPE, id, data); if (outputSO.error) { throw new Error(outputSO.error.message); @@ -112,7 +118,7 @@ class OutputService { } public async list(soClient: SavedObjectsClientContract) { - const outputs = await soClient.find({ + const outputs = await soClient.find({ type: SAVED_OBJECT_TYPE, page: 1, perPage: 1000, @@ -121,6 +127,7 @@ class OutputService { return { items: outputs.saved_objects.map((outputSO) => { return { + id: outputSO.id, ...outputSO.attributes, }; }), diff --git a/x-pack/plugins/ingest_manager/server/types/index.tsx b/x-pack/plugins/ingest_manager/server/types/index.tsx index e8ae8146d4fa2..2218d967fa8aa 100644 --- a/x-pack/plugins/ingest_manager/server/types/index.tsx +++ b/x-pack/plugins/ingest_manager/server/types/index.tsx @@ -19,14 +19,17 @@ export { AgentActionSOAttributes, Datasource, NewDatasource, + DatasourceSOAttributes, FullAgentConfigDatasource, FullAgentConfig, AgentConfig, + AgentConfigSOAttributes, NewAgentConfig, AgentConfigStatus, DataStream, Output, NewOutput, + OutputSOAttributes, OutputType, EnrollmentAPIKey, EnrollmentAPIKeySOAttributes,