Skip to content

Commit

Permalink
Hide UI elements when using local foreman-advisor-engine
Browse files Browse the repository at this point in the history
  Hide the sync recommendations toggle
  Remove InsightsHeader
  • Loading branch information
jeremylenz committed Jan 15, 2025
1 parent dc80828 commit 1ffb31d
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 112 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
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

namespace 'advisor_engine' do
Expand Down
13 changes: 12 additions & 1 deletion lib/foreman_rh_cloud/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def self.register_scheduled_task(task_class, cronline)
'foreman_inventory_upload/cloud_status': [:index],
'foreman_inventory_upload/uploads_settings': [:index],
'foreman_inventory_upload/missing_hosts': [:index],
'api/v2/rh_cloud/advisor_engine_config': [:show],
'react': [:index]
)
permission(
Expand Down Expand Up @@ -107,7 +108,13 @@ def self.register_scheduled_task(task_class, cronline)

# Adding a sub menu after hosts menu
divider :top_menu, caption: N_('Insights'), parent: :configure_menu
menu :top_menu, :inventory_upload, caption: N_('Inventory Upload'), url: '/foreman_rh_cloud/inventory_upload', url_hash: { controller: :react, action: :index }, parent: :configure_menu
menu :top_menu,
:inventory_upload,
caption: N_('Inventory Upload'),
url: '/foreman_rh_cloud/inventory_upload',
url_hash: { controller: :react, action: :index },
parent: :configure_menu,
if: -> { !ForemanRhCloud.with_local_advisor_engine? }
menu :top_menu, :insights_hits, caption: N_('Recommendations'), url: '/foreman_rh_cloud/insights_cloud', url_hash: { controller: :react, action: :index }, parent: :configure_menu

register_facet InsightsFacet, :insights do
Expand Down Expand Up @@ -200,4 +207,8 @@ def self.register_scheduled_task(task_class, cronline)
end
end
end

def self.with_local_advisor_engine?
SETTINGS.dig(:foreman_rh_cloud, :use_local_advisor_engine) || false
end
end
3 changes: 3 additions & 0 deletions test/controllers/insights_sync/settings_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

class SettingsControllerTest < ActionController::TestCase
tests InsightsCloud::SettingsController
def setup
ForemanRhCloud.stubs(:with_local_advisor_engine?).returns(false)
end

test 'should return allow_auto_insights_sync setting' do
Setting[:allow_auto_insights_sync] = false
Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions webpack/InsightsCloudSync/Components/InsightsHeader/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import React, { Component } 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 './insightsSettings.scss';
import { useAdvisorEngineConfig } from '../../../common/Hooks/ConfigHooks';

class InsightsSettings extends Component {
componentDidMount() {
const { getInsightsSyncSettings } = this.props;
const InsightsSettings = ({
insightsSyncEnabled,
getInsightsSyncSettings,
setInsightsSyncEnabled,
}) => {
const isLocalAdvisorEngine = useAdvisorEngineConfig();
useEffect(() => {
getInsightsSyncSettings();
}
}, [getInsightsSyncSettings]);

render() {
const { insightsSyncEnabled, setInsightsSyncEnabled } = this.props;
return (
<div className="insights_settings">
<SwitcherPF4
id="insights_sync_switcher"
label={__('Sync automatically')}
tooltip={__(
'Enable automatic synchronization of Insights recommendations from the Red Hat cloud'
)}
isChecked={insightsSyncEnabled}
onChange={() => setInsightsSyncEnabled(!insightsSyncEnabled)}
/>
</div>
);
}
}
if (isLocalAdvisorEngine) return null;

return (
<div className="insights_settings">
<SwitcherPF4
id="insights_sync_switcher"
label={__('Sync automatically')}
tooltip={__(
'Enable automatic synchronization of Insights recommendations from the Red Hat cloud'
)}
isChecked={insightsSyncEnabled}
onChange={() => setInsightsSyncEnabled(!insightsSyncEnabled)}
/>
</div>
);
};

InsightsSettings.propTypes = {
insightsSyncEnabled: PropTypes.bool.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const getInsightsSyncSettings = () => async dispatch => {
},
},
});
} catch ({ message }) {
} catch (err) {
const { message } = err;
dispatch(
addToast({
sticky: true,
Expand Down

This file was deleted.

This file was deleted.

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 @@ -31,10 +31,11 @@ export const hasPlaybookFormatter = ({ title: hasPlaybook }) => ({
});

export const actionsFormatter = (props, { rowData = {} }) => {
const { recommendationUrl, accessRHUrl } = rowData;
const { recommendationUrl, accessRHUrl, isLocalAdvisorEngine } = rowData;
const dropdownItems = [];

recommendationUrl &&
!isLocalAdvisorEngine &&
dropdownItems.push(
<DropdownItem key="recommendation-url">
<a href={recommendationUrl} target="_blank" rel="noopener noreferrer">
Expand Down Expand Up @@ -121,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
5 changes: 5 additions & 0 deletions webpack/InsightsCloudSync/Components/ToolbarDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ 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 { useAdvisorEngineConfig } from '../../common/Hooks/ConfigHooks';

const ToolbarDropdown = ({ onRecommendationSync }) => {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const isLocalAdvisorEngine = useAdvisorEngineConfig();
if (isLocalAdvisorEngine) {
return null;
}
const dropdownItems = [
<DropdownItem
key="recommendation-manual-sync"
Expand Down

This file was deleted.

This file was deleted.

3 changes: 1 addition & 2 deletions webpack/InsightsCloudSync/InsightsCloudSync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import PageLayout from 'foremanReact/routes/common/PageLayout/PageLayout';
import InsightsHeader from './Components/InsightsHeader';
import InsightsTable from './Components/InsightsTable';
import RemediationModal from './Components/RemediationModal';
import {
Expand Down Expand Up @@ -37,7 +36,7 @@ const InsightsCloudSync = ({ syncInsights, query, fetchInsights }) => {
header={INSIGHTS_SYNC_PAGE_TITLE}
toolbarButtons={toolbarButtons}
searchQuery={query}
beforeToolbarComponent={<InsightsHeader />}
beforeToolbarComponent={null}
>
<InsightsTable />
</PageLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`InsightsCloudSync render 1`] = `
>
<Connect(InsightsSettings) />
<PageLayout
beforeToolbarComponent={<InsightsHeader />}
beforeToolbarComponent={null}
header="Red Hat Insights"
onSearch={[Function]}
searchProps={
Expand Down
4 changes: 3 additions & 1 deletion webpack/InsightsHostDetailsTab/NewHostDetailsTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import {
selectHits,
} from '../InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors';
import { redHatAdvisorSystems } from '../InsightsCloudSync/InsightsCloudSyncHelpers';
import { useAdvisorEngineConfig } from '../common/Hooks/ConfigHooks';

const NewHostDetailsTab = ({ hostName, router }) => {
const dispatch = useDispatch();
const query = useSelector(selectSearch);
const hits = useSelector(selectHits);
const isLocalAdvisorEngine = useAdvisorEngineConfig();

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

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

if (hits.length) {
if (hits.length && !isLocalAdvisorEngine) {
const { host_uuid: uuid } = hits[0];
dropdownItems.push(
<DropdownItem key="insights-advisor-link" ouiaId="insights-advisor-link">
Expand Down
3 changes: 3 additions & 0 deletions webpack/__mocks__/foremanReact/common/hooks/API/APIHooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const useAPI = jest.fn(() => ({
response: {},
}));
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 1ffb31d

Please sign in to comment.