Skip to content

Commit

Permalink
set up plugin types (elastic#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
orouz authored Jan 13, 2022
1 parent e07bb84 commit 1bafbb2
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { routes } from './routes';
import { UnknownRoute } from '../components/unknown_route';
import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';
import type { AppMountParameters, CoreStart } from '../../../../../src/core/public';
import type { CspStart } from '../types';
import type { CspClientPluginStartDeps } from '../types';

const queryClient = new QueryClient();

interface CspAppDeps {
export interface CspAppDeps {
core: CoreStart;
deps: CspStart;
deps: CspClientPluginStartDeps;
params: AppMountParameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import ReactDOM from 'react-dom';
import { CspApp } from './app';

import type { AppMountParameters, CoreStart } from '../../../../../src/core/public';
import type { CspStart } from '../types';
import type { CspClientPluginStartDeps } from '../types';

export const renderApp = (core: CoreStart, deps: CspStart, params: AppMountParameters) => {
export const renderApp = (
core: CoreStart,
deps: CspClientPluginStartDeps,
params: AppMountParameters
) => {
ReactDOM.render(<CspApp core={core} params={params} deps={deps} />, params.element);

return () => ReactDOM.unmountComponentAtNode(params.element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ import React from 'react';
import { I18nProvider } from '@kbn/i18n-react';
import { Router, Switch, Route } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from 'react-query';

import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';

import type { AppMountParameters, CoreStart } from '../../../../../src/core/public';
import type { CspStart } from '../types';
import type { CspAppDeps } from './app';

const queryClient = new QueryClient();

interface CspAppDeps {
core: CoreStart;
deps: CspStart;
params: AppMountParameters;
}

export const TestProvider: React.FC<CspAppDeps> = ({ core, deps, params, children }) => {
return (
<KibanaContextProvider services={{ ...deps, ...core }}>
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cloud_security_posture/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

import { CspPlugin } from './plugin';

export { CspPluginSetup, CspPluginStart } from './types';
export { CspClientPluginSetup, CspClientPluginStart } from './types';

export const plugin = () => new CspPlugin();
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import * as TEST_SUBJECTS from './test_subjects';
import type { DataView, TimeRange } from '../../../../../../src/plugins/data/common';
import type { FindingsFetchState } from './types';
import type { CspPluginSetup } from '../../types';
import type { FindingsUrlQuery } from './findings_container';
import type { CspClientPluginStartDeps } from '../../types';
import { PLUGIN_NAME } from '../../../common';

interface BaseFindingsSearchBarProps {
Expand All @@ -38,7 +38,7 @@ export const FindingsSearchBar = ({
query: queryService,
ui: { SearchBar },
},
} = useKibana<CspPluginSetup>().services;
} = useKibana<CspClientPluginStartDeps>().services;

useEffect(() => {
const subscription = queryService.filterManager.getUpdates$().subscribe(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
IKibanaSearchResponse,
TimeRange,
} from '../../../../../../src/plugins/data/common';
import type { CspPluginSetup } from '../../types';
import type { CspClientPluginStartDeps } from '../../types';
import { CSP_KUBEBEAT_INDEX_NAME } from '../../../common/constants';
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';

Expand All @@ -30,7 +30,7 @@ export const isNonNullable = <T extends unknown>(v: T): v is NonNullable<T> =>
export const useKubebeatDataView = () => {
const {
data: { dataViews },
} = useKibana<CspPluginSetup>().services;
} = useKibana<CspClientPluginStartDeps>().services;

const createDataView = () =>
dataViews.createAndSave({
Expand Down Expand Up @@ -60,7 +60,7 @@ export const useEsClientMutation = <T extends unknown>({
dateRange: TimeRange;
filters: Filter[];
}) => {
const { data: dataService } = useKibana<CspPluginSetup>().services;
const { data: dataService } = useKibana<CspClientPluginStartDeps>().services;
const { query: queryService, search: searchService } = dataService;
return useMutation(async () => {
queryService.queryString.setQuery(query);
Expand Down
26 changes: 21 additions & 5 deletions x-pack/plugins/cloud_security_posture/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@
*/

import type { AppMountParameters, CoreSetup, CoreStart, Plugin } from '../../../../src/core/public';
import type { CspSetup, CspStart, CspPluginSetup, CspPluginStart } from './types';
import type {
CspClientPluginSetup,
CspClientPluginStart,
CspClientPluginSetupDeps,
CspClientPluginStartDeps,
} from './types';
import { AppNavLinkStatus, AppStatus } from '../../../../src/core/public';
import { PLUGIN_NAME, PLUGIN_ID } from '../common';

export class CspPlugin implements Plugin<CspSetup, CspStart, CspPluginSetup, CspPluginStart> {
public setup(core: CoreSetup<CspPluginStart, CspStart>, plugins: CspPluginSetup): CspSetup {
export class CspPlugin
implements
Plugin<
CspClientPluginSetup,
CspClientPluginStart,
CspClientPluginSetupDeps,
CspClientPluginStartDeps
>
{
public setup(
core: CoreSetup<CspClientPluginStartDeps, CspClientPluginStart>,
plugins: CspClientPluginSetup
): CspClientPluginSetup {
// Register an application into the side navigation menu

core.application.register({
Expand All @@ -25,14 +41,14 @@ export class CspPlugin implements Plugin<CspSetup, CspStart, CspPluginSetup, Csp
// Get start services as specified in kibana.json
const [coreStart, depsStart] = await core.getStartServices();
// Render the application
return renderApp(coreStart, depsStart as CspStart, params);
return renderApp(coreStart, depsStart, params);
},
});

// Return methods that should be available to other plugins
return {};
}
public start(core: CoreStart, plugins: CspPluginStart): CspStart {
public start(core: CoreStart, plugins: CspClientPluginStartDeps): CspClientPluginStart {
return {};
}

Expand Down
24 changes: 16 additions & 8 deletions x-pack/plugins/cloud_security_posture/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@
* 2.0.
*/

import type { DataPublicPluginStart } from '../../../../src/plugins/data/public';
import type { NavigationPublicPluginStart } from '../../../../src/plugins/navigation/public';
import type {
DataPublicPluginSetup,
DataPublicPluginStart,
} from '../../../../src/plugins/data/public';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CspSetup {}
export interface CspClientPluginSetup {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CspStart {}
export interface CspClientPluginStart {}

export interface CspPluginSetup {
data: DataPublicPluginStart;
export interface CspClientPluginSetupDeps {
// required
data: DataPublicPluginSetup;

// optional
}
export interface CspPluginStart {

export interface CspClientPluginStartDeps {
// required
data: DataPublicPluginStart;
navigation: NavigationPublicPluginStart;

// optional
}
2 changes: 1 addition & 1 deletion x-pack/plugins/cloud_security_posture/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { PluginInitializerContext } from '../../../../src/core/server';
import { CspPlugin } from './plugin';

export { CspPluginSetup, CspPluginStart } from './types';
export { CspServerPluginSetup, CspServerPluginStart } from './types';

export const plugin = (initializerContext: PluginInitializerContext) =>
new CspPlugin(initializerContext);
26 changes: 21 additions & 5 deletions x-pack/plugins/cloud_security_posture/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,41 @@ import type {
Logger,
} from '../../../../src/core/server';
import { createFindingsIndexTemplate } from './index_template/create_index_template';
import type { CspSetup, CspStart, CspPluginSetup, CspPluginStart } from './types';
import type {
CspServerPluginSetup,
CspServerPluginStart,
CspServerPluginSetupDeps,
CspServerPluginStartDeps,
} from './types';
import { defineRoutes } from './routes';

export class CspPlugin implements Plugin<CspSetup, CspStart, CspPluginSetup, CspPluginStart> {
export class CspPlugin
implements
Plugin<
CspServerPluginSetup,
CspServerPluginStart,
CspServerPluginSetupDeps,
CspServerPluginStartDeps
>
{
private readonly logger: Logger;
constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
}

public setup(core: CoreSetup<CspPluginStart>) {
public setup(
core: CoreSetup<CspServerPluginSetup>,
plugins: CspServerPluginSetupDeps
): CspServerPluginSetup {
this.logger.debug('csp: Setup');
const router = core.http.createRouter();
// Register server side APIs

defineRoutes(router);

return {};
}

public start(core: CoreStart) {
public start(core: CoreStart, plugins: CspServerPluginStartDeps): CspServerPluginStart {
this.logger.debug('csp: Started');
createFindingsIndexTemplate(core.elasticsearch.client.asInternalUser).catch(this.logger.error);
return {};
Expand Down
28 changes: 22 additions & 6 deletions x-pack/plugins/cloud_security_posture/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@
* 2.0.
*/

/* eslint-disable @typescript-eslint/no-empty-interface */
import type {
PluginSetup as DataPluginSetup,
PluginStart as DataPluginStart,
} from '../../../../src/plugins/data/server';

export interface CspSetup {}
export interface CspStart {}
export interface CspPluginSetup {}
export interface CspPluginStart {}
/* eslint-enable @typescript-eslint/no-empty-interface */
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CspServerPluginSetup {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CspServerPluginStart {}

export interface CspServerPluginSetupDeps {
// required
data: DataPluginSetup;

// optional
}

export interface CspServerPluginStartDeps {
// required
data: DataPluginStart;

// optional
}

0 comments on commit 1bafbb2

Please sign in to comment.