Skip to content

Commit

Permalink
[Stack Monitoring] Convert elasticsearch routes to TypeScript (elasti…
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort authored and kertal committed May 24, 2022
1 parent d4167a7 commit 742a2a2
Show file tree
Hide file tree
Showing 40 changed files with 696 additions and 295 deletions.
15 changes: 15 additions & 0 deletions x-pack/plugins/monitoring/common/http_api/elasticsearch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

export * from './post_elasticsearch_ccr';
export * from './post_elasticsearch_ccr_shard';
export * from './post_elasticsearch_index_detail';
export * from './post_elasticsearch_indices';
export * from './post_elasticsearch_ml_jobs';
export * from './post_elasticsearch_node_detail';
export * from './post_elasticsearch_nodes';
export * from './post_elasticsearch_overview';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postElasticsearchCcrRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postElasticsearchCcrRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
}),
]);

export type PostElasticsearchCcrRequestPayload = rt.TypeOf<
typeof postElasticsearchCcrRequestPayloadRT
>;

export const postElasticsearchCcrResponsePayloadRT = rt.type({
// TODO: add payload entries
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postElasticsearchCcrShardRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
index: rt.string,
shardId: rt.string,
});

export const postElasticsearchCcrShardRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
}),
]);

export type PostElasticsearchCcrShardRequestPayload = rt.TypeOf<
typeof postElasticsearchCcrShardRequestPayloadRT
>;

export const postElasticsearchCcrShardResponsePayloadRT = rt.type({
// TODO: add payload entries
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 * as rt from 'io-ts';
import { ccsRT, clusterUuidRT, createLiteralValueFromUndefinedRT, timeRangeRT } from '../shared';

export const postElasticsearchIndexDetailRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
id: rt.string,
});

export const postElasticsearchIndexDetailRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
is_advanced: rt.union([rt.boolean, createLiteralValueFromUndefinedRT(false)]),
timeRange: timeRangeRT,
}),
]);

export type PostElasticsearchIndexDetailRequestPayload = rt.TypeOf<
typeof postElasticsearchIndexDetailRequestPayloadRT
>;

export const postElasticsearchIndexDetailResponsePayloadRT = rt.type({
// TODO: add payload entries
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 * as rt from 'io-ts';
import {
booleanFromStringRT,
ccsRT,
clusterUuidRT,
createLiteralValueFromUndefinedRT,
timeRangeRT,
} from '../shared';

export const postElasticsearchIndicesRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postElasticsearchIndicesRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
is_advanced: rt.union([rt.boolean, createLiteralValueFromUndefinedRT(false)]),
timeRange: timeRangeRT,
}),
]);

export type PostElasticsearchIndicesRequestPayload = rt.TypeOf<
typeof postElasticsearchIndicesRequestPayloadRT
>;

export const postElasticsearchIndicesRequestQueryRT = rt.type({
show_system_indices: booleanFromStringRT,
});

export const postElasticsearchIndicesResponsePayloadRT = rt.type({
// TODO: add payload entries
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postElasticsearchMlJobsRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postElasticsearchMlJobsRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
}),
]);

export type PostElasticsearchMlJobsRequestPayload = rt.TypeOf<
typeof postElasticsearchMlJobsRequestPayloadRT
>;

export const postElasticsearchMlJobsResponsePayloadRT = rt.type({
// TODO: add payload entries
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 * as rt from 'io-ts';
import { ccsRT, clusterUuidRT, createLiteralValueFromUndefinedRT, timeRangeRT } from '../shared';

export const postElasticsearchNodeDetailRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
nodeUuid: rt.string,
});

export const postElasticsearchNodeDetailRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
showSystemIndices: rt.boolean, // show/hide system indices in shard allocation table
}),
rt.type({
is_advanced: rt.union([rt.boolean, createLiteralValueFromUndefinedRT(false)]),
timeRange: timeRangeRT,
}),
]);

export type PostElasticsearchNodeDetailRequestPayload = rt.TypeOf<
typeof postElasticsearchNodeDetailRequestPayloadRT
>;

export const postElasticsearchNodeDetailResponsePayloadRT = rt.type({
// TODO: add payload entries
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT, paginationRT, sortingRT } from '../shared';

export const postElasticsearchNodesRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postElasticsearchNodesRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
queryText: rt.string,
sort: sortingRT,
}),
rt.type({
timeRange: timeRangeRT,
pagination: paginationRT,
}),
]);

export type PostElasticsearchNodesRequestPayload = rt.TypeOf<
typeof postElasticsearchNodesRequestPayloadRT
>;

export const postElasticsearchNodesResponsePayloadRT = rt.type({
// TODO: add payload entries
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postElasticsearchOverviewRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postElasticsearchOverviewRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
}),
]);

export type PostElasticsearchOverviewRequestPayload = rt.TypeOf<
typeof postElasticsearchOverviewRequestPayloadRT
>;

export const postElasticsearchOverviewResponsePayloadRT = rt.type({
// TODO: add payload entries
});
6 changes: 5 additions & 1 deletion x-pack/plugins/monitoring/common/http_api/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* 2.0.
*/

export * from './cluster';
export * from './ccs';
export * from './cluster';
export * from './literal_value';
export * from './pagination';
export * from './query_string_boolean';
export * from './sorting';
export * from './time_range';
20 changes: 20 additions & 0 deletions x-pack/plugins/monitoring/common/http_api/shared/literal_value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 * as rt from 'io-ts';

export const createLiteralValueFromUndefinedRT = <LiteralValue extends string | number | boolean>(
literalValue: LiteralValue
) =>
rt.undefined.pipe(
new rt.Type<LiteralValue, undefined, unknown>(
'BooleanFromString',
rt.literal(literalValue).is,
(_value, _context) => rt.success(literalValue),
() => undefined
)
);
13 changes: 13 additions & 0 deletions x-pack/plugins/monitoring/common/http_api/shared/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 * as rt from 'io-ts';

export const paginationRT = rt.type({
index: rt.number,
size: rt.number,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 { chain } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import * as rt from 'io-ts';

export const booleanFromStringRT = new rt.Type<boolean, string, unknown>(
'BooleanFromString',
rt.boolean.is,
(value, context) =>
pipe(
rt.string.validate(value, context),
chain((stringValue) =>
stringValue === 'true'
? rt.success(true)
: stringValue === 'false'
? rt.success(false)
: rt.failure(value, context)
)
),
String
);
18 changes: 18 additions & 0 deletions x-pack/plugins/monitoring/common/http_api/shared/sorting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 * as rt from 'io-ts';

const sortingDirectionRT = rt.keyof({
asc: null,
desc: null,
});

export const sortingRT = rt.partial({
field: rt.string,
direction: sortingDirectionRT,
});
Loading

0 comments on commit 742a2a2

Please sign in to comment.