Skip to content

Commit

Permalink
fix cleanup request
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Dec 17, 2019
1 parent bdd834f commit a3f48b6
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 40 deletions.
2 changes: 0 additions & 2 deletions src/core/server/elasticsearch/api_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ import {
TasksCancelParams,
TasksGetParams,
TasksListParams,
SnapshotCleanupRepositoryParams,
} from 'elasticsearch';

/**
Expand Down Expand Up @@ -299,7 +298,6 @@ export interface APICaller {
(endpoint: 'snapshot.restore', params: SnapshotRestoreParams, options?: CallAPIOptions): ReturnType<Client['snapshot']['restore']>;
(endpoint: 'snapshot.status', params: SnapshotStatusParams, options?: CallAPIOptions): ReturnType<Client['snapshot']['status']>;
(endpoint: 'snapshot.verifyRepository', params: SnapshotVerifyRepositoryParams, options?: CallAPIOptions): ReturnType<Client['snapshot']['verifyRepository']>;
(endpoint: 'snapshot.cleanupRepository', params: SnapshotCleanupRepositoryParams, options?: CallAPIOptions): ReturnType<Client['snapshot']['cleanupRepository']>;

// tasks namespace
(endpoint: 'tasks.cancel', params: TasksCancelParams, options?: CallAPIOptions): ReturnType<Client['tasks']['cancel']>;
Expand Down
2 changes: 0 additions & 2 deletions src/legacy/core_plugins/elasticsearch/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ import {
SnapshotRestoreParams,
SnapshotStatusParams,
SnapshotVerifyRepositoryParams,
SnapshotCleanupRepositoryParams,
// tasks
TasksCancelParams,
TasksGetParams,
Expand Down Expand Up @@ -342,7 +341,6 @@ export interface CallClusterWithRequest {
(request: Request, endpoint: 'snapshot.restore', params: SnapshotRestoreParams, options?: CallClusterOptions): ReturnType<ESClient['snapshot']['restore']>;
(request: Request, endpoint: 'snapshot.status', params: SnapshotStatusParams, options?: CallClusterOptions): ReturnType<ESClient['snapshot']['status']>;
(request: Request, endpoint: 'snapshot.verifyRepository', params: SnapshotVerifyRepositoryParams, options?: CallClusterOptions): ReturnType<ESClient['snapshot']['verifyRepository']>;
(request: Request, endpoint: 'snapshot.cleanupRepository', params: SnapshotCleanupRepositoryParams, options?: CallClusterOptions): ReturnType<ESClient['snapshot']['cleanupRepository']>;

// tasks namespace
(request: Request, endpoint: 'tasks.cancel', params: TasksCancelParams, options?: CallClusterOptions): ReturnType<ESClient['tasks']['cancel']>;
Expand Down
12 changes: 0 additions & 12 deletions x-pack/legacy/plugins/snapshot_restore/common/types/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,4 @@ export interface InvalidRepositoryVerification {
error: object;
}

export interface ValidRepositoryCleanup {
cleaned: true;
response: object;
}

export interface InvalidRepositoryCleanup {
cleaned: false;
error: object;
}

export type RepositoryVerification = ValidRepositoryVerification | InvalidRepositoryVerification;

export type RepositoryCleanup = ValidRepositoryCleanup | InvalidRepositoryCleanup;
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
export const elasticsearchJsPlugin = (Client: any, config: any, components: any) => {
const ca = components.clientAction.factory;

Client.prototype.slm = components.clientAction.namespaceFactory();
const slm = Client.prototype.slm.prototype;
Client.prototype.sr = components.clientAction.namespaceFactory();
const sr = Client.prototype.sr.prototype;

slm.policies = ca({
sr.policies = ca({
urls: [
{
fmt: '/_slm/policy',
Expand All @@ -19,7 +19,7 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
method: 'GET',
});

slm.policy = ca({
sr.policy = ca({
urls: [
{
fmt: '/_slm/policy/<%=name%>',
Expand All @@ -33,7 +33,7 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
method: 'GET',
});

slm.deletePolicy = ca({
sr.deletePolicy = ca({
urls: [
{
fmt: '/_slm/policy/<%=name%>',
Expand All @@ -47,7 +47,7 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
method: 'DELETE',
});

slm.executePolicy = ca({
sr.executePolicy = ca({
urls: [
{
fmt: '/_slm/policy/<%=name%>/_execute',
Expand All @@ -61,7 +61,7 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
method: 'PUT',
});

slm.updatePolicy = ca({
sr.updatePolicy = ca({
urls: [
{
fmt: '/_slm/policy/<%=name%>',
Expand All @@ -75,12 +75,26 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
method: 'PUT',
});

slm.executeRetention = ca({
sr.executeRetention = ca({
urls: [
{
fmt: '/_slm/_execute_retention',
},
],
method: 'POST',
});

sr.cleanupRepository = ca({
urls: [
{
fmt: '/_snapshot/<%=name%>/_cleanup',
req: {
name: {
type: 'string',
},
},
},
],
method: 'POST',
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const getAllHandler: RouterRouteHandler = async (
// Get policies
const policiesByName: {
[key: string]: SlmPolicyEs;
} = await callWithRequest('slm.policies', {
} = await callWithRequest('sr.policies', {
human: true,
});

Expand All @@ -62,7 +62,7 @@ export const getOneHandler: RouterRouteHandler = async (
const { name } = req.params;
const policiesByName: {
[key: string]: SlmPolicyEs;
} = await callWithRequest('slm.policy', {
} = await callWithRequest('sr.policy', {
name,
human: true,
});
Expand All @@ -82,7 +82,7 @@ export const getOneHandler: RouterRouteHandler = async (

export const executeHandler: RouterRouteHandler = async (req, callWithRequest) => {
const { name } = req.params;
const { snapshot_name: snapshotName } = await callWithRequest('slm.executePolicy', {
const { snapshot_name: snapshotName } = await callWithRequest('sr.executePolicy', {
name,
});
return { snapshotName };
Expand All @@ -98,7 +98,7 @@ export const deleteHandler: RouterRouteHandler = async (req, callWithRequest) =>

await Promise.all(
policyNames.map(name => {
return callWithRequest('slm.deletePolicy', { name })
return callWithRequest('sr.deletePolicy', { name })
.then(() => response.itemsDeleted.push(name))
.catch(e =>
response.errors.push({
Expand All @@ -122,7 +122,7 @@ export const createHandler: RouterRouteHandler = async (req, callWithRequest) =>

// Check that policy with the same name doesn't already exist
try {
const policyByName = await callWithRequest('slm.policy', { name });
const policyByName = await callWithRequest('sr.policy', { name });
if (policyByName[name]) {
throw conflictError;
}
Expand All @@ -134,7 +134,7 @@ export const createHandler: RouterRouteHandler = async (req, callWithRequest) =>
}

// Otherwise create new policy
return await callWithRequest('slm.updatePolicy', {
return await callWithRequest('sr.updatePolicy', {
name,
body: serializePolicy(policy),
});
Expand All @@ -146,10 +146,10 @@ export const updateHandler: RouterRouteHandler = async (req, callWithRequest) =>

// Check that policy with the given name exists
// If it doesn't exist, 404 will be thrown by ES and will be returned
await callWithRequest('slm.policy', { name });
await callWithRequest('sr.policy', { name });

// Otherwise update policy
return await callWithRequest('slm.updatePolicy', {
return await callWithRequest('sr.updatePolicy', {
name,
body: serializePolicy(policy),
});
Expand Down Expand Up @@ -210,5 +210,5 @@ export const updateRetentionSettingsHandler: RouterRouteHandler = async (req, ca
};

export const executeRetentionHandler: RouterRouteHandler = async (_req, callWithRequest) => {
return await callWithRequest('slm.executeRetention');
return await callWithRequest('sr.executeRetention');
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { cleanup } from '@testing-library/react';
import { Router, RouterRouteHandler } from '../../../../../server/lib/create_router';
import {
wrapCustomError,
Expand Down Expand Up @@ -77,7 +76,7 @@ export const getAllHandler: RouterRouteHandler = async (
try {
const policiesByName: {
[key: string]: SlmPolicyEs;
} = await callWithRequest('slm.policies', {
} = await callWithRequest('sr.policies', {
human: true,
});
const managedRepositoryPolicy = Object.entries(policiesByName)
Expand Down Expand Up @@ -182,14 +181,16 @@ export const getCleanupHandler: RouterRouteHandler = async (
cleanup: RepositoryCleanup | {};
}> => {
const { name } = req.params;
const cleanupResults = await callWithRequest('snapshot.cleanupRepository', {
repository: name,

const cleanupResults = await callWithRequest('sr.cleanupRepository', {
name,
}).catch(e => ({
cleaned: false,
error: e.response ? JSON.parse(e.response) : e,
}));

return {
cleanup: cleanupResults.errorgi
cleanup: cleanupResults.error
? cleanupResults
: {
cleaned: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const getAllHandler: RouterRouteHandler = async (
// Attempt to retrieve policies
// This could fail if user doesn't have access to read SLM policies
try {
const policiesByName = await callWithRequest('slm.policies');
const policiesByName = await callWithRequest('sr.policies');
policies = Object.keys(policiesByName);
} catch (e) {
// Silently swallow error as policy names aren't required in UI
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/snapshot_restore/server/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n';
import { Legacy } from 'kibana';
import { createRouter, Router } from '../../../server/lib/create_router';
import { registerLicenseChecker } from '../../../server/lib/register_license_checker';
import { elasticsearchJsPlugin } from './client/elasticsearch_slm';
import { elasticsearchJsPlugin } from './client/elasticsearch_sr';
import { CloudSetup } from '../../../../plugins/cloud/server';
export interface Core {
http: {
Expand Down

0 comments on commit a3f48b6

Please sign in to comment.