From aaa72c087e37eb65b95ba267affe7653794afda9 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Sun, 1 Nov 2020 14:52:38 -0500 Subject: [PATCH 1/2] Replace OutputType enum with type. Add outputTypes array. --- x-pack/plugins/ingest_manager/common/constants/output.ts | 6 +++--- .../plugins/ingest_manager/common/types/models/output.ts | 5 ++--- .../services/agents/checkin/state_new_actions.test.ts | 4 ++-- .../plugins/ingest_manager/server/types/models/output.ts | 7 ++----- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/x-pack/plugins/ingest_manager/common/constants/output.ts b/x-pack/plugins/ingest_manager/common/constants/output.ts index ac2d6117be921..250b0ccb50d84 100644 --- a/x-pack/plugins/ingest_manager/common/constants/output.ts +++ b/x-pack/plugins/ingest_manager/common/constants/output.ts @@ -3,13 +3,13 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { OutputType } from '../types'; +import { NewOutput } from '../types'; export const OUTPUT_SAVED_OBJECT_TYPE = 'ingest-outputs'; -export const DEFAULT_OUTPUT = { +export const DEFAULT_OUTPUT: NewOutput = { name: 'default', is_default: true, - type: OutputType.Elasticsearch, + type: 'elasticsearch', hosts: [''], }; 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 a02c884270919..9103285c41a0e 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/output.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/output.ts @@ -4,9 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -export enum OutputType { - Elasticsearch = 'elasticsearch', -} +export const outputTypes = ['elasticsearch'] as const; +export type OutputType = typeof outputTypes[number]; export interface NewOutput { is_default: boolean; diff --git a/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts b/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts index 04f7f206b3bcb..da0105ab04c0f 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts @@ -10,7 +10,7 @@ import { createNewActionsSharedObservable, } from './state_new_actions'; import { getNewActionsSince } from '../actions'; -import { OutputType, Agent, AgentAction, AgentPolicyAction } from '../../../types'; +import { Agent, AgentAction, AgentPolicyAction } from '../../../types'; jest.mock('../../app_context', () => ({ appContextService: { @@ -128,7 +128,7 @@ describe('test agent checkin new action services', () => { id: 'policy1', outputs: { default: { - type: OutputType.Elasticsearch, + type: 'elasticsearch', hosts: [], ca_sha256: undefined, api_key: undefined, diff --git a/x-pack/plugins/ingest_manager/server/types/models/output.ts b/x-pack/plugins/ingest_manager/server/types/models/output.ts index c5aab6395adab..8f5ede623e3a0 100644 --- a/x-pack/plugins/ingest_manager/server/types/models/output.ts +++ b/x-pack/plugins/ingest_manager/server/types/models/output.ts @@ -4,14 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ import { schema } from '@kbn/config-schema'; - -export enum OutputType { - Elasticsearch = 'elasticsearch', -} +import { outputTypes } from '../../../common/types'; const OutputBaseSchema = { name: schema.string(), - type: schema.oneOf([schema.literal(OutputType.Elasticsearch)]), + type: schema.oneOf([schema.literal(outputTypes[0])]), hosts: schema.maybe(schema.arrayOf(schema.string())), api_key: schema.maybe(schema.string()), fleet_enroll_username: schema.maybe(schema.string()), From a4c8c9a33f5cf314d69a2b79ae8626d18b779dfd Mon Sep 17 00:00:00 2001 From: John Schulz Date: Mon, 2 Nov 2020 16:49:11 -0500 Subject: [PATCH 2/2] Switch to new 'as const' + typeof + ValueOf approach --- x-pack/plugins/ingest_manager/common/constants/output.ts | 6 +++++- .../plugins/ingest_manager/common/types/models/output.ts | 8 +++++--- .../services/agents/checkin/state_new_actions.test.ts | 3 ++- .../plugins/ingest_manager/server/types/models/output.ts | 4 ++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/ingest_manager/common/constants/output.ts b/x-pack/plugins/ingest_manager/common/constants/output.ts index 250b0ccb50d84..021a58b8dbc07 100644 --- a/x-pack/plugins/ingest_manager/common/constants/output.ts +++ b/x-pack/plugins/ingest_manager/common/constants/output.ts @@ -7,9 +7,13 @@ import { NewOutput } from '../types'; export const OUTPUT_SAVED_OBJECT_TYPE = 'ingest-outputs'; +export const outputType = { + Elasticsearch: 'elasticsearch', +} as const; + export const DEFAULT_OUTPUT: NewOutput = { name: 'default', is_default: true, - type: 'elasticsearch', + type: outputType.Elasticsearch, hosts: [''], }; 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 9103285c41a0e..f2b8f2e6f0491 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/output.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/output.ts @@ -4,13 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -export const outputTypes = ['elasticsearch'] as const; -export type OutputType = typeof outputTypes[number]; +import { outputType } from '../../constants'; +import type { ValueOf } from '../index'; + +export type OutputType = typeof outputType; export interface NewOutput { is_default: boolean; name: string; - type: OutputType; + type: ValueOf; hosts?: string[]; ca_sha256?: string; api_key?: string; diff --git a/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts b/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts index da0105ab04c0f..28ddf9704bd92 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.test.ts @@ -11,6 +11,7 @@ import { } from './state_new_actions'; import { getNewActionsSince } from '../actions'; import { Agent, AgentAction, AgentPolicyAction } from '../../../types'; +import { outputType } from '../../../../common/constants'; jest.mock('../../app_context', () => ({ appContextService: { @@ -128,7 +129,7 @@ describe('test agent checkin new action services', () => { id: 'policy1', outputs: { default: { - type: 'elasticsearch', + type: outputType.Elasticsearch, hosts: [], ca_sha256: undefined, api_key: undefined, diff --git a/x-pack/plugins/ingest_manager/server/types/models/output.ts b/x-pack/plugins/ingest_manager/server/types/models/output.ts index 8f5ede623e3a0..28255415d1ddb 100644 --- a/x-pack/plugins/ingest_manager/server/types/models/output.ts +++ b/x-pack/plugins/ingest_manager/server/types/models/output.ts @@ -4,11 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ import { schema } from '@kbn/config-schema'; -import { outputTypes } from '../../../common/types'; +import { outputType } from '../../../common/constants'; const OutputBaseSchema = { name: schema.string(), - type: schema.oneOf([schema.literal(outputTypes[0])]), + type: schema.oneOf([schema.literal(outputType.Elasticsearch)]), hosts: schema.maybe(schema.arrayOf(schema.string())), api_key: schema.maybe(schema.string()), fleet_enroll_username: schema.maybe(schema.string()),