-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infrastructure UI] Create InventoryViewsService and InventoryViewsCl…
…ient (#155126) ## 📓 Summary Depends on #154900 Closes #155110 This PR implements the `InventoryViewsService` and `InventoryViewsClient`, injecting an instance of the client in the KibanaContextForPlugin and exposing so a set of utilities to retrieve/update inventory views: - `findInventoryViews` - `getInventoryView` - `createInventoryView` - `updateInventoryView` - `deleteInventoryView` ## 👣 Next steps - Implement #154725 to consume the service --------- Co-authored-by: Marco Antonio Ghiani <marcoantonio.ghiani@elastic.co> Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
636674c
commit 1351760
Showing
11 changed files
with
264 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
/* eslint-disable max-classes-per-file */ | ||
|
||
export class FetchInventoryViewError extends Error { | ||
constructor(message: string, public cause?: Error) { | ||
super(message); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
this.name = 'FetchInventoryViewError'; | ||
} | ||
} | ||
|
||
export class UpsertInventoryViewError extends Error { | ||
constructor(message: string, public cause?: Error) { | ||
super(message); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
this.name = 'UpsertInventoryViewError'; | ||
} | ||
} | ||
|
||
export class DeleteInventoryViewError extends Error { | ||
constructor(message: string, public cause?: Error) { | ||
super(message); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
this.name = 'DeleteInventoryViewError'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
x-pack/plugins/infra/public/services/inventory_views/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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_client'; | ||
export * from './inventory_views_service'; | ||
export * from './types'; |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/infra/public/services/inventory_views/inventory_views_client.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 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 { IInventoryViewsClient } from './types'; | ||
|
||
export const createInventoryViewsClientMock = (): jest.Mocked<IInventoryViewsClient> => ({ | ||
findInventoryViews: jest.fn(), | ||
getInventoryView: jest.fn(), | ||
createInventoryView: jest.fn(), | ||
updateInventoryView: jest.fn(), | ||
deleteInventoryView: jest.fn(), | ||
}); |
120 changes: 120 additions & 0 deletions
120
x-pack/plugins/infra/public/services/inventory_views/inventory_views_client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* 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 { HttpStart } from '@kbn/core/public'; | ||
import { | ||
CreateInventoryViewAttributesRequestPayload, | ||
createInventoryViewRequestPayloadRT, | ||
findInventoryViewResponsePayloadRT, | ||
getInventoryViewUrl, | ||
inventoryViewResponsePayloadRT, | ||
UpdateInventoryViewAttributesRequestPayload, | ||
} from '../../../common/http_api/latest'; | ||
import { | ||
DeleteInventoryViewError, | ||
FetchInventoryViewError, | ||
InventoryView, | ||
UpsertInventoryViewError, | ||
} from '../../../common/inventory_views'; | ||
import { decodeOrThrow } from '../../../common/runtime_types'; | ||
import { IInventoryViewsClient } from './types'; | ||
|
||
export class InventoryViewsClient implements IInventoryViewsClient { | ||
constructor(private readonly http: HttpStart) {} | ||
|
||
async findInventoryViews(): Promise<InventoryView[]> { | ||
const response = await this.http.get(getInventoryViewUrl()).catch((error) => { | ||
throw new FetchInventoryViewError(`Failed to fetch inventory views: ${error}`); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
findInventoryViewResponsePayloadRT, | ||
(message: string) => | ||
new FetchInventoryViewError(`Failed to decode inventory views: ${message}"`) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
async getInventoryView(inventoryViewId: string): Promise<InventoryView> { | ||
const response = await this.http.get(getInventoryViewUrl(inventoryViewId)).catch((error) => { | ||
throw new FetchInventoryViewError( | ||
`Failed to fetch inventory view "${inventoryViewId}": ${error}` | ||
); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
inventoryViewResponsePayloadRT, | ||
(message: string) => | ||
new FetchInventoryViewError( | ||
`Failed to decode inventory view "${inventoryViewId}": ${message}"` | ||
) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
async createInventoryView( | ||
inventoryViewAttributes: CreateInventoryViewAttributesRequestPayload | ||
): Promise<InventoryView> { | ||
const response = await this.http | ||
.post(getInventoryViewUrl(), { | ||
body: JSON.stringify( | ||
createInventoryViewRequestPayloadRT.encode({ attributes: inventoryViewAttributes }) | ||
), | ||
}) | ||
.catch((error) => { | ||
throw new UpsertInventoryViewError(`Failed to create new inventory view : ${error}`); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
inventoryViewResponsePayloadRT, | ||
(message: string) => | ||
new UpsertInventoryViewError(`Failed to decode newly written inventory view: ${message}"`) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
async updateInventoryView( | ||
inventoryViewId: string, | ||
inventoryViewAttributes: UpdateInventoryViewAttributesRequestPayload | ||
): Promise<InventoryView> { | ||
const response = await this.http | ||
.put(getInventoryViewUrl(inventoryViewId), { | ||
body: JSON.stringify( | ||
createInventoryViewRequestPayloadRT.encode({ attributes: inventoryViewAttributes }) | ||
), | ||
}) | ||
.catch((error) => { | ||
throw new UpsertInventoryViewError( | ||
`Failed to update inventory view "${inventoryViewId}": ${error}` | ||
); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
inventoryViewResponsePayloadRT, | ||
(message: string) => | ||
new UpsertInventoryViewError( | ||
`Failed to decode updated inventory view "${inventoryViewId}": ${message}"` | ||
) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
deleteInventoryView(inventoryViewId: string): Promise<null> { | ||
return this.http | ||
.delete(getInventoryViewUrl(inventoryViewId)) | ||
.then(() => null) | ||
.catch((error) => { | ||
throw new DeleteInventoryViewError( | ||
`Failed to delete inventory view "${inventoryViewId}": ${error}` | ||
); | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/infra/public/services/inventory_views/inventory_views_service.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 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 { createInventoryViewsClientMock } from './inventory_views_client.mock'; | ||
import { InventoryViewsServiceStart } from './types'; | ||
|
||
export const createInventoryViewsServiceStartMock = () => ({ | ||
client: createInventoryViewsClientMock(), | ||
}); | ||
|
||
export const _ensureTypeCompatibility = (): InventoryViewsServiceStart => | ||
createInventoryViewsServiceStartMock(); |
25 changes: 25 additions & 0 deletions
25
x-pack/plugins/infra/public/services/inventory_views/inventory_views_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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 { InventoryViewsClient } from './inventory_views_client'; | ||
import { | ||
InventoryViewsServiceStartDeps, | ||
InventoryViewsServiceSetup, | ||
InventoryViewsServiceStart, | ||
} from './types'; | ||
|
||
export class InventoryViewsService { | ||
public setup(): InventoryViewsServiceSetup {} | ||
|
||
public start({ http }: InventoryViewsServiceStartDeps): InventoryViewsServiceStart { | ||
const client = new InventoryViewsClient(http); | ||
|
||
return { | ||
client, | ||
}; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/infra/public/services/inventory_views/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { HttpStart } from '@kbn/core/public'; | ||
import { InventoryView, InventoryViewAttributes } from '../../../common/inventory_views'; | ||
|
||
export type InventoryViewsServiceSetup = void; | ||
|
||
export interface InventoryViewsServiceStart { | ||
client: IInventoryViewsClient; | ||
} | ||
|
||
export interface InventoryViewsServiceStartDeps { | ||
http: HttpStart; | ||
} | ||
|
||
export interface IInventoryViewsClient { | ||
findInventoryViews(): Promise<InventoryView[]>; | ||
getInventoryView(inventoryViewId: string): Promise<InventoryView>; | ||
createInventoryView( | ||
inventoryViewAttributes: Partial<InventoryViewAttributes> | ||
): Promise<InventoryView>; | ||
updateInventoryView( | ||
inventoryViewId: string, | ||
inventoryViewAttributes: Partial<InventoryViewAttributes> | ||
): Promise<InventoryView>; | ||
deleteInventoryView(inventoryViewId: string): Promise<null>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters