Skip to content

Commit

Permalink
Handle preconfiguration errors with toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Apr 13, 2021
1 parent bea7b3a commit 71e3955
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

export interface PostIngestSetupResponse {
isInitialized: boolean;
preconfigurationError: { name: string; message: string } | undefined;
}
11 changes: 10 additions & 1 deletion x-pack/plugins/fleet/public/applications/fleet/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
sendSetup,
useBreadcrumbs,
useConfig,
useStartServices,
} from './hooks';
import { Error, Loading } from './components';
import { IntraAppStateProvider } from './hooks/use_intra_app_state';
Expand Down Expand Up @@ -59,6 +60,7 @@ const Panel = styled(EuiPanel)`

export const WithPermissionsAndSetup: React.FC = memo(({ children }) => {
useBreadcrumbs('base');
const { notifications } = useStartServices();

const [isPermissionsLoading, setIsPermissionsLoading] = useState<boolean>(false);
const [permissionsError, setPermissionsError] = useState<string>();
Expand All @@ -81,6 +83,13 @@ export const WithPermissionsAndSetup: React.FC = memo(({ children }) => {
if (setupResponse.error) {
setInitializationError(setupResponse.error);
}
if (setupResponse.data.preconfigurationError) {
notifications.toasts.addError(setupResponse.data.preconfigurationError, {
title: i18n.translate('xpack.fleet.setup.uiPreconfigurationErrorTitle', {
defaultMessage: 'Configuration error',
}),
});
}
} catch (err) {
setInitializationError(err);
}
Expand All @@ -92,7 +101,7 @@ export const WithPermissionsAndSetup: React.FC = memo(({ children }) => {
setPermissionsError('REQUEST_ERROR');
}
})();
}, []);
}, [notifications.toasts]);

if (isPermissionsLoading || permissionsError) {
return (
Expand Down
7 changes: 3 additions & 4 deletions x-pack/plugins/fleet/server/routes/setup/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export const createFleetSetupHandler: RequestHandler<
try {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;
await setupIngestManager(soClient, esClient);
const body = await setupIngestManager(soClient, esClient);
await setupFleet(soClient, esClient, {
forceRecreate: request.body?.forceRecreate ?? false,
});

return response.ok({
body: { isInitialized: true },
body,
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
Expand All @@ -81,8 +81,7 @@ export const FleetSetupHandler: RequestHandler = async (context, request, respon
const esClient = context.core.elasticsearch.client.asCurrentUser;

try {
const body: PostIngestSetupResponse = { isInitialized: true };
await setupIngestManager(soClient, esClient);
const body: PostIngestSetupResponse = await setupIngestManager(soClient, esClient);
return response.ok({
body,
});
Expand Down
24 changes: 15 additions & 9 deletions x-pack/plugins/fleet/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const FLEET_ENROLL_USERNAME = 'fleet_enroll';
const FLEET_ENROLL_ROLE = 'fleet_enroll';

export interface SetupStatus {
isIntialized: true | undefined;
isInitialized: boolean;
preconfigurationError: { name: string; message: string } | undefined;
}

export async function setupIngestManager(
Expand Down Expand Up @@ -86,14 +87,19 @@ async function createSetupSideEffects(

const policies = policiesOrUndefined ?? [];
const packages = packagesOrUndefined ?? [];
let preconfigurationError;

await ensurePreconfiguredPackagesAndPolicies(
soClient,
esClient,
policies,
packages,
defaultOutput
);
try {
await ensurePreconfiguredPackagesAndPolicies(
soClient,
esClient,
policies,
packages,
defaultOutput
);
} catch (e) {
preconfigurationError = { name: e.name, message: e.message };
}

// Ensure the predefined default policies AFTER loading preconfigured policies. This allows the kibana config
// to override the default agent policies.
Expand Down Expand Up @@ -170,7 +176,7 @@ async function createSetupSideEffects(

await ensureAgentActionPolicyChangeExists(soClient);

return { isIntialized: true };
return { isInitialized: true, preconfigurationError };
}

async function updateFleetRoleIfExists(esClient: ElasticsearchClient) {
Expand Down

0 comments on commit 71e3955

Please sign in to comment.