diff --git a/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts b/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts index d318f964ccd11..13410561e497e 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts @@ -945,4 +945,69 @@ describe('Fleet - validatePackagePolicyConfig', () => { expect(res).toBeNull(); }); }); + + describe('Select', () => { + it('should return an error message if the value is not an option value', () => { + const res = validatePackagePolicyConfig( + { + type: 'select', + value: 'c', + }, + { + name: 'myvariable', + type: 'select', + options: [ + { value: 'a', text: 'A' }, + { value: 'b', text: 'B' }, + ], + }, + 'myvariable', + safeLoad + ); + + expect(res).toEqual(['Invalid value for select type']); + }); + + it('should accept a select with a valid value', () => { + const res = validatePackagePolicyConfig( + { + type: 'select', + value: 'b', + }, + { + name: 'myvariable', + type: 'select', + options: [ + { value: 'a', text: 'A' }, + { value: 'b', text: 'B' }, + ], + }, + 'myvariable', + safeLoad + ); + + expect(res).toBeNull(); + }); + + it('should accept a select with undefined value', () => { + const res = validatePackagePolicyConfig( + { + type: 'select', + value: undefined, + }, + { + name: 'myvariable', + type: 'select', + options: [ + { value: 'a', text: 'A' }, + { value: 'b', text: 'B' }, + ], + }, + 'myvariable', + safeLoad + ); + + expect(res).toBeNull(); + }); + }); }); diff --git a/x-pack/plugins/fleet/common/services/validate_package_policy.ts b/x-pack/plugins/fleet/common/services/validate_package_policy.ts index 82fb8c82da39c..0af746ad2453e 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.ts @@ -337,6 +337,16 @@ export const validatePackagePolicyConfig = ( } } + if (varDef.type === 'select' && parsedValue) { + if (!varDef.options?.map((o) => o.value).includes(parsedValue)) { + errors.push( + i18n.translate('xpack.fleet.packagePolicyValidation.invalidSelectValueErrorMessage', { + defaultMessage: 'Invalid value for select type', + }) + ); + } + } + return errors.length ? errors : null; }; diff --git a/x-pack/plugins/fleet/common/types/models/epm.ts b/x-pack/plugins/fleet/common/types/models/epm.ts index db3497d20c349..96977bc351b12 100644 --- a/x-pack/plugins/fleet/common/types/models/epm.ts +++ b/x-pack/plugins/fleet/common/types/models/epm.ts @@ -378,6 +378,7 @@ export type RegistryVarType = | 'integer' | 'bool' | 'password' + | 'select' | 'text' | 'yaml' | 'string' @@ -390,6 +391,7 @@ export enum RegistryVarsEntryKeys { required = 'required', show_user = 'show_user', multi = 'multi', + options = 'options', default = 'default', os = 'os', } @@ -405,6 +407,7 @@ export interface RegistryVarsEntry { [RegistryVarsEntryKeys.required]?: boolean; [RegistryVarsEntryKeys.show_user]?: boolean; [RegistryVarsEntryKeys.multi]?: boolean; + [RegistryVarsEntryKeys.options]?: Array<{ value: string; text: string }>; [RegistryVarsEntryKeys.default]?: string | string[] | boolean; [RegistryVarsEntryKeys.os]?: { [key: string]: { diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/components/steps/components/package_policy_input_var_field.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/components/steps/components/package_policy_input_var_field.tsx index e8a8bd16ed64e..283a87fdc9e3c 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/components/steps/components/package_policy_input_var_field.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/components/steps/components/package_policy_input_var_field.tsx @@ -17,6 +17,7 @@ import { EuiFieldPassword, EuiCodeBlock, EuiTextArea, + EuiSelect, } from '@elastic/eui'; import styled from 'styled-components'; @@ -58,7 +59,7 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{ isEditPage = false, }) => { const [isDirty, setIsDirty] = useState(false); - const { multi, required, type, title, name, description } = varDef; + const { multi, required, type, title, name, description, options } = varDef; const isInvalid = (isDirty || forceShowErrors) && !!varErrors; const errors = isInvalid ? varErrors : null; const fieldLabel = title || name; @@ -154,6 +155,10 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{ disabled={frozen} /> ); + case 'select': + return ( + onChange(e.target.value)} /> + ); default: return (