diff --git a/app/services/foreman_rh_cloud/url_remediations_retriever.rb b/app/services/foreman_rh_cloud/url_remediations_retriever.rb index c8b6ae25..1da9c3b4 100644 --- a/app/services/foreman_rh_cloud/url_remediations_retriever.rb +++ b/app/services/foreman_rh_cloud/url_remediations_retriever.rb @@ -1,5 +1,5 @@ module ForemanRhCloud - class UrlRemediationsRetriever < RemediationsRetriever + class URLRemediationsRetriever < RemediationsRetriever attr_reader :url, :payload, :headers def initialize(url:, organization_id:, payload: '', headers: {}, logger: Logger.new(IO::NULL)) diff --git a/config/initializers/zeitwerk.rb b/config/initializers/zeitwerk.rb new file mode 100644 index 00000000..79849aed --- /dev/null +++ b/config/initializers/zeitwerk.rb @@ -0,0 +1,3 @@ +Rails.autoloaders.main.ignore( + ForemanRhCloud::Engine.root.join('lib/foreman_rh_cloud/version.rb') +) diff --git a/lib/foreman_rh_cloud/engine.rb b/lib/foreman_rh_cloud/engine.rb index 5c41a61c..0e682003 100644 --- a/lib/foreman_rh_cloud/engine.rb +++ b/lib/foreman_rh_cloud/engine.rb @@ -20,15 +20,6 @@ def self.register_scheduled_task(task_class, cronline) rescue ActiveRecord::TransactionIsolationError end - initializer 'foreman_rh_cloud.load_default_settings', :before => :load_config_initializers do - require_dependency File.expand_path('settings.rb', __dir__) - end - - config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"] - config.autoload_paths += Dir["#{config.root}/app/helpers/concerns"] - config.autoload_paths += Dir["#{config.root}/app/models/concerns"] - config.autoload_paths += Dir["#{config.root}/app/services"] - config.autoload_paths += Dir["#{config.root}/app/overrides"] config.autoload_paths += Dir["#{config.root}/lib"] config.eager_load_paths += Dir["#{config.root}/lib"] @@ -40,109 +31,124 @@ def self.register_scheduled_task(task_class, cronline) end end - initializer 'foreman_rh_cloud.register_plugin', :before => :finisher_hook do |_app| - Foreman::Plugin.register :foreman_rh_cloud do - requires_foreman '>= 3.7' - register_gettext - - apipie_documented_controllers ["#{ForemanRhCloud::Engine.root}/app/controllers/api/v2/**/*.rb"] - - # Add permissions - security_block :foreman_rh_cloud do - permission( - :generate_foreman_rh_cloud, - 'foreman_inventory_upload/reports': [:generate], - 'foreman_inventory_upload/tasks': [:create], - 'api/v2/rh_cloud/inventory': [:get_hosts, :remove_hosts, :sync_inventory_status, :download_file, :generate_report, :enable_cloud_connector], - 'foreman_inventory_upload/uploads': [:enable_cloud_connector], - 'foreman_inventory_upload/uploads_settings': [:set_advanced_setting], - 'foreman_inventory_upload/missing_hosts': [:remove_hosts], - 'insights_cloud/settings': [:update], - 'insights_cloud/tasks': [:create] - ) - permission( - :view_foreman_rh_cloud, - 'foreman_inventory_upload/accounts': [:index], - 'foreman_inventory_upload/reports': [:last], - 'foreman_inventory_upload/uploads': [:auto_upload, :show_auto_upload, :download_file, :last], - 'foreman_inventory_upload/tasks': [:show], - 'foreman_inventory_upload/cloud_status': [:index], - 'foreman_inventory_upload/uploads_settings': [:index], - 'foreman_inventory_upload/missing_hosts': [:index], - 'react': [:index] - ) - permission( - :view_insights_hits, - { - '/foreman_rh_cloud/insights_cloud': [:index], # for bookmarks and later for showing the page - 'insights_cloud/hits': [:index, :show, :auto_complete_search, :resolutions], - 'insights_cloud/settings': [:index, :show], - 'react': [:index], - }, - :resource_type => ::InsightsHit.name - ) - permission( - :dispatch_cloud_requests, - 'api/v2/rh_cloud/cloud_request': [:update] - ) - permission( - :control_organization_insights, - 'insights_cloud/settings': [:set_org_parameter] - ) - end + initializer 'foreman_rh_cloud.register_plugin', :before => :finisher_hook do |app| + app.reloader.to_prepare do + Foreman::Plugin.register :foreman_rh_cloud do + requires_foreman '>= 3.13' + register_gettext + + apipie_documented_controllers ["#{ForemanRhCloud::Engine.root}/app/controllers/api/v2/**/*.rb"] + + settings do + category(:rh_cloud, N_('RHCloud')) do + setting('allow_auto_inventory_upload', type: :boolean, description: N_('Enable automatic upload of your host inventory to the Red Hat cloud'), default: true, full_name: N_('Automatic inventory upload')) + setting('allow_auto_insights_sync', type: :boolean, description: N_('Enable automatic synchronization of Insights recommendations from the Red Hat cloud'), default: false, full_name: N_('Synchronize recommendations Automatically')) + setting('allow_auto_insights_mismatch_delete', type: :boolean, description: N_('Enable automatic deletion of mismatched host records from the Red Hat cloud'), default: false, full_name: N_('Automatic mismatch deletion')) + setting('obfuscate_inventory_hostnames', type: :boolean, description: N_('Obfuscate host names sent to the Red Hat cloud'), default: false, full_name: N_('Obfuscate host names')) + setting('obfuscate_inventory_ips', type: :boolean, description: N_('Obfuscate ipv4 addresses sent to the Red Hat cloud'), default: false, full_name: N_('Obfuscate host ipv4 addresses')) + setting('exclude_installed_packages', type: :boolean, description: N_('Exclude installed packages from being uploaded to the Red Hat cloud'), default: false, full_name: N_("Exclude installed Packages")) + setting('include_parameter_tags', type: :boolean, description: N_('Should import include parameter tags from Foreman?'), default: false, full_name: N_('Include parameters in insights-client reports')) + setting('rhc_instance_id', type: :string, description: N_('RHC daemon id'), default: nil, full_name: N_('ID of the RHC(Yggdrasil) daemon')) + end + end - plugin_permissions = [:view_foreman_rh_cloud, :generate_foreman_rh_cloud, :view_insights_hits, :dispatch_cloud_requests, :control_organization_insights] + # Add permissions + security_block :foreman_rh_cloud do + permission( + :generate_foreman_rh_cloud, + 'foreman_inventory_upload/reports': [:generate], + 'foreman_inventory_upload/tasks': [:create], + 'api/v2/rh_cloud/inventory': [:get_hosts, :remove_hosts, :sync_inventory_status, :download_file, :generate_report, :enable_cloud_connector], + 'foreman_inventory_upload/uploads': [:enable_cloud_connector], + 'foreman_inventory_upload/uploads_settings': [:set_advanced_setting], + 'foreman_inventory_upload/missing_hosts': [:remove_hosts], + 'insights_cloud/settings': [:update], + 'insights_cloud/tasks': [:create] + ) + permission( + :view_foreman_rh_cloud, + 'foreman_inventory_upload/accounts': [:index], + 'foreman_inventory_upload/reports': [:last], + 'foreman_inventory_upload/uploads': [:auto_upload, :show_auto_upload, :download_file, :last], + 'foreman_inventory_upload/tasks': [:show], + 'foreman_inventory_upload/cloud_status': [:index], + 'foreman_inventory_upload/uploads_settings': [:index], + 'foreman_inventory_upload/missing_hosts': [:index], + 'react': [:index] + ) + permission( + :view_insights_hits, + { + '/foreman_rh_cloud/insights_cloud': [:index], # for bookmarks and later for showing the page + 'insights_cloud/hits': [:index, :show, :auto_complete_search, :resolutions], + 'insights_cloud/settings': [:index, :show], + 'react': [:index], + }, + :resource_type => ::InsightsHit.name + ) + permission( + :dispatch_cloud_requests, + 'api/v2/rh_cloud/cloud_request': [:update] + ) + permission( + :control_organization_insights, + 'insights_cloud/settings': [:set_org_parameter] + ) + end + + plugin_permissions = [:view_foreman_rh_cloud, :generate_foreman_rh_cloud, :view_insights_hits, :dispatch_cloud_requests, :control_organization_insights] - role 'ForemanRhCloud', plugin_permissions, 'Role granting permissions to view the hosts inventory, - generate a report, upload it to the cloud and download it locally' + role 'ForemanRhCloud', plugin_permissions, 'Role granting permissions to view the hosts inventory, + generate a report, upload it to the cloud and download it locally' - add_permissions_to_default_roles Role::ORG_ADMIN => plugin_permissions, - Role::MANAGER => plugin_permissions, - Role::SYSTEM_ADMIN => plugin_permissions + add_permissions_to_default_roles Role::ORG_ADMIN => plugin_permissions, + Role::MANAGER => plugin_permissions, + Role::SYSTEM_ADMIN => plugin_permissions - # Adding a sub menu after hosts menu - divider :top_menu, caption: N_('RH Cloud'), 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, :insights_hits, caption: N_('Insights'), url: '/foreman_rh_cloud/insights_cloud', url_hash: { controller: :react, action: :index }, parent: :configure_menu + # Adding a sub menu after hosts menu + divider :top_menu, caption: N_('RH Cloud'), 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, :insights_hits, caption: N_('Insights'), url: '/foreman_rh_cloud/insights_cloud', url_hash: { controller: :react, action: :index }, parent: :configure_menu - register_facet InsightsFacet, :insights do - configure_host do - set_dependent_action :destroy + register_facet InsightsFacet, :insights do + configure_host do + set_dependent_action :destroy + end end - end - register_global_js_file 'global' + register_global_js_file 'global' - register_custom_status InventorySync::InventoryStatus - register_custom_status InsightsClientReportStatus + register_custom_status InventorySync::InventoryStatus + register_custom_status InsightsClientReportStatus - describe_host do - overview_buttons_provider :insights_host_overview_buttons - end + describe_host do + overview_buttons_provider :insights_host_overview_buttons + end - extend_page 'hosts/show' do |context| - context.add_pagelet :main_tabs, - partial: 'hosts/insights_tab', - name: _('Insights'), - id: 'insights', - onlyif: proc { |host| host.insights } - end + extend_page 'hosts/show' do |context| + context.add_pagelet :main_tabs, + partial: 'hosts/insights_tab', + name: _('Insights'), + id: 'insights', + onlyif: proc { |host| host.insights } + end - extend_page 'hosts/_list' do |context| - context.with_profile :cloud, _('RH Cloud'), default: true do - add_pagelet :hosts_table_column_header, key: :insights_recommendations_count, label: _('Recommendations'), sortable: true, width: '12%', class: 'hidden-xs ellipsis', priority: 100, - export_data: CsvExporter::ExportDefinition.new(:insights_recommendations_count, callback: ->(host) { host&.insights_hits&.count }) - add_pagelet :hosts_table_column_content, key: :insights_recommendations_count, callback: ->(host) { hits_counts_cell(host) }, class: 'hidden-xs ellipsis text-center', priority: 100 + extend_page 'hosts/_list' do |context| + context.with_profile :cloud, _('RH Cloud'), default: true do + add_pagelet :hosts_table_column_header, key: :insights_recommendations_count, label: _('Recommendations'), sortable: true, width: '12%', class: 'hidden-xs ellipsis', priority: 100, + export_data: CsvExporter::ExportDefinition.new(:insights_recommendations_count, callback: ->(host) { host&.insights_hits&.count }) + add_pagelet :hosts_table_column_content, key: :insights_recommendations_count, callback: ->(host) { hits_counts_cell(host) }, class: 'hidden-xs ellipsis text-center', priority: 100 + end end - end - extend_template_helpers ForemanRhCloud::TemplateRendererHelper - allowed_template_helpers :remediations_playbook, :download_rh_playbook - end + extend_template_helpers ForemanRhCloud::TemplateRendererHelper + allowed_template_helpers :remediations_playbook, :download_rh_playbook + end - ::Katello::UINotifications::Subscriptions::ManifestImportSuccess.include ForemanInventoryUpload::Notifications::ManifestImportSuccessNotificationOverride if defined?(Katello) + ::Katello::UINotifications::Subscriptions::ManifestImportSuccess.include ForemanInventoryUpload::Notifications::ManifestImportSuccessNotificationOverride if defined?(Katello) - ::Host::Managed.include RhCloudHost + ::Host::Managed.include RhCloudHost + end end rake_tasks do diff --git a/lib/foreman_rh_cloud/settings.rb b/lib/foreman_rh_cloud/settings.rb deleted file mode 100644 index 04bd6413..00000000 --- a/lib/foreman_rh_cloud/settings.rb +++ /dev/null @@ -1,12 +0,0 @@ -Foreman::SettingManager.define(:foreman) do - category(:rh_cloud, N_('RHCloud')) do - setting('allow_auto_inventory_upload', type: :boolean, description: N_('Enable automatic upload of your host inventory to the Red Hat cloud'), default: true, full_name: N_('Automatic inventory upload')) - setting('allow_auto_insights_sync', type: :boolean, description: N_('Enable automatic synchronization of Insights recommendations from the Red Hat cloud'), default: false, full_name: N_('Synchronize recommendations Automatically')) - setting('allow_auto_insights_mismatch_delete', type: :boolean, description: N_('Enable automatic deletion of mismatched host records from the Red Hat cloud'), default: false, full_name: N_('Automatic mismatch deletion')) - setting('obfuscate_inventory_hostnames', type: :boolean, description: N_('Obfuscate host names sent to the Red Hat cloud'), default: false, full_name: N_('Obfuscate host names')) - setting('obfuscate_inventory_ips', type: :boolean, description: N_('Obfuscate ipv4 addresses sent to the Red Hat cloud'), default: false, full_name: N_('Obfuscate host ipv4 addresses')) - setting('exclude_installed_packages', type: :boolean, description: N_('Exclude installed packages from being uploaded to the Red Hat cloud'), default: false, full_name: N_("Exclude installed Packages")) - setting('include_parameter_tags', type: :boolean, description: N_('Should import include parameter tags from Foreman?'), default: false, full_name: N_('Include parameters in insights-client reports')) - setting('rhc_instance_id', type: :string, description: N_('RHC daemon id'), default: nil, full_name: N_('ID of the RHC(Yggdrasil) daemon')) - end -end diff --git a/test/unit/services/foreman_rh_cloud/url_remediations_retriever_test.rb b/test/unit/services/foreman_rh_cloud/url_remediations_retriever_test.rb index 8d194314..e1ed40e8 100644 --- a/test/unit/services/foreman_rh_cloud/url_remediations_retriever_test.rb +++ b/test/unit/services/foreman_rh_cloud/url_remediations_retriever_test.rb @@ -1,8 +1,8 @@ require 'test_plugin_helper' -class UrlRemediationsRetrieverTest < ActiveSupport::TestCase +class URLRemediationsRetrieverTest < ActiveSupport::TestCase test 'Calls the given url' do - retreiver = ForemanRhCloud::UrlRemediationsRetriever.new( + retreiver = ForemanRhCloud::URLRemediationsRetriever.new( organization_id: FactoryBot.create(:organization).id, url: 'http://test.example.com', payload: 'TEST_PAYLOAD',