Skip to content

Commit 7bd0a3a

Browse files
committed
[Ingest] Fix GET /enrollment-api-keys/null error (#64595)
1 parent c9b1796 commit 7bd0a3a

File tree

15 files changed

+108
-390
lines changed

15 files changed

+108
-390
lines changed

x-pack/plugins/ingest_manager/public/applications/ingest_manager/hooks/use_request/agent_config.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66
import { HttpFetchQuery } from 'src/core/public';
7-
import { useRequest, sendRequest } from './use_request';
7+
import {
8+
useRequest,
9+
sendRequest,
10+
useConditionalRequest,
11+
SendConditionalRequestConfig,
12+
} from './use_request';
813
import { agentConfigRouteService } from '../../services';
914
import {
1015
GetAgentConfigsResponse,
@@ -25,11 +30,12 @@ export const useGetAgentConfigs = (query: HttpFetchQuery = {}) => {
2530
});
2631
};
2732

28-
export const useGetOneAgentConfig = (agentConfigId: string) => {
29-
return useRequest<GetOneAgentConfigResponse>({
30-
path: agentConfigRouteService.getInfoPath(agentConfigId),
33+
export const useGetOneAgentConfig = (agentConfigId: string | undefined) => {
34+
return useConditionalRequest<GetOneAgentConfigResponse>({
35+
path: agentConfigId ? agentConfigRouteService.getInfoPath(agentConfigId) : undefined,
3136
method: 'get',
32-
});
37+
shouldSendRequest: !!agentConfigId,
38+
} as SendConditionalRequestConfig);
3339
};
3440

3541
export const useGetOneAgentConfigFull = (agentConfigId: string) => {

x-pack/plugins/ingest_manager/public/applications/ingest_manager/hooks/use_request/enrollment_api_keys.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { useRequest, UseRequestConfig, sendRequest } from './use_request';
7+
import {
8+
useRequest,
9+
UseRequestConfig,
10+
sendRequest,
11+
useConditionalRequest,
12+
SendConditionalRequestConfig,
13+
} from './use_request';
814
import { enrollmentAPIKeyRouteService } from '../../services';
915
import {
1016
GetOneEnrollmentAPIKeyResponse,
@@ -14,12 +20,12 @@ import {
1420

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

17-
export function useGetOneEnrollmentAPIKey(keyId: string, options?: RequestOptions) {
18-
return useRequest<GetOneEnrollmentAPIKeyResponse>({
23+
export function useGetOneEnrollmentAPIKey(keyId: string | undefined) {
24+
return useConditionalRequest<GetOneEnrollmentAPIKeyResponse>({
1925
method: 'get',
20-
path: enrollmentAPIKeyRouteService.getInfoPath(keyId),
21-
...options,
22-
});
26+
path: keyId ? enrollmentAPIKeyRouteService.getInfoPath(keyId) : undefined,
27+
shouldSendRequest: !!keyId,
28+
} as SendConditionalRequestConfig);
2329
}
2430

2531
export function sendGetOneEnrollmentAPIKey(keyId: string, options?: RequestOptions) {

x-pack/plugins/ingest_manager/public/applications/ingest_manager/hooks/use_request/use_request.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
import { useState, useEffect } from 'react';
67
import { HttpSetup } from 'src/core/public';
78
import {
89
SendRequestConfig,
@@ -35,3 +36,68 @@ export const useRequest = <D = any>(config: UseRequestConfig) => {
3536
}
3637
return _useRequest<D>(httpClient, config);
3738
};
39+
40+
export type SendConditionalRequestConfig =
41+
| (SendRequestConfig & { shouldSendRequest: true })
42+
| (Partial<SendRequestConfig> & { shouldSendRequest: false });
43+
44+
export const useConditionalRequest = <D = any>(config: SendConditionalRequestConfig) => {
45+
const [state, setState] = useState<{
46+
error: Error | null;
47+
data: D | null;
48+
isLoading: boolean;
49+
}>({
50+
error: null,
51+
data: null,
52+
isLoading: false,
53+
});
54+
55+
const { path, method, shouldSendRequest, query, body } = config;
56+
57+
async function sendGetOneEnrollmentAPIKeyRequest() {
58+
if (!config.shouldSendRequest) {
59+
setState({
60+
data: null,
61+
isLoading: false,
62+
error: null,
63+
});
64+
return;
65+
}
66+
67+
try {
68+
setState({
69+
data: null,
70+
isLoading: true,
71+
error: null,
72+
});
73+
const res = await sendRequest<D>({
74+
method: config.method,
75+
path: config.path,
76+
query: config.query,
77+
body: config.body,
78+
});
79+
if (res.error) {
80+
throw res.error;
81+
}
82+
setState({
83+
data: res.data,
84+
isLoading: false,
85+
error: null,
86+
});
87+
return res;
88+
} catch (error) {
89+
setState({
90+
data: null,
91+
isLoading: false,
92+
error,
93+
});
94+
}
95+
}
96+
97+
useEffect(() => {
98+
sendGetOneEnrollmentAPIKeyRequest();
99+
// eslint-disable-next-line react-hooks/exhaustive-deps
100+
}, [path, method, shouldSendRequest, JSON.stringify(query), JSON.stringify(body)]);
101+
102+
return { ...state, sendRequest: sendGetOneEnrollmentAPIKeyRequest };
103+
};

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/details_page/components/yaml/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const ConfigYamlView = memo<{ config: AgentConfig }>(({ config }) => {
4545
page: 1,
4646
perPage: 1000,
4747
});
48-
const apiKeyRequest = useGetOneEnrollmentAPIKey(apiKeysRequest.data?.list?.[0]?.id as string);
48+
const apiKeyRequest = useGetOneEnrollmentAPIKey(apiKeysRequest.data?.list?.[0]?.id);
4949

5050
if (fullConfigRequest.isLoading && !fullConfigRequest.data) {
5151
return <Loading />;

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_details_page/components/details_section.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const AgentDetailSection: React.FunctionComponent<Props> = ({ agent }) =>
7171

7272
// Fetch AgentConfig information
7373
const { isLoading: isAgentConfigLoading, data: agentConfigData } = useGetOneAgentConfig(
74-
agent.config_id as string
74+
agent.config_id
7575
);
7676

7777
const items = [

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const AgentEnrollmentFlyout: React.FunctionComponent<Props> = ({
3030
onClose,
3131
agentConfigs = [],
3232
}) => {
33-
const [selectedAPIKeyId, setSelectedAPIKeyId] = useState<string | null>(null);
33+
const [selectedAPIKeyId, setSelectedAPIKeyId] = useState<string | undefined>();
3434

3535
return (
3636
<EuiFlyout onClose={onClose} size="l" maxWidth={640}>

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/instructions.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import React, { useState } from 'react';
77
import { i18n } from '@kbn/i18n';
88
import { EuiSpacer, EuiText, EuiButtonGroup, EuiSteps } from '@elastic/eui';
99
import { FormattedMessage } from '@kbn/i18n/react';
10-
import { useEnrollmentApiKey } from '../enrollment_api_keys';
1110
import {
1211
ShellEnrollmentInstructions,
1312
ManualInstructions,
1413
} from '../../../../../components/enrollment_instructions';
15-
import { useCore, useGetAgents } from '../../../../../hooks';
14+
import { useCore, useGetAgents, useGetOneEnrollmentAPIKey } from '../../../../../hooks';
1615
import { Loading } from '../../../components';
1716

1817
interface Props {
19-
selectedAPIKeyId: string | null;
18+
selectedAPIKeyId: string | undefined;
2019
}
2120
function useNewEnrolledAgents() {
2221
// New enrolled agents
@@ -44,7 +43,7 @@ export const EnrollmentInstructions: React.FunctionComponent<Props> = ({ selecte
4443
const core = useCore();
4544
const [installType, setInstallType] = useState<'quickInstall' | 'manual'>('quickInstall');
4645

47-
const apiKey = useEnrollmentApiKey(selectedAPIKeyId);
46+
const apiKey = useGetOneEnrollmentAPIKey(selectedAPIKeyId);
4847

4948
const newAgents = useNewEnrolledAgents();
5049
if (!apiKey.data) {

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/key_selection.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ import {
1616
EuiFieldText,
1717
} from '@elastic/eui';
1818
import { FormattedMessage } from '@kbn/i18n/react';
19-
import { useEnrollmentApiKeys } from '../enrollment_api_keys';
2019
import { AgentConfig } from '../../../../../types';
21-
import { useInput, useCore, sendRequest } from '../../../../../hooks';
20+
import { useInput, useCore, sendRequest, useGetEnrollmentAPIKeys } from '../../../../../hooks';
2221
import { enrollmentAPIKeyRouteService } from '../../../../../services';
2322

2423
interface Props {
25-
onKeyChange: (keyId: string | null) => void;
24+
onKeyChange: (keyId: string | undefined) => void;
2625
agentConfigs: AgentConfig[];
2726
}
2827

29-
function useCreateApiKeyForm(configId: string | null, onSuccess: (keyId: string) => void) {
28+
function useCreateApiKeyForm(configId: string | undefined, onSuccess: (keyId: string) => void) {
3029
const { notifications } = useCore();
3130
const [isLoading, setIsLoading] = useState(false);
3231
const apiKeyNameInput = useInput('');
@@ -62,17 +61,16 @@ function useCreateApiKeyForm(configId: string | null, onSuccess: (keyId: string)
6261
}
6362

6463
export const APIKeySelection: React.FunctionComponent<Props> = ({ onKeyChange, agentConfigs }) => {
65-
const enrollmentAPIKeysRequest = useEnrollmentApiKeys({
66-
currentPage: 1,
67-
pageSize: 1000,
64+
const enrollmentAPIKeysRequest = useGetEnrollmentAPIKeys({
65+
page: 1,
66+
perPage: 1000,
6867
});
6968

7069
const [selectedState, setSelectedState] = useState<{
71-
agentConfigId: string | null;
72-
enrollmentAPIKeyId: string | null;
70+
agentConfigId?: string;
71+
enrollmentAPIKeyId?: string;
7372
}>({
74-
agentConfigId: agentConfigs.length ? agentConfigs[0].id : null,
75-
enrollmentAPIKeyId: null,
73+
agentConfigId: agentConfigs.length ? agentConfigs[0].id : undefined,
7674
});
7775
const filteredEnrollmentAPIKeys = React.useMemo(() => {
7876
if (!selectedState.agentConfigId || !enrollmentAPIKeysRequest.data) {
@@ -99,10 +97,10 @@ export const APIKeySelection: React.FunctionComponent<Props> = ({ onKeyChange, a
9997

10098
const [showAPIKeyForm, setShowAPIKeyForm] = useState(false);
10199
const apiKeyForm = useCreateApiKeyForm(selectedState.agentConfigId, async (keyId: string) => {
102-
const res = await enrollmentAPIKeysRequest.refresh();
100+
const res = await enrollmentAPIKeysRequest.sendRequest();
103101
setSelectedState({
104102
...selectedState,
105-
enrollmentAPIKeyId: res.data?.list.find(key => key.id === keyId)?.id ?? null,
103+
enrollmentAPIKeyId: res.data?.list.find(key => key.id === keyId)?.id,
106104
});
107105
setShowAPIKeyForm(false);
108106
});
@@ -135,7 +133,7 @@ export const APIKeySelection: React.FunctionComponent<Props> = ({ onKeyChange, a
135133
onChange={e =>
136134
setSelectedState({
137135
agentConfigId: e.target.value,
138-
enrollmentAPIKeyId: null,
136+
enrollmentAPIKeyId: undefined,
139137
})
140138
}
141139
/>

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/enrollment_api_keys/confirm_delete_modal.tsx

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)