Skip to content

Commit

Permalink
finish plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed Nov 27, 2019
1 parent 70dba2f commit 526d33e
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 121 deletions.
9 changes: 3 additions & 6 deletions x-pack/legacy/plugins/upgrade_assistant/index.ts
Expand Up @@ -43,9 +43,11 @@ export function upgradeAssistant(kibana: any) {
init(server: Legacy.Server) {
// Add server routes and initialize the plugin here
const instance = plugin({} as any);
const { usageCollection } = server.newPlatform.setup.plugins;

const { usageCollection, cloud } = server.newPlatform.setup.plugins;
instance.setup(server.newPlatform.setup.core, {
usageCollection,
cloud,
__LEGACY: {
// Legacy objects
events: server.events,
Expand All @@ -58,11 +60,6 @@ export function upgradeAssistant(kibana: any) {
plugins: {
elasticsearch: server.plugins.elasticsearch,
xpack_main: server.plugins.xpack_main,
cloud: {
config: {
isCloudEnabled: _.get(server.plugins, 'cloud.config.isCloudEnabled', false),
},
},
},
},
});
Expand Down
Expand Up @@ -11,13 +11,19 @@ import { registerUpgradeAssistantUsageCollector } from './lib/telemetry';
import { registerClusterCheckupRoutes } from './routes/cluster_checkup';
import { registerDeprecationLoggingRoutes } from './routes/deprecation_logging';
import { registerReindexIndicesRoutes, registerReindexWorker } from './routes/reindex_indices';

import { CloudSetup } from '../../../../../plugins/cloud/server';
import { registerTelemetryRoutes } from './routes/telemetry';

interface PluginsSetup {
__LEGACY: ServerShim
usageCollection: UsageCollectionSetup;
cloud: CloudSetup;
}

export class UpgradeAssistantServerPlugin implements Plugin<void, void, object, object> {
setup(
{ http }: CoreSetup,
{ __LEGACY, usageCollection }: { usageCollection: UsageCollectionSetup; __LEGACY: ServerShim }
{ __LEGACY, usageCollection, cloud }: PluginsSetup
) {
const router = http.createRouter();
const shimWithRouter: ServerShimWithRouter = { ...__LEGACY, router };
Expand Down
Expand Up @@ -12,11 +12,6 @@ export interface ServerShim {
plugins: {
elasticsearch: ElasticsearchPlugin;
xpack_main: XPackMainPlugin;
cloud: {
config: {
isCloudEnabled: boolean;
};
};
};
log: any;
events: any;
Expand Down
47 changes: 0 additions & 47 deletions x-pack/plugins/cloud/_remaining.md

This file was deleted.

34 changes: 34 additions & 0 deletions x-pack/plugins/cloud/common/is_cloud_enabled.test.ts
@@ -0,0 +1,34 @@
/*
* 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 { getIsCloudEnabled } from './is_cloud_enabled';

const MOCK_CLOUD_ID_STAGING =
'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==';
const MOCK_CLOUD_ID_PROD =
'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==';

describe('getIsCloudEnabled', () => {
it('returns `false` if `id` is not undefined', async () => {
const cloudId = undefined;
const isCloudEnabled = getIsCloudEnabled(cloudId);
expect(isCloudEnabled).toBe(false);
});

it('returns `false` if `id` is not a string', async () => {
const cloudId = 123 as any;
const isCloudEnabled = getIsCloudEnabled(cloudId);
expect(isCloudEnabled).toBe(false);
});

it('returns `true` if `id` is a string', async () => {
const isCloudEnabledStaging = getIsCloudEnabled(MOCK_CLOUD_ID_STAGING);
const isCloudEnabledProd = getIsCloudEnabled(MOCK_CLOUD_ID_PROD);
expect(isCloudEnabledStaging).toBe(true);
expect(isCloudEnabledProd).toBe(true);
});
});

9 changes: 9 additions & 0 deletions x-pack/plugins/cloud/common/is_cloud_enabled.ts
@@ -0,0 +1,9 @@
/*
* 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.
*/

export function getIsCloudEnabled(cloudId?: string) {
return typeof cloudId === 'string';
}
13 changes: 13 additions & 0 deletions x-pack/plugins/cloud/public/index.ts
@@ -0,0 +1,13 @@
/*
* 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 { PluginInitializerContext } from '../../../../src/core/public';
import { CloudPlugin } from './plugin';

export { CloudSetup } from './plugin';
export function plugin(initializerContext: PluginInitializerContext) {
return new CloudPlugin(initializerContext);
}
37 changes: 37 additions & 0 deletions x-pack/plugins/cloud/public/plugin.ts
@@ -0,0 +1,37 @@
/*
* 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 {
CoreSetup,
Plugin,
PluginInitializerContext,
} from 'src/core/public';
import { getIsCloudEnabled } from '../common/is_cloud_enabled';

interface CloudConfigType {
id?: string;
}

export interface CloudSetup {
cloudId?: string;
isCloudEnabled: boolean;
}

export class CloudPlugin implements Plugin<CloudSetup> {
constructor(private readonly initializerContext: PluginInitializerContext) {}

public async setup(core: CoreSetup) {
const { id } = this.initializerContext.config.get<CloudConfigType>();
const isCloudEnabled = getIsCloudEnabled(id);

return {
cloudId: id,
isCloudEnabled,
}
}

public start() {}
}
Expand Up @@ -4,57 +4,31 @@
* you may not use this file except in compliance with the Elastic License.
*/

import sinon from 'sinon';
import { Server } from 'hapi';
import { createCollectorFetch, createCloudUsageCollector } from './cloud_usage_collector';

const CLOUD_ID_STAGING =
'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==';
const CLOUD_ID =
'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==';
import { createCloudUsageCollector } from './cloud_usage_collector';

const mockUsageCollection = () => ({
makeUsageCollector: sinon.stub(),
makeUsageCollector: jest.fn(),
});

const getMockServer = (cloudId?: string) =>
({
config() {
return {
get(path: string) {
switch (path) {
case 'xpack.cloud':
return { id: cloudId };
default:
throw Error(`server.config().get(${path}) should not be called by this collector.`);
}
},
};
},
} as Server);

describe('Cloud usage collector', () => {
describe('collector', () => {
it('returns `isCloudEnabled: false` if `xpack.cloud.id` is not defined', async () => {
const mockServer = getMockServer();
const collector = await createCollectorFetch(mockServer)();
expect(collector.isCloudEnabled).toBe(false);
});

it('returns `isCloudEnabled: true` if `xpack.cloud.id` is defined', async () => {
const stagingCollector = await createCollectorFetch(getMockServer(CLOUD_ID))();
const collector = await createCollectorFetch(getMockServer(CLOUD_ID_STAGING))();
expect(collector.isCloudEnabled).toBe(true);
expect(stagingCollector.isCloudEnabled).toBe(true);
});
});
});
const getMockConfigs = (isCloudEnabled: boolean) => ({
isCloudEnabled: isCloudEnabled,
})

describe('createCloudUsageCollector', () => {
it('returns calls `makeUsageCollector`', () => {
const mockServer = getMockServer();
it('calls `makeUsageCollector`', () => {
const mockConfigs = getMockConfigs(false);
const usageCollection = mockUsageCollection();
createCloudUsageCollector(usageCollection as any, mockServer);
expect(usageCollection.makeUsageCollector.calledOnce).toBe(true);
createCloudUsageCollector(usageCollection as any, mockConfigs);
expect(usageCollection.makeUsageCollector).toBeCalledTimes(1);
});

describe('Fetched Usage data', () => {
it('return isCloudEnabled boolean', () => {
const mockConfigs = getMockConfigs(true);
const usageCollection = mockUsageCollection() as any;
const collector = createCloudUsageCollector(usageCollection, mockConfigs);

expect(collector.fetch().isCloudEnabled).toBe(true);
})
})
});
24 changes: 15 additions & 9 deletions x-pack/plugins/cloud/server/config.ts
Expand Up @@ -5,21 +5,27 @@
*/

import { schema, TypeOf } from '@kbn/config-schema';
import { PluginConfigDescriptor } from 'kibana/server';

const apmSchema = schema.object({
const apmConfigSchema = schema.object({
url: schema.maybe(schema.string()),
secret_token: schema.maybe(schema.string()),
ui: schema.maybe(schema.object({
url: schema.maybe(schema.string()),
})),
});

export const config = {
schema: schema.object({
enabled: schema.boolean({ defaultValue: true }),
id: schema.maybe(schema.string()),
apm: schema.maybe(apmSchema),
}),
};
const configSchema = schema.object({
enabled: schema.boolean({ defaultValue: true }),
id: schema.maybe(schema.string()),
apm: schema.maybe(apmConfigSchema),
});

export type CloudConfigSchema = TypeOf<typeof config.schema>;
export type CloudConfigType = TypeOf<typeof configSchema>;

export const config: PluginConfigDescriptor<CloudConfigType> = {
exposeToBrowser: {
id: true,
},
schema: configSchema,
};
5 changes: 4 additions & 1 deletion x-pack/plugins/cloud/server/index.ts
Expand Up @@ -7,5 +7,8 @@
import { PluginInitializerContext } from 'src/core/server';
import { CloudPlugin } from './plugin';

export { CloudSetup } from './plugin';
export { config } from './config';
export const plugin = (context: PluginInitializerContext) => new CloudPlugin(context);
export const plugin = (initializerContext: PluginInitializerContext) => {
return new CloudPlugin(initializerContext);
}
13 changes: 7 additions & 6 deletions x-pack/plugins/cloud/server/plugin.ts
Expand Up @@ -4,11 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { first, Observable } from 'rxjs/operators';
import { first } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { CloudConfigSchema } from './config';
import { CloudConfigType } from './config';
import { registerCloudUsageCollector } from './collectors';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { getIsCloudEnabled } from '../common/is_cloud_enabled';
import {
CoreSetup,
Logger,
Expand All @@ -25,18 +26,18 @@ export interface CloudSetup {

export class CloudPlugin implements Plugin<CloudSetup> {
private readonly logger: Logger;
private readonly config$: Observable<CloudConfigSchema>;
private readonly config$: Observable<CloudConfigType>;

constructor(private readonly context: PluginInitializerContext) {
this.logger = this.context.logger.get();
this.config$ = this.context.config.create<CloudConfigSchema>();
this.config$ = this.context.config.create<CloudConfigType>();
}

public async setup(core: CoreSetup, { usageCollection }: PluginsSetup) {
this.logger.debug('Setting up Cloud plugin');
const config = await this.config$.pipe(first()).toPromise();
const isCloudEnabled = !!config.id;
registerCloudUsageCollector(usageCollection, { isCloudEnabled })
const isCloudEnabled = getIsCloudEnabled(config.id);
registerCloudUsageCollector(usageCollection, { isCloudEnabled });

return {
isCloudEnabled,
Expand Down

0 comments on commit 526d33e

Please sign in to comment.