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] Fix GET /enrollment-api-keys/null error #64595

Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { HttpFetchQuery } from 'src/core/public';
import { useRequest, sendRequest } from './use_request';
import { useRequest, sendRequest, useConditionnalRequest } from './use_request';
import { agentConfigRouteService } from '../../services';
import {
GetAgentConfigsResponse,
Expand All @@ -25,11 +25,18 @@ export const useGetAgentConfigs = (query: HttpFetchQuery = {}) => {
});
};

export const useGetOneAgentConfig = (agentConfigId: string) => {
return useRequest<GetOneAgentConfigResponse>({
path: agentConfigRouteService.getInfoPath(agentConfigId),
method: 'get',
});
export const useGetOneAgentConfig = (agentConfigId: string | undefined) => {
return useConditionnalRequest<GetOneAgentConfigResponse>(
agentConfigId
? {
path: agentConfigRouteService.getInfoPath(agentConfigId),
method: 'get',
shouldSendRequest: true,
}
: {
shouldSendRequest: false,
}
);
};

export const useGetOneAgentConfigFull = (agentConfigId: string) => {
Expand Down
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 { useRequest, UseRequestConfig, sendRequest } from './use_request';
import { useRequest, UseRequestConfig, sendRequest, useConditionnalRequest } from './use_request';
import { enrollmentAPIKeyRouteService } from '../../services';
import {
GetOneEnrollmentAPIKeyResponse,
Expand All @@ -14,12 +14,18 @@ import {

type RequestOptions = Pick<Partial<UseRequestConfig>, 'pollIntervalMs'>;

export function useGetOneEnrollmentAPIKey(keyId: string, options?: RequestOptions) {
return useRequest<GetOneEnrollmentAPIKeyResponse>({
method: 'get',
path: enrollmentAPIKeyRouteService.getInfoPath(keyId),
...options,
});
export function useGetOneEnrollmentAPIKey(keyId: string | undefined) {
return useConditionnalRequest<GetOneEnrollmentAPIKeyResponse>(
keyId
? {
method: 'get',
path: enrollmentAPIKeyRouteService.getInfoPath(keyId),
shouldSendRequest: true,
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
}
: {
shouldSendRequest: false,
}
);
}

export function sendGetOneEnrollmentAPIKey(keyId: string, options?: RequestOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { useState, useEffect } from 'react';
import { HttpSetup } from 'src/core/public';
import {
SendRequestConfig,
Expand Down Expand Up @@ -35,3 +36,68 @@ export const useRequest = <D = any>(config: UseRequestConfig) => {
}
return _useRequest<D>(httpClient, config);
};

type SendConditionnalRequestConfig =
| (SendRequestConfig & { shouldSendRequest: true })
| (Partial<SendRequestConfig> & { shouldSendRequest: false });

export const useConditionnalRequest = <D = any>(config: SendConditionnalRequestConfig) => {
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
const [state, setState] = useState<{
error: Error | null;
data: D | null;
isLoading: boolean;
}>({
error: null,
data: null,
isLoading: false,
});

const { path, method, shouldSendRequest, query, body } = config;

async function sendGetOneEnrollmentAPIKeyRequest() {
if (!config.shouldSendRequest) {
setState({
data: null,
isLoading: false,
error: null,
});
return;
}

try {
setState({
data: null,
isLoading: true,
error: null,
});
const res = await sendRequest<D>({
method: config.method,
path: config.path,
query: config.query,
body: config.body,
});
if (res.error) {
throw res.error;
}
setState({
data: res.data,
isLoading: false,
error: null,
});
return res;
} catch (error) {
setState({
data: null,
isLoading: false,
error,
});
}
}

useEffect(() => {
sendGetOneEnrollmentAPIKeyRequest();
// eslint-disable-next-line react-hooks/exhaustive-deps
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
}, [path, method, shouldSendRequest, JSON.stringify(query), JSON.stringify(body)]);

return { ...state, sendRequest: sendGetOneEnrollmentAPIKeyRequest };
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const ConfigYamlView = memo<{ config: AgentConfig }>(({ config }) => {
page: 1,
perPage: 1000,
});
const apiKeyRequest = useGetOneEnrollmentAPIKey(apiKeysRequest.data?.list?.[0]?.id as string);
const apiKeyRequest = useGetOneEnrollmentAPIKey(apiKeysRequest.data?.list?.[0]?.id);

if (fullConfigRequest.isLoading && !fullConfigRequest.data) {
return <Loading />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const AgentDetailSection: React.FunctionComponent<Props> = ({ agent }) =>

// Fetch AgentConfig information
const { isLoading: isAgentConfigLoading, data: agentConfigData } = useGetOneAgentConfig(
agent.config_id as string
agent.config_id
);

const items = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const AgentEnrollmentFlyout: React.FunctionComponent<Props> = ({
onClose,
agentConfigs = [],
}) => {
const [selectedAPIKeyId, setSelectedAPIKeyId] = useState<string | null>(null);
const [selectedAPIKeyId, setSelectedAPIKeyId] = useState<string | undefined>();

return (
<EuiFlyout onClose={onClose} size="l" maxWidth={640}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import React, { useState } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiSpacer, EuiText, EuiButtonGroup, EuiSteps } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { useEnrollmentApiKey } from '../enrollment_api_keys';
import {
ShellEnrollmentInstructions,
ManualInstructions,
} from '../../../../../components/enrollment_instructions';
import { useCore, useGetAgents } from '../../../../../hooks';
import { useCore, useGetAgents, useGetOneEnrollmentAPIKey } from '../../../../../hooks';
import { Loading } from '../../../components';

interface Props {
selectedAPIKeyId: string | null;
selectedAPIKeyId: string | undefined;
}
function useNewEnrolledAgents() {
// New enrolled agents
Expand Down Expand Up @@ -44,7 +43,7 @@ export const EnrollmentInstructions: React.FunctionComponent<Props> = ({ selecte
const core = useCore();
const [installType, setInstallType] = useState<'quickInstall' | 'manual'>('quickInstall');

const apiKey = useEnrollmentApiKey(selectedAPIKeyId);
const apiKey = useGetOneEnrollmentAPIKey(selectedAPIKeyId);

const newAgents = useNewEnrolledAgents();
if (!apiKey.data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import {
EuiFieldText,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { useEnrollmentApiKeys } from '../enrollment_api_keys';
import { AgentConfig } from '../../../../../types';
import { useInput, useCore, sendRequest } from '../../../../../hooks';
import { useInput, useCore, sendRequest, useGetEnrollmentAPIKeys } from '../../../../../hooks';
import { enrollmentAPIKeyRouteService } from '../../../../../services';

interface Props {
onKeyChange: (keyId: string | null) => void;
onKeyChange: (keyId: string | undefined) => void;
agentConfigs: AgentConfig[];
}

Expand Down Expand Up @@ -62,17 +61,17 @@ function useCreateApiKeyForm(configId: string | null, onSuccess: (keyId: string)
}

export const APIKeySelection: React.FunctionComponent<Props> = ({ onKeyChange, agentConfigs }) => {
const enrollmentAPIKeysRequest = useEnrollmentApiKeys({
currentPage: 1,
pageSize: 1000,
const enrollmentAPIKeysRequest = useGetEnrollmentAPIKeys({
page: 1,
perPage: 1000,
});

const [selectedState, setSelectedState] = useState<{
agentConfigId: string | null;
enrollmentAPIKeyId: string | null;
enrollmentAPIKeyId: string | undefined;
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
}>({
agentConfigId: agentConfigs.length ? agentConfigs[0].id : null,
enrollmentAPIKeyId: null,
enrollmentAPIKeyId: undefined,
});
const filteredEnrollmentAPIKeys = React.useMemo(() => {
if (!selectedState.agentConfigId || !enrollmentAPIKeysRequest.data) {
Expand All @@ -99,10 +98,10 @@ export const APIKeySelection: React.FunctionComponent<Props> = ({ onKeyChange, a

const [showAPIKeyForm, setShowAPIKeyForm] = useState(false);
const apiKeyForm = useCreateApiKeyForm(selectedState.agentConfigId, async (keyId: string) => {
const res = await enrollmentAPIKeysRequest.refresh();
const res = await enrollmentAPIKeysRequest.sendRequest();
setSelectedState({
...selectedState,
enrollmentAPIKeyId: res.data?.list.find(key => key.id === keyId)?.id ?? null,
enrollmentAPIKeyId: res.data?.list.find(key => key.id === keyId)?.id,
});
setShowAPIKeyForm(false);
});
Expand Down Expand Up @@ -135,7 +134,7 @@ export const APIKeySelection: React.FunctionComponent<Props> = ({ onKeyChange, a
onChange={e =>
setSelectedState({
agentConfigId: e.target.value,
enrollmentAPIKeyId: null,
enrollmentAPIKeyId: undefined,
})
}
/>
Expand Down

This file was deleted.

Loading