Skip to content

Commit

Permalink
[Infrastructure UI] Implement inventory views CRUD endpoints (elastic…
Browse files Browse the repository at this point in the history
…#154900)

## 📓  Summary

Part of elastic#152617 
Closes elastic#155158 

This PR implements the CRUD endpoints for the inventory views.
Following the approach used for the LogViews service, it exposes a
client that abstracts all the logic concerned to the `inventory-view`
saved objects.

It also follows the guideline provided for [Versioning
interfaces](https://docs.elastic.dev/kibana-dev-docs/versioning-interfaces)
and [Versioning HTTP
APIs](https://docs.elastic.dev/kibana-dev-docs/versioning-http-apis),
preparing for the serverless.

## 🤓 Tips for the reviewer
You can open the Kibana dev tools and play with the following snippet to
test the create APIs, or you can perform the same requests with your
preferred client:
```
// Get all
GET kbn:/api/infra/inventory_views

// Create one
POST kbn:/api/infra/inventory_views
{
  "attributes": {
    "name": "My inventory view"
  }
}

// Get one
GET kbn:/api/infra/inventory_views/<switch-with-id>

// Update one
PUT kbn:/api/infra/inventory_views/<switch-with-id>
{
  "attributes": {
    "name": "My inventory view 2"
  }
}

// Delete one
DELETE kbn:/api/infra/inventory_views/<switch-with-id>
```

## 👣 Next steps
- Replicate the same logic for the metrics explorer saved object
- Create a client-side abstraction to consume the service
- Update the existing react custom hooks to consume the endpoint

---------

Co-authored-by: Marco Antonio Ghiani <marcoantonio.ghiani@elastic.co>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
3 people authored and nikitaindik committed Apr 25, 2023
1 parent e987038 commit aacbd4d
Show file tree
Hide file tree
Showing 42 changed files with 1,855 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-io-ts-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

export type { IndexPatternType } from './src/index_pattern_rt';
export type { NonEmptyStringBrand } from './src/non_empty_string_rt';
export type { NonEmptyString, NonEmptyStringBrand } from './src/non_empty_string_rt';

export { deepExactRt } from './src/deep_exact_rt';
export { indexPatternRt } from './src/index_pattern_rt';
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/infra/common/http_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ export * from './log_alerts';
export * from './snapshot_api';
export * from './host_details';
export * from './infra';

/**
* Exporting versioned APIs types
*/
export * from './latest';
export * as inventoryViewsV1 from './inventory_views/v1';
66 changes: 66 additions & 0 deletions x-pack/plugins/infra/common/http_api/inventory_views/v1/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 { nonEmptyStringRt } from '@kbn/io-ts-utils';
import * as rt from 'io-ts';
import { either } from 'fp-ts/Either';

export const INVENTORY_VIEW_URL = '/api/infra/inventory_views';
export const INVENTORY_VIEW_URL_ENTITY = `${INVENTORY_VIEW_URL}/{inventoryViewId}`;
export const getInventoryViewUrl = (inventoryViewId?: string) =>
[INVENTORY_VIEW_URL, inventoryViewId].filter(Boolean).join('/');

const inventoryViewIdRT = new rt.Type<string, string, unknown>(
'InventoryViewId',
rt.string.is,
(u, c) =>
either.chain(rt.string.validate(u, c), (id) => {
return id === '0'
? rt.failure(u, c, `The inventory view with id ${id} is not configurable.`)
: rt.success(id);
}),
String
);

export const inventoryViewRequestParamsRT = rt.type({
inventoryViewId: inventoryViewIdRT,
});

export type InventoryViewRequestParams = rt.TypeOf<typeof inventoryViewRequestParamsRT>;

export const inventoryViewRequestQueryRT = rt.partial({
sourceId: rt.string,
});

export type InventoryViewRequestQuery = rt.TypeOf<typeof inventoryViewRequestQueryRT>;

const inventoryViewAttributesResponseRT = rt.intersection([
rt.strict({
name: nonEmptyStringRt,
isDefault: rt.boolean,
isStatic: rt.boolean,
}),
rt.UnknownRecord,
]);

const inventoryViewResponseRT = rt.exact(
rt.intersection([
rt.type({
id: rt.string,
attributes: inventoryViewAttributesResponseRT,
}),
rt.partial({
updatedAt: rt.number,
version: rt.string,
}),
])
);

export const inventoryViewResponsePayloadRT = rt.type({
data: inventoryViewResponseRT,
});

export type GetInventoryViewResponsePayload = rt.TypeOf<typeof inventoryViewResponsePayloadRT>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 { nonEmptyStringRt } from '@kbn/io-ts-utils';
import * as rt from 'io-ts';

export const createInventoryViewAttributesRequestPayloadRT = rt.intersection([
rt.type({
name: nonEmptyStringRt,
}),
rt.UnknownRecord,
rt.exact(rt.partial({ isDefault: rt.undefined, isStatic: rt.undefined })),
]);

export type CreateInventoryViewAttributesRequestPayload = rt.TypeOf<
typeof createInventoryViewAttributesRequestPayloadRT
>;

export const createInventoryViewRequestPayloadRT = rt.type({
attributes: createInventoryViewAttributesRequestPayloadRT,
});

export type CreateInventoryViewRequestPayload = rt.TypeOf<
typeof createInventoryViewRequestPayloadRT
>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { nonEmptyStringRt } from '@kbn/io-ts-utils';
import * as rt from 'io-ts';

export const findInventoryViewAttributesResponseRT = rt.strict({
name: nonEmptyStringRt,
isDefault: rt.boolean,
isStatic: rt.boolean,
});

const findInventoryViewResponseRT = rt.exact(
rt.intersection([
rt.type({
id: rt.string,
attributes: findInventoryViewAttributesResponseRT,
}),
rt.partial({
updatedAt: rt.number,
version: rt.string,
}),
])
);

export const findInventoryViewResponsePayloadRT = rt.type({
data: rt.array(findInventoryViewResponseRT),
});

export type FindInventoryViewResponsePayload = rt.TypeOf<typeof findInventoryViewResponsePayloadRT>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 getInventoryViewRequestParamsRT = rt.type({
inventoryViewId: rt.string,
});

export type GetInventoryViewRequestParams = rt.TypeOf<typeof getInventoryViewRequestParamsRT>;
12 changes: 12 additions & 0 deletions x-pack/plugins/infra/common/http_api/inventory_views/v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 './common';
export * from './get_inventory_view';
export * from './find_inventory_view';
export * from './create_inventory_view';
export * from './update_inventory_view';
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 { nonEmptyStringRt } from '@kbn/io-ts-utils';
import * as rt from 'io-ts';

export const updateInventoryViewAttributesRequestPayloadRT = rt.intersection([
rt.type({
name: nonEmptyStringRt,
}),
rt.UnknownRecord,
rt.exact(rt.partial({ isDefault: rt.undefined, isStatic: rt.undefined })),
]);

export type UpdateInventoryViewAttributesRequestPayload = rt.TypeOf<
typeof updateInventoryViewAttributesRequestPayloadRT
>;

export const updateInventoryViewRequestPayloadRT = rt.type({
attributes: updateInventoryViewAttributesRequestPayloadRT,
});

export type UpdateInventoryViewRequestPayload = rt.TypeOf<
typeof updateInventoryViewRequestPayloadRT
>;
8 changes: 8 additions & 0 deletions x-pack/plugins/infra/common/http_api/latest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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 './inventory_views/v1';
52 changes: 52 additions & 0 deletions x-pack/plugins/infra/common/inventory_views/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 { i18n } from '@kbn/i18n';
import type { NonEmptyString } from '@kbn/io-ts-utils';
import type { InventoryViewAttributes } from './types';

export const staticInventoryViewId = '0';

export const staticInventoryViewAttributes: InventoryViewAttributes = {
name: i18n.translate('xpack.infra.savedView.defaultViewNameHosts', {
defaultMessage: 'Default view',
}) as NonEmptyString,
isDefault: false,
isStatic: true,
metric: {
type: 'cpu',
},
groupBy: [],
nodeType: 'host',
view: 'map',
customOptions: [],
boundsOverride: {
max: 1,
min: 0,
},
autoBounds: true,
accountId: '',
region: '',
customMetrics: [],
legend: {
palette: 'cool',
steps: 10,
reverseColors: false,
},
source: 'default',
sort: {
by: 'name',
direction: 'desc',
},
timelineOpen: false,
filterQuery: {
kind: 'kuery',
expression: '',
},
time: Date.now(),
autoReload: false,
};
9 changes: 9 additions & 0 deletions x-pack/plugins/infra/common/inventory_views/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 './defaults';
export * from './types';
24 changes: 24 additions & 0 deletions x-pack/plugins/infra/common/inventory_views/inventory_view.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { staticInventoryViewAttributes } from './defaults';
import type { InventoryView, InventoryViewAttributes } from './types';

export const createInventoryViewMock = (
id: string,
attributes: InventoryViewAttributes,
updatedAt?: number,
version?: string
): InventoryView => ({
id,
attributes: {
...staticInventoryViewAttributes,
...attributes,
},
updatedAt,
version,
});
35 changes: 35 additions & 0 deletions x-pack/plugins/infra/common/inventory_views/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 { nonEmptyStringRt } from '@kbn/io-ts-utils';
import * as rt from 'io-ts';

export const inventoryViewAttributesRT = rt.intersection([
rt.strict({
name: nonEmptyStringRt,
isDefault: rt.boolean,
isStatic: rt.boolean,
}),
rt.UnknownRecord,
]);

export type InventoryViewAttributes = rt.TypeOf<typeof inventoryViewAttributesRT>;

export const inventoryViewRT = rt.exact(
rt.intersection([
rt.type({
id: rt.string,
attributes: inventoryViewAttributesRT,
}),
rt.partial({
updatedAt: rt.number,
version: rt.string,
}),
])
);

export type InventoryView = rt.TypeOf<typeof inventoryViewRT>;
51 changes: 51 additions & 0 deletions x-pack/plugins/infra/common/metrics_explorer_views/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 { i18n } from '@kbn/i18n';
import type { NonEmptyString } from '@kbn/io-ts-utils';
import type { MetricsExplorerViewAttributes } from './types';

export const staticMetricsExplorerViewId = 'static';

export const staticMetricsExplorerViewAttributes: MetricsExplorerViewAttributes = {
name: i18n.translate('xpack.infra.savedView.defaultViewNameHosts', {
defaultMessage: 'Default view',
}) as NonEmptyString,
isDefault: false,
isStatic: true,
options: {
aggregation: 'avg',
metrics: [
{
aggregation: 'avg',
field: 'system.cpu.total.norm.pct',
color: 'color0',
},
{
aggregation: 'avg',
field: 'kubernetes.pod.cpu.usage.node.pct',
color: 'color1',
},
{
aggregation: 'avg',
field: 'docker.cpu.total.pct',
color: 'color2',
},
],
source: 'default',
},
chartOptions: {
type: 'line',
yAxisMode: 'fromZero',
stack: false,
},
currentTimerange: {
from: 'now-1h',
to: 'now',
interval: '>=10s',
},
};
Loading

0 comments on commit aacbd4d

Please sign in to comment.