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

[Fleet] Add fleet server host UI #142894

Merged
merged 26 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
60ee8aa
[Fleet] Add UI to edit Fleet server hosts
nchaulet Oct 6, 2022
583be59
Fix jest tests
criamico Oct 11, 2022
3aa0060
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 11, 2022
8ea731b
Fix translations
criamico Oct 11, 2022
f38eccb
Add agent policy handling
criamico Oct 11, 2022
0ff3209
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 12, 2022
ed388be
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 12, 2022
582e9f7
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 17, 2022
bad7b3a
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 19, 2022
bf0aa59
Update quick start form
criamico Oct 19, 2022
5b4e681
Update advanced form
criamico Oct 24, 2022
5a51794
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 24, 2022
917aeef
Fix tests
nchaulet Oct 25, 2022
28b1596
Merge branch 'main' of github.com:elastic/kibana into feature-fleet-s…
nchaulet Oct 25, 2022
025d217
Fix checks and translations
criamico Oct 25, 2022
469fd89
Fix socket hangup error
nchaulet Oct 25, 2022
b308e89
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Oct 25, 2022
823c917
Fix cypress tests
criamico Oct 26, 2022
8b4be95
Fix API request in cypress test
criamico Oct 26, 2022
b2b2707
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 26, 2022
4e5b01a
Fix last failing test
criamico Oct 27, 2022
d00c606
Add timeout
criamico Oct 27, 2022
d2c3b1b
Fix tests
nchaulet Oct 27, 2022
c177dfb
Fix tests
nchaulet Oct 27, 2022
cde1379
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 28, 2022
234bb69
Merge branch 'main' into feature-fleet-server-host-ui
kibanamachine Oct 28, 2022
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
Prev Previous commit
Next Next commit
Update quick start form
  • Loading branch information
