From d610f0b95a26c84f64c527e7ff87583451df4e36 Mon Sep 17 00:00:00 2001 From: Anna Vitova Date: Mon, 21 Aug 2023 15:00:56 +0200 Subject: [PATCH] feat(HMS-1245): UI AWS permission check --- src/API/index.js | 11 +++++- src/API/queryKeys.js | 1 + src/Components/SourcesSelect/index.js | 57 ++++++++++++++++++++------- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/API/index.js b/src/API/index.js index f8f018d8..62fec197 100644 --- a/src/API/index.js +++ b/src/API/index.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import { AZURE_PROVIDER } from '../constants'; +import { AWS_PROVIDER, AZURE_PROVIDER } from '../constants'; import { imageBuilderURL, provisioningUrl } from './helpers'; const typesUrlForProvider = (provider, region) => { @@ -72,3 +72,12 @@ export const fetchLaunchTemplates = async (sourceID, region) => { } = await axios.get(provisioningUrl(`sources/${sourceID}/launch_templates?region=${region}`)); return data; }; + +export const checkPermissions = async (provider, sourceID, region) => { + switch (provider) { + case AWS_PROVIDER: { + const { data } = await axios.get(provisioningUrl(`sources/${sourceID}/validate_permissions?region=${region}`)); + return data; + } + } +}; diff --git a/src/API/queryKeys.js b/src/API/queryKeys.js index 10267d52..1db666e1 100644 --- a/src/API/queryKeys.js +++ b/src/API/queryKeys.js @@ -4,3 +4,4 @@ export const PUBKEYS_QUERY_KEY = 'pubkeys'; export const instanceTypesQueryKeys = (region) => ['instanceTypes', region]; export const IMAGE_REGIONS_KEY = 'image_region'; export const TEMPLATES_KEY = 'templates'; +export const PERMISSION_CHECK_KEY = 'permissions'; diff --git a/src/Components/SourcesSelect/index.js b/src/Components/SourcesSelect/index.js index d769f61e..e20cb126 100644 --- a/src/Components/SourcesSelect/index.js +++ b/src/Components/SourcesSelect/index.js @@ -1,13 +1,15 @@ import PropTypes from 'prop-types'; import React from 'react'; import { Alert, Select, SelectOption, Spinner } from '@patternfly/react-core'; +import { useQuery } from 'react-query'; import { imageProps } from '../ProvisioningWizard/helpers'; import { useSourcesForImage } from '../Common/Hooks/sources'; import { useWizardContext } from '../Common/WizardContext'; +import { checkPermissions } from '../../API'; const SourcesSelect = ({ setValidation, image }) => { - const [{ chosenSource }, setWizardContext] = useWizardContext(); + const [{ chosenSource, chosenRegion }, setWizardContext] = useWizardContext(); const [isOpen, setIsOpen] = React.useState(false); const [selected, setSelected] = React.useState(null); const selectObject = (id, name) => ({ @@ -15,6 +17,14 @@ const SourcesSelect = ({ setValidation, image }) => { toString: () => name, compareTo: (other) => other.id === id, }); + const { data: missingPermissions } = useQuery( + [`permissions`, `${chosenRegion}-${chosenSource}`], + () => checkPermissions(image.provider, chosenSource, chosenRegion), + { + select: (perm) => perm.missing_entities, + enabled: !!chosenRegion && !!chosenSource, + } + ); const { isLoading, error, sources } = useSourcesForImage(image, { onSuccess: (sources) => { if (chosenSource) { @@ -65,20 +75,37 @@ const SourcesSelect = ({ setValidation, image }) => { } return ( - + <> + {(missingPermissions || []).length != 0 && ( + + <> +

+ Check if policies to your {image.provider} account are set as recommended by + + {' '} + our docs + + . Following permissions might be missing: +

+

{(missingPermissions || []).join(', ')}

+ +
+ )} + + ); };