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

[7.x] [Snapshot Restore] fix snapshot and repository endpoints to align with ES API #60508

Merged
merged 1 commit into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ describe('[Snapshot and Restore API Routes] Repositories', () => {
[name]: { type: '', settings: {} },
};
const mockEsSnapshotResponse = {
responses: [
{
repository: name,
snapshots: [{}, {}],
},
],
snapshots: [{}, {}],
};

router.callAsCurrentUserResponses = [
Expand Down
20 changes: 3 additions & 17 deletions x-pack/plugins/snapshot_restore/server/routes/api/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,11 @@ export function registerRepositoriesRoutes({
return res.internalError({ body: e });
}

const {
responses: snapshotResponses,
}: {
responses: Array<{
repository: string;
snapshots: any[];
}>;
} = await callAsCurrentUser('snapshot.get', {
const { snapshots } = await callAsCurrentUser('snapshot.get', {
repository: name,
snapshot: '_all',
}).catch(e => ({
responses: [
{
snapshots: null,
},
],
snapshots: null,
}));

if (repositoryByName[name]) {
Expand All @@ -152,10 +141,7 @@ export function registerRepositoriesRoutes({
},
isManagedRepository: managedRepository === name,
snapshots: {
count:
snapshotResponses && snapshotResponses[0] && snapshotResponses[0].snapshots
? snapshotResponses[0].snapshots.length
: null,
count: snapshots ? snapshots.length : null,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,11 @@ describe('[Snapshot and Restore API Routes] Snapshots', () => {
};

const mockGetSnapshotsFooResponse = Promise.resolve({
responses: [
{
repository: 'fooRepository',
snapshots: [{ snapshot: 'snapshot1' }],
},
],
snapshots: [{ snapshot: 'snapshot1' }],
});

const mockGetSnapshotsBarResponse = Promise.resolve({
responses: [
{
repository: 'barRepository',
snapshots: [{ snapshot: 'snapshot2' }],
},
],
snapshots: [{ snapshot: 'snapshot2' }],
});

router.callAsCurrentUserResponses = [
Expand Down Expand Up @@ -168,12 +158,7 @@ describe('[Snapshot and Restore API Routes] Snapshots', () => {

test('returns snapshot object with repository name if returned from ES', async () => {
const mockSnapshotGetEsResponse = {
responses: [
{
repository,
snapshots: [{ snapshot }],
},
],
snapshots: [{ snapshot }],
};

router.callAsCurrentUserResponses = [
Expand Down
28 changes: 9 additions & 19 deletions x-pack/plugins/snapshot_restore/server/routes/api/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,19 @@ export function registerSnapshotsRoutes({
try {
// If any of these repositories 504 they will cost the request significant time.
const {
responses: fetchedResponses,
snapshots: fetchedSnapshots,
}: {
responses: Array<{
repository: 'string';
snapshots: SnapshotDetailsEs[];
}>;
snapshots: SnapshotDetailsEs[];
} = await callAsCurrentUser('snapshot.get', {
repository,
snapshot: '_all',
ignore_unavailable: true, // Allow request to succeed even if some snapshots are unavailable.
});

// Decorate each snapshot with the repository with which it's associated.
fetchedResponses.forEach(({ snapshots: fetchedSnapshots }) => {
fetchedSnapshots.forEach(snapshot => {
snapshots.push(deserializeSnapshotDetails(repository, snapshot, managedRepository));
});

fetchedSnapshots.forEach((snapshot: SnapshotDetailsEs) => {
snapshots.push(deserializeSnapshotDetails(repository, snapshot, managedRepository));
});

repositories.push(repository);
Expand Down Expand Up @@ -128,22 +124,16 @@ export function registerSnapshotsRoutes({

try {
const {
responses: snapshotsResponse,
snapshots: fetchedSnapshots,
}: {
responses: Array<{
repository: string;
snapshots: SnapshotDetailsEs[];
error?: any;
}>;
snapshots: SnapshotDetailsEs[];
} = await callAsCurrentUser('snapshot.get', {
repository,
snapshot: '_all',
ignore_unavailable: true,
});

const snapshotsList =
snapshotsResponse && snapshotsResponse[0] && snapshotsResponse[0].snapshots;
const selectedSnapshot = snapshotsList.find(
const selectedSnapshot = fetchedSnapshots.find(
({ snapshot: snapshotName }) => snapshot === snapshotName
) as SnapshotDetailsEs;

Expand All @@ -152,7 +142,7 @@ export function registerSnapshotsRoutes({
return res.notFound({ body: 'Snapshot not found' });
}

const successfulSnapshots = snapshotsList
const successfulSnapshots = fetchedSnapshots
.filter(({ state }) => state === 'SUCCESS')
.sort((a, b) => {
return +new Date(b.end_time) - +new Date(a.end_time);
Expand Down