Skip to content

Commit

Permalink
[Uptime] Setup synthetics index template (elastic#119727) (elastic#11…
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Nov 29, 2021
1 parent 32bcf31 commit b605837
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/uptime/common/constants/rest_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ export enum API_URLS {
DELETE_RULE = '/api/alerting/rule/',
RULES_FIND = '/api/alerting/rules/_find',
CONNECTOR_TYPES = '/api/actions/connector_types',

// Service end points
INDEX_TEMPLATES = '/api/uptime/service/index_templates',
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { UptimeESClient } from '../../lib';
import type { UptimeRouter } from '../../../types';
import { SecurityPluginStart } from '../../../../../security/server';
import { CloudSetup } from '../../../../../cloud/server';
import { FleetStartContract } from '../../../../../fleet/server';
import { UptimeConfig } from '../../../../common/config';

export type UMElasticsearchQueryFn<P, R = any> = (
Expand All @@ -41,7 +42,8 @@ export type UMSavedObjectsQueryFn<T = any, P = undefined> = (
export interface UptimeCoreSetup {
router: UptimeRouter;
config: UptimeConfig;
cloud: CloudSetup;
cloud?: CloudSetup;
fleet: FleetStartContract;
security: SecurityPluginStart;
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
}
Expand All @@ -59,6 +61,7 @@ export interface UptimeCorePluginsSetup {

export interface UptimeCorePluginsStart {
security: SecurityPluginStart;
fleet: FleetStartContract;
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
}

Expand Down
32 changes: 28 additions & 4 deletions x-pack/plugins/uptime/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Plugin as PluginType,
ISavedObjectsRepository,
Logger,
SavedObjectsClient,
} from '../../../../src/core/server';
import { uptimeRuleFieldMap } from '../common/rules/uptime_rule_field_map';
import { initServerWithKibana } from './kibana.index';
Expand All @@ -25,17 +26,19 @@ import { registerUptimeSavedObjects, savedObjectsAdapter } from './lib/saved_obj
import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map';
import { Dataset } from '../../rule_registry/server';
import { UptimeConfig } from '../common/config';
import { installSyntheticsIndexTemplates } from './rest_api/synthetics_service/install_index_templates';

export type UptimeRuleRegistry = ReturnType<Plugin['setup']>['ruleRegistry'];

export class Plugin implements PluginType {
private savedObjectsClient?: ISavedObjectsRepository;
private initContext: PluginInitializerContext;
private logger?: Logger;
private logger: Logger;
private server?: UptimeCoreSetup;

constructor(_initializerContext: PluginInitializerContext<UptimeConfig>) {
this.initContext = _initializerContext;
constructor(initializerContext: PluginInitializerContext<UptimeConfig>) {
this.initContext = initializerContext;
this.logger = initializerContext.logger.get();
}

public setup(core: CoreSetup, plugins: UptimeCorePluginsSetup) {
Expand All @@ -60,8 +63,8 @@ export class Plugin implements PluginType {
});

this.server = {
router: core.http.createRouter(),
config,
router: core.http.createRouter(),
cloud: plugins.cloud,
} as UptimeCoreSetup;

Expand All @@ -83,8 +86,29 @@ export class Plugin implements PluginType {
this.savedObjectsClient = core.savedObjects.createInternalRepository();
if (this.server) {
this.server.security = plugins.security;
this.server.fleet = plugins.fleet;
this.server.encryptedSavedObjects = plugins.encryptedSavedObjects;
}

if (this.server?.config?.unsafe?.service.enabled) {
const esClient = core.elasticsearch.client.asInternalUser;
installSyntheticsIndexTemplates({
esClient,
server: this.server,
savedObjectsClient: new SavedObjectsClient(core.savedObjects.createInternalRepository()),
}).then(
(result) => {
if (result.name === 'synthetics' && result.install_status === 'installed') {
this.logger.info('Installed synthetics index templates');
} else if (result.name === 'synthetics' && result.install_status === 'install_failed') {
this.logger.warn('Failed to install synthetics index templates');
}
},
() => {
this.logger.warn('Failed to install synthetics index templates');
}
);
}
}

public stop() {}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/uptime/server/rest_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { createGetIndexStatusRoute } from './index_state';
import { createNetworkEventsRoute } from './network_events';
import { createJourneyFailedStepsRoute } from './pings/journeys';
import { createLastSuccessfulStepRoute } from './synthetics/last_successful_step';
import { installIndexTemplatesRoute } from './synthetics_service/install_index_templates';

export * from './types';
export { createRouteWithAuth } from './create_route_with_auth';
Expand All @@ -51,4 +52,5 @@ export const restApiRoutes: UMRestApiRouteFactory[] = [
createJourneyFailedStepsRoute,
createLastSuccessfulStepRoute,
createJourneyScreenshotBlocksRoute,
installIndexTemplatesRoute,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { ElasticsearchClient, SavedObjectsClientContract } from 'kibana/server';
import { UMRestApiRouteFactory } from '../types';
import { API_URLS } from '../../../common/constants';
import { UptimeCoreSetup } from '../../lib/adapters';

export const installIndexTemplatesRoute: UMRestApiRouteFactory = () => ({
method: 'GET',
path: API_URLS.INDEX_TEMPLATES,
validate: {},
handler: async ({ server, request, savedObjectsClient, uptimeEsClient }): Promise<any> => {
return installSyntheticsIndexTemplates({
server,
savedObjectsClient,
esClient: uptimeEsClient.baseESClient,
});
},
});

export async function installSyntheticsIndexTemplates({
esClient,
server,
savedObjectsClient,
}: {
server: UptimeCoreSetup;
esClient: ElasticsearchClient;
savedObjectsClient: SavedObjectsClientContract;
}) {
// no need to add error handling here since fleetSetupCompleted is already wrapped in try/catch and will log
// warning if setup fails to complete
await server.fleet.fleetSetupCompleted();

return await server.fleet.packageService.ensureInstalledPackage({
esClient,
savedObjectsClient,
pkgName: 'synthetics',
});
}

0 comments on commit b605837

Please sign in to comment.