Skip to content

Commit

Permalink
Create Spaces OSS plugin (#87585)
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner authored Jan 12, 2021
1 parent 5fb5605 commit 07a3756
Show file tree
Hide file tree
Showing 75 changed files with 496 additions and 108 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @elastic/kib
# Security
/src/core/server/csp/ @elastic/kibana-security @elastic/kibana-core
/src/plugins/security_oss/ @elastic/kibana-security
/src/plugins/spaces_oss/ @elastic/kibana-security
/test/security_functional/ @elastic/kibana-security
/x-pack/plugins/spaces/ @elastic/kibana-security
/x-pack/plugins/encrypted_saved_objects/ @elastic/kibana-security
Expand Down
4 changes: 4 additions & 0 deletions docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ so they can properly protect the data within their clusters.
|Replaces the legacy ui/share module for registering share context menus.
|{kib-repo}blob/{branch}/src/plugins/spaces_oss/README.md[spacesOss]
|Bridge plugin for consumption of the Spaces feature from OSS plugins.
|{kib-repo}blob/{branch}/src/plugins/telemetry/README.md[telemetry]
|Telemetry allows Kibana features to have usage tracked in the wild. The general term "telemetry" refers to multiple things:
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-optimizer/limits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ pageLoadAssetSize:
stackAlerts: 29684
presentationUtil: 28545
runtimeFieldEditor: 46986
spacesOss: 18817
3 changes: 3 additions & 0 deletions src/plugins/spaces_oss/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SpacesOss

Bridge plugin for consumption of the Spaces feature from OSS plugins.
20 changes: 20 additions & 0 deletions src/plugins/spaces_oss/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { Space } from './types';
29 changes: 29 additions & 0 deletions src/plugins/spaces_oss/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export interface Space {
id: string;
name: string;
description?: string;
color?: string;
initials?: string;
disabledFeatures: string[];
_reserved?: boolean;
imageUrl?: string;
}
24 changes: 24 additions & 0 deletions src/plugins/spaces_oss/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../..',
roots: ['<rootDir>/src/plugins/spaces_oss'],
};
8 changes: 8 additions & 0 deletions src/plugins/spaces_oss/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "spacesOss",
"version": "kibana",
"server": false,
"ui": true,
"requiredPlugins": [],
"optionalPlugins": []
}
30 changes: 30 additions & 0 deletions src/plugins/spaces_oss/public/api.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { of } from 'rxjs';
import { SpacesApi } from './api';

const createApiMock = (): jest.Mocked<SpacesApi> => ({
activeSpace$: of(),
getActiveSpace: jest.fn(),
});

export const spacesApiMock = {
create: createApiMock,
};
29 changes: 29 additions & 0 deletions src/plugins/spaces_oss/public/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Observable } from 'rxjs';
import { Space } from '../common';

/**
* @public
*/
export interface SpacesApi {
readonly activeSpace$: Observable<Space>;
getActiveSpace(): Promise<Space>;
}
26 changes: 26 additions & 0 deletions src/plugins/spaces_oss/public/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { SpacesOssPlugin } from './plugin';

export { SpacesOssPluginSetup, SpacesOssPluginStart } from './types';

export { SpacesApi } from './api';

export const plugin = () => new SpacesOssPlugin();
35 changes: 35 additions & 0 deletions src/plugins/spaces_oss/public/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { spacesApiMock } from '../api.mock';
import { SpacesOssPluginSetup, SpacesOssPluginStart } from '..';

const createSetupContract = (): jest.Mocked<SpacesOssPluginSetup> => ({
registerSpacesApi: jest.fn(),
});

const createStartContract = (): jest.Mocked<SpacesOssPluginStart> => ({
isSpacesAvailable: true,
...spacesApiMock.create(),
});

export const spacesOssPluginMock = {
createSetupContract,
createStartContract,
};
65 changes: 65 additions & 0 deletions src/plugins/spaces_oss/public/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { spacesApiMock } from './api.mock';
import { SpacesOssPlugin } from './plugin';

describe('SpacesOssPlugin', () => {
let plugin: SpacesOssPlugin;

beforeEach(() => {
plugin = new SpacesOssPlugin();
});

describe('#setup', () => {
it('only allows the API to be registered once', async () => {
const spacesApi = spacesApiMock.create();
const { registerSpacesApi } = plugin.setup();

expect(() => registerSpacesApi(spacesApi)).not.toThrow();

expect(() => registerSpacesApi(spacesApi)).toThrowErrorMatchingInlineSnapshot(
`"Spaces API can only be registered once"`
);
});
});

describe('#start', () => {
it('returns the spaces API if registered', async () => {
const spacesApi = spacesApiMock.create();
const { registerSpacesApi } = plugin.setup();

registerSpacesApi(spacesApi);

const { isSpacesAvailable, ...api } = plugin.start();

expect(isSpacesAvailable).toBe(true);
expect(api).toStrictEqual(spacesApi);
});

it('does not return the spaces API if not registered', async () => {
plugin.setup();

const { isSpacesAvailable, ...api } = plugin.start();

expect(isSpacesAvailable).toBe(false);
expect(Object.keys(api)).toHaveLength(0);
});
});
});
52 changes: 52 additions & 0 deletions src/plugins/spaces_oss/public/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Plugin } from 'src/core/public';
import { SpacesOssPluginSetup, SpacesOssPluginStart } from './types';
import { SpacesApi } from './api';

export class SpacesOssPlugin implements Plugin<SpacesOssPluginSetup, SpacesOssPluginStart, {}, {}> {
private api?: SpacesApi;

constructor() {}

public setup() {
return {
registerSpacesApi: (provider: SpacesApi) => {
if (this.api) {
throw new Error('Spaces API can only be registered once');
}
this.api = provider;
},
};
}

public start() {
if (this.api) {
return {
isSpacesAvailable: true as true,
...this.api!,
};
} else {
return {
isSpacesAvailable: false as false,
};
}
}
}
39 changes: 39 additions & 0 deletions src/plugins/spaces_oss/public/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { SpacesApi } from './api';

interface SpacesAvailableStartContract extends SpacesApi {
isSpacesAvailable: true;
}

interface SpacesUnavailableStartContract {
isSpacesAvailable: false;
}

export interface SpacesOssPluginSetup {
/**
* Register a provider for the Spaces API.
*
* Only one provider can be registered, subsequent calls to this method will fail.
*/
registerSpacesApi(provider: SpacesApi): void;
}

export type SpacesOssPluginStart = SpacesAvailableStartContract | SpacesUnavailableStartContract;
Loading

0 comments on commit 07a3756

Please sign in to comment.