-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathspaces_client.ts
179 lines (149 loc) · 5.45 KB
/
spaces_client.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
172
173
174
175
176
177
178
179
/*
* 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 Boom from '@hapi/boom';
import { omit } from 'lodash';
import type { LegacyUrlAliasTarget } from '@kbn/core-saved-objects-common';
import type {
ISavedObjectsPointInTimeFinder,
ISavedObjectsRepository,
SavedObject,
} from '@kbn/core/server';
import type { GetAllSpacesOptions, GetAllSpacesPurpose, GetSpaceResult, Space } from '../../common';
import { isReservedSpace } from '../../common';
import type { ConfigType } from '../config';
const SUPPORTED_GET_SPACE_PURPOSES: GetAllSpacesPurpose[] = [
'any',
'copySavedObjectsIntoSpace',
'findSavedObjects',
'shareSavedObjectsIntoSpace',
];
const DEFAULT_PURPOSE = 'any';
const LEGACY_URL_ALIAS_TYPE = 'legacy-url-alias';
/**
* Client interface for interacting with spaces.
*/
export interface ISpacesClient {
/**
* Retrieve all available spaces.
* @param options controls which spaces are retrieved.
*/
getAll(options?: GetAllSpacesOptions): Promise<GetSpaceResult[]>;
/**
* Retrieve a space by its id.
* @param id the space id.
*/
get(id: string): Promise<Space>;
/**
* Creates a space.
* @param space the space to create.
*/
create(space: Space): Promise<Space>;
/**
* Updates a space.
* @param id the id of the space to update.
* @param space the updated space.
*/
update(id: string, space: Space): Promise<Space>;
/**
* Returns a {@link ISavedObjectsPointInTimeFinder} to help page through
* saved objects within the specified space.
* @param id the id of the space to search.
*/
createSavedObjectFinder(id: string): ISavedObjectsPointInTimeFinder<unknown, unknown>;
/**
* Deletes a space, and all saved objects belonging to that space.
* @param id the id of the space to delete.
*/
delete(id: string): Promise<void>;
/**
* Disables the specified legacy URL aliases.
* @param aliases the aliases to disable.
*/
disableLegacyUrlAliases(aliases: LegacyUrlAliasTarget[]): Promise<void>;
}
/**
* Client for interacting with spaces.
*/
export class SpacesClient implements ISpacesClient {
constructor(
private readonly debugLogger: (message: string) => void,
private readonly config: ConfigType,
private readonly repository: ISavedObjectsRepository,
private readonly nonGlobalTypeNames: string[]
) {}
public async getAll(options: GetAllSpacesOptions = {}): Promise<GetSpaceResult[]> {
const { purpose = DEFAULT_PURPOSE } = options;
if (!SUPPORTED_GET_SPACE_PURPOSES.includes(purpose)) {
throw Boom.badRequest(`unsupported space purpose: ${purpose}`);
}
this.debugLogger(`SpacesClient.getAll(). querying all spaces`);
const { saved_objects: savedObjects } = await this.repository.find({
type: 'space',
page: 1,
perPage: this.config.maxSpaces,
sortField: 'name.keyword',
});
this.debugLogger(`SpacesClient.getAll(). Found ${savedObjects.length} spaces.`);
return savedObjects.map(this.transformSavedObjectToSpace);
}
public async get(id: string) {
const savedObject = await this.repository.get('space', id);
return this.transformSavedObjectToSpace(savedObject);
}
public async create(space: Space) {
const { total } = await this.repository.find({
type: 'space',
page: 1,
perPage: 0,
});
if (total >= this.config.maxSpaces) {
throw Boom.badRequest(
'Unable to create Space, this exceeds the maximum number of spaces set by the xpack.spaces.maxSpaces setting'
);
}
this.debugLogger(`SpacesClient.create(), using RBAC. Attempting to create space`);
const attributes = omit(space, ['id', '_reserved']);
const id = space.id;
const createdSavedObject = await this.repository.create('space', attributes, { id });
this.debugLogger(`SpacesClient.create(), created space object`);
return this.transformSavedObjectToSpace(createdSavedObject);
}
public async update(id: string, space: Space) {
const attributes = omit(space, 'id', '_reserved');
await this.repository.update('space', id, attributes);
const updatedSavedObject = await this.repository.get('space', id);
return this.transformSavedObjectToSpace(updatedSavedObject);
}
public createSavedObjectFinder(id: string) {
return this.repository.createPointInTimeFinder({
type: this.nonGlobalTypeNames,
namespaces: [id],
});
}
public async delete(id: string) {
const existingSavedObject = await this.repository.get('space', id);
if (isReservedSpace(this.transformSavedObjectToSpace(existingSavedObject))) {
throw Boom.badRequest(`The ${id} space cannot be deleted because it is reserved.`);
}
await this.repository.deleteByNamespace(id);
await this.repository.delete('space', id);
}
public async disableLegacyUrlAliases(aliases: LegacyUrlAliasTarget[]) {
const attributes = { disabled: true };
const objectsToUpdate = aliases.map(({ targetSpace, targetType, sourceId }) => {
const id = `${targetSpace}:${targetType}:${sourceId}`;
return { type: LEGACY_URL_ALIAS_TYPE, id, attributes };
});
await this.repository.bulkUpdate(objectsToUpdate);
}
private transformSavedObjectToSpace(savedObject: SavedObject<any>) {
return {
id: savedObject.id,
...savedObject.attributes,
} as Space;
}
}