mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[Monitoring] Convert elasticsearch_settings dir to typescript (#108112)
* convert elasticsearch_settings dir files to typescript * fix type * change tests to ts
This commit is contained in:
parent
fe3b7d61c8
commit
dd85150f73
12 changed files with 110 additions and 82 deletions
|
@ -1,39 +0,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 { get } from 'lodash';
|
||||
import { findReason } from './find_reason';
|
||||
|
||||
export function handleResponse(response, isCloudEnabled) {
|
||||
const sources = ['persistent', 'transient', 'defaults'];
|
||||
for (const source of sources) {
|
||||
const monitoringSettings = get(response[source], 'xpack.monitoring');
|
||||
if (monitoringSettings !== undefined) {
|
||||
const check = findReason(
|
||||
monitoringSettings,
|
||||
{
|
||||
context: `cluster ${source}`,
|
||||
},
|
||||
isCloudEnabled
|
||||
);
|
||||
|
||||
if (check.found) {
|
||||
return check;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { found: false };
|
||||
}
|
||||
|
||||
export async function checkClusterSettings(req) {
|
||||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin');
|
||||
const { cloud } = req.server.newPlatform.setup.plugins;
|
||||
const isCloudEnabled = !!(cloud && cloud.isCloudEnabled);
|
||||
const response = await callWithRequest(req, 'cluster.getSettings', { include_defaults: true });
|
||||
return handleResponse(response, isCloudEnabled);
|
||||
}
|
|
@ -5,10 +5,15 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { ClusterGetSettingsResponse } from '@elastic/elasticsearch/api/types';
|
||||
import { checkClusterSettings } from '.';
|
||||
import { LegacyRequest } from '../../types';
|
||||
|
||||
describe('Elasticsearch Cluster Settings', () => {
|
||||
const makeResponse = (property, response = {}) => {
|
||||
const makeResponse = (
|
||||
property: keyof ClusterGetSettingsResponse,
|
||||
response: any = {}
|
||||
): ClusterGetSettingsResponse => {
|
||||
const result = {
|
||||
persistent: {},
|
||||
transient: {},
|
||||
|
@ -18,8 +23,8 @@ describe('Elasticsearch Cluster Settings', () => {
|
|||
return result;
|
||||
};
|
||||
|
||||
const getReq = (response) => {
|
||||
return {
|
||||
const getReq = (response: ClusterGetSettingsResponse) => {
|
||||
return ({
|
||||
server: {
|
||||
newPlatform: {
|
||||
setup: {
|
||||
|
@ -40,20 +45,14 @@ describe('Elasticsearch Cluster Settings', () => {
|
|||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
} as unknown) as LegacyRequest;
|
||||
};
|
||||
|
||||
it('should return { found: false } given no response from ES', async () => {
|
||||
const mockReq = getReq(makeResponse('ignore', {}));
|
||||
const result = await checkClusterSettings(mockReq);
|
||||
expect(result).toEqual({ found: false });
|
||||
});
|
||||
|
||||
it('should find default collection interval reason', async () => {
|
||||
const setting = {
|
||||
xpack: { monitoring: { collection: { interval: -1 } } },
|
||||
};
|
||||
const makeExpected = (source) => ({
|
||||
const makeExpected = (source: keyof ClusterGetSettingsResponse) => ({
|
||||
found: true,
|
||||
reason: {
|
||||
context: `cluster ${source}`,
|
||||
|
@ -82,7 +81,7 @@ describe('Elasticsearch Cluster Settings', () => {
|
|||
const setting = {
|
||||
xpack: { monitoring: { exporters: { myCoolExporter: {} } } },
|
||||
};
|
||||
const makeExpected = (source) => ({
|
||||
const makeExpected = (source: keyof ClusterGetSettingsResponse) => ({
|
||||
found: true,
|
||||
reason: {
|
||||
context: `cluster ${source}`,
|
||||
|
@ -111,7 +110,7 @@ describe('Elasticsearch Cluster Settings', () => {
|
|||
const setting = {
|
||||
xpack: { monitoring: { enabled: 'false' } },
|
||||
};
|
||||
const makeExpected = (source) => ({
|
||||
const makeExpected = (source: keyof ClusterGetSettingsResponse) => ({
|
||||
found: true,
|
||||
reason: {
|
||||
context: `cluster ${source}`,
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* 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 { get } from 'lodash';
|
||||
import { ClusterGetSettingsResponse } from '@elastic/elasticsearch/api/types';
|
||||
import { findReason } from './find_reason';
|
||||
import { ClusterSettingsReasonResponse, LegacyRequest } from '../../types';
|
||||
|
||||
export function handleResponse(
|
||||
response: ClusterGetSettingsResponse,
|
||||
isCloudEnabled: boolean
|
||||
): ClusterSettingsReasonResponse {
|
||||
let source: keyof ClusterGetSettingsResponse;
|
||||
for (source in response) {
|
||||
if (Object.prototype.hasOwnProperty.call(response, source)) {
|
||||
const monitoringSettings = get(response[source], 'xpack.monitoring');
|
||||
if (monitoringSettings !== undefined) {
|
||||
const check = findReason(
|
||||
monitoringSettings,
|
||||
{
|
||||
context: `cluster ${source}`,
|
||||
},
|
||||
isCloudEnabled
|
||||
);
|
||||
|
||||
if (check.found) {
|
||||
return check;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { found: false };
|
||||
}
|
||||
|
||||
export async function checkClusterSettings(req: LegacyRequest) {
|
||||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin');
|
||||
const { cloud } = req.server.newPlatform.setup.plugins;
|
||||
const isCloudEnabled = !!(cloud && cloud.isCloudEnabled);
|
||||
const response = await callWithRequest(req, 'cluster.getSettings', { include_defaults: true });
|
||||
return handleResponse(response, isCloudEnabled);
|
||||
}
|
|
@ -19,7 +19,8 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
interval: -1,
|
||||
},
|
||||
},
|
||||
context
|
||||
context,
|
||||
false
|
||||
);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
|
@ -39,7 +40,8 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
enabled: false,
|
||||
},
|
||||
},
|
||||
context
|
||||
context,
|
||||
false
|
||||
);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
|
@ -61,7 +63,8 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
coolExporterToIgnore: {},
|
||||
},
|
||||
},
|
||||
context
|
||||
context,
|
||||
false
|
||||
);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
|
@ -76,14 +79,14 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
|
||||
describe('collection interval', () => {
|
||||
it('should not flag collection interval if value is > 0', async () => {
|
||||
const result = await findReason({ collection: { interval: 1 } }, context);
|
||||
const result = await findReason({ collection: { interval: 1 } }, context, false);
|
||||
expect(result).toEqual({ found: false });
|
||||
});
|
||||
|
||||
it('should flag collection interval for any invalid value', async () => {
|
||||
let result;
|
||||
|
||||
result = await findReason({ collection: { interval: 0 } }, context);
|
||||
result = await findReason({ collection: { interval: 0 } }, context, false);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
reason: {
|
||||
|
@ -93,7 +96,7 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
},
|
||||
});
|
||||
|
||||
result = await findReason({ collection: { interval: -10 } }, context);
|
||||
result = await findReason({ collection: { interval: -10 } }, context, false);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
reason: {
|
||||
|
@ -103,7 +106,7 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
},
|
||||
});
|
||||
|
||||
result = await findReason({ collection: { interval: null } }, context);
|
||||
result = await findReason({ collection: { interval: null } }, context, false);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
reason: {
|
||||
|
@ -116,16 +119,16 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
});
|
||||
|
||||
it('should not flag enabled if value is true', async () => {
|
||||
const result = await findReason({ enabled: true }, context);
|
||||
const result = await findReason({ enabled: true }, context, false);
|
||||
expect(result).toEqual({ found: false });
|
||||
});
|
||||
|
||||
it('should not flag exporters if value is undefined/null', async () => {
|
||||
let result;
|
||||
result = await findReason({ exporters: undefined }, context);
|
||||
result = await findReason({ exporters: undefined }, context, false);
|
||||
expect(result).toEqual({ found: false });
|
||||
|
||||
result = await findReason({ exporters: null }, context);
|
||||
result = await findReason({ exporters: null }, context, false);
|
||||
expect(result).toEqual({ found: false });
|
||||
});
|
||||
|
||||
|
@ -151,7 +154,7 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
},
|
||||
},
|
||||
};
|
||||
const result = await findReason(input, context);
|
||||
const result = await findReason(input, context, false);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
reason: {
|
||||
|
@ -204,7 +207,7 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
},
|
||||
},
|
||||
};
|
||||
const result = await findReason(input, context);
|
||||
const result = await findReason(input, context, false);
|
||||
expect(result).toEqual({
|
||||
found: true,
|
||||
reason: {
|
||||
|
@ -236,7 +239,7 @@ describe('Elasticsearch Settings Find Reason for No Data', () => {
|
|||
},
|
||||
},
|
||||
};
|
||||
const result = await findReason(input, context);
|
||||
const result = await findReason(input, context, false);
|
||||
expect(result).toEqual({ found: false });
|
||||
});
|
||||
});
|
|
@ -6,17 +6,22 @@
|
|||
*/
|
||||
|
||||
import { get } from 'lodash';
|
||||
import { ClusterSettingsReasonResponse } from '../../types';
|
||||
|
||||
/*
|
||||
* Return true if the settings property is enabled or is using its default state of enabled
|
||||
* Note: this assumes that a 0 corresponds to disabled
|
||||
*/
|
||||
const isEnabledOrDefault = (property) => {
|
||||
const isEnabledOrDefault = (property: string) => {
|
||||
return property === undefined || (Boolean(property) && property !== 'false');
|
||||
};
|
||||
|
||||
export function findReason(settingsSource, context, isCloudEnabled) {
|
||||
const iterateReasons = () => {
|
||||
export function findReason(
|
||||
settingsSource: any,
|
||||
context: { context: string },
|
||||
isCloudEnabled: boolean
|
||||
) {
|
||||
const iterateReasons = (): ClusterSettingsReasonResponse => {
|
||||
// PluginEnabled: check for `monitoring.enabled: false`
|
||||
const monitoringEnabled = get(settingsSource, 'enabled');
|
||||
if (!isEnabledOrDefault(monitoringEnabled)) {
|
||||
|
@ -92,9 +97,8 @@ export function findReason(settingsSource, context, isCloudEnabled) {
|
|||
return exporter.type !== 'local' && isEnabledOrDefault(exporter.enabled);
|
||||
});
|
||||
if (allEnabledRemote.length > 0 && allEnabledLocal.length === 0) {
|
||||
let ret = {};
|
||||
if (isCloudEnabled) {
|
||||
ret = {
|
||||
return {
|
||||
found: true,
|
||||
reason: {
|
||||
property: 'xpack.monitoring.exporters.cloud_enabled',
|
||||
|
@ -102,7 +106,7 @@ export function findReason(settingsSource, context, isCloudEnabled) {
|
|||
},
|
||||
};
|
||||
} else {
|
||||
ret = {
|
||||
return {
|
||||
found: true,
|
||||
reason: {
|
||||
property: 'xpack.monitoring.exporters',
|
||||
|
@ -112,11 +116,9 @@ export function findReason(settingsSource, context, isCloudEnabled) {
|
|||
},
|
||||
};
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { found: false };
|
||||
};
|
||||
|
|
@ -6,10 +6,11 @@
|
|||
*/
|
||||
|
||||
import { checkNodesSettings } from '.';
|
||||
import { LegacyRequest } from '../../types';
|
||||
|
||||
describe('Elasticsearch Nodes Settings', () => {
|
||||
const getReq = (response) => {
|
||||
return {
|
||||
const getReq = (response?: any) => {
|
||||
return ({
|
||||
server: {
|
||||
newPlatform: {
|
||||
setup: {
|
||||
|
@ -23,12 +24,14 @@ describe('Elasticsearch Nodes Settings', () => {
|
|||
plugins: {
|
||||
elasticsearch: {
|
||||
getCluster() {
|
||||
return { callWithRequest: () => Promise.resolve(response) };
|
||||
return {
|
||||
callWithRequest: () => Promise.resolve(response),
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
} as unknown) as LegacyRequest;
|
||||
};
|
||||
|
||||
it('should return { found: false } given no response from ES', async () => {
|
|
@ -6,9 +6,10 @@
|
|||
*/
|
||||
|
||||
import { get } from 'lodash';
|
||||
import { LegacyRequest } from '../../types';
|
||||
import { findReason } from './find_reason';
|
||||
|
||||
export function handleResponse({ nodes = {} } = {}, isCloudEnabled) {
|
||||
export function handleResponse({ nodes = {} } = {}, isCloudEnabled: boolean) {
|
||||
const nodeIds = Object.keys(nodes);
|
||||
for (const nodeId of nodeIds) {
|
||||
const nodeSettings = get(nodes, [nodeId, 'settings']);
|
||||
|
@ -31,7 +32,7 @@ export function handleResponse({ nodes = {} } = {}, isCloudEnabled) {
|
|||
return { found: false };
|
||||
}
|
||||
|
||||
export async function checkNodesSettings(req) {
|
||||
export async function checkNodesSettings(req: LegacyRequest) {
|
||||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin');
|
||||
const { cloud } = req.server.newPlatform.setup.plugins;
|
||||
const isCloudEnabled = !!(cloud && cloud.isCloudEnabled);
|
|
@ -5,7 +5,9 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
export function setCollectionDisabled(req) {
|
||||
import { LegacyRequest } from '../../../types';
|
||||
|
||||
export function setCollectionDisabled(req: LegacyRequest) {
|
||||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin');
|
||||
const params = {
|
||||
body: {
|
|
@ -5,7 +5,9 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
export function setCollectionEnabled(req) {
|
||||
import { LegacyRequest } from '../../../types';
|
||||
|
||||
export function setCollectionEnabled(req: LegacyRequest) {
|
||||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin');
|
||||
const params = {
|
||||
body: {
|
|
@ -5,7 +5,9 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
export function setCollectionInterval(req) {
|
||||
import { LegacyRequest } from '../../../types';
|
||||
|
||||
export function setCollectionInterval(req: LegacyRequest) {
|
||||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin');
|
||||
const params = {
|
||||
body: {
|
|
@ -172,3 +172,10 @@ export interface Bucket {
|
|||
export interface Aggregation {
|
||||
buckets: Bucket[];
|
||||
}
|
||||
export interface ClusterSettingsReasonResponse {
|
||||
found: boolean;
|
||||
reason?: {
|
||||
property?: string;
|
||||
data?: string;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue