-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
snapshots.ts
83 lines (71 loc) · 2.72 KB
/
snapshots.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* 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 { Router, RouterRouteHandler } from '../../../../../server/lib/create_router';
import { SnapshotDetails } from '../../../common/types';
import { deserializeSnapshotDetails } from '../../lib';
import { SnapshotDetailsEs } from '../../types';
export function registerSnapshotsRoutes(router: Router) {
router.get('snapshots', getAllHandler);
router.get('snapshots/{repository}/{snapshot}', getOneHandler);
}
export const getAllHandler: RouterRouteHandler = async (
req,
callWithRequest
): Promise<{
snapshots: SnapshotDetails[];
errors: any[];
repositories: string[];
}> => {
const repositoriesByName = await callWithRequest('snapshot.getRepository', {
repository: '_all',
});
const repositoryNames = Object.keys(repositoriesByName);
if (repositoryNames.length === 0) {
return { snapshots: [], errors: [], repositories: [] };
}
const snapshots: SnapshotDetails[] = [];
const errors: any = {};
const repositories: string[] = [];
const fetchSnapshotsForRepository = async (repository: string) => {
try {
// If any of these repositories 504 they will cost the request significant time.
const {
snapshots: fetchedSnapshots,
}: { snapshots: SnapshotDetailsEs[] } = await callWithRequest('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.
fetchedSnapshots.forEach((snapshot: SnapshotDetailsEs) => {
snapshots.push(deserializeSnapshotDetails(repository, snapshot));
});
repositories.push(repository);
} catch (error) {
// These errors are commonly due to a misconfiguration in the repository or plugin errors,
// which can result in a variety of 400, 404, and 500 errors.
errors[repository] = error;
}
};
await Promise.all(repositoryNames.map(fetchSnapshotsForRepository));
return {
snapshots,
repositories,
errors,
};
};
export const getOneHandler: RouterRouteHandler = async (
req,
callWithRequest
): Promise<SnapshotDetails> => {
const { repository, snapshot } = req.params;
const { snapshots }: { snapshots: SnapshotDetailsEs[] } = await callWithRequest('snapshot.get', {
repository,
snapshot,
});
// If the snapshot is missing the endpoint will return a 404, so we'll never get to this point.
return deserializeSnapshotDetails(repository, snapshots[0]);
};