From e54d2c0f115ab123f1ed5242eb317add32a20743 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Tue, 3 Nov 2020 15:06:48 -0500 Subject: [PATCH] [Ingest Manager] Replace logs/metrics strings with const (#82424) ## Summary Use new runtime & type values added in https://github.com/elastic/kibana/pull/82231 (related to #82188 ) instead of strings. Same results. Stronger type safety. Some before/after pics below: Screen Shot 2020-11-03 at 6 40 04 AM Screen Shot 2020-11-03 at 6 40 19 AM Screen Shot 2020-11-03 at 7 47 42 AM Screen Shot 2020-11-03 at 7 47 34 AM Screen Shot 2020-11-03 at 7 54 17 AM Screen Shot 2020-11-03 at 7 54 45 AM --- .../ingest_manager/common/types/models/agent_policy.ts | 4 ++-- .../sections/agent_policy/components/agent_policy_form.tsx | 7 ++++--- .../list_page/components/create_agent_policy.tsx | 3 ++- .../plugins/ingest_manager/server/services/agent_policy.ts | 5 +++-- .../ingest_manager/server/types/models/agent_policy.ts | 6 ++++-- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/ingest_manager/common/types/models/agent_policy.ts b/x-pack/plugins/ingest_manager/common/types/models/agent_policy.ts index 6685c725b5e7e..f43f65fb317f3 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/agent_policy.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/agent_policy.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { agentPolicyStatuses } from '../../constants'; -import { ValueOf } from '../../types'; +import { DataType, ValueOf } from '../../types'; import { PackagePolicy, PackagePolicyPackage } from './package_policy'; import { Output } from './output'; @@ -15,7 +15,7 @@ export interface NewAgentPolicy { namespace: string; description?: string; is_default?: boolean; - monitoring_enabled?: Array<'logs' | 'metrics'>; + monitoring_enabled?: Array>; } export interface AgentPolicy extends NewAgentPolicy { diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/components/agent_policy_form.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/components/agent_policy_form.tsx index 919b6d3669a6b..8a9ba9bae93a9 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/components/agent_policy_form.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/components/agent_policy_form.tsx @@ -23,6 +23,7 @@ import { import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import styled from 'styled-components'; +import { dataTypes } from '../../../../../../common'; import { NewAgentPolicy, AgentPolicy } from '../../../types'; import { isValidNamespace } from '../../../services'; import { AgentPolicyDeleteProvider } from './agent_policy_delete_provider'; @@ -211,7 +212,7 @@ export const AgentPolicyForm: React.FunctionComponent = ({ = ({ ), }, { - id: 'metrics', + id: dataTypes.Metrics, label: ( <> = ({ { logs: false, metrics: false } )} onChange={(id) => { - if (id !== 'logs' && id !== 'metrics') { + if (id !== dataTypes.Logs && id !== dataTypes.Metrics) { return; } diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/list_page/components/create_agent_policy.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/list_page/components/create_agent_policy.tsx index d2c3fc64aa9e6..f10f36174fe82 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/list_page/components/create_agent_policy.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_policy/list_page/components/create_agent_policy.tsx @@ -21,6 +21,7 @@ import { EuiFlyoutProps, EuiSpacer, } from '@elastic/eui'; +import { dataTypes } from '../../../../../../../common'; import { NewAgentPolicy, AgentPolicy } from '../../../../types'; import { useCapabilities, useCore, sendCreateAgentPolicy } from '../../../../hooks'; import { AgentPolicyForm, agentPolicyFormValidation } from '../../components'; @@ -44,7 +45,7 @@ export const CreateAgentPolicyFlyout: React.FunctionComponent = ({ description: '', namespace: 'default', is_default: undefined, - monitoring_enabled: ['logs', 'metrics'], + monitoring_enabled: Object.values(dataTypes), }); const [isLoading, setIsLoading] = useState(false); const [withSysMonitoring, setWithSysMonitoring] = useState(true); diff --git a/x-pack/plugins/ingest_manager/server/services/agent_policy.ts b/x-pack/plugins/ingest_manager/server/services/agent_policy.ts index e12a7890f0694..0fd41d074effa 100644 --- a/x-pack/plugins/ingest_manager/server/services/agent_policy.ts +++ b/x-pack/plugins/ingest_manager/server/services/agent_policy.ts @@ -25,6 +25,7 @@ import { Settings, agentPolicyStatuses, storedPackagePoliciesToAgentInputs, + dataTypes, } from '../../common'; import { AgentPolicyNameExistsError } from '../errors'; import { createAgentPolicyAction, listAgents } from './agents'; @@ -538,8 +539,8 @@ class AgentPolicyService { monitoring: { use_output: defaultOutput.name, enabled: true, - logs: agentPolicy.monitoring_enabled.indexOf('logs') >= 0, - metrics: agentPolicy.monitoring_enabled.indexOf('metrics') >= 0, + logs: agentPolicy.monitoring_enabled.includes(dataTypes.Logs), + metrics: agentPolicy.monitoring_enabled.includes(dataTypes.Metrics), }, }, } diff --git a/x-pack/plugins/ingest_manager/server/types/models/agent_policy.ts b/x-pack/plugins/ingest_manager/server/types/models/agent_policy.ts index 0155b0ddc2632..a054353e9c9e1 100644 --- a/x-pack/plugins/ingest_manager/server/types/models/agent_policy.ts +++ b/x-pack/plugins/ingest_manager/server/types/models/agent_policy.ts @@ -5,14 +5,16 @@ */ import { schema } from '@kbn/config-schema'; import { PackagePolicySchema, NamespaceSchema } from './package_policy'; -import { agentPolicyStatuses } from '../../../common'; +import { agentPolicyStatuses, dataTypes } from '../../../common'; const AgentPolicyBaseSchema = { name: schema.string({ minLength: 1 }), namespace: NamespaceSchema, description: schema.maybe(schema.string()), monitoring_enabled: schema.maybe( - schema.arrayOf(schema.oneOf([schema.literal('logs'), schema.literal('metrics')])) + schema.arrayOf( + schema.oneOf([schema.literal(dataTypes.Logs), schema.literal(dataTypes.Metrics)]) + ) ), };