From 58c342001b0962b47d5127c4c46292941aa38d88 Mon Sep 17 00:00:00 2001 From: nnamdifrankie Date: Thu, 19 Mar 2020 15:49:55 -0400 Subject: [PATCH] EMT-248: use core client --- .../server/routes/agent/actions_handlers.test.ts | 9 +++++++-- .../server/routes/agent/actions_handlers.ts | 2 +- .../plugins/ingest_manager/server/routes/agent/index.ts | 1 - .../server/services/agents/actions.test.ts | 4 ++++ .../ingest_manager/server/services/agents/actions.ts | 4 +--- 5 files changed, 13 insertions(+), 7 deletions(-) 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 ade4030a0f5e5..a20ba4a880537 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 @@ -78,13 +78,18 @@ describe('test actions handlers', () => { getAgent: jest.fn().mockReturnValueOnce({ id: 'agent', }), - getSavedObjectsClientContract: jest.fn().mockReturnValueOnce(mockSavedObjectsClient), updateAgentActions: jest.fn().mockReturnValueOnce(agentAction), } as jest.Mocked; const postNewAgentActionHandler = postNewAgentActionHandlerBuilder(actionsService); await postNewAgentActionHandler( - ({} as unknown) as RequestHandlerContext, + ({ + core: { + savedObjects: { + client: mockSavedObjectsClient, + }, + }, + } as unknown) as RequestHandlerContext, mockRequest, mockResponse ); diff --git a/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.ts b/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.ts index 6ea8695e27e71..2b9c230803593 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.ts @@ -22,7 +22,7 @@ export const postNewAgentActionHandlerBuilder = function( > { return async (context, request, response) => { try { - const soClient = actionsService.getSavedObjectsClientContract(request); + const soClient = context.core.savedObjects.client; const agent = await actionsService.getAgent(soClient, request.params.agentId); diff --git a/x-pack/plugins/ingest_manager/server/routes/agent/index.ts b/x-pack/plugins/ingest_manager/server/routes/agent/index.ts index d94029b561001..d461027017842 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/index.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/index.ts @@ -122,7 +122,6 @@ export const registerRoutes = (router: IRouter) => { }, postNewAgentActionHandlerBuilder({ getAgent: AgentService.getAgent, - getSavedObjectsClientContract: getInternalUserSOClient, updateAgentActions: AgentService.updateAgentActions, }) ); diff --git a/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts b/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts index c12108b0107e6..b500aeb825fec 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts @@ -16,11 +16,13 @@ interface UpdatedActions { describe('test agent actions services', () => { it('should update agent current actions with new action', async () => { const mockSavedObjectsClient = savedObjectsClientMock.create(); + const newAgentAction: NewAgentAction = { type: 'CONFIG_CHANGE', data: 'data', sent_at: '2020-03-14T19:45:02.620Z', }; + await updateAgentActions( mockSavedObjectsClient, ({ @@ -41,6 +43,7 @@ describe('test agent actions services', () => { const updatedAgentActions = (mockSavedObjectsClient.update.mock .calls[0][2] as unknown) as UpdatedActions; + expect(updatedAgentActions.actions.length).toEqual(2); const actualAgentAction = updatedAgentActions.actions.find(action => action?.data === 'data'); expect(actualAgentAction?.type).toEqual(newAgentAction.type); @@ -56,6 +59,7 @@ describe('test agent actions services', () => { }; const now = new Date(); const agentAction = createAgentAction(now, newAgentAction); + expect(agentAction.type).toEqual(newAgentAction.type); expect(agentAction.data).toEqual(newAgentAction.data); expect(agentAction.sent_at).toEqual(newAgentAction.sent_at); 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 6f7d61ed3eed0..2f8ed9f504453 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/actions.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/actions.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { KibanaRequest, SavedObjectsClientContract } from 'kibana/server'; +import { SavedObjectsClientContract } from 'kibana/server'; import uuid from 'uuid'; import { Agent, @@ -42,8 +42,6 @@ export function createAgentAction(createdAt: Date, newAgentAction: NewAgentActio export interface ActionsService { getAgent: (soClient: SavedObjectsClientContract, agentId: string) => Promise; - getSavedObjectsClientContract: (kibanaRequest: KibanaRequest) => SavedObjectsClientContract; - updateAgentActions: ( soClient: SavedObjectsClientContract, agent: Agent,