[Uptime] Determine es hosts from synthetics service (#119708) (#119779)

This commit is contained in:
Shahzad 2021-11-26 17:22:13 +01:00 committed by GitHub
parent 7a2b07cde5
commit f6b4d051d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 3 deletions

View file

@ -6,10 +6,11 @@
"id": "uptime",
"kibanaVersion": "kibana",
"optionalPlugins": [
"cloud",
"data",
"fleet",
"home",
"ml",
"fleet"
"ml"
],
"requiredPlugins": [
"alerting",

View file

@ -23,6 +23,7 @@ import { RuleRegistryPluginSetupContract } from '../../../../../rule_registry/se
import { UptimeESClient } from '../../lib';
import type { UptimeRouter } from '../../../types';
import { SecurityPluginStart } from '../../../../../security/server';
import { CloudSetup } from '../../../../../cloud/server';
import { UptimeConfig } from '../../../../common/config';
export type UMElasticsearchQueryFn<P, R = any> = (
@ -40,6 +41,7 @@ export type UMSavedObjectsQueryFn<T = any, P = undefined> = (
export interface UptimeCoreSetup {
router: UptimeRouter;
config: UptimeConfig;
cloud: CloudSetup;
security: SecurityPluginStart;
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
}
@ -50,6 +52,7 @@ export interface UptimeCorePluginsSetup {
observability: ObservabilityPluginSetup;
usageCollection: UsageCollectionSetup;
ml: MlSetup;
cloud?: CloudSetup;
ruleRegistry: RuleRegistryPluginSetupContract;
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup;
}

View file

@ -0,0 +1,65 @@
/*
* 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.
*/
/*
* 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 { getEsHosts } from './get_es_hosts';
import { CloudSetup } from '../../../../cloud/server';
describe('getEsHostsTest', () => {
const cloudSetup = {
cloudId:
'TLS_Test:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDI0ZDYwY2NjYmZjODRhZmZhNGRjYTQ3M2M2YjFlZDgwJGUxMjkyY2YzMTczZTRkNTViZDViM2NlNzYyZDg1NzY3',
isCloudEnabled: true,
} as CloudSetup;
it('should return expected host in cloud', function () {
const esHosts = getEsHosts({
cloud: cloudSetup,
config: {},
});
expect(esHosts).toEqual([
'https://24d60cccbfc84affa4dca473c6b1ed80.us-central1.gcp.cloud.es.io:443',
]);
});
it('should return expected host from config', function () {
const esHosts = getEsHosts({
config: {
unsafe: {
service: {
hosts: ['http://localhost:9200'],
},
},
},
});
expect(esHosts).toEqual(['http://localhost:9200']);
});
it('should return cloud hosts when both config and cloud are present', function () {
const esHosts = getEsHosts({
cloud: cloudSetup,
config: {
unsafe: {
service: {
hosts: ['http://localhost:9200'],
},
},
},
});
expect(esHosts).toEqual([
'https://24d60cccbfc84affa4dca473c6b1ed80.us-central1.gcp.cloud.es.io:443',
]);
});
});

View file

@ -0,0 +1,40 @@
/*
* 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.
*/
/*
* 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 { CloudSetup } from '../../../../cloud/server';
import { decodeCloudId } from '../../../../fleet/common';
import { UptimeConfig } from '../../../common/config';
export function getEsHosts({
cloud,
config,
}: {
cloud?: CloudSetup;
config: UptimeConfig;
}): string[] {
const cloudId = cloud?.isCloudEnabled && cloud.cloudId;
const cloudUrl = cloudId && decodeCloudId(cloudId)?.elasticsearchUrl;
const cloudHosts = cloudUrl ? [cloudUrl] : undefined;
if (cloudHosts && cloudHosts.length > 0) {
return cloudHosts;
}
const flagHosts = config?.unsafe?.service?.hosts;
if (flagHosts && flagHosts.length > 0) {
return flagHosts;
}
return [];
}

View file

@ -59,7 +59,11 @@ export class Plugin implements PluginType {
],
});
this.server = { router: core.http.createRouter(), config } as UptimeCoreSetup;
this.server = {
router: core.http.createRouter(),
config,
cloud: plugins.cloud,
} as UptimeCoreSetup;
initServerWithKibana(this.server, plugins, ruleDataClient, this.logger);