Skip to content

Commit

Permalink
[IngestManager] Convert AgentPolicyStatus from enum to type (#82186) (#…
Browse files Browse the repository at this point in the history
…82653)

## Summary

Expands on pattern added in #82188

Split `enum AgentPolicyStatus` into `type AgentPolicyStatus` and JS value `agentPolicyStatuses`. Still have type safety

<img width="604" alt="Screen Shot 2020-11-02 at 6 05 29 PM" src="https://user-images.githubusercontent.com/57655/97929572-7ba8d100-1d37-11eb-88db-3785381ebfbf.png">
<img width="931" alt="Screen Shot 2020-11-02 at 6 08 41 PM" src="https://user-images.githubusercontent.com/57655/97929574-7ba8d100-1d37-11eb-8db5-ee5d07d171c6.png">
<img width="605" alt="Screen Shot 2020-11-02 at 6 09 02 PM" src="https://user-images.githubusercontent.com/57655/97929575-7c416780-1d37-11eb-8297-e90ffe695161.png">

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
John Schulz and kibanamachine authored Nov 4, 2020
1 parent 2da2427 commit ec5163c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
15 changes: 11 additions & 4 deletions x-pack/plugins/ingest_manager/common/constants/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { AgentPolicyStatus, DefaultPackages } from '../types';

import { AgentPolicy, DefaultPackages } from '../types';
export const AGENT_POLICY_SAVED_OBJECT_TYPE = 'ingest-agent-policies';

export const DEFAULT_AGENT_POLICY = {
export const agentPolicyStatuses = {
Active: 'active',
Inactive: 'inactive',
} as const;

export const DEFAULT_AGENT_POLICY: Omit<
AgentPolicy,
'id' | 'updated_at' | 'updated_by' | 'revision'
> = {
name: 'Default policy',
namespace: 'default',
description: 'Default agent policy created by Kibana',
status: AgentPolicyStatus.Active,
status: agentPolicyStatuses.Active,
package_policies: [],
is_default: true,
monitoring_enabled: ['logs', 'metrics'] as Array<'logs' | 'metrics'>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { agentPolicyStatuses } from '../../constants';
import { ValueOf } from '../../types';
import { PackagePolicy, PackagePolicyPackage } from './package_policy';
import { Output } from './output';

export enum AgentPolicyStatus {
Active = 'active',
Inactive = 'inactive',
}
export type AgentPolicyStatus = typeof agentPolicyStatuses;

export interface NewAgentPolicy {
name: string;
Expand All @@ -21,7 +20,7 @@ export interface NewAgentPolicy {

export interface AgentPolicy extends NewAgentPolicy {
id: string;
status: AgentPolicyStatus;
status: ValueOf<AgentPolicyStatus>;
package_policies: string[] | PackagePolicy[];
updated_at: string;
updated_by: string;
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import {
AgentPolicy,
AgentPolicySOAttributes,
FullAgentPolicy,
AgentPolicyStatus,
ListWithKuery,
} from '../types';
import {
DeleteAgentPolicyResponse,
Settings,
agentPolicyStatuses,
storedPackagePoliciesToAgentInputs,
} from '../../common';
import { AgentPolicyNameExistsError } from '../errors';
Expand Down Expand Up @@ -61,8 +61,8 @@ class AgentPolicyService {
}

if (
oldAgentPolicy.status === AgentPolicyStatus.Inactive &&
agentPolicy.status !== AgentPolicyStatus.Active
oldAgentPolicy.status === agentPolicyStatuses.Inactive &&
agentPolicy.status !== agentPolicyStatuses.Active
) {
throw new Error(
`Agent policy ${id} cannot be updated because it is ${oldAgentPolicy.status}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { schema } from '@kbn/config-schema';
import { PackagePolicySchema, NamespaceSchema } from './package_policy';
import { AgentPolicyStatus } from '../../../common';
import { agentPolicyStatuses } from '../../../common';

const AgentPolicyBaseSchema = {
name: schema.string({ minLength: 1 }),
Expand All @@ -24,8 +24,8 @@ export const AgentPolicySchema = schema.object({
...AgentPolicyBaseSchema,
id: schema.string(),
status: schema.oneOf([
schema.literal(AgentPolicyStatus.Active),
schema.literal(AgentPolicyStatus.Inactive),
schema.literal(agentPolicyStatuses.Active),
schema.literal(agentPolicyStatuses.Inactive),
]),
package_policies: schema.oneOf([
schema.arrayOf(schema.string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import {
GetPackagesResponse,
} from '../../../ingest_manager/common/types/rest_spec';
import {
AgentPolicyStatus,
EsAssetReference,
InstallationStatus,
KibanaAssetReference,
} from '../../../ingest_manager/common/types/models';
import { agentPolicyStatuses } from '../../../ingest_manager/common/constants';
import { firstNonNullValue } from './models/ecs_safety_helpers';

export type Event = AlertEvent | SafeEndpointEvent;
Expand Down Expand Up @@ -1235,7 +1235,7 @@ export class EndpointDocGenerator {
return {
id: this.seededUUIDv4(),
name: 'Agent Policy',
status: AgentPolicyStatus.Active,
status: agentPolicyStatuses.Active,
description: 'Some description',
namespace: 'default',
monitoring_enabled: ['logs', 'metrics'],
Expand Down

0 comments on commit ec5163c

Please sign in to comment.