Skip to content

Commit

Permalink
[Fleet][Kafka][Fix] Validate number of events fields (elastic#165202)
Browse files Browse the repository at this point in the history
This PR adds field verification for `Partitioning` part of `Kafka`
output UI.
Might close elastic#165063



https://github.com/elastic/kibana/assets/29123534/1f0a3704-efec-40d7-9e3a-f5504ddd77af
  • Loading branch information
szwarckonrad authored Aug 31, 2023
1 parent be96350 commit 99032df
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const OutputFormKafkaPartitioning: React.FunctionComponent<{
defaultMessage="Number of events"
/>
}
{...inputs.kafkaPartitionTypeRandomInput.formRowProps}
>
<EuiFieldText
data-test-subj="settingsOutputsFlyout.kafkaPartitionTypeRandomInput"
Expand All @@ -73,6 +74,7 @@ export const OutputFormKafkaPartitioning: React.FunctionComponent<{
defaultMessage="Number of events"
/>
}
{...inputs.kafkaPartitionTypeRoundRobinInput.formRowProps}
>
<EuiFieldText
data-test-subj="settingsOutputsFlyout.kafkaPartitionTypeRoundRobinInput"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ export function validateKafkaClientId(value: string) {
];
}

export function validateKafkaPartitioningGroupEvents(value: string) {
const regex = /^[0-9]+$/;
return regex.test(value)
? undefined
: [
i18n.translate(
'xpack.fleet.settings.outputForm.kafkaPartitioningGroupEventsFormattingMessage',
{
defaultMessage: 'Number of events must be a number',
}
),
];
}

export function validateKafkaTopics(
topics: Array<{
topic: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
validateKafkaTopics,
validateKafkaClientId,
validateKafkaHosts,
validateKafkaPartitioningGroupEvents,
} from './output_form_validators';
import { confirmUpdate } from './confirm_update';

Expand Down Expand Up @@ -342,8 +343,10 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
);

const kafkaPartitionTypeRandomInput = useInput(
kafkaOutput?.random?.group_events ? `${kafkaOutput.random.group_events}` : undefined,
undefined,
kafkaOutput?.random?.group_events ? `${kafkaOutput.random.group_events}` : '1',
kafkaPartitionTypeInput.value === kafkaPartitionType.Random
? validateKafkaPartitioningGroupEvents
: undefined,
isDisabled('partition')
);
const kafkaPartitionTypeHashInput = useInput(
Expand All @@ -352,8 +355,10 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
isDisabled('partition')
);
const kafkaPartitionTypeRoundRobinInput = useInput(
kafkaOutput?.round_robin?.group_events ? `${kafkaOutput.round_robin.group_events}` : undefined,
undefined,
kafkaOutput?.round_robin?.group_events ? `${kafkaOutput.round_robin.group_events}` : '1',
kafkaPartitionTypeInput.value === kafkaPartitionType.RoundRobin
? validateKafkaPartitioningGroupEvents
: undefined,
isDisabled('partition')
);

Expand Down Expand Up @@ -492,6 +497,8 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
const sslCertificateValid = sslCertificateInput.validate();
const sslKeyValid = sslKeyInput.validate();
const diskQueuePathValid = diskQueuePathInput.validate();
const partitioningRandomGroupEventsValid = kafkaPartitionTypeRandomInput.validate();
const partitioningRoundRobinGroupEventsValid = kafkaPartitionTypeRoundRobinInput.validate();

if (isLogstash) {
// validate logstash
Expand All @@ -516,7 +523,9 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
kafkaDefaultTopicValid &&
kafkaTopicsValid &&
additionalYamlConfigValid &&
kafkaClientIDValid
kafkaClientIDValid &&
partitioningRandomGroupEventsValid &&
partitioningRoundRobinGroupEventsValid
);
} else {
// validate ES
Expand Down Expand Up @@ -546,6 +555,8 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
sslCertificateInput,
sslKeyInput,
diskQueuePathInput,
kafkaPartitionTypeRandomInput,
kafkaPartitionTypeRoundRobinInput,
isLogstash,
isKafka,
]);
Expand Down Expand Up @@ -674,7 +685,8 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
: {}),

partition: kafkaPartitionTypeInput.value,
...(kafkaPartitionTypeRandomInput.value
...(kafkaPartitionTypeInput.value === kafkaPartitionType.Random &&
kafkaPartitionTypeRandomInput.value
? {
random: {
group_events: parseIntegerIfStringDefined(
Expand All @@ -683,7 +695,8 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
},
}
: {}),
...(kafkaPartitionTypeRoundRobinInput.value
...(kafkaPartitionTypeInput.value === kafkaPartitionType.RoundRobin &&
kafkaPartitionTypeRoundRobinInput.value
? {
round_robin: {
group_events: parseIntegerIfStringDefined(
Expand All @@ -692,7 +705,8 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
},
}
: {}),
...(kafkaPartitionTypeHashInput.value
...(kafkaPartitionTypeInput.value === kafkaPartitionType.Hash &&
kafkaPartitionTypeHashInput.value
? {
hash: {
hash: kafkaPartitionTypeHashInput.value,
Expand Down

0 comments on commit 99032df

Please sign in to comment.