diff --git a/app/controllers/insights_cloud/settings_controller.rb b/app/controllers/insights_cloud/settings_controller.rb index 08bdebda..97942eee 100644 --- a/app/controllers/insights_cloud/settings_controller.rb +++ b/app/controllers/insights_cloud/settings_controller.rb @@ -1,6 +1,12 @@ module InsightsCloud class SettingsController < ::ApplicationController def show + if SETTINGS[:insights][:use_insights_on_premise] + 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/lib/foreman_rh_cloud/engine.rb b/lib/foreman_rh_cloud/engine.rb index 2c838d51..63baf83d 100644 --- a/lib/foreman_rh_cloud/engine.rb +++ b/lib/foreman_rh_cloud/engine.rb @@ -107,7 +107,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: lambda { !SETTINGS[:insights][:use_insights_on_premise] } 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 diff --git a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js index bd8cb733..587857e0 100644 --- a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js +++ b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettings.js @@ -1,32 +1,46 @@ -import React, { Component } from 'react'; +import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { translate as __ } from 'foremanReact/common/I18n'; import SwitcherPF4 from '../../../common/Switcher/SwitcherPF4'; import './insightsSettings.scss'; -class InsightsSettings extends Component { - componentDidMount() { - const { getInsightsSyncSettings } = this.props; - getInsightsSyncSettings(); - } +const InsightsSettings = ({ + insightsSyncEnabled, + getInsightsSyncSettings, + setInsightsSyncEnabled, +}) => { + const [isOnPrem, setIsOnPrem] = useState(false); + useEffect(() => { + async function fetchData() { + try { + await getInsightsSyncSettings(); + } catch (err) { + if (err.cause?.response?.status === 422) { + setIsOnPrem(true); + } else { + throw err; + } + } + } + fetchData(); + }, [getInsightsSyncSettings, setInsightsSyncEnabled]); - render() { - const { insightsSyncEnabled, setInsightsSyncEnabled } = this.props; - return ( -
- setInsightsSyncEnabled(!insightsSyncEnabled)} - /> -
- ); - } -} + if (isOnPrem) return null; + + return ( +
+ setInsightsSyncEnabled(!insightsSyncEnabled)} + /> +
+ ); +}; InsightsSettings.propTypes = { insightsSyncEnabled: PropTypes.bool.isRequired, diff --git a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js index 4e7f3d80..6c16c070 100644 --- a/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js +++ b/webpack/InsightsCloudSync/Components/InsightsSettings/InsightsSettingsActions.js @@ -19,7 +19,11 @@ export const getInsightsSyncSettings = () => async dispatch => { }, }, }); - } catch ({ message }) { + } catch (err) { + const { message } = err; + if (err?.response?.status === 422) { + throw new Error(message, { cause: err }); + } dispatch( addToast({ sticky: true,