Skip to content

Commit

Permalink
Initial commit of versioned common space interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jeramysoucy committed Jun 22, 2023
1 parent f0f24ca commit 85df764
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
8 changes: 7 additions & 1 deletion x-pack/plugins/spaces/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ export {
DEFAULT_SPACE_ID,
} from './constants';
export { addSpaceIdToPath, getSpaceIdFromPath } from './lib/spaces_url_parser';
export type { Space, GetAllSpacesOptions, GetAllSpacesPurpose, GetSpaceResult } from './types';
export type {
Space,
GetAllSpacesOptions,
GetAllSpacesPurpose,
GetSpaceResult,
} from './types/latest';
export { spaceV1 } from './types';
8 changes: 8 additions & 0 deletions x-pack/plugins/spaces/common/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * as spaceV1 from './space/v1';
8 changes: 8 additions & 0 deletions x-pack/plugins/spaces/common/types/latest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './space/v1';
File renamed without changes.
39 changes: 27 additions & 12 deletions x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ describe('#getAll', () => {
attributes: {
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// bar: 'foo-bar', // what is this for?
},
},
{
id: 'bar',
attributes: {
name: 'bar-name',
description: 'bar-description',
bar: 'bar-bar',
// bar: 'bar-bar', // what is this for?
},
},
{
id: 'baz',
attributes: {
name: 'baz-name',
description: 'baz-description',
bar: 'baz-bar',
// bar: 'baz-bar', // what is this for?
},
},
];
Expand All @@ -53,19 +53,34 @@ describe('#getAll', () => {
id: 'foo',
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// color: undefined,
// initials: undefined,
// imageUrl: undefined,
// disabledFeatures: undefined,
// _reserved: undefined,
// bar: 'foo-bar',
},
{
id: 'bar',
name: 'bar-name',
description: 'bar-description',
bar: 'bar-bar',
// color: undefined,
// initials: undefined,
// imageUrl: undefined,
// disabledFeatures: undefined,
// _reserved: undefined,
// bar: 'bar-bar',
},
{
id: 'baz',
name: 'baz-name',
description: 'baz-description',
bar: 'baz-bar',
// color: undefined,
// initials: undefined,
// imageUrl: undefined,
// disabledFeatures: undefined,
// _reserved: undefined,
// bar: 'baz-bar',
},
];

Expand Down Expand Up @@ -108,15 +123,15 @@ describe('#get', () => {
attributes: {
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// bar: 'foo-bar', // what is this for?
},
};

const expectedSpace = {
id: 'foo',
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// bar: 'foo-bar',
};

test(`gets space using callWithRequestRepository`, async () => {
Expand Down Expand Up @@ -160,7 +175,7 @@ describe('#create', () => {
attributes: {
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// bar: 'foo-bar', // what is this for?
disabledFeatures: [],
},
};
Expand All @@ -169,7 +184,7 @@ describe('#create', () => {
id,
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// bar: 'foo-bar',
disabledFeatures: [],
};

Expand Down Expand Up @@ -249,7 +264,7 @@ describe('#update', () => {
attributes: {
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// bar: 'foo-bar',
_reserved: true,
disabledFeatures: [],
},
Expand All @@ -259,7 +274,7 @@ describe('#update', () => {
id: 'foo',
name: 'foo-name',
description: 'foo-description',
bar: 'foo-bar',
// bar: 'foo-bar',
_reserved: true,
disabledFeatures: [],
};
Expand Down
44 changes: 29 additions & 15 deletions x-pack/plugins/spaces/server/spaces_client/spaces_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import type {
SavedObject,
} from '@kbn/core/server';

import type { GetAllSpacesOptions, GetAllSpacesPurpose, GetSpaceResult, Space } from '../../common';
import { isReservedSpace } from '../../common';
import type { spaceV1 as v1 } from '../../common';
import type { ConfigType } from '../config';

const SUPPORTED_GET_SPACE_PURPOSES: GetAllSpacesPurpose[] = [
const SUPPORTED_GET_SPACE_PURPOSES: v1.GetAllSpacesPurpose[] = [
'any',
'copySavedObjectsIntoSpace',
'findSavedObjects',
Expand All @@ -36,26 +36,26 @@ export interface ISpacesClient {
* Retrieve all available spaces.
* @param options controls which spaces are retrieved.
*/
getAll(options?: GetAllSpacesOptions): Promise<GetSpaceResult[]>;
getAll(options?: v1.GetAllSpacesOptions): Promise<v1.GetSpaceResult[]>;

/**
* Retrieve a space by its id.
* @param id the space id.
*/
get(id: string): Promise<Space>;
get(id: string): Promise<v1.Space>;

/**
* Creates a space.
* @param space the space to create.
*/
create(space: Space): Promise<Space>;
create(space: v1.Space): Promise<v1.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>;
update(id: string, space: v1.Space): Promise<v1.Space>;

/**
* Returns a {@link ISavedObjectsPointInTimeFinder} to help page through
Expand Down Expand Up @@ -88,7 +88,7 @@ export class SpacesClient implements ISpacesClient {
private readonly nonGlobalTypeNames: string[]
) {}

public async getAll(options: GetAllSpacesOptions = {}): Promise<GetSpaceResult[]> {
public async getAll(options: v1.GetAllSpacesOptions = {}): Promise<v1.GetSpaceResult[]> {
const { purpose = DEFAULT_PURPOSE } = options;
if (!SUPPORTED_GET_SPACE_PURPOSES.includes(purpose)) {
throw Boom.badRequest(`unsupported space purpose: ${purpose}`);
Expand All @@ -113,7 +113,7 @@ export class SpacesClient implements ISpacesClient {
return this.transformSavedObjectToSpace(savedObject);
}

public async create(space: Space) {
public async create(space: v1.Space) {
const { total } = await this.repository.find({
type: 'space',
page: 1,
Expand All @@ -127,18 +127,22 @@ export class SpacesClient implements ISpacesClient {

this.debugLogger(`SpacesClient.create(), using RBAC. Attempting to create space`);

const attributes = omit(space, ['id', '_reserved']);
// const attributes = omit(space, ['id', '_reserved']);
const id = space.id;
const createdSavedObject = await this.repository.create('space', attributes, { id });
const createdSavedObject = await this.repository.create(
'space',
this.generateSpaceAttributes(space),
{ 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);
public async update(id: string, space: v1.Space) {
// const attributes = omit(space, 'id', '_reserved');
await this.repository.update('space', id, this.generateSpaceAttributes(space));
const updatedSavedObject = await this.repository.get('space', id);
return this.transformSavedObjectToSpace(updatedSavedObject);
}
Expand Down Expand Up @@ -173,7 +177,17 @@ export class SpacesClient implements ISpacesClient {
private transformSavedObjectToSpace(savedObject: SavedObject<any>) {
return {
id: savedObject.id,
...savedObject.attributes,
} as Space;
name: savedObject.attributes.name,
description: savedObject.attributes.description,
color: savedObject.attributes.color,
initials: savedObject.attributes.initials,
imageUrl: savedObject.attributes.imageUrl,
disabledFeatures: savedObject.attributes.disabledFeatures,
_reserved: savedObject.attributes._reserved,
} as v1.Space;
}

private generateSpaceAttributes(space: v1.Space) {
return omit(space, ['id', '_reserved']);
}
}

0 comments on commit 85df764

Please sign in to comment.