Skip to content

Commit

Permalink
use simple appraoch
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Oct 20, 2021
1 parent 211dacf commit b353322
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 27 deletions.
6 changes: 2 additions & 4 deletions x-pack/plugins/uptime/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { initUptimeServer } from './uptime_server';
import { UptimeCorePlugins, UptimeCoreSetup } from './lib/adapters/framework';
import { umDynamicSettings } from './lib/saved_objects';
import { UptimeRuleRegistry } from './plugin';
import { UptimeConfig } from './config';

export interface KibanaRouteOptions {
path: string;
Expand All @@ -32,8 +31,7 @@ export const initServerWithKibana = (
server: UptimeCoreSetup,
plugins: UptimeCorePlugins,
ruleRegistry: UptimeRuleRegistry,
logger: Logger,
config: UptimeConfig
logger: Logger
) => {
const { features } = plugins;
const libs = compose(server);
Expand Down Expand Up @@ -115,5 +113,5 @@ export const initServerWithKibana = (
},
});

initUptimeServer(server, libs, plugins, ruleRegistry, logger, config);
initUptimeServer(server, libs, plugins, ruleRegistry, logger);
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { MlPluginSetup as MlSetup } from '../../../../../ml/server';
import { RuleRegistryPluginSetupContract } from '../../../../../rule_registry/server';
import { UptimeESClient } from '../../lib';
import type { UptimeRouter } from '../../../types';
import { UptimeConfig } from '../../../config';