criamico committed Oct 19, 2022
commit bf0aa594cbc5cb42f4698e5bca4e112b00e1e8c1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const AdvancedTab: React.FunctionComponent<AdvancedTabProps> = ({ selecte
getInstallFleetServerStep({
isFleetServerReady,
serviceToken,
fleetServerHost: fleetServerHostForm.fleetServerHost,
fleetServerHost: fleetServerHostForm.fleetServerHost?.host_urls[0],
fleetServerPolicyId: fleetServerPolicyId || selectedPolicyId,
deploymentMode,
disabled: !Boolean(serviceToken),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
*/
import React, { useState } from 'react';
import type { EuiComboBoxOptionOption } from '@elastic/eui';

import { EuiComboBox, EuiText } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { i18n } from '@kbn/i18n';

import type { FleetServerHost } from '../../../types';

interface Props {
fleetServerHost: string | undefined;
fleetServerHost: FleetServerHost | undefined;
fleetServerHostSettings: string[];
isDisabled: boolean;
isInvalid: boolean;
Expand Down Expand Up @@ -42,7 +45,6 @@ export const FleetServerHostComboBox: React.FunctionComponent<Props> = ({
setCreatedOptions([...createdOptions, option]);
onFleetServerHostChange(option);
};

return (
<EuiComboBox<string>
fullWidth
Expand All @@ -57,7 +59,11 @@ export const FleetServerHostComboBox: React.FunctionComponent<Props> = ({
values: { searchValuePlaceholder: '{searchValue}' },
}
)}
selectedOptions={fleetServerHost ? [{ label: fleetServerHost, value: fleetServerHost }] : []}
selectedOptions={
fleetServerHost
? [{ label: fleetServerHost.host_urls[0], value: fleetServerHost.host_urls[0] }]
: []
}
prepend={
<EuiText>
<FormattedMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,82 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { useCallback, useEffect, useState } from 'react';

import { sendPutSettings, useGetSettings } from '../../../hooks';
import { intersection } from 'lodash';

const URL_REGEX = /^(https):\/\/[^\s$.?#].[^\s]*$/gm;
import { sendPostFleetServerHost, useGetFleetServerHosts } from '../../../hooks';
import type { FleetServerHost } from '../../../types';

export interface FleetServerHostForm {
saveFleetServerHost: () => Promise<void>;
fleetServerHost?: string;
saveFleetServerHost: (host: FleetServerHost) => Promise<void>;
fleetServerHost?: FleetServerHost;
fleetServerHostSettings: string[];
isFleetServerHostSubmitted: boolean;
setFleetServerHost: React.Dispatch<React.SetStateAction<string | undefined>>;
setFleetServerHost: React.Dispatch<React.SetStateAction<FleetServerHost | undefined>>;
error?: string;
validateFleetServerHost: () => boolean;
}

export const useFleetServerHost = (): FleetServerHostForm => {
const [fleetServerHost, setFleetServerHost] = useState<string>();
const [fleetServerHost, setFleetServerHost] = useState<FleetServerHost>();
const [isFleetServerHostSubmitted, setIsFleetServerHostSubmitted] = useState<boolean>(false);
const [error, setError] = useState<string>();

const { data: settings } = useGetSettings();
const { data } = useGetFleetServerHosts();

useEffect(() => {
const settingsFleetServerHosts = settings?.item.fleet_server_hosts ?? [];

if (settingsFleetServerHosts.length) {
setFleetServerHost(settingsFleetServerHosts[0]);
const fleetServerHosts = data?.items ?? [];
const defaultHost = fleetServerHosts.find((item) => item.is_default === true);

// Get the default host, otherwise the first fleet server found
if (defaultHost) {
setFleetServerHost(defaultHost);
} else {
setFleetServerHost(fleetServerHosts[0]);
}
}, [settings?.item.fleet_server_hosts]);

const validateFleetServerHost = useCallback(() => {
if (!fleetServerHost) {
setError(
i18n.translate('xpack.fleet.fleetServerHost.requiredError', {
defaultMessage: 'Fleet server host is required.',
})
);

return false;
} else if (!fleetServerHost.startsWith('https')) {
setError(
i18n.translate('xpack.fleet.fleetServerHost.requiresHttpsError', {
defaultMessage: 'Fleet server host must begin with "https"',
})
);

return false;
} else if (!fleetServerHost.match(URL_REGEX)) {
setError(
i18n.translate('xpack.fleet.fleetServerSetup.addFleetServerHostInvalidUrlError', {
defaultMessage: 'Invalid URL',
})
);

return false;
}

return true;
}, [fleetServerHost]);

const saveFleetServerHost = useCallback(async () => {
setIsFleetServerHostSubmitted(false);

if (!validateFleetServerHost()) {
return;
}

// If the Fleet Server host provided already exists in settings, don't submit it
if (settings?.item.fleet_server_hosts.includes(fleetServerHost!)) {
}, [data?.items, fleetServerHost]);

const saveFleetServerHost = useCallback(
async (newFleetServerHost: FleetServerHost) => {
setIsFleetServerHostSubmitted(false);
setFleetServerHost(newFleetServerHost);

// if (!isValid) {
// console.log('not valid, return');
// return;
// }

const fleetServerHostExists = data?.items.reduce((acc, curr) => {
const hostsIntersection = intersection(curr.host_urls, newFleetServerHost?.host_urls);
return hostsIntersection.length > 0 || acc;
}, false);

// If the Fleet Server host provided already exists in settings, don't submit it
if (fleetServerHostExists) {
setIsFleetServerHostSubmitted(true);
return;
}
if (newFleetServerHost) {
const res = await sendPostFleetServerHost({
name: newFleetServerHost?.name,
host_urls: newFleetServerHost?.host_urls,
is_default: newFleetServerHost?.is_default,
});
if (res.error) {
throw res.error;
}
}
setIsFleetServerHostSubmitted(true);
return;
}

const res = await sendPutSettings({
fleet_server_hosts: [fleetServerHost!, ...(settings?.item.fleet_server_hosts || [])],
});

if (res.error) {
throw res.error;
}

setIsFleetServerHostSubmitted(true);
}, [fleetServerHost, settings?.item.fleet_server_hosts, validateFleetServerHost]);
},
[data?.items]
);

return {
saveFleetServerHost,
fleetServerHost,
fleetServerHostSettings: settings?.item.fleet_server_hosts ?? [],
fleetServerHostSettings: data?.items[0]?.host_urls ?? [],
isFleetServerHostSubmitted,
setFleetServerHost,
error,
validateFleetServerHost,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@
import { useState, useCallback, useEffect } from 'react';
import { i18n } from '@kbn/i18n';

import { sendCreateAgentPolicy, sendGetOneAgentPolicy, useStartServices } from '../../../hooks';
import {
sendCreateAgentPolicy,
sendGetOneAgentPolicy,
useStartServices,
useComboInput,
useInput,
} from '../../../hooks';

import type { NewAgentPolicy } from '../../../types';

import type { FleetServerHost } from '../../../types';

import {
validateName,
validateFleetServerHosts,
} from '../../../sections/settings/components/fleet_server_hosts_flyout/use_fleet_server_host_form';

import { useSelectFleetServerPolicy } from './use_select_fleet_server_policy';
import { useServiceToken } from './use_service_token';
import { useFleetServerHost } from './use_fleet_server_host';
Expand All @@ -32,12 +45,11 @@ export interface QuickStartCreateForm {
status: QuickStartCreateFormStatus;
error?: string;
submit: () => void;
fleetServerHost?: string;
fleetServerHostSettings: string[];
fleetServerHost?: FleetServerHost;
isFleetServerHostSubmitted: boolean;
onFleetServerHostChange: (value: string) => void;
fleetServerPolicyId?: string;
serviceToken?: string;
inputs: any;
}

/**
Expand All @@ -52,12 +64,10 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => {

const {
fleetServerHost,
fleetServerHostSettings,
isFleetServerHostSubmitted,
setFleetServerHost,
validateFleetServerHost,
saveFleetServerHost,
error: fleetServerError,
setFleetServerHost,
} = useFleetServerHost();

// When a validation error is surfaced from the Fleet Server host form, we want to treat it
Expand All @@ -71,18 +81,34 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => {
const { fleetServerPolicyId, setFleetServerPolicyId } = useSelectFleetServerPolicy();
const { serviceToken, generateServiceToken } = useServiceToken();

const onFleetServerHostChange = useCallback(
(value: string) => {
setFleetServerHost(value);
},
[setFleetServerHost]
const isPreconfigured = fleetServerHost?.is_preconfigured ?? false;
const nameInput = useInput(fleetServerHost?.name ?? '', validateName, isPreconfigured);

const hostUrlsInput = useComboInput(
'hostUrls',
fleetServerHost?.host_urls || [],
validateFleetServerHosts,
isPreconfigured
);
const validate = useCallback(
() => hostUrlsInput.validate() && nameInput.validate(),
[hostUrlsInput, nameInput]
);

const submit = useCallback(async () => {
try {
if (validateFleetServerHost()) {
if (validate()) {
setStatus('loading');
await saveFleetServerHost();

const newFleetServerHost = {
name: nameInput.value,
host_urls: hostUrlsInput.value,
is_default: true,
id: 'fleet-server-host',
is_preconfigured: false,
};
setFleetServerHost(newFleetServerHost);
await saveFleetServerHost(newFleetServerHost);
await generateServiceToken();

const existingPolicy = await sendGetOneAgentPolicy(
Expand All @@ -99,11 +125,9 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => {
withSysMonitoring: true,
}
);

setFleetServerPolicyId(createPolicyResponse.data?.item.id);
}

setFleetServerHost(fleetServerHost);
setStatus('success');
}
} catch (err) {
Expand All @@ -117,13 +141,14 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => {
setError(err.message);
}
}, [
validateFleetServerHost,
nameInput.value,
hostUrlsInput.value,
setFleetServerHost,
saveFleetServerHost,
generateServiceToken,
setFleetServerHost,
fleetServerHost,
setFleetServerPolicyId,
notifications.toasts,
validate,
]);

return {
Expand All @@ -132,9 +157,11 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => {
submit,
fleetServerPolicyId,
fleetServerHost,
fleetServerHostSettings,
isFleetServerHostSubmitted,
onFleetServerHostChange,
serviceToken,
inputs: {
hostUrlsInput,
nameInput,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@ import {
} from './steps';

export const QuickStartTab: React.FunctionComponent = () => {
const quickStartCreateForm = useQuickStartCreateForm();
const { fleetServerHost, fleetServerPolicyId, serviceToken, status, error, submit, inputs } =
useQuickStartCreateForm();
const { isFleetServerReady } = useWaitForFleetServer();

const steps = [
getGettingStartedStep({
quickStartCreateForm,
fleetServerHost,
fleetServerPolicyId,
serviceToken,
status,
error,
submit,
isFleetServerHostSubmitted: false,
inputs,
}),
getInstallFleetServerStep({
isFleetServerReady,
fleetServerHost: quickStartCreateForm.fleetServerHost,
fleetServerPolicyId: quickStartCreateForm.fleetServerPolicyId,
serviceToken: quickStartCreateForm.serviceToken,
fleetServerHost: fleetServerHost?.host_urls[0],
fleetServerPolicyId,
serviceToken,
deploymentMode: 'quickstart',
disabled: quickStartCreateForm.status !== 'success',
disabled: status !== 'success',
}),
getConfirmFleetServerConnectionStep({
isFleetServerReady,
disabled: quickStartCreateForm.status !== 'success',
disabled: status !== 'success',
}),
];

Expand Down
Loading