Skip to content

Commit

Permalink
Onboarding integration flow fleet (#203443)
Browse files Browse the repository at this point in the history
## Summary

Summarize your PR. If it involves visual changes include a screenshot or
gif.


### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...
  • Loading branch information
angorayc authored Dec 10, 2024
1 parent 9a4bf3f commit 9551f4b
Show file tree
Hide file tree
Showing 40 changed files with 1,072 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import {
interface AdvancedTabProps {
onClose: () => void;
selectedPolicyId?: string;
handleContinueAddingAgentClick?: () => void;
}

export const AdvancedTab: React.FunctionComponent<AdvancedTabProps> = ({
selectedPolicyId,
onClose,
handleContinueAddingAgentClick,
}) => {
const {
isSelectFleetServerPolicyLoading,
Expand Down Expand Up @@ -81,6 +83,7 @@ export const AdvancedTab: React.FunctionComponent<AdvancedTabProps> = ({
getConfirmFleetServerConnectionStep({
hasRecentlyEnrolledFleetServers,
disabled: !Boolean(serviceToken),
handleContinueAddingAgentClick,
}),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,33 @@ interface Props {
onClose: () => void;
}

const useFleetServerTabs = (onClose: () => void) => {
export const useFleetServerTabs = (
onClose: () => void,
handleContinueAddingAgentClick?: () => void
) => {
const [currentTab, setCurrentTab] = useState('quickStart');

const quickStartTab = {
id: 'quickStart',
label: 'Quick Start',
content: <QuickStartTab onClose={onClose} />,
content: (
<QuickStartTab
onClose={onClose}
handleContinueAddingAgentClick={handleContinueAddingAgentClick}
/>
),
'data-test-subj': 'fleetServerFlyoutTab-quickStart',
};

const advancedTab = {
id: 'advanced',
label: 'Advanced',
content: <AdvancedTab onClose={onClose} />,
content: (
<AdvancedTab
onClose={onClose}
handleContinueAddingAgentClick={handleContinueAddingAgentClick}
/>
),
'data-test-subj': 'fleetServerFlyoutTab-advanced',
};

Expand All @@ -62,7 +75,7 @@ const useFleetServerTabs = (onClose: () => void) => {
return { tabs: [quickStartTab, advancedTab], currentTab, setCurrentTab, currentTabContent };
};

const Header: React.FunctionComponent<{
export const Header: React.FunctionComponent<{
isFlyout?: boolean;
currentTab: string;
tabs: Array<{ id: string; label: string; content: React.ReactNode }>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ import { useLatestFleetServers } from './hooks/use_latest_fleet_servers';

interface Props {
onClose: () => void;
handleContinueAddingAgentClick?: () => void;
}

export const QuickStartTab: React.FunctionComponent<Props> = ({ onClose }) => {
export const QuickStartTab: React.FunctionComponent<Props> = ({
onClose,
handleContinueAddingAgentClick,
}) => {
const {
fleetServerHost,
setFleetServerHost,
Expand Down Expand Up @@ -62,6 +66,7 @@ export const QuickStartTab: React.FunctionComponent<Props> = ({ onClose }) => {
getConfirmFleetServerConnectionStep({
hasRecentlyEnrolledFleetServers,
disabled: status !== 'success',
handleContinueAddingAgentClick,
}),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { useFleetStatus, useFlyoutContext } from '../../../hooks';
export function getConfirmFleetServerConnectionStep({
disabled,
hasRecentlyEnrolledFleetServers,
handleContinueAddingAgentClick,
}: {
disabled: boolean;
hasRecentlyEnrolledFleetServers: boolean;
handleContinueAddingAgentClick?: () => void;
}): EuiStepProps {
return {
title: hasRecentlyEnrolledFleetServers
Expand All @@ -34,14 +36,16 @@ export function getConfirmFleetServerConnectionStep({
children: !disabled && (
<ConfirmFleetServerConnectionStepContent
hasRecentlyEnrolledFleetServers={hasRecentlyEnrolledFleetServers}
handleContinueAddingAgentClick={handleContinueAddingAgentClick}
/>
),
};
}

const ConfirmFleetServerConnectionStepContent: React.FunctionComponent<{
hasRecentlyEnrolledFleetServers: boolean;
}> = ({ hasRecentlyEnrolledFleetServers }) => {
handleContinueAddingAgentClick?: () => void;
}> = ({ handleContinueAddingAgentClick, hasRecentlyEnrolledFleetServers }) => {
const flyoutContext = useFlyoutContext();
const fleetStatus = useFleetStatus();

Expand All @@ -64,7 +68,7 @@ const ConfirmFleetServerConnectionStepContent: React.FunctionComponent<{

<EuiButton
color="primary"
onClick={handleContinueClick}
onClick={handleContinueAddingAgentClick ?? handleContinueClick}
data-test-subj="fleetServerFlyoutContinueEnrollingButton"
>
<FormattedMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';

import { FormattedMessage } from '@kbn/i18n-react';

import { Error } from '../../../../components';

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

export const EmbeddedIntegrationStepsLayout: React.FunctionComponent<
EmbeddedIntegrationStepsLayoutProps
> = (props) => {
const { steps, currentStep, error } = props;

if (error) {
return (
<Error
title={
<FormattedMessage
id="xpack.fleet.createPackagePolicy.errorLoadingPackageTitle"
defaultMessage="Error loading package information"
/>
}
error={error}
/>
);
}

const StepComponent = steps[currentStep].component;

return <StepComponent {...props} />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { i18n } from '@kbn/i18n';

import { splitPkgKey } from '../../../../../../../common/services';

import {
useGetPackageInfoByKeyQuery,
useFleetServerHostsForPolicy,
useStartServices,
} from '../../../../hooks';

import { useIntegrationsStateContext } from '../../../../../integrations/hooks';

import type { AgentPolicy } from '../../../../types';
import { useGetAgentPolicyOrDefault } from '../multi_page_layout/hooks';
import { Loading } from '../../../../components';

import { onboardingManagedSteps } from './onboarding_steps';

import { EmbeddedIntegrationStepsLayout } from './embedded_integration_steps_layout';
import type { EmbeddedIntegrationFlowProps } from './types';

export const EmbeddedIntegrationFlow: React.FC<EmbeddedIntegrationFlowProps> = ({
prerelease,
integrationName: integration,
onStepNext,
onCancel,
handleViewAssets,
from,
}) => {
const { notifications } = useStartServices();

const { pkgkey: pkgKeyContext } = useIntegrationsStateContext();
const pkgkey = pkgKeyContext || '';
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
const [currentStep, setCurrentStep] = useState(0);
const [isManaged, setIsManaged] = useState(true);
const [enrolledAgentIds, setEnrolledAgentIds] = useState<string[]>([]);
const [selectedAgentPolicies, setSelectedAgentPolicies] = useState<AgentPolicy[]>();
const toggleIsManaged = (newIsManaged: boolean) => {
setIsManaged(newIsManaged);
setCurrentStep(0);
};

const agentPolicyId = useMemo(() => selectedAgentPolicies?.[0]?.id, [selectedAgentPolicies]);
const {
data: packageInfoData,
error: packageInfoError,
isLoading: isPackageInfoLoading,
} = useGetPackageInfoByKeyQuery(pkgName, pkgVersion, { prerelease, full: true });

const {
agentPolicy,
enrollmentAPIKey,
error: agentPolicyError,
isLoading: isAgentPolicyLoading,
} = useGetAgentPolicyOrDefault(agentPolicyId);

const packageInfo = useMemo(() => packageInfoData?.item, [packageInfoData]);

const integrationInfo = useMemo(() => {
if (!integration) return;
return packageInfo?.policy_templates?.find(
(policyTemplate) => policyTemplate.name === integration
);
}, [packageInfo?.policy_templates, integration]);

const { fleetServerHost, fleetProxy, downloadSource } = useFleetServerHostsForPolicy(agentPolicy);

const stepsNext = useCallback(
(props?: { selectedAgentPolicies?: AgentPolicy[]; toStep?: number }) => {
if (currentStep === onboardingManagedSteps.length - 1) {
return;
}

setCurrentStep(props?.toStep ?? currentStep + 1);
onStepNext?.(props?.toStep ?? currentStep + 1);

// selected agent policy is set after integration is configured
if (props?.selectedAgentPolicies) {
setSelectedAgentPolicies(props?.selectedAgentPolicies);
}
},
[currentStep, onStepNext]
);

const stepsBack = () => {
if (currentStep === 0) {
return;
}

setCurrentStep(currentStep - 1);
};

useEffect(() => {
if (!isPackageInfoLoading && packageInfoError) {
notifications.toasts.addError(packageInfoError, {
title: i18n.translate('xpack.fleet.createPackagePolicy.errorLoadingPackageTitle', {
defaultMessage: 'Error loading package information',
}),
toastMessage: packageInfoError.message,
});
}
}, [isPackageInfoLoading, notifications.toasts, packageInfoError]);

useEffect(() => {
if (!isAgentPolicyLoading && agentPolicyError) {
notifications.toasts.addError(agentPolicyError, {
title: i18n.translate('xpack.fleet.createPackagePolicy.errorLoadingAgentPolicyTitle', {
defaultMessage: 'Error loading agent policy information',
}),
toastMessage: agentPolicyError.message,
});
}
}, [isAgentPolicyLoading, notifications.toasts, agentPolicyError]);

return !isPackageInfoLoading && packageInfo ? (
<EmbeddedIntegrationStepsLayout
fleetServerHost={fleetServerHost}
fleetProxy={fleetProxy}
downloadSource={downloadSource}
agentPolicy={selectedAgentPolicies?.[0] ?? agentPolicy}
enrollmentAPIKey={enrollmentAPIKey}
currentStep={currentStep}
steps={onboardingManagedSteps}
packageInfo={packageInfo}
integrationInfo={integrationInfo}
onNext={stepsNext}
onBack={stepsBack}
isManaged={isManaged}
setIsManaged={toggleIsManaged}
setEnrolledAgentIds={setEnrolledAgentIds}
enrolledAgentIds={enrolledAgentIds}
onCancel={onCancel}
prerelease={prerelease}
handleViewAssets={handleViewAssets}
from={from}
selectedAgentPolicies={selectedAgentPolicies}
/>
) : (
<Loading />
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';

import { EuiSpacer } from '@elastic/eui';

import { useCheckPermissions } from '../../../../../hooks';

import { useFleetServerTabs } from '../../../../../components';
import { Header } from '../../../../../components/fleet_server_instructions';
import { FleetServerMissingESPrivileges } from '../../../../agents/components';
import type { EmbeddedIntegrationStepsLayoutProps } from '../types';

export const AddFleetServerStepFromOnboardingHub: React.FC<EmbeddedIntegrationStepsLayoutProps> = ({
onCancel,
onNext,
}) => {
const { tabs, currentTab, setCurrentTab, currentTabContent } = useFleetServerTabs(
onCancel,
onNext
);

const { permissionsError, isPermissionsLoading } = useCheckPermissions();

return (
<>
<Header tabs={tabs} currentTab={currentTab} onTabClick={(id) => setCurrentTab(id)} />
{!isPermissionsLoading && permissionsError === 'MISSING_FLEET_SERVER_SETUP_PRIVILEGES' ? (
<FleetServerMissingESPrivileges />
) : (
<>
<EuiSpacer size="xl" />
{currentTabContent}
</>
)}
</>
);
};
Loading

0 comments on commit 9551f4b

Please sign in to comment.