Skip to content

Commit

Permalink
[Fleet] Support editing bool variable in agent policy (#85070)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Dec 9, 2020
1 parent 3d8b95f commit 2355dde
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,15 @@ export interface RegistryElasticsearch {
'index_template.mappings'?: object;
}

export type RegistryVarType = 'integer' | 'bool' | 'password' | 'text' | 'yaml';
// EPR types this as `[]map[string]interface{}`
// which means the official/possible type is Record<string, any>
// but we effectively only see this shape
export interface RegistryVarsEntry {
name: string;
title?: string;
description?: string;
type: string;
type: RegistryVarType;
required?: boolean;
show_user?: boolean;
multi?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
import React, { useState, memo, useMemo } from 'react';
import ReactMarkdown from 'react-markdown';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiFormRow, EuiFieldText, EuiComboBox, EuiText, EuiCodeEditor } from '@elastic/eui';
import {
EuiFormRow,
EuiSwitch,
EuiFieldText,
EuiComboBox,
EuiText,
EuiCodeEditor,
} from '@elastic/eui';
import { RegistryVarsEntry } from '../../../../types';

import 'brace/mode/yaml';
Expand All @@ -23,6 +30,7 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{
const { multi, required, type, title, name, description } = varDef;
const isInvalid = (isDirty || forceShowErrors) && !!varErrors;
const errors = isInvalid ? varErrors : null;
const fieldLabel = title || name;

const field = useMemo(() => {
if (multi) {
Expand Down Expand Up @@ -59,6 +67,18 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{
/>
);
}
if (type === 'bool') {
return (
<EuiSwitch
label={fieldLabel}
checked={value}
showLabel={false}
onChange={(e) => onChange(e.target.checked)}
onBlur={() => setIsDirty(true)}
/>
);
}

return (
<EuiFieldText
isInvalid={isInvalid}
Expand All @@ -67,15 +87,18 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{
onBlur={() => setIsDirty(true)}
/>
);
}, [isInvalid, multi, onChange, type, value]);
}, [isInvalid, multi, onChange, type, value, fieldLabel]);

// Boolean cannot be optional by default set to false
const isOptional = type !== 'bool' && !required;

return (
<EuiFormRow
isInvalid={isInvalid}
error={errors}
label={title || name}
label={fieldLabel}
labelAppend={
!required ? (
isOptional ? (
<EuiText size="xs" color="subdued">
<FormattedMessage
id="xpack.fleet.createPackagePolicy.stepConfigure.inputVarFieldOptionalLabel"
Expand Down

0 comments on commit 2355dde

Please sign in to comment.