Skip to content

Commit

Permalink
useAPI, not context, to get config
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Jan 10, 2025
1 parent fc55460 commit a3d0455
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -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
6 changes: 0 additions & 6 deletions app/controllers/insights_cloud/settings_controller.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<div className="insights_settings">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
INSIGHTS_SET_SELECTED_IDS,
INSIGHTS_SET_SELECT_ALL_ALERT,
INSIGHTS_SET_SELECT_ALL,
ADVISOR_ENGINE_CONFIG_KEY,

Check failure on line 11 in webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableActions.js

View workflow job for this annotation

GitHub Actions / test_js

'ADVISOR_ENGINE_CONFIG_KEY' is defined but never used
ADVISOR_ENGINE_CONFIG_PATH,

Check failure on line 12 in webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableActions.js

View workflow job for this annotation

GitHub Actions / test_js

'ADVISOR_ENGINE_CONFIG_PATH' is defined but never used
} from './InsightsTableConstants';
import {
getServerQueryForHostname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<DropdownItem key="recommendation-url">
<a href={recommendationUrl} target="_blank" rel="noopener noreferrer">
Expand Down Expand Up @@ -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/';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const modifySelectedRows = (
hits,
selectedIds,
showSelectAllAlert,
hideHost
hideHost,
isLocalAdvisorEngine
) => {
if (hits.length === 0) return [];

Expand Down Expand Up @@ -34,6 +35,7 @@ export const modifySelectedRows = (
selected: selectedIds[id] || (disableCheckbox && showSelectAllAlert),
recommendationUrl: results_url,
accessRHUrl: solution_url,
isLocalAdvisorEngine,
};
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 9 in webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js

View workflow job for this annotation

GitHub Actions / test_js

Replace `·ADVISOR_ENGINE_CONFIG_KEY,·INSIGHTS_HITS_API_KEY·` with `⏎··ADVISOR_ENGINE_CONFIG_KEY,⏎··INSIGHTS_HITS_API_KEY,⏎`

Check failure on line 9 in webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js

View workflow job for this annotation

GitHub Actions / test_js

'ADVISOR_ENGINE_CONFIG_KEY' is defined but never used
import { selectInsightsCloudSync } from '../../../ForemanRhCloudSelectors';

export const selectQuery = state => selectRouterLocation(state).query;
Expand Down
8 changes: 4 additions & 4 deletions webpack/InsightsCloudSync/Components/ToolbarDropdown.js
Original file line number Diff line number Diff line change
@@ -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 = [
<DropdownItem
key="recommendation-manual-sync"
Expand All @@ -30,7 +30,7 @@ const ToolbarDropdown = ({ onRecommendationSync }) => {
</a>
</DropdownItem>,
];
const dropdownItems = isLocalInsightsAdvisor
const dropdownItems = isLocalAdvisorEngine
? baseItems
: baseItems.concat(cloudItems);
return (
Expand Down
31 changes: 12 additions & 19 deletions webpack/InsightsCloudSync/InsightsCloudSync.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = (
<>
Expand All @@ -32,22 +29,18 @@ const InsightsCloudSync = ({ syncInsights, query, fetchInsights }) => {

return (
<div className="rh-cloud-insights">
<InsightsConfigContext.Provider
value={{ isLocalInsightsAdvisor, setIsLocalInsightsAdvisor }}
<InsightsSettings />
<PageLayout
searchable
searchProps={INSIGHTS_SEARCH_PROPS}
onSearch={nextQuery => fetchInsights({ query: nextQuery, page: 1 })}
header={INSIGHTS_SYNC_PAGE_TITLE}
toolbarButtons={toolbarButtons}
searchQuery={query}
beforeToolbarComponent={<InsightsHeader />}
>
<InsightsSettings />
<PageLayout
searchable
searchProps={INSIGHTS_SEARCH_PROPS}
onSearch={nextQuery => fetchInsights({ query: nextQuery, page: 1 })}
header={INSIGHTS_SYNC_PAGE_TITLE}
toolbarButtons={toolbarButtons}
searchQuery={query}
beforeToolbarComponent={<InsightsHeader />}
>
<InsightsTable />
</PageLayout>
</InsightsConfigContext.Provider>
<InsightsTable />
</PageLayout>
</div>
);
};
Expand Down
8 changes: 4 additions & 4 deletions webpack/InsightsHostDetailsTab/NewHostDetailsTab.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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]);

Expand All @@ -43,7 +43,7 @@ const NewHostDetailsTab = ({ hostName, router }) => {
</DropdownItem>,
];

if (hits.length && !isLocalInsightsAdvisor) {
if (hits.length && !isLocalAdvisorEngine) {
const { host_uuid: uuid } = hits[0];
dropdownItems.push(
<DropdownItem key="insights-advisor-link" ouiaId="insights-advisor-link">
Expand Down
19 changes: 19 additions & 0 deletions webpack/common/Hooks/ConfigHooks.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit a3d0455

Please sign in to comment.