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] Allow to reassign agent to a new config #63847

Merged
3 changes: 2 additions & 1 deletion x-pack/plugins/ingest_manager/common/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export const AGENT_API_ROUTES = {
ACKS_PATTERN: `${FLEET_API_ROOT}/agents/{agentId}/acks`,
ACTIONS_PATTERN: `${FLEET_API_ROOT}/agents/{agentId}/actions`,
ENROLL_PATTERN: `${FLEET_API_ROOT}/agents/enroll`,
UNENROLL_PATTERN: `${FLEET_API_ROOT}/agents/unenroll`,
UNENROLL_PATTERN: `${FLEET_API_ROOT}/agents/{agentId}/unenroll`,
REASSIGN_PATTERN: `${FLEET_API_ROOT}/agents/{agentId}/reassign`,
STATUS_PATTERN: `${FLEET_API_ROOT}/agent-status`,
};

Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/ingest_manager/common/services/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export const agentRouteService = {
getInfoPath: (agentId: string) => AGENT_API_ROUTES.INFO_PATTERN.replace('{agentId}', agentId),
getUpdatePath: (agentId: string) => AGENT_API_ROUTES.UPDATE_PATTERN.replace('{agentId}', agentId),
getEventsPath: (agentId: string) => AGENT_API_ROUTES.EVENTS_PATTERN.replace('{agentId}', agentId),
getUnenrollPath: () => AGENT_API_ROUTES.UNENROLL_PATTERN,
getUnenrollPath: (agentId: string) =>
AGENT_API_ROUTES.UNENROLL_PATTERN.replace('{agentId}', agentId),
getReassignPath: (agentId: string) =>
AGENT_API_ROUTES.REASSIGN_PATTERN.replace('{agentId}', agentId),
getListPath: () => AGENT_API_ROUTES.LIST_PATTERN,
getStatusPath: () => AGENT_API_ROUTES.STATUS_PATTERN,
};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/ingest_manager/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ interface AgentBase {
access_api_key_id?: string;
default_api_key?: string;
config_id?: string;
config_revision?: number;
config_revision?: number | null;
config_newest_revision?: number;
last_checkin?: string;
}
Expand Down
21 changes: 14 additions & 7 deletions x-pack/plugins/ingest_manager/common/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,23 @@ export interface PostNewAgentActionResponse {
}

export interface PostAgentUnenrollRequest {
body: { kuery: string } | { ids: string[] };
params: {
agentId: string;
};
}

export interface PostAgentUnenrollResponse {
results: Array<{
success: boolean;
error?: any;
id: string;
action: string;
}>;
success: boolean;
}

export interface PutAgentReassignRequest {
params: {
agentId: string;
};
body: { config_id: string };
}

export interface PutAgentReassignResponse {
success: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
GetOneAgentResponse,
GetOneAgentEventsResponse,
GetOneAgentEventsRequest,
PutAgentReassignRequest,
PutAgentReassignResponse,
GetAgentsRequest,
GetAgentsResponse,
GetAgentStatusRequest,
Expand Down Expand Up @@ -59,3 +61,16 @@ export function sendGetAgentStatus(
...options,
});
}

export function sendPutAgentReassign(
agentId: string,
body: PutAgentReassignRequest['body'],
options?: RequestOptions
) {
return sendRequest<PutAgentReassignResponse>({
method: 'put',
path: agentRouteService.getReassignPath(agentId),
body,
...options,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useState, Fragment } from 'react';
import React, { useState, Fragment, useCallback } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import {
EuiTitle,
Expand All @@ -13,10 +13,13 @@ import {
EuiFlexItem,
EuiDescriptionList,
EuiButton,
EuiPopover,
EuiDescriptionListTitle,
EuiDescriptionListDescription,
EuiButtonEmpty,
EuiIconTip,
EuiContextMenuPanel,
EuiContextMenuItem,
EuiTextColor,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
Expand All @@ -26,7 +29,7 @@ import { Agent } from '../../../../types';
import { AgentHealth } from '../../components/agent_health';
import { useCapabilities, useGetOneAgentConfig } from '../../../../hooks';
import { Loading } from '../../../../components';
import { ConnectedLink } from '../../components';
import { ConnectedLink, AgentReassignConfigFlyout } from '../../components';
import { AgentUnenrollProvider } from '../../components/agent_unenroll_provider';

const Item: React.FunctionComponent<{ label: string }> = ({ label, children }) => {
Expand Down Expand Up @@ -56,6 +59,15 @@ export const AgentDetailSection: React.FunctionComponent<Props> = ({ agent }) =>
const hasWriteCapabilites = useCapabilities().write;
const metadataFlyout = useFlyout();
const refreshAgent = useAgentRefresh();
// Actions menu
const [isActionsPopoverOpen, setIsActionsPopoverOpen] = useState(false);
const handleCloseMenu = useCallback(() => setIsActionsPopoverOpen(false), [
setIsActionsPopoverOpen,
]);
const handleToggleMenu = useCallback(() => setIsActionsPopoverOpen(!isActionsPopoverOpen), [
isActionsPopoverOpen,
]);
const [isReassignFlyoutOpen, setIsReassignFlyoutOpen] = useState(false);

// Fetch AgentConfig information
const { isLoading: isAgentConfigLoading, data: agentConfigData } = useGetOneAgentConfig(
Expand Down Expand Up @@ -111,6 +123,9 @@ export const AgentDetailSection: React.FunctionComponent<Props> = ({ agent }) =>

return (
<>
{isReassignFlyoutOpen && (
<AgentReassignConfigFlyout agent={agent} onClose={() => setIsReassignFlyoutOpen(false)} />
)}
<EuiFlexGroup alignItems="center" justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiTitle size="l">
Expand All @@ -123,21 +138,55 @@ export const AgentDetailSection: React.FunctionComponent<Props> = ({ agent }) =>
</EuiTitle>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<AgentUnenrollProvider>
{unenrollAgentsPrompt => (
<EuiButton
disabled={!hasWriteCapabilites || !agent.active}
onClick={() => {
unenrollAgentsPrompt([agent.id], 1, refreshAgent);
}}
>
<EuiPopover
anchorPosition="downRight"
panelPaddingSize="none"
button={
<EuiButton onClick={handleToggleMenu}>
<FormattedMessage
id="xpack.ingestManager.agentDetails.unenrollButtonText"
defaultMessage="Unenroll"
id="xpack.ingestManager.agentDetails.actionsButton"
defaultMessage="Actions"
/>
</EuiButton>
)}
</AgentUnenrollProvider>
}
isOpen={isActionsPopoverOpen}
closePopover={handleCloseMenu}
>
<EuiContextMenuPanel
items={[
<EuiContextMenuItem
icon="pencil"
onClick={() => {
handleCloseMenu();
setIsReassignFlyoutOpen(true);
}}
key="reassignConfig"
>
<FormattedMessage
id="xpack.ingestManager.agentList.reassignActionText"
defaultMessage="Assign new agent config"
/>
</EuiContextMenuItem>,

<AgentUnenrollProvider>
{unenrollAgentsPrompt => (
<EuiContextMenuItem
icon="cross"
disabled={!hasWriteCapabilites || !agent.active}
onClick={() => {
unenrollAgentsPrompt([agent.id], 1, refreshAgent);
}}
>
<FormattedMessage
id="xpack.ingestManager.agentList.unenrollOneButton"
defaultMessage="Unenroll"
/>
</EuiContextMenuItem>
)}
</AgentUnenrollProvider>,
]}
/>
</EuiPopover>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size={'xl'} />
Expand Down
Loading