Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest Pipelines] Load from json #70297

Merged
merged 13 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, { FunctionComponent } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';

import { usePipelineProcessorsContext } from '../context';
import { usePipelineProcessorsContext } from '../pipeline_processors_editor/context';

export const OnFailureProcessorsTitle: FunctionComponent = () => {
const { links } = usePipelineProcessorsContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import { FormattedMessage } from '@kbn/i18n/react';
import { EuiButton, EuiButtonEmpty, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';

import { useForm, Form, FormConfig } from '../../../shared_imports';
import { Pipeline } from '../../../../common/types';
import { Pipeline, Processor } from '../../../../common/types';

import {
OnUpdateHandlerArg,
OnUpdateHandler,
SerializeResult,
} from '../pipeline_processors_editor';
import './pipeline_form.scss';

import { OnUpdateHandlerArg, OnUpdateHandler } from '../pipeline_processors_editor';

import { PipelineRequestFlyout } from './pipeline_request_flyout';
import { PipelineTestFlyout } from './pipeline_test_flyout';
import { PipelineFormFields } from './pipeline_form_fields';
import { PipelineFormError } from './pipeline_form_error';
import { pipelineFormSchema } from './schema';
import { PipelineForm as IPipelineForm } from './types';

export interface PipelineFormProps {
onSave: (pipeline: Pipeline) => void;
Expand All @@ -32,14 +31,15 @@ export interface PipelineFormProps {
isEditing?: boolean;
}

const defaultFormValue: Pipeline = Object.freeze({
name: '',
description: '',
processors: [],
on_failure: [],
});

export const PipelineForm: React.FunctionComponent<PipelineFormProps> = ({
defaultValue = {
name: '',
description: '',
processors: [],
on_failure: [],
version: '',
},
defaultValue = defaultFormValue,
onSave,
isSaving,
saveError,
Expand All @@ -50,34 +50,42 @@ export const PipelineForm: React.FunctionComponent<PipelineFormProps> = ({

const [isTestingPipeline, setIsTestingPipeline] = useState<boolean>(false);

const processorStateRef = useRef<OnUpdateHandlerArg>();
const {
processors: initialProcessors,
on_failure: initialOnFailureProcessors,
...defaultFormValues
} = defaultValue;

const [processorsState, setProcessorsState] = useState<{
processors: Processor[];
onFailure?: Processor[];
}>({
processors: initialProcessors,
onFailure: initialOnFailureProcessors,
});

const handleSave: FormConfig['onSubmit'] = async (formData, isValid) => {
let override: SerializeResult | undefined;
const processorStateRef = useRef<OnUpdateHandlerArg>();

const handleSave: FormConfig<IPipelineForm>['onSubmit'] = async (formData, isValid) => {
if (!isValid) {
return;
}

if (processorStateRef.current) {
const processorsState = processorStateRef.current;
if (await processorsState.validate()) {
override = processorsState.getData();
} else {
return;
const state = processorStateRef.current;
jloleysens marked this conversation as resolved.
Show resolved Hide resolved
if (await state.validate()) {
onSave({ ...formData, ...state.getData() });
}
}

onSave({ ...formData, ...(override || {}) } as Pipeline);
};

const handleTestPipelineClick = () => {
setIsTestingPipeline(true);
};

const { form } = useForm({
const { form } = useForm<IPipelineForm>({
schema: pipelineFormSchema,
defaultValue,
defaultValue: defaultFormValues,
onSubmit: handleSave,
});

Expand Down Expand Up @@ -121,9 +129,12 @@ export const PipelineForm: React.FunctionComponent<PipelineFormProps> = ({

{/* All form fields */}
<PipelineFormFields
onLoadJson={({ processors, on_failure: onFailure }) => {
setProcessorsState({ processors, onFailure });
}}
onEditorFlyoutOpen={onEditorFlyoutOpen}
initialProcessors={defaultValue.processors}
initialOnFailureProcessors={defaultValue.on_failure}
processors={processorsState.processors}
onFailure={processorsState.onFailure}
onProcessorsUpdate={onProcessorsChangeHandler}
hasVersion={Boolean(defaultValue.version)}
isTestButtonDisabled={isTestingPipeline || form.isValid === false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@

import React, { useState } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiSpacer, EuiSwitch } from '@elastic/eui';
import { EuiSpacer, EuiSwitch, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';

import { Processor } from '../../../../common/types';
import { FormDataProvider } from '../../../shared_imports';
import { PipelineProcessorsEditor, OnUpdateHandler } from '../pipeline_processors_editor';

import { getUseField, getFormRow, Field, useKibana } from '../../../shared_imports';

import {
PipelineProcessorsContextProvider,
GlobalOnFailureProcessorsEditor,
ProcessorsEditor,
OnUpdateHandler,
OnDoneLoadJsonHandler,
} from '../pipeline_processors_editor';

import { ProcessorsHeader } from './processors_header';
import { OnFailureProcessorsTitle } from './on_failure_processors_title';

interface Props {
initialProcessors: Processor[];
initialOnFailureProcessors?: Processor[];
processors: Processor[];
onFailure?: Processor[];
onLoadJson: OnDoneLoadJsonHandler;
onProcessorsUpdate: OnUpdateHandler;
hasVersion: boolean;
isTestButtonDisabled: boolean;
Expand All @@ -29,8 +39,9 @@ const UseField = getUseField({ component: Field });
const FormRow = getFormRow({ titleTag: 'h3' });

export const PipelineFormFields: React.FunctionComponent<Props> = ({
initialProcessors,
initialOnFailureProcessors,
processors,
onFailure,
onLoadJson,
onProcessorsUpdate,
isEditing,
hasVersion,
Expand Down Expand Up @@ -113,30 +124,37 @@ export const PipelineFormFields: React.FunctionComponent<Props> = ({
</FormRow>

{/* Pipeline Processors Editor */}
<FormDataProvider pathsToWatch={['processors', 'on_failure']}>
{({ processors, on_failure: onFailure }) => {
const processorProp =
typeof processors === 'string' && processors
? JSON.parse(processors)
: initialProcessors ?? [];

const onFailureProp =
typeof onFailure === 'string' && onFailure
? JSON.parse(onFailure)
: initialOnFailureProcessors ?? [];

return (
<PipelineProcessorsEditor
onFlyoutOpen={onEditorFlyoutOpen}
esDocsBasePath={services.documentation.getEsDocsBasePath()}
isTestButtonDisabled={isTestButtonDisabled}
onTestPipelineClick={onTestPipelineClick}
onUpdate={onProcessorsUpdate}
value={{ processors: processorProp, onFailure: onFailureProp }}
/>
);
}}
</FormDataProvider>
<PipelineProcessorsContextProvider
onFlyoutOpen={onEditorFlyoutOpen}
links={{ esDocsBasePath: services.documentation.getEsDocsBasePath() }}
onUpdate={onProcessorsUpdate}
value={{ processors, onFailure }}
>
<div className="pipelineProcessorsEditor">
<EuiFlexGroup gutterSize="m" responsive={false} direction="column">
<EuiFlexItem grow={false}>
<ProcessorsHeader
onLoadJson={onLoadJson}
onTestPipelineClick={onTestPipelineClick}
isTestButtonDisabled={isTestButtonDisabled}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<ProcessorsEditor />
</EuiFlexItem>
<EuiFlexItem>
<EuiSpacer size="s" />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<OnFailureProcessorsTitle />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<GlobalOnFailureProcessorsEditor />
</EuiFlexItem>
</EuiFlexGroup>
</div>
</PipelineProcessorsContextProvider>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiLink, EuiText, EuiTitle } from
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';

import { usePipelineProcessorsContext } from '../context';
import { usePipelineProcessorsContext } from '../pipeline_processors_editor/context';

import { LoadFromJsonButton, OnDoneLoadJsonHandler } from '../pipeline_processors_editor';

export interface Props {
onTestPipelineClick: () => void;
isTestButtonDisabled: boolean;
onLoadJson: OnDoneLoadJsonHandler;
}

export const ProcessorsTitleAndTestButton: FunctionComponent<Props> = ({
export const ProcessorsHeader: FunctionComponent<Props> = ({
onTestPipelineClick,
isTestButtonDisabled,
onLoadJson,
}) => {
const { links } = usePipelineProcessorsContext();
return (
<EuiFlexGroup
alignItems="center"
gutterSize="none"
gutterSize="s"
justifyContent="spaceBetween"
responsive={false}
>
Expand Down Expand Up @@ -55,6 +59,9 @@ export const ProcessorsTitleAndTestButton: FunctionComponent<Props> = ({
/>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<LoadFromJsonButton onDone={onLoadJson} />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
data-test-subj="testPipelineButton"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';
import { FormSchema, FIELD_TYPES, fieldValidators, fieldFormatters } from '../../../shared_imports';

import { PipelineForm } from './types';

const { emptyField } = fieldValidators;
const { toInt } = fieldFormatters;

export const pipelineFormSchema: FormSchema<PipelineForm> = {
name: {
type: FIELD_TYPES.TEXT,
label: i18n.translate('xpack.ingestPipelines.form.nameFieldLabel', {
defaultMessage: 'Name',
}),
validations: [
{
validator: emptyField(
i18n.translate('xpack.ingestPipelines.form.pipelineNameRequiredError', {
defaultMessage: 'Name is required.',
})
),
},
],
},
description: {
type: FIELD_TYPES.TEXTAREA,
label: i18n.translate('xpack.ingestPipelines.form.descriptionFieldLabel', {
defaultMessage: 'Description (optional)',
}),
},
version: {
type: FIELD_TYPES.NUMBER,
label: i18n.translate('xpack.ingestPipelines.form.versionFieldLabel', {
defaultMessage: 'Version (optional)',
}),
formatters: [toInt],
},
};
Loading