Skip to content

Commit

Permalink
[Fleet] showing agent policy creation error message on UI (#125931)
Browse files Browse the repository at this point in the history
* showing agent policy creation error message on UI

* mapping the error instead of showing from the backend

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
juliaElastic and kibanamachine authored Feb 21, 2022
1 parent 88354aa commit 1df6ecf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const StyledEuiAccordion = styled(EuiAccordion)`
`;

interface Props {
updateAgentPolicy: (u: AgentPolicy | null) => void;
updateAgentPolicy: (u: AgentPolicy | null, errorMessage?: JSX.Element) => void;
isFleetServerPolicy?: boolean;
agentPolicyName: string;
}
Expand Down Expand Up @@ -84,12 +84,24 @@ export const AgentPolicyCreateInlineForm: React.FunctionComponent<Props> = ({
updateAgentPolicy(resp.data.item);
}
} catch (e) {
updateAgentPolicy(null);
updateAgentPolicy(null, mapError(e));
} finally {
setIsLoading(false);
}
}, [newAgentPolicy, withSysMonitoring, updateAgentPolicy]);

function mapError(e: { statusCode: number }): JSX.Element | undefined {
switch (e.statusCode) {
case 409:
return (
<FormattedMessage
id="xpack.fleet.agentPolicyCreation.errorMessage"
defaultMessage="An agent policy already exists with this name."
/>
);
}
}

return (
<EuiForm>
<EuiText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@
import React from 'react';
import { EuiSpacer, EuiCallOut } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

export enum CREATE_STATUS {
INITIAL = 'initial',
CREATED = 'created',
FAILED = 'failed',
}

export interface AgentPolicyCreateState {
status: CREATE_STATUS;
errorMessage?: JSX.Element;
}

interface Props {
createStatus: CREATE_STATUS;
createState: AgentPolicyCreateState;
}

export const AgentPolicyCreatedCallOut: React.FunctionComponent<Props> = ({ createStatus }) => {
export const AgentPolicyCreatedCallOut: React.FunctionComponent<Props> = ({ createState }) => {
return (
<>
<EuiSpacer size="m" />
{createStatus === CREATE_STATUS.CREATED ? (
{createState.status === CREATE_STATUS.CREATED ? (
<EuiCallOut
data-test-subj="agentPolicyCreateStatusCallOut"
title={
Expand All @@ -45,7 +51,9 @@ export const AgentPolicyCreatedCallOut: React.FunctionComponent<Props> = ({ crea
}
color="danger"
iconType="cross"
/>
>
{createState.errorMessage ?? null}
</EuiCallOut>
)}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import React, { useState, useCallback, useEffect } from 'react';

import type { AgentPolicyCreateState } from '../../applications/fleet/sections/agents/components';
import {
AgentPolicyCreatedCallOut,
CREATE_STATUS,
Expand Down Expand Up @@ -40,7 +41,9 @@ export const SelectCreateAgentPolicy: React.FC<Props> = ({
}) => {
const [showCreatePolicy, setShowCreatePolicy] = useState(agentPolicies.length === 0);

const [createStatus, setCreateStatus] = useState(CREATE_STATUS.INITIAL);
const [createState, setCreateState] = useState<AgentPolicyCreateState>({
status: CREATE_STATUS.INITIAL,
});

const [newName, setNewName] = useState(incrementPolicyName(agentPolicies, isFleetServerPolicy));

Expand All @@ -52,13 +55,13 @@ export const SelectCreateAgentPolicy: React.FC<Props> = ({
}, [agentPolicies, isFleetServerPolicy]);

const onAgentPolicyCreated = useCallback(
async (policy: AgentPolicy | null) => {
async (policy: AgentPolicy | null, errorMessage?: JSX.Element) => {
if (!policy) {
setCreateStatus(CREATE_STATUS.FAILED);
setCreateState({ status: CREATE_STATUS.FAILED, errorMessage });
return;
}
setShowCreatePolicy(false);
setCreateStatus(CREATE_STATUS.CREATED);
setCreateState({ status: CREATE_STATUS.CREATED });
if (onAgentPolicyChange) {
onAgentPolicyChange(policy.id, policy!);
}
Expand Down Expand Up @@ -88,8 +91,8 @@ export const SelectCreateAgentPolicy: React.FC<Props> = ({
isFleetServerPolicy={isFleetServerPolicy}
/>
)}
{createStatus !== CREATE_STATUS.INITIAL && (
<AgentPolicyCreatedCallOut createStatus={createStatus} />
{createState.status !== CREATE_STATUS.INITIAL && (
<AgentPolicyCreatedCallOut createState={createState} />
)}
</>
);
Expand Down

0 comments on commit 1df6ecf

Please sign in to comment.