Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spaces] - code cleanup #22802

Merged
merged 9 commits into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ui/public/chrome/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare class Chrome {
public getXsrfToken(): string;
public getKibanaVersion(): string;
public getUiSettingsClient(): any;
public setVisible(visible: boolean): any;
}

declare const chrome: Chrome;
Expand Down
1 change: 1 addition & 0 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@kbn/test": "link:../packages/kbn-test",
"@types/expect.js": "^0.3.29",
"@types/jest": "^23.3.1",
"@types/joi": "^10.4.4",
"@types/mocha": "^5.2.5",
"@types/pngjs": "^3.3.1",
"@types/supertest": "^2.0.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
*/

import { isReservedSpace } from './is_reserved_space';
import { Space } from './model/space';

test('it returns true for reserved spaces', () => {
const space = {
_reserved: true
const space: Space = {
id: '',
name: '',
_reserved: true,
};

expect(isReservedSpace(space)).toEqual(true);
});

test('it returns false for non-reserved spaces', () => {
const space = {};
const space: Space = {
id: '',
name: '',
};

expect(isReservedSpace(space)).toEqual(false);
});

test('it handles empty imput', () => {
test('it handles empty input', () => {
// @ts-ignore
expect(isReservedSpace()).toEqual(false);
});
2 changes: 1 addition & 1 deletion x-pack/plugins/spaces/common/is_reserved_space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import { Space } from './model/space';
* @param space the space
* @returns boolean
*/
export function isReservedSpace(space: Space | null): boolean {
export function isReservedSpace(space?: Partial<Space> | null): boolean {
return get(space, '_reserved', false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getSpaceInitials, getSpaceColor } from "./space_attributes";
import { getSpaceColor, getSpaceInitials } from './space_attributes';

describe("getSpaceColor", () => {
describe('getSpaceColor', () => {
test('uses color on the space, when provided', () => {
const space = {
name: 'Foo',
color: '#aabbcc'
color: '#aabbcc',
};

expect(getSpaceColor(space)).toEqual('#aabbcc');
Expand All @@ -37,11 +37,11 @@ describe("getSpaceColor", () => {
});
});

describe("getSpaceInitials", () => {
describe('getSpaceInitials', () => {
test('uses initials on the space, when provided', () => {
const space = {
name: 'Foo',
initials: 'JK'
initials: 'JK',
};

expect(getSpaceInitials(space)).toEqual('JK');
Expand Down
139 changes: 0 additions & 139 deletions x-pack/plugins/spaces/index.js

This file was deleted.

155 changes: 155 additions & 0 deletions x-pack/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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 { resolve } from 'path';
// @ts-ignore
import { AuditLogger } from '../../server/lib/audit_logger';
// @ts-ignore
import { watchStatusAndLicenseToInitialize } from '../../server/lib/watch_status_and_license_to_initialize';
import { registerUserProfileCapabilityFactory } from '../xpack_main/server/lib/user_profile_registry';
import mappings from './mappings.json';
import { SpacesAuditLogger } from './server/lib/audit_logger';
import { checkLicense } from './server/lib/check_license';
import { createDefaultSpace } from './server/lib/create_default_space';
import { createSpacesService } from './server/lib/create_spaces_service';
import { wrapError } from './server/lib/errors';
import { getActiveSpace } from './server/lib/get_active_space';
import { getSpacesUsageCollector } from './server/lib/get_spaces_usage_collector';
import { spacesSavedObjectsClientWrapperFactory } from './server/lib/saved_objects_client/saved_objects_client_wrapper_factory';
import { initSpacesRequestInterceptors } from './server/lib/space_request_interceptors';
import { SpacesClient } from './server/lib/spaces_client';
import { createSpacesTutorialContextFactory } from './server/lib/spaces_tutorial_context_factory';
import { initPublicSpacesApi } from './server/routes/api/public';
import { initPrivateApis } from './server/routes/api/v1';

export const spaces = (kibana: any) =>
new kibana.Plugin({
id: 'spaces',
configPrefix: 'xpack.spaces',
publicDir: resolve(__dirname, 'public'),
require: ['kibana', 'elasticsearch', 'xpack_main'],

config(Joi: any) {
return Joi.object({
enabled: Joi.boolean().default(true),
}).default();
},

uiExports: {
chromeNavControls: ['plugins/spaces/views/nav_control'],
managementSections: ['plugins/spaces/views/management'],
apps: [
{
id: 'space_selector',
title: 'Spaces',
main: 'plugins/spaces/views/space_selector',
url: 'space_selector',
hidden: true,
},
],
hacks: [],
mappings,
savedObjectsSchema: {
space: {
isNamespaceAgnostic: true,
},
},
home: ['plugins/spaces/register_feature'],
injectDefaultVars(server: any) {
return {
spaces: [],
activeSpace: null,
spaceSelectorURL: server.config().get('server.basePath') || '/',
};
},
async replaceInjectedVars(vars: any, request: any, server: any) {
const spacesClient = server.plugins.spaces.spacesClient.getScopedClient(request);
try {
vars.activeSpace = {
valid: true,
space: await getActiveSpace(
spacesClient,
request.getBasePath(),
server.config().get('server.basePath')
),
};
} catch (e) {
vars.activeSpace = {
valid: false,
error: wrapError(e).output.payload,
};
}
return vars;
},
},

async init(server: any) {
const thisPlugin = this;
const xpackMainPlugin = server.plugins.xpack_main;

watchStatusAndLicenseToInitialize(xpackMainPlugin, thisPlugin, async () => {
await createDefaultSpace(server);
});

// Register a function that is called whenever the xpack info changes,
// to re-compute the license check results for this plugin
xpackMainPlugin.info
.feature(thisPlugin.id)
.registerLicenseCheckResultsGenerator(checkLicense);

const spacesService = createSpacesService(server);
server.expose('getSpaceId', (request: any) => spacesService.getSpaceId(request));

const config = server.config();

const spacesAuditLogger = new SpacesAuditLogger(config, new AuditLogger(server, 'spaces'));

server.expose('spacesClient', {
getScopedClient: (request: any) => {
const adminCluster = server.plugins.elasticsearch.getCluster('admin');
const { callWithRequest, callWithInternalUser } = adminCluster;
const callCluster = (...args: any[]) => callWithRequest(request, ...args);
const { savedObjects } = server;
const internalRepository = savedObjects.getSavedObjectsRepository(callWithInternalUser);
const callWithRequestRepository = savedObjects.getSavedObjectsRepository(callCluster);
const authorization = server.plugins.security
? server.plugins.security.authorization
: null;
return new SpacesClient(
spacesAuditLogger,
authorization,
callWithRequestRepository,
internalRepository,
request
);
},
});

const { addScopedSavedObjectsClientWrapperFactory, types } = server.savedObjects;
addScopedSavedObjectsClientWrapperFactory(
Number.MAX_VALUE,
spacesSavedObjectsClientWrapperFactory(spacesService, types)
);

server.addScopedTutorialContextFactory(createSpacesTutorialContextFactory(spacesService));

initPrivateApis(server);
initPublicSpacesApi(server);

initSpacesRequestInterceptors(server);

registerUserProfileCapabilityFactory(async request => {
const spacesClient = server.plugins.spaces.spacesClient.getScopedClient(request);

return {
manageSpaces: await spacesClient.canEnumerateSpaces(),
};
});

// Register a function with server to manage the collection of usage stats
server.usage.collectorSet.register(getSpacesUsageCollector(server));
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { shallow } from 'enzyme';
import React from 'react';
import { SpaceAvatar } from './space_avatar';

test('renders without crashing', () => {
const wrapper = shallow(<SpaceAvatar space={{}} />);
const wrapper = shallow(<SpaceAvatar space={{ name: '', id: '' }} />);
expect(wrapper).toMatchSnapshot();
});
Loading