From 2a3054fbe3ab454b47a4b77c27bd7a9cdcaa81b6 Mon Sep 17 00:00:00 2001 From: nnamdifrankie Date: Thu, 19 Mar 2020 12:56:45 -0400 Subject: [PATCH] EMT-248: review comments and some clean up --- .../ingest_manager/common/types/models/agent.ts | 9 +++++---- .../server/routes/agent/actions_handlers.test.ts | 9 ++++++--- .../ingest_manager/server/services/agents/actions.ts | 10 ++-------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/ingest_manager/common/types/models/agent.ts b/x-pack/plugins/ingest_manager/common/types/models/agent.ts index 78856873e97ca..bca8d80e1e5d5 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/agent.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/agent.ts @@ -14,15 +14,16 @@ export type AgentType = export type AgentStatus = 'offline' | 'error' | 'online' | 'inactive' | 'warning'; -export interface AgentAction extends SavedObjectAttributes { +export interface NewAgentAction { type: 'CONFIG_CHANGE' | 'DATA_DUMP' | 'RESUME' | 'PAUSE'; - id: string; - created_at: string; data?: string; sent_at?: string; } -export type NewAgentAction = Pick; +export interface AgentAction extends NewAgentAction, SavedObjectAttributes { + id: string; + created_at: string; +} export interface AgentEvent { type: 'STATE' | 'ERROR' | 'ACTION_RESULT' | 'ACTION'; diff --git a/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.test.ts b/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.test.ts index 9072cfc19659d..756e1b06dfba0 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.test.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.test.ts @@ -15,7 +15,10 @@ import { httpServerMock } from '../../../../../../src/core/server/http/http_serv import { ActionsService } from '../../services/agents'; import { AgentAction } from '../../../common/types/models'; import { postNewAgentActionHandlerBuilder } from './actions_handlers'; -import { PostNewAgentActionResponse } from '../../../common/types/rest_spec'; +import { + PostNewAgentActionRequest, + PostNewAgentActionResponse, +} from '../../../common/types/rest_spec'; describe('test actions handlers schema', () => { it('validate that new agent actions schema is valid', async () => { @@ -48,7 +51,7 @@ describe('test actions handlers', () => { }); it('should succeed on valid new agent action', async () => { - const mockRequest = httpServerMock.createKibanaRequest({ + const mockRequest = httpServerMock.createKibanaRequest(({ body: { action: { type: 'CONFIG_CHANGE', @@ -59,7 +62,7 @@ describe('test actions handlers', () => { params: { agentId: 'id', }, - }); + } as unknown) as PostNewAgentActionRequest); const agentAction = ({ type: 'CONFIG_CHANGE', diff --git a/x-pack/plugins/ingest_manager/server/services/agents/actions.ts b/x-pack/plugins/ingest_manager/server/services/agents/actions.ts index d2684e5066f3d..7de56a17d7e33 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/actions.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/actions.ts @@ -31,18 +31,12 @@ export async function updateAgentActions( } export function createAgentAction(createdAt: Date, newAgentAction: NewAgentAction): AgentAction { - const agentAction: object = { + const agentAction = { id: uuid.v4(), created_at: createdAt.toISOString(), }; - Object.assign(agentAction, ...keys(newAgentAction).map(key => ({ [key]: newAgentAction[key] }))); - - return agentAction as AgentAction; -} - -function keys(obj: O): Array { - return Object.keys(obj) as Array; + return Object.assign(agentAction, newAgentAction) as AgentAction; } export interface ActionsService {