Skip to content

Commit

Permalink
Add force unenroll
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Jun 30, 2020
1 parent 2d0406f commit 06579cd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/ingest_manager/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export type AgentType =
| typeof AGENT_TYPE_TEMPORARY;

export type AgentStatus = 'offline' | 'error' | 'online' | 'inactive' | 'warning';

export type AgentActionType = 'CONFIG_CHANGE' | 'DATA_DUMP' | 'RESUME' | 'PAUSE' | 'UNENROLL';
export interface NewAgentAction {
type: 'CONFIG_CHANGE' | 'DATA_DUMP' | 'RESUME' | 'PAUSE' | 'UNENROLL';
type: AgentActionType;
data?: any;
sent_at?: string;
}
Expand All @@ -26,7 +26,7 @@ export interface AgentAction extends NewAgentAction {
}

export interface AgentActionSOAttributes {
type: 'CONFIG_CHANGE' | 'DATA_DUMP' | 'RESUME' | 'PAUSE';
type: AgentActionType;
sent_at?: string;
timestamp?: string;
created_at: string;
Expand Down
21 changes: 0 additions & 21 deletions x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
GetOneAgentEventsResponse,
PostAgentCheckinResponse,
PostAgentEnrollResponse,
PostAgentUnenrollResponse,
GetAgentStatusResponse,
PutAgentReassignResponse,
} from '../../../common/types';
Expand All @@ -25,7 +24,6 @@ import {
GetOneAgentEventsRequestSchema,
PostAgentCheckinRequestSchema,
PostAgentEnrollRequestSchema,
PostAgentUnenrollRequestSchema,
GetAgentStatusRequestSchema,
PutAgentReassignRequestSchema,
} from '../../types';
Expand Down Expand Up @@ -302,25 +300,6 @@ export const getAgentsHandler: RequestHandler<
}
};

export const postAgentsUnenrollHandler: RequestHandler<TypeOf<
typeof PostAgentUnenrollRequestSchema.params
>> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
try {
await AgentService.unenrollAgent(soClient, request.params.agentId);

const body: PostAgentUnenrollResponse = {
success: true,
};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
}
};

export const putAgentsReassignHandler: RequestHandler<
TypeOf<typeof PutAgentReassignRequestSchema.params>,
undefined,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/ingest_manager/server/routes/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ import {
getAgentEventsHandler,
postAgentCheckinHandler,
postAgentEnrollHandler,
postAgentsUnenrollHandler,
getAgentStatusForConfigHandler,
putAgentsReassignHandler,
} from './handlers';
import { postAgentAcksHandlerBuilder } from './acks_handlers';
import * as AgentService from '../../services/agents';
import { postNewAgentActionHandlerBuilder } from './actions_handlers';
import { appContextService } from '../../services';
import { postAgentsUnenrollHandler } from './unenroll_handler';

export const registerRoutes = (router: IRouter) => {
// Get one
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { RequestHandler } from 'src/core/server';
import { TypeOf } from '@kbn/config-schema';
import { PostAgentUnenrollResponse } from '../../../common/types';
import { PostAgentUnenrollRequestSchema } from '../../types';
import * as AgentService from '../../services/agents';

export const postAgentsUnenrollHandler: RequestHandler<
TypeOf<typeof PostAgentUnenrollRequestSchema.params>,
undefined,
TypeOf<typeof PostAgentUnenrollRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
try {
if (request.body?.force === true) {
await AgentService.unenrollAgent(soClient, request.params.agentId);
} else {
await AgentService.forceUnenrollAgent(soClient, request.params.agentId);
}

const body: PostAgentUnenrollResponse = {
success: true,
};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
}
};
5 changes: 5 additions & 0 deletions x-pack/plugins/ingest_manager/server/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export const PostAgentUnenrollRequestSchema = {
params: schema.object({
agentId: schema.string(),
}),
body: schema.maybe(
schema.object({
force: schema.boolean(),
})
),
};

export const PutAgentReassignRequestSchema = {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/test/api_integration/apis/fleet/unenroll_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function (providerContext: FtrProviderContext) {
.post(`/api/ingest_manager/fleet/agents/agent1/unenroll`)
.set('kbn-xsrf', 'xxx')
.send({
ids: ['agent1'],
force: true,
})
.expect(200);

Expand All @@ -80,7 +80,7 @@ export default function (providerContext: FtrProviderContext) {
.post(`/api/ingest_manager/fleet/agents/agent1/unenroll`)
.set('kbn-xsrf', 'xxx')
.send({
ids: ['agent1'],
force: true,
})
.expect(200);

Expand Down

0 comments on commit 06579cd

Please sign in to comment.