Skip to content

Commit

Permalink
[Snapshot & Restore] Fix repository types on Cloud (#125656)
Browse files Browse the repository at this point in the history
* [Snapshot & Restore] Fix repository types on Cloud

* [Snapshot & Restore] Move repo types tests into `on-prem` describe block

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 37dfc22)

# Conflicts:
#	x-pack/plugins/snapshot_restore/server/routes/api/repositories.test.ts
#	x-pack/plugins/snapshot_restore/server/routes/api/repositories.ts
  • Loading branch information
yuliacech committed Feb 16, 2022
1 parent 91ee460 commit 9f0c7c8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 26 deletions.
9 changes: 6 additions & 3 deletions x-pack/plugins/snapshot_restore/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ export enum REPOSITORY_TYPES {
}

// Deliberately do not include `source` as a default repository since we treat it as a flag
export const DEFAULT_REPOSITORY_TYPES: RepositoryType[] = [
export const ON_PREM_REPOSITORY_TYPES: RepositoryType[] = [
REPOSITORY_TYPES.fs,
REPOSITORY_TYPES.url,
];

export const MODULE_REPOSITORY_TYPES: RepositoryType[] = [
REPOSITORY_TYPES.azure,
REPOSITORY_TYPES.gcs,
REPOSITORY_TYPES.s3,
REPOSITORY_TYPES.fs,
REPOSITORY_TYPES.url,
];

export const PLUGIN_REPOSITORY_TYPES: RepositoryType[] = [REPOSITORY_TYPES.hdfs];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* 2.0.
*/

import { DEFAULT_REPOSITORY_TYPES, REPOSITORY_PLUGINS_MAP } from '../../../common/constants';
import {
ON_PREM_REPOSITORY_TYPES,
MODULE_REPOSITORY_TYPES,
REPOSITORY_PLUGINS_MAP,
} from '../../../common';
import { addBasePath } from '../helpers';
import { registerRepositoriesRoutes } from './repositories';
import { RouterMock, routeDependencies, RequestMock } from '../../test/helpers';
Expand Down Expand Up @@ -253,32 +257,40 @@ describe('[Snapshot and Restore API Routes] Repositories', () => {
path: addBasePath('repository_types'),
};

it('should return default types if no repository plugins returned from ES', async () => {
catPluginsFn.mockResolvedValue({ body: {} });
// TODO add Cloud specific tests for repo types
describe('on prem', () => {
it('returns module types and on-prem types if no repository plugins returned from ES', async () => {
catPluginsFn.mockResolvedValue({ body: {} });

const expectedResponse = [...DEFAULT_REPOSITORY_TYPES];
await expect(router.runRequest(mockRequest)).resolves.toEqual({ body: expectedResponse });
});
const expectedResponse = [...MODULE_REPOSITORY_TYPES, ...ON_PREM_REPOSITORY_TYPES];
await expect(router.runRequest(mockRequest)).resolves.toEqual({ body: expectedResponse });
});

it('should return default types with any repository plugins returned from ES', async () => {
const pluginNames = Object.keys(REPOSITORY_PLUGINS_MAP);
const pluginTypes = Object.entries(REPOSITORY_PLUGINS_MAP).map(([key, value]) => value);
it('returns module types and on-prem types with any repository plugins returned from ES', async () => {
const pluginNames = Object.keys(REPOSITORY_PLUGINS_MAP);
const pluginTypes = Object.entries(REPOSITORY_PLUGINS_MAP).map(([key, value]) => value);

const mockEsResponse = [...pluginNames.map((key) => ({ component: key }))];
catPluginsFn.mockResolvedValue({ body: mockEsResponse });
const mockEsResponse = [...pluginNames.map((key) => ({ component: key }))];
catPluginsFn.mockResolvedValue({ body: mockEsResponse });

const expectedResponse = [...DEFAULT_REPOSITORY_TYPES, ...pluginTypes];
await expect(router.runRequest(mockRequest)).resolves.toEqual({ body: expectedResponse });
});

it('should not return non-repository plugins returned from ES', async () => {
const pluginNames = ['foo-plugin', 'bar-plugin'];
const mockEsResponse = [...pluginNames.map((key) => ({ component: key }))];
catPluginsFn.mockResolvedValue({ body: mockEsResponse });
const expectedResponse = [
...MODULE_REPOSITORY_TYPES,
...ON_PREM_REPOSITORY_TYPES,
...pluginTypes,
];
await expect(router.runRequest(mockRequest)).resolves.toEqual({ body: expectedResponse });
});

const expectedResponse = [...DEFAULT_REPOSITORY_TYPES];
it(`doesn't return non-repository plugins returned from ES`, async () => {
const pluginNames = ['foo-plugin', 'bar-plugin'];
const mockEsResponse = [...pluginNames.map((key) => ({ component: key }))];
catPluginsFn.mockResolvedValue({ body: mockEsResponse });

await expect(router.runRequest(mockRequest)).resolves.toEqual({ body: expectedResponse });
const expectedResponse = [...MODULE_REPOSITORY_TYPES, ...ON_PREM_REPOSITORY_TYPES];

await expect(router.runRequest(mockRequest)).resolves.toEqual({ body: expectedResponse });
});
});

it('should throw if ES error', async () => {
Expand Down
13 changes: 10 additions & 3 deletions x-pack/plugins/snapshot_restore/server/routes/api/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import type {
SnapshotRepositorySettings,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import { DEFAULT_REPOSITORY_TYPES, REPOSITORY_PLUGINS_MAP } from '../../../common/constants';
import {
ON_PREM_REPOSITORY_TYPES,
REPOSITORY_PLUGINS_MAP,
MODULE_REPOSITORY_TYPES,
} from '../../../common';
import { Repository, RepositoryType } from '../../../common/types';
import { RouteDependencies } from '../../types';
import { addBasePath } from '../helpers';
Expand Down Expand Up @@ -158,8 +162,11 @@ export function registerRepositoriesRoutes({
{ path: addBasePath('repository_types'), validate: false },
license.guardApiRoute(async (ctx, req, res) => {
const { client: clusterClient } = ctx.core.elasticsearch;
// In ECE/ESS, do not enable the default types
const types: RepositoryType[] = isCloudEnabled ? [] : [...DEFAULT_REPOSITORY_TYPES];
// module repo types are available everywhere out of the box
// on-prem repo types are not available on Cloud
const types: RepositoryType[] = isCloudEnabled
? [...MODULE_REPOSITORY_TYPES]
: [...MODULE_REPOSITORY_TYPES, ...ON_PREM_REPOSITORY_TYPES];

try {
// Call with internal user so that the requesting user does not need `monitoring` cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
describe('Snapshot and Restore', () => {
loadTestFile(require.resolve('./policies'));
loadTestFile(require.resolve('./snapshots'));
loadTestFile(require.resolve('./repositories'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';

import { FtrProviderContext } from '../../../ftr_provider_context';

const API_BASE_PATH = '/api/snapshot_restore';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const deployment = getService('deployment');

describe('snapshot repositories', function () {
describe('repository types', () => {
it('returns a list of default repository types', async () => {
const { body } = await supertest.get(`${API_BASE_PATH}/repository_types`).expect(200);

const isCloud = await deployment.isCloud();
if (isCloud) {
// on Cloud there are only module repo types
expect(body).to.eql(['azure', 'gcs', 's3']);
} else {
// on prem there are module repo types and file system and url repo types
expect(body).to.eql(['azure', 'gcs', 's3', 'fs', 'url']);
}
});
});
});
}

0 comments on commit 9f0c7c8

Please sign in to comment.