-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
copy_to_space.ts
171 lines (163 loc) · 5.58 KB
/
copy_to_space.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* 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 { schema } from '@kbn/config-schema';
import _ from 'lodash';
import { SavedObject } from 'src/core/server';
import {
copySavedObjectsToSpacesFactory,
resolveCopySavedObjectsToSpacesConflictsFactory,
} from '../../../lib/copy_to_spaces';
import { ExternalRouteDeps } from '.';
import { SPACE_ID_REGEX } from '../../../lib/space_schema';
import { createLicensedRouteHandler } from '../../lib';
type SavedObjectIdentifier = Pick<SavedObject, 'id' | 'type'>;
const areObjectsUnique = (objects: SavedObjectIdentifier[]) =>
_.uniqBy(objects, (o: SavedObjectIdentifier) => `${o.type}:${o.id}`).length === objects.length;
export function initCopyToSpacesApi(deps: ExternalRouteDeps) {
const { externalRouter, spacesService, getImportExportObjectLimit, getStartServices } = deps;
externalRouter.post(
{
path: '/api/spaces/_copy_saved_objects',
options: {
tags: ['access:copySavedObjectsToSpaces'],
},
validate: {
body: schema.object(
{
spaces: schema.arrayOf(
schema.string({
validate: (value) => {
if (!SPACE_ID_REGEX.test(value)) {
return `lower case, a-z, 0-9, "_", and "-" are allowed`;
}
},
}),
{
validate: (spaceIds) => {
if (_.uniq(spaceIds).length !== spaceIds.length) {
return 'duplicate space ids are not allowed';
}
},
}
),
objects: schema.arrayOf(
schema.object({
type: schema.string(),
id: schema.string(),
}),
{
validate: (objects) => {
if (!areObjectsUnique(objects)) {
return 'duplicate objects are not allowed';
}
},
}
),
includeReferences: schema.boolean({ defaultValue: false }),
overwrite: schema.boolean({ defaultValue: false }),
createNewCopies: schema.boolean({ defaultValue: false }),
},
{
validate: (object) => {
if (object.overwrite && object.createNewCopies) {
return 'cannot use [overwrite] with [createNewCopies]';
}
},
}
),
},
},
createLicensedRouteHandler(async (context, request, response) => {
const [startServices] = await getStartServices();
const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
startServices.savedObjects,
getImportExportObjectLimit,
request
);
const {
spaces: destinationSpaceIds,
objects,
includeReferences,
overwrite,
createNewCopies,
} = request.body;
const sourceSpaceId = spacesService.getSpaceId(request);
const copyResponse = await copySavedObjectsToSpaces(sourceSpaceId, destinationSpaceIds, {
objects,
includeReferences,
overwrite,
createNewCopies,
});
return response.ok({ body: copyResponse });
})
);
externalRouter.post(
{
path: '/api/spaces/_resolve_copy_saved_objects_errors',
options: {
tags: ['access:copySavedObjectsToSpaces'],
},
validate: {
body: schema.object({
retries: schema.recordOf(
schema.string({
validate: (spaceId) => {
if (!SPACE_ID_REGEX.test(spaceId)) {
return `Invalid space id: ${spaceId}`;
}
},
}),
schema.arrayOf(
schema.object({
type: schema.string(),
id: schema.string(),
overwrite: schema.boolean({ defaultValue: false }),
destinationId: schema.maybe(schema.string()),
createNewCopy: schema.maybe(schema.boolean()),
ignoreMissingReferences: schema.maybe(schema.boolean()),
})
)
),
objects: schema.arrayOf(
schema.object({
type: schema.string(),
id: schema.string(),
}),
{
validate: (objects) => {
if (!areObjectsUnique(objects)) {
return 'duplicate objects are not allowed';
}
},
}
),
includeReferences: schema.boolean({ defaultValue: false }),
createNewCopies: schema.boolean({ defaultValue: false }),
}),
},
},
createLicensedRouteHandler(async (context, request, response) => {
const [startServices] = await getStartServices();
const resolveCopySavedObjectsToSpacesConflicts = resolveCopySavedObjectsToSpacesConflictsFactory(
startServices.savedObjects,
getImportExportObjectLimit,
request
);
const { objects, includeReferences, retries, createNewCopies } = request.body;
const sourceSpaceId = spacesService.getSpaceId(request);
const resolveConflictsResponse = await resolveCopySavedObjectsToSpacesConflicts(
sourceSpaceId,
{
objects,
includeReferences,
retries,
createNewCopies,
}
);
return response.ok({ body: resolveConflictsResponse });
})
);
}