-
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.
Add inventory metadata api with regions and accounts (#52660)
* Add inventory metadata api with regions and accounts * Comment magic number * Refactor to be compat with new platform * Fix types * Use RequiredDataset to get account details. Fix hook decodes * Remove unused import * Update x-pack/legacy/plugins/infra/server/routes/inventory_metadata/lib/get_aws_metadata.ts Co-Authored-By: Chris Cowan <[email protected]> * Rename some things again * Fix type
- Loading branch information
Showing
19 changed files
with
272 additions
and
17 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
x-pack/legacy/plugins/infra/common/http_api/inventory_meta_api.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,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as rt from 'io-ts'; | ||
import { ItemTypeRT } from '../inventory_models/types'; | ||
|
||
const AWSAccountRT = rt.type({ | ||
value: rt.string, | ||
name: rt.string, | ||
}); | ||
|
||
export const InventoryMetaResponseRT = rt.type({ | ||
accounts: rt.array(AWSAccountRT), | ||
projects: rt.array(rt.string), | ||
regions: rt.array(rt.string), | ||
}); | ||
|
||
export const InventoryMetaRequestRT = rt.type({ | ||
sourceId: rt.string, | ||
nodeType: ItemTypeRT, | ||
}); | ||
|
||
export type InventoryMetaRequest = rt.TypeOf<typeof InventoryMetaRequestRT>; | ||
export type InventoryMetaResponse = rt.TypeOf<typeof InventoryMetaResponseRT>; | ||
export type InventoryAWSAccount = rt.TypeOf<typeof AWSAccountRT>; |
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
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
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
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
47 changes: 47 additions & 0 deletions
47
x-pack/legacy/plugins/infra/public/containers/inventory_metadata/use_inventory_meta.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,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { fold } from 'fp-ts/lib/Either'; | ||
import { identity } from 'fp-ts/lib/function'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { useEffect } from 'react'; | ||
import { throwErrors, createPlainError } from '../../../common/runtime_types'; | ||
import { useHTTPRequest } from '../../hooks/use_http_request'; | ||
import { | ||
InventoryMetaResponseRT, | ||
InventoryMetaResponse, | ||
} from '../../../common/http_api/inventory_meta_api'; | ||
import { InfraNodeType } from '../../graphql/types'; | ||
|
||
export function useInventoryMeta(sourceId: string, nodeType: InfraNodeType) { | ||
const decodeResponse = (response: any) => { | ||
return pipe( | ||
InventoryMetaResponseRT.decode(response), | ||
fold(throwErrors(createPlainError), identity) | ||
); | ||
}; | ||
|
||
const { error, loading, response, makeRequest } = useHTTPRequest<InventoryMetaResponse>( | ||
'/api/infra/inventory/meta', | ||
'POST', | ||
JSON.stringify({ | ||
sourceId, | ||
nodeType, | ||
}), | ||
decodeResponse | ||
); | ||
|
||
useEffect(() => { | ||
makeRequest(); | ||
}, [makeRequest]); | ||
|
||
return { | ||
error, | ||
loading, | ||
accounts: response ? response.accounts : [], | ||
regions: response ? response.regions : [], | ||
makeRequest, | ||
}; | ||
} |
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
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
61 changes: 61 additions & 0 deletions
61
x-pack/legacy/plugins/infra/server/routes/inventory_metadata/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,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
import Boom from 'boom'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { fold } from 'fp-ts/lib/Either'; | ||
import { identity } from 'fp-ts/lib/function'; | ||
import { InfraBackendLibs } from '../../lib/infra_types'; | ||
import { throwErrors } from '../../../common/runtime_types'; | ||
import { getAWSMetadata } from './lib/get_aws_metadata'; | ||
import { | ||
InventoryMetaRequestRT, | ||
InventoryMetaResponseRT, | ||
} from '../../../common/http_api/inventory_meta_api'; | ||
|
||
const escapeHatch = schema.object({}, { allowUnknowns: true }); | ||
|
||
export const initInventoryMetaRoute = (libs: InfraBackendLibs) => { | ||
const { framework } = libs; | ||
|
||
framework.registerRoute( | ||
{ | ||
method: 'post', | ||
path: '/api/infra/inventory/meta', | ||
validate: { | ||
body: escapeHatch, | ||
}, | ||
}, | ||
async (requestContext, request, response) => { | ||
try { | ||
const { sourceId, nodeType } = pipe( | ||
InventoryMetaRequestRT.decode(request.body), | ||
fold(throwErrors(Boom.badRequest), identity) | ||
); | ||
|
||
const { configuration } = await libs.sources.getSourceConfiguration( | ||
requestContext, | ||
sourceId | ||
); | ||
const awsMetadata = await getAWSMetadata( | ||
framework, | ||
requestContext, | ||
configuration, | ||
nodeType | ||
); | ||
|
||
return response.ok({ | ||
body: InventoryMetaResponseRT.encode(awsMetadata), | ||
}); | ||
} catch (error) { | ||
return response.internalError({ | ||
body: error.message, | ||
}); | ||
} | ||
} | ||
); | ||
}; |
Oops, something went wrong.