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 Manager] Add UI to enroll standalone agent #71288

Merged
merged 3 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const CONFIG_KEYS_ORDER = [
'name',
'revision',
'type',
'settings',
'outputs',
'settings',
'inputs',
'enabled',
'use_output',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export const useGetOneAgentConfigFull = (agentConfigId: string) => {
});
};

export const sendGetOneAgentConfigFull = (
agentConfigId: string,
query: { standalone?: boolean } = {}
) => {
return sendRequest<GetFullAgentConfigResponse>({
path: agentConfigRouteService.getInfoFullPath(agentConfigId),
method: 'get',
query,
});
};

export const sendGetOneAgentConfig = (agentConfigId: string) => {
return sendRequest<GetOneAgentConfigResponse>({
path: agentConfigRouteService.getInfoPath(agentConfigId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ export function sendDeleteOneEnrollmentAPIKey(keyId: string, options?: RequestOp
});
}

export function sendGetEnrollmentAPIKeys(
query: GetEnrollmentAPIKeysRequest['query'],
options?: RequestOptions
) {
return sendRequest<GetEnrollmentAPIKeysResponse>({
method: 'get',
path: enrollmentAPIKeyRouteService.getListPath(),
query,
...options,
});
}

export function useGetEnrollmentAPIKeys(
query: GetEnrollmentAPIKeysRequest['query'],
options?: RequestOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,99 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiSelect, EuiSpacer, EuiText, EuiButtonEmpty } from '@elastic/eui';
import { AgentConfig } from '../../../../types';
import { useGetEnrollmentAPIKeys } from '../../../../hooks';
import { AgentConfig, GetEnrollmentAPIKeysResponse } from '../../../../types';
import { sendGetEnrollmentAPIKeys, useCore } from '../../../../hooks';
import { AgentConfigPackageBadges } from '../agent_config_package_badges';

interface Props {
type Props = {
agentConfigs: AgentConfig[];
onKeyChange: (key: string) => void;
}
onConfigChange?: (key: string) => void;
} & (
| {
withKeySelection: true;
onKeyChange?: (key: string) => void;
}
| {
withKeySelection: false;
}
);

export const EnrollmentStepAgentConfig: React.FC<Props> = ({ agentConfigs, onKeyChange }) => {
const [isAuthenticationSettingsOpen, setIsAuthenticationSettingsOpen] = useState(false);
const enrollmentAPIKeysRequest = useGetEnrollmentAPIKeys({
page: 1,
perPage: 1000,
});
export const EnrollmentStepAgentConfig: React.FC<Props> = (props) => {
const { notifications } = useCore();
const { withKeySelection, agentConfigs, onConfigChange } = props;
const onKeyChange = props.withKeySelection && props.onKeyChange;

const [isAuthenticationSettingsOpen, setIsAuthenticationSettingsOpen] = useState(false);
const [enrollmentAPIKeys, setEnrollmentAPIKeys] = useState<GetEnrollmentAPIKeysResponse['list']>(
[]
);
const [selectedState, setSelectedState] = useState<{
agentConfigId?: string;
enrollmentAPIKeyId?: string;
}>({
agentConfigId: agentConfigs.length ? agentConfigs[0].id : undefined,
});
const filteredEnrollmentAPIKeys = React.useMemo(() => {
if (!selectedState.agentConfigId || !enrollmentAPIKeysRequest.data) {
return [];

useEffect(() => {
if (onConfigChange && selectedState.agentConfigId) {
onConfigChange(selectedState.agentConfigId);
}
}, [selectedState.agentConfigId, onConfigChange]);

return enrollmentAPIKeysRequest.data.list.filter(
(key) => key.config_id === selectedState.agentConfigId
);
}, [enrollmentAPIKeysRequest.data, selectedState.agentConfigId]);
useEffect(() => {
if (!withKeySelection) {
return;
}
if (!selectedState.agentConfigId) {
setEnrollmentAPIKeys([]);
return;
}

async function fetchEnrollmentAPIKeys() {
try {
const res = await sendGetEnrollmentAPIKeys({
page: 1,
perPage: 10000,
});
if (res.error) {
throw res.error;
}

if (!res.data) {
throw new Error('No data while fetching enrollment API keys');
}

setEnrollmentAPIKeys(
res.data.list.filter((key) => key.config_id === selectedState.agentConfigId)
);
} catch (error) {
notifications.toasts.addError(error, {
title: 'Error',
});
}
}
fetchEnrollmentAPIKeys();
}, [withKeySelection, selectedState.agentConfigId, notifications.toasts]);

// Select first API key when config change
React.useEffect(() => {
if (!selectedState.enrollmentAPIKeyId && filteredEnrollmentAPIKeys.length > 0) {
const enrollmentAPIKeyId = filteredEnrollmentAPIKeys[0].id;
if (!withKeySelection || !onKeyChange) {
return;
}
if (!selectedState.enrollmentAPIKeyId && enrollmentAPIKeys.length > 0) {
const enrollmentAPIKeyId = enrollmentAPIKeys[0].id;
setSelectedState({
agentConfigId: selectedState.agentConfigId,
enrollmentAPIKeyId,
});
onKeyChange(enrollmentAPIKeyId);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filteredEnrollmentAPIKeys, selectedState.enrollmentAPIKeyId, selectedState.agentConfigId]);
}, [enrollmentAPIKeys, selectedState.enrollmentAPIKeyId, selectedState.agentConfigId]);

return (
<>
Expand Down Expand Up @@ -85,43 +130,47 @@ export const EnrollmentStepAgentConfig: React.FC<Props> = ({ agentConfigs, onKey
{selectedState.agentConfigId && (
<AgentConfigPackageBadges agentConfigId={selectedState.agentConfigId} />
)}
<EuiSpacer size="m" />
<EuiButtonEmpty
flush="left"
iconType={isAuthenticationSettingsOpen ? 'arrowDown' : 'arrowRight'}
onClick={() => setIsAuthenticationSettingsOpen(!isAuthenticationSettingsOpen)}
>
<FormattedMessage
id="xpack.ingestManager.enrollmentStepAgentConfig.showAuthenticationSettingsButton"
defaultMessage="Authentication settings"
/>
</EuiButtonEmpty>
{isAuthenticationSettingsOpen && (
{withKeySelection && onKeyChange && (
<>
<EuiSpacer size="m" />
<EuiSelect
fullWidth
options={filteredEnrollmentAPIKeys.map((key) => ({
value: key.id,
text: key.name,
}))}
value={selectedState.enrollmentAPIKeyId || undefined}
prepend={
<EuiText>
<FormattedMessage
id="xpack.ingestManager.enrollmentStepAgentConfig.enrollmentTokenSelectLabel"
defaultMessage="Enrollment token"
/>
</EuiText>
}
onChange={(e) => {
setSelectedState({
...selectedState,
enrollmentAPIKeyId: e.target.value,
});
onKeyChange(e.target.value);
}}
/>
<EuiButtonEmpty
flush="left"
iconType={isAuthenticationSettingsOpen ? 'arrowDown' : 'arrowRight'}
onClick={() => setIsAuthenticationSettingsOpen(!isAuthenticationSettingsOpen)}
>
<FormattedMessage
id="xpack.ingestManager.enrollmentStepAgentConfig.showAuthenticationSettingsButton"
defaultMessage="Authentication settings"
/>
</EuiButtonEmpty>
{isAuthenticationSettingsOpen && (
<>
<EuiSpacer size="m" />
<EuiSelect
fullWidth
options={enrollmentAPIKeys.map((key) => ({
value: key.id,
text: key.name,
}))}
value={selectedState.enrollmentAPIKeyId || undefined}
prepend={
<EuiText>
<FormattedMessage
id="xpack.ingestManager.enrollmentStepAgentConfig.enrollmentTokenSelectLabel"
defaultMessage="Enrollment token"
/>
</EuiText>
}
onChange={(e) => {
setSelectedState({
...selectedState,
enrollmentAPIKeyId: e.target.value,
});
onKeyChange(e.target.value);
}}
/>
</>
)}
</>
)}
</>
Expand Down
Loading