From 595e9c2d8d5d131f1ab2dfddfa2c33b8aa0a08cb Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Wed, 8 Jul 2020 14:08:53 -0400 Subject: [PATCH] [Ingest Manager] Fix agent config out of date display (#71103) --- .../common/openapi/spec_oas3.json | 3 -- .../common/types/models/agent.ts | 1 - .../sections/fleet/agent_list_page/index.tsx | 18 ++++++++--- .../server/saved_objects/index.ts | 1 - .../server/services/agent_config_update.ts | 6 +--- .../server/services/agents/reassign.ts | 1 - .../server/services/agents/update.ts | 32 ------------------- 7 files changed, 14 insertions(+), 48 deletions(-) diff --git a/x-pack/plugins/ingest_manager/common/openapi/spec_oas3.json b/x-pack/plugins/ingest_manager/common/openapi/spec_oas3.json index c374cbb3bb146..4b10dab5d1ae5 100644 --- a/x-pack/plugins/ingest_manager/common/openapi/spec_oas3.json +++ b/x-pack/plugins/ingest_manager/common/openapi/spec_oas3.json @@ -4146,9 +4146,6 @@ "config_revision": { "type": ["number", "null"] }, - "config_newest_revision": { - "type": "number" - }, "last_checkin": { "type": "string" }, 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 27f0c61685fd4..1f4718acc2c1f 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/agent.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/agent.ts @@ -81,7 +81,6 @@ interface AgentBase { default_api_key_id?: string; config_id?: string; config_revision?: number | null; - config_newest_revision?: number; last_checkin?: string; user_provided_metadata: AgentMetadata; local_metadata: AgentMetadata; diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx index ec58789becb72..30204603e764c 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx @@ -3,7 +3,7 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import React, { useState } from 'react'; +import React, { useState, useMemo } from 'react'; import { EuiBasicTable, EuiButton, @@ -25,7 +25,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage, FormattedRelative } from '@kbn/i18n/react'; import { CSSProperties } from 'styled-components'; import { AgentEnrollmentFlyout } from '../components'; -import { Agent } from '../../../types'; +import { Agent, AgentConfig } from '../../../types'; import { usePagination, useCapabilities, @@ -220,6 +220,13 @@ export const AgentListPage: React.FunctionComponent<{}> = () => { }); const agentConfigs = agentConfigsRequest.data ? agentConfigsRequest.data.items : []; + const agentConfigsIndexedById = useMemo(() => { + return agentConfigs.reduce((acc, config) => { + acc[config.id] = config; + + return acc; + }, {} as { [k: string]: AgentConfig }); + }, [agentConfigs]); const { isLoading: isAgentConfigsLoading } = agentConfigsRequest; const columns = [ @@ -271,9 +278,10 @@ export const AgentListPage: React.FunctionComponent<{}> = () => { )} - {agent.config_revision && - agent.config_newest_revision && - agent.config_newest_revision > agent.config_revision && ( + {agent.config_id && + agent.config_revision && + agentConfigsIndexedById[agent.config_id] && + agentConfigsIndexedById[agent.config_id].revision > agent.config_revision && ( diff --git a/x-pack/plugins/ingest_manager/server/saved_objects/index.ts b/x-pack/plugins/ingest_manager/server/saved_objects/index.ts index b47cf4f7e7c3b..a5b5cc4337908 100644 --- a/x-pack/plugins/ingest_manager/server/saved_objects/index.ts +++ b/x-pack/plugins/ingest_manager/server/saved_objects/index.ts @@ -64,7 +64,6 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = { last_updated: { type: 'date' }, last_checkin: { type: 'date' }, config_revision: { type: 'integer' }, - config_newest_revision: { type: 'integer' }, default_api_key_id: { type: 'keyword' }, default_api_key: { type: 'binary', index: false }, updated_at: { type: 'date' }, diff --git a/x-pack/plugins/ingest_manager/server/services/agent_config_update.ts b/x-pack/plugins/ingest_manager/server/services/agent_config_update.ts index 1cca165906732..3d40d128afda8 100644 --- a/x-pack/plugins/ingest_manager/server/services/agent_config_update.ts +++ b/x-pack/plugins/ingest_manager/server/services/agent_config_update.ts @@ -6,7 +6,7 @@ import { SavedObjectsClientContract } from 'src/core/server'; import { generateEnrollmentAPIKey, deleteEnrollmentApiKeyForConfigId } from './api_keys'; -import { updateAgentsForConfigId, unenrollForConfigId } from './agents'; +import { unenrollForConfigId } from './agents'; import { outputService } from './output'; export async function agentConfigUpdateEventHandler( @@ -26,10 +26,6 @@ export async function agentConfigUpdateEventHandler( }); } - if (action === 'updated') { - await updateAgentsForConfigId(soClient, configId); - } - if (action === 'deleted') { await unenrollForConfigId(soClient, configId); await deleteEnrollmentApiKeyForConfigId(soClient, configId); diff --git a/x-pack/plugins/ingest_manager/server/services/agents/reassign.ts b/x-pack/plugins/ingest_manager/server/services/agents/reassign.ts index f8142af376eb3..ecc2c987d04b6 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/reassign.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/reassign.ts @@ -23,6 +23,5 @@ export async function reassignAgent( await soClient.update(AGENT_SAVED_OBJECT_TYPE, agentId, { config_id: newConfigId, config_revision: null, - config_newest_revision: config.revision, }); } diff --git a/x-pack/plugins/ingest_manager/server/services/agents/update.ts b/x-pack/plugins/ingest_manager/server/services/agents/update.ts index ec7a42ff11b7a..11ad76fe81784 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/update.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/update.ts @@ -8,38 +8,6 @@ import { SavedObjectsClientContract } from 'src/core/server'; import { listAgents } from './crud'; import { AGENT_SAVED_OBJECT_TYPE } from '../../constants'; import { unenrollAgent } from './unenroll'; -import { agentConfigService } from '../agent_config'; - -export async function updateAgentsForConfigId( - soClient: SavedObjectsClientContract, - configId: string -) { - const config = await agentConfigService.get(soClient, configId); - if (!config) { - throw new Error('Config not found'); - } - let hasMore = true; - let page = 1; - while (hasMore) { - const { agents } = await listAgents(soClient, { - kuery: `${AGENT_SAVED_OBJECT_TYPE}.config_id:"${configId}"`, - page: page++, - perPage: 1000, - showInactive: true, - }); - if (agents.length === 0) { - hasMore = false; - break; - } - const agentUpdate = agents.map((agent) => ({ - id: agent.id, - type: AGENT_SAVED_OBJECT_TYPE, - attributes: { config_newest_revision: config.revision }, - })); - - await soClient.bulkUpdate(agentUpdate); - } -} export async function unenrollForConfigId(soClient: SavedObjectsClientContract, configId: string) { let hasMore = true;