Skip to content

Commit 45bf976

Browse files
committed
Hide UI elements when using local foreman-advisor-engine
Hide the sync recommendations toggle Remove InsightsHeader
1 parent e4db854 commit 45bf976

File tree

21 files changed

+115
-114
lines changed

21 files changed

+115
-114
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Api
2+
module V2
3+
module RhCloud
4+
class AdvisorEngineConfigController < ::Api::V2::BaseController
5+
include ::Api::Version2
6+
7+
api :GET, "/rh_cloud/advisor_engine_config", N_("Show if system is configured to use local Foreman Advisor Engine.")
8+
def show
9+
render json: {
10+
use_local_advisor_engine: ForemanRhCloud.with_local_advisor_engine?,
11+
}, status: :ok
12+
end
13+
end
14+
end
15+
end
16+
end

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262

6363
namespace 'rh_cloud' do
6464
post 'enable_connector', to: 'inventory#enable_cloud_connector'
65-
6665
post 'cloud_request', to: 'cloud_request#update'
66+
get 'advisor_engine_config', to: 'advisor_engine_config#show'
6767
end
6868
end
6969
end

lib/foreman_rh_cloud/engine.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def self.register_scheduled_task(task_class, cronline)
7474
'foreman_inventory_upload/cloud_status': [:index],
7575
'foreman_inventory_upload/uploads_settings': [:index],
7676
'foreman_inventory_upload/missing_hosts': [:index],
77+
'api/v2/rh_cloud/advisor_engine_config': [:show],
7778
'react': [:index]
7879
)
7980
permission(
@@ -106,9 +107,15 @@ def self.register_scheduled_task(task_class, cronline)
106107
Role::SYSTEM_ADMIN => plugin_permissions
107108

108109
# Adding a sub menu after hosts menu
109-
divider :top_menu, caption: N_('RH Cloud'), parent: :configure_menu
110-
menu :top_menu, :inventory_upload, caption: N_('Inventory Upload'), url: '/foreman_rh_cloud/inventory_upload', url_hash: { controller: :react, action: :index }, parent: :configure_menu
111-
menu :top_menu, :insights_hits, caption: N_('Insights'), url: '/foreman_rh_cloud/insights_cloud', url_hash: { controller: :react, action: :index }, parent: :configure_menu
110+
divider :top_menu, caption: N_('Insights'), parent: :configure_menu
111+
menu :top_menu,
112+
:inventory_upload,
113+
caption: N_('Inventory Upload'),
114+
url: '/foreman_rh_cloud/inventory_upload',
115+
url_hash: { controller: :react, action: :index },
116+
parent: :configure_menu,
117+
if: -> { !ForemanRhCloud.with_local_advisor_engine? }
118+
menu :top_menu, :insights_hits, caption: N_('Recommendations'), url: '/foreman_rh_cloud/insights_cloud', url_hash: { controller: :react, action: :index }, parent: :configure_menu
112119

113120
register_facet InsightsFacet, :insights do
114121
configure_host do
@@ -193,4 +200,8 @@ def self.register_scheduled_task(task_class, cronline)
193200
end
194201
end
195202
end
203+
204+
def self.with_local_advisor_engine?
205+
SETTINGS.dig(:foreman_rh_cloud, :use_local_advisor_engine) || false
206+
end
196207
end

test/controllers/insights_sync/settings_controller_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
class SettingsControllerTest < ActionController::TestCase
44
tests InsightsCloud::SettingsController
5+
def setup
6+
ForemanRhCloud.stubs(:with_local_advisor_engine?).returns(false)
7+
end
58

69
test 'should return allow_auto_insights_sync setting' do
710
Setting[:allow_auto_insights_sync] = false

webpack/InsightsCloudSync/Components/InsightsHeader/InsightsHeader.scss

Lines changed: 0 additions & 8 deletions
This file was deleted.

webpack/InsightsCloudSync/Components/InsightsHeader/index.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
import React, { Component } from 'react';
1+
import React, { useEffect } from 'react';
22
import PropTypes from 'prop-types';
33
import { translate as __ } from 'foremanReact/common/I18n';
44
import SwitcherPF4 from '../../../common/Switcher/SwitcherPF4';
55
import './insightsSettings.scss';
6+
import { useAdvisorEngineConfig } from '../../../common/Hooks/ConfigHooks';
67

7-
class InsightsSettings extends Component {
8-
componentDidMount() {
9-
const { getInsightsSyncSettings } = this.props;
8+
const InsightsSettings = ({
9+
insightsSyncEnabled,
10+
getInsightsSyncSettings,
11+
setInsightsSyncEnabled,
12+
}) => {
13+
const isLocalAdvisorEngine = useAdvisorEngineConfig();
14+
useEffect(() => {
1015
getInsightsSyncSettings();
11-
}
16+
}, [getInsightsSyncSettings]);
1217

13-
render() {
14-
const { insightsSyncEnabled, setInsightsSyncEnabled } = this.props;
15-
return (
16-
<div className="insights_settings">
17-
<SwitcherPF4
18-
id="insights_sync_switcher"
19-
label={__('Sync automatically')}
20-
tooltip={__(
21-
'Enable automatic synchronization of Insights recommendations from the Red Hat cloud'
22-
)}
23-
isChecked={insightsSyncEnabled}
24-
onChange={() => setInsightsSyncEnabled(!insightsSyncEnabled)}
25-
/>
26-
</div>
27-
);
28-
}
29-
}
18+
if (isLocalAdvisorEngine) return null;
19+
20+
return (
21+
<div className="insights_settings">
22+
<SwitcherPF4
23+
id="insights_sync_switcher"
24+
label={__('Sync automatically')}
25+
tooltip={__(
26+
'Enable automatic synchronization of Insights recommendations from the Red Hat cloud'
27+
)}
28+
isChecked={insightsSyncEnabled}
29+
onChange={() => setInsightsSyncEnabled(!insightsSyncEnabled)}
30+
/>
31+
</div>
32+
);
33+
};
3034

3135
InsightsSettings.propTypes = {
3236
insightsSyncEnabled: PropTypes.bool.isRequired,

webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const getInsightsSyncSettings = () => async dispatch => {
1919
},
2020
},
2121
});
22-
} catch ({ message }) {
22+
} catch (err) {
23+
const { message } = err;
2324
dispatch(
2425
addToast({
2526
sticky: true,

webpack/InsightsCloudSync/Components/InsightsSettings/__tests__/InsightsSettings.test.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

webpack/InsightsCloudSync/Components/InsightsSettings/__tests__/__snapshots__/InsightsSettings.test.js.snap

Lines changed: 0 additions & 15 deletions
This file was deleted.

webpack/InsightsCloudSync/Components/InsightsTable/InsightsTable.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import TableEmptyState from '../../../common/table/EmptyState';
1212
import { modifySelectedRows, getSortColumnIndex } from './InsightsTableHelpers';
1313
import Pagination from './Pagination';
1414
import './table.scss';
15+
import { useAdvisorEngineConfig } from '../../../common/Hooks/ConfigHooks';
1516

1617
const InsightsTable = ({
1718
page,
@@ -43,9 +44,17 @@ const InsightsTable = ({
4344
fetchInsights({ page, perPage, query, sortBy, sortOrder });
4445
}, [hostname]);
4546

47+
const isLocalAdvisorEngine = useAdvisorEngineConfig();
48+
4649
useEffect(() => {
4750
setRows(
48-
modifySelectedRows(hits, selectedIds, showSelectAllAlert, hideHost)
51+
modifySelectedRows(
52+
hits,
53+
selectedIds,
54+
showSelectAllAlert,
55+
hideHost,
56+
isLocalAdvisorEngine
57+
)
4958
);
5059

5160
if (hideHost) setColumns(getColumnsWithoutHostname());

webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableConstants.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ export const hasPlaybookFormatter = ({ title: hasPlaybook }) => ({
3131
});
3232

3333
export const actionsFormatter = (props, { rowData = {} }) => {
34-
const { recommendationUrl, accessRHUrl } = rowData;
34+
const { recommendationUrl, accessRHUrl, isLocalAdvisorEngine } = rowData;
3535
const dropdownItems = [];
3636

3737
recommendationUrl &&
38+
!isLocalAdvisorEngine &&
3839
dropdownItems.push(
3940
<DropdownItem key="recommendation-url">
4041
<a href={recommendationUrl} target="_blank" rel="noopener noreferrer">
@@ -121,4 +122,10 @@ export const INSIGHTS_SET_SELECT_ALL_ALERT = 'INSIGHTS_SET_SELECT_ALL_ALERT';
121122

122123
export const INSIGHTS_SET_SELECT_ALL = 'INSIGHTS_SET_SELECT_ALL';
123124

125+
export const ADVISOR_ENGINE_CONFIG_KEY = 'ADVISOR_ENGINE_CONFIG';
126+
127+
export const ADVISOR_ENGINE_CONFIG_PATH = foremanUrl(
128+
'/api/v2/rh_cloud/advisor_engine_config'
129+
);
130+
124131
export const NEW_HOST_PATH = '/new/hosts/';

webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableHelpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export const modifySelectedRows = (
66
hits,
77
selectedIds,
88
showSelectAllAlert,
9-
hideHost
9+
hideHost,
10+
isLocalAdvisorEngine
1011
) => {
1112
if (hits.length === 0) return [];
1213

@@ -34,6 +35,7 @@ export const modifySelectedRows = (
3435
selected: selectedIds[id] || (disableCheckbox && showSelectAllAlert),
3536
recommendationUrl: results_url,
3637
accessRHUrl: solution_url,
38+
isLocalAdvisorEngine,
3739
};
3840
}
3941
);

webpack/InsightsCloudSync/Components/ToolbarDropdown.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import { translate as __ } from 'foremanReact/common/I18n';
44
import { Dropdown, DropdownItem, KebabToggle } from '@patternfly/react-core';
55
import { ExternalLinkAltIcon } from '@patternfly/react-icons';
66
import { redHatAdvisorSystems } from '../InsightsCloudSyncHelpers';
7+
import { useAdvisorEngineConfig } from '../../common/Hooks/ConfigHooks';
78

89
const ToolbarDropdown = ({ onRecommendationSync }) => {
910
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
11+
const isLocalAdvisorEngine = useAdvisorEngineConfig();
12+
if (isLocalAdvisorEngine) {
13+
return null;
14+
}
1015
const dropdownItems = [
1116
<DropdownItem
1217
key="recommendation-manual-sync"

webpack/InsightsCloudSync/Components/__tests__/InsightsHeader.test.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

webpack/InsightsCloudSync/Components/__tests__/__snapshots__/InsightsHeader.test.js.snap

Lines changed: 0 additions & 13 deletions
This file was deleted.

webpack/InsightsCloudSync/InsightsCloudSync.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import PageLayout from 'foremanReact/routes/common/PageLayout/PageLayout';
4-
import InsightsHeader from './Components/InsightsHeader';
54
import InsightsTable from './Components/InsightsTable';
65
import RemediationModal from './Components/RemediationModal';
76
import {
@@ -37,7 +36,7 @@ const InsightsCloudSync = ({ syncInsights, query, fetchInsights }) => {
3736
header={INSIGHTS_SYNC_PAGE_TITLE}
3837
toolbarButtons={toolbarButtons}
3938
searchQuery={query}
40-
beforeToolbarComponent={<InsightsHeader />}
39+
beforeToolbarComponent={null}
4140
>
4241
<InsightsTable />
4342
</PageLayout>

webpack/InsightsCloudSync/__snapshots__/InsightsCloudSync.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[`InsightsCloudSync render 1`] = `
66
>
77
<Connect(InsightsSettings) />
88
<PageLayout
9-
beforeToolbarComponent={<InsightsHeader />}
9+
beforeToolbarComponent={null}
1010
header="Red Hat Insights"
1111
onSearch={[Function]}
1212
searchProps={

webpack/InsightsHostDetailsTab/NewHostDetailsTab.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import {
2121
selectHits,
2222
} from '../InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors';
2323
import { redHatAdvisorSystems } from '../InsightsCloudSync/InsightsCloudSyncHelpers';
24+
import { useAdvisorEngineConfig } from '../common/Hooks/ConfigHooks';
2425

2526
const NewHostDetailsTab = ({ hostName, router }) => {
2627
const dispatch = useDispatch();
2728
const query = useSelector(selectSearch);
2829
const hits = useSelector(selectHits);
30+
const isLocalAdvisorEngine = useAdvisorEngineConfig();
2931

3032
useEffect(() => () => router.replace({ search: null }), [router]);
3133

@@ -41,7 +43,7 @@ const NewHostDetailsTab = ({ hostName, router }) => {
4143
</DropdownItem>,
4244
];
4345

44-
if (hits.length) {
46+
if (hits.length && !isLocalAdvisorEngine) {
4547
const { host_uuid: uuid } = hits[0];
4648
dropdownItems.push(
4749
<DropdownItem key="insights-advisor-link" ouiaId="insights-advisor-link">
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const useAPI = jest.fn(() => ({
2+
response: {},
3+
}));

webpack/common/Hooks/ConfigHooks.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useAPI } from 'foremanReact/common/hooks/API/APIHooks';
2+
import {
3+
ADVISOR_ENGINE_CONFIG_KEY,
4+
ADVISOR_ENGINE_CONFIG_PATH,
5+
} from '../../InsightsCloudSync/Components/InsightsTable/InsightsTableConstants';
6+
7+
export const useAdvisorEngineConfig = () => {
8+
const { response: advisorEngineConfig } = useAPI(
9+
'get',
10+
ADVISOR_ENGINE_CONFIG_PATH,
11+
{
12+
key: ADVISOR_ENGINE_CONFIG_KEY,
13+
}
14+
);
15+
16+
// eslint-disable-next-line camelcase
17+
const isLocalAdvisorEngine = advisorEngineConfig?.use_local_advisor_engine;
18+
return isLocalAdvisorEngine;
19+
};

0 commit comments

Comments
 (0)