From a3d04556ed05635296cc63eaddaf5fc59f930e15 Mon Sep 17 00:00:00 2001 From: Jeremy Lenz Date: Fri, 10 Jan 2025 16:33:41 -0500 Subject: [PATCH] useAPI, not context, to get config --- .../advisor_engine_config_controller.rb | 16 ++++++++++ .../insights_cloud/settings_controller.rb | 6 ---- config/routes.rb | 2 +- .../__tests__/AccountListIntegration.test.js | 4 +++ .../InsightsSettings/InsightsSettings.js | 25 ++++----------- .../InsightsSettingsActions.js | 3 -- .../Components/InsightsTable/InsightsTable.js | 11 ++++++- .../InsightsTable/InsightsTableActions.js | 2 ++ .../InsightsTable/InsightsTableConstants.js | 10 ++++-- .../InsightsTable/InsightsTableHelpers.js | 4 ++- .../InsightsTable/InsightsTableSelectors.js | 2 +- .../Components/ToolbarDropdown.js | 8 ++--- .../InsightsCloudSync/InsightsCloudSync.js | 31 +++++++------------ .../NewHostDetailsTab.js | 8 ++--- webpack/common/Hooks/ConfigHooks.js | 19 ++++++++++++ 15 files changed, 90 insertions(+), 61 deletions(-) create mode 100644 app/controllers/api/v2/rh_cloud/advisor_engine_config_controller.rb create mode 100644 webpack/common/Hooks/ConfigHooks.js diff --git a/app/controllers/api/v2/rh_cloud/advisor_engine_config_controller.rb b/app/controllers/api/v2/rh_cloud/advisor_engine_config_controller.rb new file mode 100644 index 00000000..decd1286 --- /dev/null +++ b/app/controllers/api/v2/rh_cloud/advisor_engine_config_controller.rb @@ -0,0 +1,16 @@ +module Api + module V2 + module RhCloud + class AdvisorEngineConfigController < ::Api::V2::BaseController + include ::Api::Version2 + + api :GET, "/rh_cloud/advisor_engine_config", N_("Show if system is configured to use local Foreman Advisor Engine.") + def show + render json: { + use_local_advisor_engine: ForemanRhCloud.with_local_advisor_engine?, + }, status: :ok + end + end + end + end +end diff --git a/app/controllers/insights_cloud/settings_controller.rb b/app/controllers/insights_cloud/settings_controller.rb index 202901b0..08bdebda 100644 --- a/app/controllers/insights_cloud/settings_controller.rb +++ b/app/controllers/insights_cloud/settings_controller.rb @@ -1,12 +1,6 @@ module InsightsCloud class SettingsController < ::ApplicationController def show - if ForemanRhCloud.with_local_advisor_engine? - render json: { - error: _('This Foreman is configured to use Insights on Premise. Syncing to Insights Cloud is disabled.'), - }, status: :unprocessable_entity - return - end render_setting(:insightsSyncEnabled, :allow_auto_insights_sync) end diff --git a/config/routes.rb b/config/routes.rb index 1c7aaa77..30733af6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -62,8 +62,8 @@ namespace 'rh_cloud' do post 'enable_connector', to: 'inventory#enable_cloud_connector' - post 'cloud_request', to: 'cloud_request#update' + get 'advisor_engine_config', to: 'advisor_engine_config#show' end end end diff --git a/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListIntegration.test.js b/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListIntegration.test.js index 85ecfe9e..aa0015d6 100644 --- a/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListIntegration.test.js +++ b/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListIntegration.test.js @@ -4,6 +4,10 @@ import { IntegrationTestHelper } from '@theforeman/test'; import AccountList from '../index'; import reducers from '../../../../ForemanRhCloudReducers'; +jest.mock('foremanReact/common/hooks/API/APIHooks', () => ({ + useAPI: jest.fn(), +})); + describe('AccountList integration test', () => { it('should flow', async () => { const integrationTestHelper = new IntegrationTestHelper(reducers); diff --git a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js index bf663124..eba59be8 100644 --- a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js +++ b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js @@ -1,34 +1,21 @@ -import React, { useEffect, useContext } from 'react'; +import React, { useEffect } from 'react'; import PropTypes from 'prop-types'; import { translate as __ } from 'foremanReact/common/I18n'; import SwitcherPF4 from '../../../common/Switcher/SwitcherPF4'; -import { InsightsConfigContext } from '../../InsightsCloudSync'; import './insightsSettings.scss'; +import { useAdvisorEngineConfig } from '../../../common/Hooks/ConfigHooks'; const InsightsSettings = ({ insightsSyncEnabled, getInsightsSyncSettings, setInsightsSyncEnabled, }) => { - const { isLocalInsightsAdvisor, setIsLocalInsightsAdvisor } = useContext( - InsightsConfigContext - ); + const isLocalAdvisorEngine = useAdvisorEngineConfig(); useEffect(() => { - async function fetchData() { - try { - await getInsightsSyncSettings(); - } catch (err) { - if (err.cause?.response?.status === 422) { - setIsLocalInsightsAdvisor(true); - } else { - throw err; - } - } - } - fetchData(); - }, [getInsightsSyncSettings, setIsLocalInsightsAdvisor]); + getInsightsSyncSettings(); + }, [getInsightsSyncSettings]); - if (isLocalInsightsAdvisor) return null; + if (isLocalAdvisorEngine) return null; return (
diff --git a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js index 6c16c070..83bed557 100644 --- a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js +++ b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js @@ -21,9 +21,6 @@ export const getInsightsSyncSettings = () => async dispatch => { }); } catch (err) { const { message } = err; - if (err?.response?.status === 422) { - throw new Error(message, { cause: err }); - } dispatch( addToast({ sticky: true, diff --git a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTable.js b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTable.js index ed0d83f1..625524ee 100644 --- a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTable.js +++ b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTable.js @@ -12,6 +12,7 @@ import TableEmptyState from '../../../common/table/EmptyState'; import { modifySelectedRows, getSortColumnIndex } from './InsightsTableHelpers'; import Pagination from './Pagination'; import './table.scss'; +import { useAdvisorEngineConfig } from '../../../common/Hooks/ConfigHooks'; const InsightsTable = ({ page, @@ -43,9 +44,17 @@ const InsightsTable = ({ fetchInsights({ page, perPage, query, sortBy, sortOrder }); }, [hostname]); + const isLocalAdvisorEngine = useAdvisorEngineConfig(); + useEffect(() => { setRows( - modifySelectedRows(hits, selectedIds, showSelectAllAlert, hideHost) + modifySelectedRows( + hits, + selectedIds, + showSelectAllAlert, + hideHost, + isLocalAdvisorEngine + ) ); if (hideHost) setColumns(getColumnsWithoutHostname()); diff --git a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableActions.js b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableActions.js index 079d898e..f2d49df9 100644 --- a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableActions.js +++ b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableActions.js @@ -8,6 +8,8 @@ import { INSIGHTS_SET_SELECTED_IDS, INSIGHTS_SET_SELECT_ALL_ALERT, INSIGHTS_SET_SELECT_ALL, + ADVISOR_ENGINE_CONFIG_KEY, + ADVISOR_ENGINE_CONFIG_PATH, } from './InsightsTableConstants'; import { getServerQueryForHostname, diff --git a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableConstants.js b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableConstants.js index c782566e..001bbe17 100644 --- a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableConstants.js +++ b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableConstants.js @@ -31,11 +31,11 @@ export const hasPlaybookFormatter = ({ title: hasPlaybook }) => ({ }); export const actionsFormatter = (props, { rowData = {} }) => { - const { recommendationUrl, accessRHUrl, isLocalInsightsAdvisor } = rowData; + const { recommendationUrl, accessRHUrl, isLocalAdvisorEngine } = rowData; const dropdownItems = []; recommendationUrl && - !isLocalInsightsAdvisor && + !isLocalAdvisorEngine && dropdownItems.push( @@ -122,4 +122,10 @@ export const INSIGHTS_SET_SELECT_ALL_ALERT = 'INSIGHTS_SET_SELECT_ALL_ALERT'; export const INSIGHTS_SET_SELECT_ALL = 'INSIGHTS_SET_SELECT_ALL'; +export const ADVISOR_ENGINE_CONFIG_KEY = 'ADVISOR_ENGINE_CONFIG'; + +export const ADVISOR_ENGINE_CONFIG_PATH = foremanUrl( + '/api/v2/rh_cloud/advisor_engine_config' +); + export const NEW_HOST_PATH = '/new/hosts/'; diff --git a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableHelpers.js b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableHelpers.js index 14e4dc83..624e71a7 100644 --- a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableHelpers.js +++ b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableHelpers.js @@ -6,7 +6,8 @@ export const modifySelectedRows = ( hits, selectedIds, showSelectAllAlert, - hideHost + hideHost, + isLocalAdvisorEngine ) => { if (hits.length === 0) return []; @@ -34,6 +35,7 @@ export const modifySelectedRows = ( selected: selectedIds[id] || (disableCheckbox && showSelectAllAlert), recommendationUrl: results_url, accessRHUrl: solution_url, + isLocalAdvisorEngine, }; } ); diff --git a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js index f874c348..6c2c5559 100644 --- a/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js +++ b/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js @@ -6,7 +6,7 @@ import { selectAPIStatus, selectAPIErrorMessage, } from 'foremanReact/redux/API/APISelectors'; -import { INSIGHTS_HITS_API_KEY } from './InsightsTableConstants'; +import { ADVISOR_ENGINE_CONFIG_KEY, INSIGHTS_HITS_API_KEY } from './InsightsTableConstants'; import { selectInsightsCloudSync } from '../../../ForemanRhCloudSelectors'; export const selectQuery = state => selectRouterLocation(state).query; diff --git a/webpack/InsightsCloudSync/Components/ToolbarDropdown.js b/webpack/InsightsCloudSync/Components/ToolbarDropdown.js index 8d78d4cf..21a62a4e 100644 --- a/webpack/InsightsCloudSync/Components/ToolbarDropdown.js +++ b/webpack/InsightsCloudSync/Components/ToolbarDropdown.js @@ -1,14 +1,14 @@ -import React, { useState, useContext } from 'react'; +import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { translate as __ } from 'foremanReact/common/I18n'; import { Dropdown, DropdownItem, KebabToggle } from '@patternfly/react-core'; import { ExternalLinkAltIcon } from '@patternfly/react-icons'; import { redHatAdvisorSystems } from '../InsightsCloudSyncHelpers'; -import { InsightsConfigContext } from '../InsightsCloudSync'; +import { useAdvisorEngineConfig } from '../../common/Hooks/ConfigHooks'; const ToolbarDropdown = ({ onRecommendationSync }) => { const [isDropdownOpen, setIsDropdownOpen] = useState(false); - const { isLocalInsightsAdvisor } = useContext(InsightsConfigContext); + const isLocalAdvisorEngine = useAdvisorEngineConfig(); const baseItems = [ { , ]; - const dropdownItems = isLocalInsightsAdvisor + const dropdownItems = isLocalAdvisorEngine ? baseItems : baseItems.concat(cloudItems); return ( diff --git a/webpack/InsightsCloudSync/InsightsCloudSync.js b/webpack/InsightsCloudSync/InsightsCloudSync.js index 259ace55..e031d128 100644 --- a/webpack/InsightsCloudSync/InsightsCloudSync.js +++ b/webpack/InsightsCloudSync/InsightsCloudSync.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React from 'react'; import PropTypes from 'prop-types'; import PageLayout from 'foremanReact/routes/common/PageLayout/PageLayout'; import InsightsHeader from './Components/InsightsHeader'; @@ -13,10 +13,7 @@ import Pagination from './Components/InsightsTable/Pagination'; import ToolbarDropdown from './Components/ToolbarDropdown'; import InsightsSettings from './Components/InsightsSettings'; -export const InsightsConfigContext = React.createContext(); - const InsightsCloudSync = ({ syncInsights, query, fetchInsights }) => { - const [isLocalInsightsAdvisor, setIsLocalInsightsAdvisor] = useState(false); const onRecommendationSync = () => syncInsights(fetchInsights, query); const toolbarButtons = ( <> @@ -32,22 +29,18 @@ const InsightsCloudSync = ({ syncInsights, query, fetchInsights }) => { return (
- + fetchInsights({ query: nextQuery, page: 1 })} + header={INSIGHTS_SYNC_PAGE_TITLE} + toolbarButtons={toolbarButtons} + searchQuery={query} + beforeToolbarComponent={} > - - fetchInsights({ query: nextQuery, page: 1 })} - header={INSIGHTS_SYNC_PAGE_TITLE} - toolbarButtons={toolbarButtons} - searchQuery={query} - beforeToolbarComponent={} - > - - - + +
); }; diff --git a/webpack/InsightsHostDetailsTab/NewHostDetailsTab.js b/webpack/InsightsHostDetailsTab/NewHostDetailsTab.js index d3c88cca..68921471 100644 --- a/webpack/InsightsHostDetailsTab/NewHostDetailsTab.js +++ b/webpack/InsightsHostDetailsTab/NewHostDetailsTab.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState, useContext } from 'react'; +import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import SearchBar from 'foremanReact/components/SearchBar'; @@ -21,13 +21,13 @@ import { selectHits, } from '../InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors'; import { redHatAdvisorSystems } from '../InsightsCloudSync/InsightsCloudSyncHelpers'; -import { InsightsConfigContext } from '../InsightsCloudSync/InsightsCloudSync'; +import { useAdvisorEngineConfig } from '../common/Hooks/ConfigHooks'; const NewHostDetailsTab = ({ hostName, router }) => { const dispatch = useDispatch(); const query = useSelector(selectSearch); const hits = useSelector(selectHits); - const { isLocalInsightsAdvisor } = useContext(InsightsConfigContext); + const isLocalAdvisorEngine = useAdvisorEngineConfig(); useEffect(() => () => router.replace({ search: null }), [router]); @@ -43,7 +43,7 @@ const NewHostDetailsTab = ({ hostName, router }) => { , ]; - if (hits.length && !isLocalInsightsAdvisor) { + if (hits.length && !isLocalAdvisorEngine) { const { host_uuid: uuid } = hits[0]; dropdownItems.push( diff --git a/webpack/common/Hooks/ConfigHooks.js b/webpack/common/Hooks/ConfigHooks.js new file mode 100644 index 00000000..bc32360d --- /dev/null +++ b/webpack/common/Hooks/ConfigHooks.js @@ -0,0 +1,19 @@ +import { useAPI } from 'foremanReact/common/hooks/API/APIHooks'; +import { + ADVISOR_ENGINE_CONFIG_KEY, + ADVISOR_ENGINE_CONFIG_PATH, +} from '../../InsightsCloudSync/Components/InsightsTable/InsightsTableConstants'; + +export const useAdvisorEngineConfig = () => { + const { response: advisorEngineConfig } = useAPI( + 'get', + ADVISOR_ENGINE_CONFIG_PATH, + { + key: ADVISOR_ENGINE_CONFIG_KEY, + } + ); + + // eslint-disable-next-line camelcase + const isLocalAdvisorEngine = advisorEngineConfig?.use_local_advisor_engine; + return isLocalAdvisorEngine; +};