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

[Fleet] Remove deprecated APIs for agents endpoints #198313

Merged
merged 30 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
390065b
POST /service-tokens
nchaulet Oct 29, 2024
da75520
remove GET /agent-status
nchaulet Oct 29, 2024
b85daf4
Remove PUT /agents/:agentid/reassign
nchaulet Oct 29, 2024
2513809
remve GET agent_status total and kuery
nchaulet Oct 30, 2024
a9b7a3a
[CI] Auto-commit changed files from 'node scripts/capture_oas_snapsho…
kibanamachine Oct 30, 2024
89b1931
remove usage of total in other plugins
nchaulet Oct 30, 2024
26bdd2d
remove list from GET agents response
nchaulet Oct 30, 2024
940855e
[CI] Auto-commit changed files from 'node scripts/capture_oas_snapsho…
kibanamachine Oct 30, 2024
f060705
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Oct 30, 2024
26faa4f
fix tests
nchaulet Oct 30, 2024
e8afe38
Merge branch 'feature-remove-deprecated-apis' of github.com:nchaulet/…
nchaulet Oct 30, 2024
0862679
fix types
nchaulet Oct 30, 2024
c46937c
fix types
nchaulet Oct 30, 2024
b941f49
Merge branch 'main' of github.com:elastic/kibana into feature-remove-…
nchaulet Oct 30, 2024
8e49c7f
fix tests
nchaulet Oct 30, 2024
faa4371
[CI] Auto-commit changed files from 'node scripts/capture_oas_snapsho…
kibanamachine Oct 30, 2024
6a25257
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Oct 30, 2024
0cb5503
fix agent status tests
nchaulet Oct 31, 2024
7daa311
fix api integration tests
nchaulet Oct 31, 2024
88089a4
Reintroduce agent status kuery parameter
nchaulet Oct 31, 2024
c65d06b
[CI] Auto-commit changed files from 'node scripts/capture_oas_snapsho…
kibanamachine Oct 31, 2024
c09e839
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Oct 31, 2024
371dd47
fix tests
nchaulet Oct 31, 2024
30560e0
Merge branch 'main' into feature-remove-deprecated-apis
elasticmachine Oct 31, 2024
fd65ce0
fix after review
nchaulet Oct 31, 2024
2dd04fc
[CI] Auto-commit changed files from 'node scripts/capture_oas_snapsho…
kibanamachine Oct 31, 2024
86c884c
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Oct 31, 2024
d821cb0
fix tests
nchaulet Oct 31, 2024
eef3ee2
Merge branch 'main' of github.com:elastic/kibana into feature-remove-…
nchaulet Oct 31, 2024
579ca1b
Merge branch 'main' into feature-remove-deprecated-apis
elasticmachine Oct 31, 2024
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
539 changes: 0 additions & 539 deletions oas_docs/bundle.json

Large diffs are not rendered by default.

539 changes: 0 additions & 539 deletions oas_docs/bundle.serverless.json

Large diffs are not rendered by default.

378 changes: 0 additions & 378 deletions oas_docs/output/kibana.serverless.yaml

Large diffs are not rendered by default.