export type UMElasticsearchQueryFn<P, R = any> = (
params: {
Expand All @@ -29,7 +28,6 @@ export type UMElasticsearchQueryFn<P, R = any> = (

export type UMSavedObjectsQueryFn<T = any, P = undefined> = (
client: SavedObjectsClientContract | ISavedObjectsRepository,
config?: UptimeConfig,
params?: P
) => Promise<T> | T;

Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/uptime/server/lib/saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { DYNAMIC_SETTINGS_DEFAULTS } from '../../common/constants';
import { DynamicSettings } from '../../common/runtime_types';
import { SavedObjectsType, SavedObjectsErrorHelpers } from '../../../../../src/core/server';
import { UMSavedObjectsQueryFn } from './adapters';
import { UptimeConfig } from '../config';

export interface UMSavedObjectsAdapter {
config: UptimeConfig;
getUptimeDynamicSettings: UMSavedObjectsQueryFn<DynamicSettings>;
setUptimeDynamicSettings: UMSavedObjectsQueryFn<void, DynamicSettings>;
}
Expand Down Expand Up @@ -55,11 +57,13 @@ export const umDynamicSettings: SavedObjectsType = {
};

export const savedObjectsAdapter: UMSavedObjectsAdapter = {
getUptimeDynamicSettings: async (client, config): Promise<DynamicSettings> => {
config: null,
getUptimeDynamicSettings: async (client): Promise<DynamicSettings> => {
try {
const obj = await client.get<DynamicSettings>(umDynamicSettings.name, settingsObjectId);
return obj?.attributes ?? DYNAMIC_SETTINGS_DEFAULTS;
} catch (getErr) {
const config = savedObjectsAdapter.config;
if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) {
if (config?.index) {
return { ...DYNAMIC_SETTINGS_DEFAULTS, heartbeatIndices: config.index };
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/uptime/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { uptimeRuleFieldMap } from '../common/rules/uptime_rule_field_map';
import { initServerWithKibana } from './kibana.index';
import { KibanaTelemetryAdapter, UptimeCorePlugins } from './lib/adapters';
import { umDynamicSettings } from './lib/saved_objects';
import { savedObjectsAdapter, umDynamicSettings } from './lib/saved_objects';
import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map';
import { Dataset } from '../../rule_registry/server';
import { UptimeConfig } from './config';
Expand All @@ -35,6 +35,8 @@ export class Plugin implements PluginType {
public setup(core: CoreSetup, plugins: UptimeCorePlugins) {
const config = this.initContext.config.get<UptimeConfig>();

savedObjectsAdapter.config = config;

this.logger = this.initContext.logger.get();
const { ruleDataService } = plugins.ruleRegistry;

Expand All @@ -55,8 +57,7 @@ export class Plugin implements PluginType {
{ router: core.http.createRouter() },
plugins,
ruleDataClient,
this.logger,
config
this.logger
);
core.savedObjects.registerType(umDynamicSettings);
KibanaTelemetryAdapter.registerUsageCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const createRouteWithAuth = (
request,
response,
savedObjectsClient,
config,
}) => {
const { statusCode, message } = libs.license(context.licensing.license);
if (statusCode === 200) {
Expand All @@ -30,7 +29,6 @@ export const createRouteWithAuth = (
request,
response,
savedObjectsClient,
config,
});
}
switch (statusCode) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const createGetDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMSer
method: 'GET',
path: '/api/uptime/dynamic_settings',
validate: false,
handler: async ({ savedObjectsClient, config }): Promise<any> => {
return savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient, config);
handler: async ({ savedObjectsClient }): Promise<any> => {
return savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient);
},
});

Expand Down
8 changes: 1 addition & 7 deletions x-pack/plugins/uptime/server/rest_api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from 'kibana/server';
import { UMServerLibs, UptimeESClient } from '../lib/lib';
import type { UptimeRequestHandlerContext } from '../types';
import { UptimeConfig } from '../config';

/**
* Defines the basic properties employed by Uptime routes.
Expand Down Expand Up @@ -59,10 +58,7 @@ export type UMRestApiRouteFactory = (libs: UMServerLibs) => UptimeRoute;
* Functions of this type accept our internal route format and output a route
* object that the Kibana platform can consume.
*/
export type UMKibanaRouteWrapper = (
uptimeRoute: UptimeRoute,
config: UptimeConfig
) => UMKibanaRoute;
export type UMKibanaRouteWrapper = (uptimeRoute: UptimeRoute) => UMKibanaRoute;

/**
* This is the contract we specify internally for route handling.
Expand All @@ -73,12 +69,10 @@ export type UMRouteHandler = ({
request,
response,
savedObjectsClient,
config,
}: {
uptimeEsClient: UptimeESClient;
context: UptimeRequestHandlerContext;
request: KibanaRequest<Record<string, any>, Record<string, any>, Record<string, any>>;
response: KibanaResponseFactory;
savedObjectsClient: SavedObjectsClientContract;
config: UptimeConfig;
}) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createUptimeESClient, inspectableEsQueriesMap } from '../lib/lib';
import { KibanaResponse } from '../../../../../src/core/server/http/router';
import { enableInspectEsQueries } from '../../../observability/common';

export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute, config) => ({
export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute) => ({
...uptimeRoute,
options: {
tags: ['access:uptime-read', ...(uptimeRoute?.writeAccess ? ['access:uptime-write'] : [])],
Expand Down Expand Up @@ -40,7 +40,6 @@ export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute, config) =>
context,
request,
response,
config,
});

if (res instanceof KibanaResponse) {
Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/uptime/server/uptime_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ import { statusCheckAlertFactory } from './lib/alerts/status_check';
import { tlsAlertFactory } from './lib/alerts/tls';
import { tlsLegacyAlertFactory } from './lib/alerts/tls_legacy';
import { durationAnomalyAlertFactory } from './lib/alerts/duration_anomaly';
import { UptimeConfig } from './config';

export const initUptimeServer = (
server: UptimeCoreSetup,
libs: UMServerLibs,
plugins: UptimeCorePlugins,
ruleDataClient: IRuleDataClient,
logger: Logger,
config: UptimeConfig
logger: Logger
) => {
restApiRoutes.forEach((route) =>
libs.framework.registerRoute(uptimeRouteWrapper(createRouteWithAuth(libs, route), config))
libs.framework.registerRoute(uptimeRouteWrapper(createRouteWithAuth(libs, route)))
);

const {
Expand Down

0 comments on commit b353322

Please sign in to comment.