[Stack Monitoring] More typescript conversion (#126654)

* Converts monitoring_bulk to TS

* Converts routes/alerts to TS, closes #117755

* Converts check_access route to TS

* Converts clusters routes to TS

Also changes some overly restrictive types that weren't accurate

* Fixes lowercase method for legacy server route
This commit is contained in:
Jason Rhodes 2022-03-02 15:26:08 -05:00 committed by GitHub
parent 9d53810791
commit 0bd772432a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 32 additions and 20 deletions

View file

@ -5,7 +5,6 @@
* 2.0.
*/
import { Logger, ICustomClusterClient, ElasticsearchClientConfig } from 'kibana/server';
// @ts-ignore
import { monitoringBulk } from '../kibana_monitoring/lib/monitoring_bulk';
import { monitoringEndpointDisableWatches } from './monitoring_endpoint_disable_watches';
import { MonitoringElasticsearchConfig } from '../config';

View file

@ -5,7 +5,9 @@
* 2.0.
*/
export function monitoringBulk(Client, _config, components) {
// TODO: Track down where this function is called by the elasticsearch client setup so we can properly type these
export function monitoringBulk(Client: any, _config: any, components: any) {
const ca = components.clientAction.factory;
Client.prototype.monitoring = components.clientAction.namespaceFactory();
const monitoring = Client.prototype.monitoring.prototype;

View file

@ -50,7 +50,7 @@ export async function getClustersFromRequest(
start,
end,
codePaths,
}: { clusterUuid: string; start: number; end: number; codePaths: string[] }
}: { clusterUuid?: string; start?: number; end?: number; codePaths: string[] }
) {
const { filebeatIndexPattern } = indexPatterns;
@ -96,13 +96,14 @@ export async function getClustersFromRequest(
cluster.ml = { jobs: mlJobs };
}
cluster.logs = isInCodePath(codePaths, [CODE_PATH_LOGS])
? await getLogTypes(req, filebeatIndexPattern, {
clusterUuid: get(cluster, 'elasticsearch.cluster.id', cluster.cluster_uuid),
start,
end,
})
: [];
cluster.logs =
start && end && isInCodePath(codePaths, [CODE_PATH_LOGS])
? await getLogTypes(req, filebeatIndexPattern, {
clusterUuid: get(cluster, 'elasticsearch.cluster.id', cluster.cluster_uuid),
start,
end,
})
: [];
} else if (!isStandaloneCluster) {
// get all clusters
if (!clusters || clusters.length === 0) {

View file

@ -26,7 +26,7 @@ import { Globals } from '../../static_globals';
* @param {String} clusterUuid (optional) If not undefined, getClusters will filter for a single cluster
* @return {Promise} A promise containing an array of clusters.
*/
export function getClustersStats(req: LegacyRequest, clusterUuid: string, ccs?: string) {
export function getClustersStats(req: LegacyRequest, clusterUuid?: string, ccs?: string) {
return (
fetchClusterStats(req, clusterUuid, ccs)
.then((response) => handleClusterStats(response))
@ -42,7 +42,7 @@ export function getClustersStats(req: LegacyRequest, clusterUuid: string, ccs?:
* @param {String} clusterUuid (optional) - if not undefined, getClusters filters for a single clusterUuid
* @return {Promise} Object representing each cluster.
*/
function fetchClusterStats(req: LegacyRequest, clusterUuid: string, ccs?: string) {
function fetchClusterStats(req: LegacyRequest, clusterUuid?: string, ccs?: string) {
const dataset = 'cluster_stats';
const moduleType = 'elasticsearch';
const indexPattern = getNewIndexPatterns({

View file

@ -72,7 +72,7 @@ interface CreateQueryOptions {
dsDataset?: string;
metricset?: string;
filters?: any[];
clusterUuid: string;
clusterUuid?: string;
uuid?: string;
start?: number;
end?: number;

View file

@ -17,6 +17,8 @@ import { LegacyRequest } from '../../types';
*
* @param req {Object} the server route handler request object
*/
// TODO: replace LegacyRequest with current request object + plugin retrieval
export async function verifyMonitoringAuth(req: LegacyRequest) {
const xpackInfo = get(req.server.plugins.monitoring, 'info');
@ -38,6 +40,8 @@ export async function verifyMonitoringAuth(req: LegacyRequest) {
* @param req {Object} the server route handler request object
* @return {Promise} That either resolves with no response (void) or an exception.
*/
// TODO: replace LegacyRequest with current request object + plugin retrieval
async function verifyHasPrivileges(req: LegacyRequest) {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring');

View file

@ -15,7 +15,7 @@ import { Globals } from '../../static_globals';
interface GetLogstashPipelineIdsParams {
req: LegacyRequest;
clusterUuid: string;
clusterUuid?: string;
size: number;
logstashUuid?: string;
ccs?: string;

View file

@ -7,17 +7,20 @@
import { verifyMonitoringAuth } from '../../../../lib/elasticsearch/verify_monitoring_auth';
import { handleError } from '../../../../lib/errors';
import { LegacyRequest, LegacyServer } from '../../../../types';
/*
* API for checking read privilege on Monitoring Data
* Used for the "Access Denied" page as something to auto-retry with.
*/
export function checkAccessRoute(server) {
// TODO: Replace this LegacyServer call with the "new platform" core Kibana route method
export function checkAccessRoute(server: LegacyServer) {
server.route({
method: 'GET',
path: '/api/monitoring/v1/check_access',
handler: async (req) => {
const response = {};
handler: async (req: LegacyRequest) => {
const response: { has_access?: boolean } = {};
try {
await verifyMonitoringAuth(req);
response.has_access = true; // response data is ignored

View file

@ -6,16 +6,19 @@
*/
import { schema } from '@kbn/config-schema';
import { LegacyRequest, LegacyServer } from '../../../../types';
import { getClustersFromRequest } from '../../../../lib/cluster/get_clusters_from_request';
import { verifyMonitoringAuth } from '../../../../lib/elasticsearch/verify_monitoring_auth';
import { handleError } from '../../../../lib/errors';
import { getIndexPatterns } from '../../../../lib/cluster/get_index_patterns';
export function clustersRoute(server) {
export function clustersRoute(server: LegacyServer) {
/*
* Monitoring Home
* Route Init (for checking license and compatibility for multi-cluster monitoring
*/
// TODO switch from the LegacyServer route() method to the "new platform" route methods
server.route({
method: 'POST',
path: '/api/monitoring/v1/clusters',
@ -30,7 +33,7 @@ export function clustersRoute(server) {
}),
},
},
handler: async (req) => {
handler: async (req: LegacyRequest) => {
let clusters = [];
const config = server.config;
@ -43,7 +46,7 @@ export function clustersRoute(server) {
filebeatIndexPattern: config.ui.logs.index,
});
clusters = await getClustersFromRequest(req, indexPatterns, {
codePaths: req.payload.codePaths,
codePaths: req.payload.codePaths as string[], // TODO remove this cast when we can properly type req by using the right route handler
});
} catch (err) {
throw handleError(err, req);