378 changes: 0 additions & 378 deletions oas_docs/output/kibana.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const createPolicies = (
const agentPolicyStatus = {
id: agentPolicy.id,
name: agentPolicy.name,
agents: agentStatusByAgentPolicyId[agentPolicy.id]?.total,
agents: agentStatusByAgentPolicyId[agentPolicy.id]?.active,
};
return {
package_policy: cloudDefendPackage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const getBenchmarksData = (
const agentPolicyStatus = {
id: agentPolicy.id,
name: agentPolicy.name,
agents: agentStatusByAgentPolicyId[agentPolicy.id]?.total,
agents: agentStatusByAgentPolicyId[agentPolicy.id]?.active,
};
return {
package_policy: cspPackage,
Expand Down
4 changes: 0 additions & 4 deletions x-pack/plugins/fleet/common/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ export const APP_API_ROUTES = {
CHECK_PERMISSIONS_PATTERN: `${API_ROOT}/check-permissions`,
GENERATE_SERVICE_TOKEN_PATTERN: `${API_ROOT}/service_tokens`,
AGENT_POLICIES_SPACES: `${INTERNAL_ROOT}/agent_policies_spaces`,
// deprecated since 8.0
GENERATE_SERVICE_TOKEN_PATTERN_DEPRECATED: `${API_ROOT}/service-tokens`,
};

// Agent API routes
Expand All @@ -159,8 +157,6 @@ export const AGENT_API_ROUTES = {
AVAILABLE_VERSIONS_PATTERN: `${API_ROOT}/agents/available_versions`,
STATUS_PATTERN: `${API_ROOT}/agent_status`,
DATA_PATTERN: `${API_ROOT}/agent_status/data`,
// deprecated since 8.0
STATUS_PATTERN_DEPRECATED: `${API_ROOT}/agent-status`,
UPGRADE_PATTERN: `${API_ROOT}/agents/{agentId}/upgrade`,
BULK_UPGRADE_PATTERN: `${API_ROOT}/agents/bulk_upgrade`,
ACTION_STATUS_PATTERN: `${API_ROOT}/agents/action_status`,
Expand Down
16 changes: 1 addition & 15 deletions x-pack/plugins/fleet/common/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import type { ListResult, ListWithKuery } from './common';

export interface GetAgentsRequest {
query: ListWithKuery & {
showInactive: boolean;
showInactive?: boolean;
showUpgradeable?: boolean;
withMetrics?: boolean;
};
}

export interface GetAgentsResponse extends ListResult<Agent> {
// deprecated in 8.x
list?: Agent[];
statusSummary?: Record<AgentStatus, number>;
}

Expand Down Expand Up @@ -128,16 +126,6 @@ export type PostBulkAgentUpgradeResponse = BulkAgentAction;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface PostAgentUpgradeResponse {}

// deprecated
export interface PutAgentReassignRequest {
params: {
agentId: string;
};
body: { policy_id: string };
}
// deprecated
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface PutAgentReassignResponse {}
export interface PostAgentReassignRequest {
params: {
agentId: string;
Expand Down Expand Up @@ -217,8 +205,6 @@ export interface GetAgentStatusRequest {
export interface GetAgentStatusResponse {
results: {
events: number;
// deprecated
total: number;
online: number;
error: number;
offline: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
let count = 0;
for (const policyId of agentPolicyIds) {
const { data } = await sendGetAgentStatus({ policyId });
if (data?.results.total) {
count += data.results.total;
if (data?.results.active) {
count += data.results.active;
}
}
setAgentCount(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ export const SettingsView = memo<{ agentPolicy: AgentPolicy }>(
if (isFleetEnabled) {
setIsLoading(true);
const { data } = await sendGetAgentStatus({ policyId: agentPolicy.id });
if (data?.results.total) {
setAgentCount(data.results.total);
if (data?.results.active) {
setAgentCount(data.results.active);
} else {
await submitUpdateAgentPolicy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ export const EditPackagePolicyForm = memo<{
let count = 0;
for (const id of packagePolicy.policy_ids) {
const { data } = await sendGetAgentStatus({ policyId: id });
if (data?.results.total) {
count += data.results.total;
if (data?.results.active) {
count += data.results.active;
}
}
setAgentCount(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
usePagination,
useGetAgentPolicies,
sendGetAgents,
sendGetAgentStatus,
useUrlParams,
useStartServices,
sendGetAgentTags,
Expand Down Expand Up @@ -218,8 +217,10 @@ export function useFetchAgentsData() {
getStatusSummary: true,
withMetrics: displayAgentMetrics,
}),
sendGetAgentStatus({
sendGetAgents({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are not going to deprecate usage of kuery in agent_status, is this change still needed? Or you prefer to change it anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove those changes 👍

kuery: AgentStatusKueryHelper.buildKueryForInactiveAgents(),
showInactive,
perPage: 0,
}),
sendGetAgentPolicies({
kuery: `${LEGACY_AGENT_POLICY_SAVED_OBJECT_TYPE}.is_managed:true`,
Expand Down Expand Up @@ -298,7 +299,7 @@ export function useFetchAgentsData() {

setAgentsOnCurrentPage(agentsResponse.data.items);
setNAgentsInTable(agentsResponse.data.total);
setTotalInactiveAgents(totalInactiveAgentsResponse.data.results.inactive || 0);
setTotalInactiveAgents(totalInactiveAgentsResponse.data.total || 0);

const managedAgentPolicies = managedAgentPoliciesResponse.data?.items ?? [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,27 @@ describe('agent_list_page', () => {
jest.useRealTimers();
});

it('should not send another agents status request if first one takes longer', () => {
mockedSendGetAgentStatus.mockImplementation(async () => {
const sleep = () => {
return new Promise((res) => {
it('should not send another call to retrieve inactive agents count if first one takes longer', () => {
let calls = 0;
mockedSendGetAgents.mockImplementation(async () => {
calls++;
if (calls === 1) {
await new Promise((res) => {
setTimeout(() => res({}), 35000);
});
}
return {
data: {
total: 0,
},
};
await sleep();

return {
data: {
results: {
inactive: 0,
items: mapAgents(['agent1', 'agent2', 'agent3', 'agent4', 'agent5']),
total: 6,
statusSummary: {
online: 6,
},
},
};
Expand All @@ -166,7 +175,7 @@ describe('agent_list_page', () => {
jest.advanceTimersByTime(65000);
});

expect(mockedSendGetAgentStatus).toHaveBeenCalledTimes(1);
expect(mockedSendGetAgents).toHaveBeenCalledTimes(2);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jest.mock('../../../../hooks', () => {
items: ['8.10.2', '8.7.0'],
},
}),
sendGetAgentStatus: jest.fn().mockResolvedValue({
data: { results: { updating: 2 } },
sendGetAgentsQuery: jest.fn().mockResolvedValue({
total: 2,
}),
sendPostBulkAgentUpgrade: jest.fn(),
useAgentVersion: jest.fn().mockReturnValue('8.10.2'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import {
useStartServices,
useKibanaVersion,
useConfig,
sendGetAgentStatus,
useAgentVersion,
sendGetAllFleetServerAgents,
sendGetAgentsQuery,
} from '../../../../hooks';

import { sendGetAgentsAvailableVersions } from '../../../../hooks';
Expand Down Expand Up @@ -135,10 +135,11 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<AgentUpgradeAgentMo

// if selection is a query, do an api call to get updating agents
try {
const res = await sendGetAgentStatus({
const res = await sendGetAgentsQuery({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

kuery: newQuery,
perPage: 0,
});
setUpdatingAgents(res?.data?.results?.updating ?? 0);
setUpdatingAgents(res?.total ?? 0);
} catch (err) {
return;
}
Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/fleet/public/hooks/use_request/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import type {
PostRetrieveAgentsByActionsResponse,
} from '../../types';

import { useRequest, sendRequest } from './use_request';
import { useRequest, sendRequest, sendRequestForRq } from './use_request';
import type { UseRequestConfig } from './use_request';

type RequestOptions = Pick<Partial<UseRequestConfig>, 'pollIntervalMs'>;
Expand Down Expand Up @@ -100,6 +100,16 @@ export function sendGetAgents(query: GetAgentsRequest['query'], options?: Reques
});
}

export function sendGetAgentsQuery(query: GetAgentsRequest['query'], options?: RequestOptions) {
return sendRequestForRq<GetAgentsResponse>({
method: 'get',
path: agentRouteService.getListPath(),
version: API_VERSIONS.public.v1,
query,
...options,
});
}

export function useGetAgentStatus(query: GetAgentStatusRequest['query'], options?: RequestOptions) {
return useRequest<GetAgentStatusResponse>({
method: 'get',
Expand Down
30 changes: 2 additions & 28 deletions x-pack/plugins/fleet/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* 2.0.
*/

import { uniq } from 'lodash';
import { omit, uniq } from 'lodash';
import { type RequestHandler, SavedObjectsErrorHelpers } from '@kbn/core/server';
import type { TypeOf } from '@kbn/config-schema';

import type {
GetAgentsResponse,
GetOneAgentResponse,
GetAgentStatusResponse,
PutAgentReassignResponse,
GetAgentTagsResponse,
GetAvailableVersionsResponse,
GetActionStatusResponse,
Expand All @@ -30,7 +29,6 @@ import type {
DeleteAgentRequestSchema,
GetAgentStatusRequestSchema,
GetAgentDataRequestSchema,
PutAgentReassignRequestSchemaDeprecated,
PostAgentReassignRequestSchema,
PostBulkAgentReassignRequestSchema,
PostBulkUpdateAgentTagsRequestSchema,
Expand Down Expand Up @@ -207,7 +205,6 @@ export const getAgentsHandler: FleetRequestHandler<
}

const body: GetAgentsResponse = {
list: agents, // deprecated
items: agents,
total,
page,
Expand Down Expand Up @@ -243,29 +240,6 @@ export const getAgentTagsHandler: RequestHandler<
}
};

export const putAgentsReassignHandlerDeprecated: RequestHandler<
TypeOf<typeof PutAgentReassignRequestSchemaDeprecated.params>,
undefined,
TypeOf<typeof PutAgentReassignRequestSchemaDeprecated.body>
> = async (context, request, response) => {
const coreContext = await context.core;
const soClient = coreContext.savedObjects.client;
const esClient = coreContext.elasticsearch.client.asInternalUser;
try {
await AgentService.reassignAgent(
soClient,
esClient,
request.params.agentId,
request.body.policy_id
);

const body: PutAgentReassignResponse = {};
return response.ok({ body });
} catch (error) {
return defaultFleetErrorHandler({ error, response });
}
};

export const postAgentReassignHandler: RequestHandler<
TypeOf<typeof PostAgentReassignRequestSchema.params>,
undefined,
Expand Down Expand Up @@ -341,7 +315,7 @@ export const getAgentStatusForAgentPolicyHandler: FleetRequestHandler<
parsePolicyIds(request.query.policyIds)
);

const body: GetAgentStatusResponse = { results };
const body: GetAgentStatusResponse = { results: omit(results, 'total') };

return response.ok({ body });
} catch (error) {
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/fleet/server/routes/agent/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ describe('schema validation', () => {
it('list agents should return valid response', async () => {
const expectedResponse: GetAgentsResponse = {
items: [agent],
list: [agent],
total: 1,
page: 1,
perPage: 1,
Expand Down Expand Up @@ -366,7 +365,6 @@ describe('schema validation', () => {
const expectedResponse: GetAgentStatusResponse = {
results: {
events: 1,
total: 1,
online: 1,
error: 1,
offline: 1,
Expand Down
35 changes: 0 additions & 35 deletions x-pack/plugins/fleet/server/routes/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
GetAgentStatusRequestSchema,
GetAgentDataRequestSchema,
PostNewAgentActionRequestSchema,
PutAgentReassignRequestSchemaDeprecated,
PostAgentReassignRequestSchema,
PostBulkAgentReassignRequestSchema,
PostAgentUpgradeRequestSchema,
Expand Down Expand Up @@ -68,7 +67,6 @@ import {
updateAgentHandler,
deleteAgentHandler,
getAgentStatusForAgentPolicyHandler,
putAgentsReassignHandlerDeprecated,
postBulkAgentReassignHandler,
getAgentDataHandler,
bulkUpdateAgentTagsHandler,
Expand Down Expand Up @@ -391,23 +389,6 @@ export const registerAPIRoutes = (router: FleetAuthzRouter, config: FleetConfigT
postAgentUnenrollHandler
);

router.versioned
.put({
path: AGENT_API_ROUTES.REASSIGN_PATTERN,
fleetAuthz: {
fleet: { allAgents: true },
},
// @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo}
deprecated: true,
})
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: { request: PutAgentReassignRequestSchemaDeprecated },
},
putAgentsReassignHandlerDeprecated
);

router.versioned
.post({
path: AGENT_API_ROUTES.REASSIGN_PATTERN,
Expand Down Expand Up @@ -613,22 +594,6 @@ export const registerAPIRoutes = (router: FleetAuthzRouter, config: FleetConfigT
},
getAgentStatusForAgentPolicyHandler
);
router.versioned
.get({
path: AGENT_API_ROUTES.STATUS_PATTERN_DEPRECATED,
fleetAuthz: {
fleet: { readAgents: true },
},
// @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo}
deprecated: true,
})
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: { request: GetAgentStatusRequestSchema },
},
getAgentStatusForAgentPolicyHandler
);
// Agent data
router.versioned
.get({
Expand Down
Loading