From 2fe2a6b01e12749677c6862da310843b031d52ac Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 1 Nov 2023 05:27:17 -0400 Subject: [PATCH] Add ability to generate report without uploading (#850) --- app/controllers/api/v2/rh_cloud/inventory_controller.rb | 4 +++- app/controllers/concerns/inventory_upload/report_actions.rb | 4 ++-- .../foreman_inventory_upload/reports_controller.rb | 3 ++- .../async/generate_all_reports_job.rb | 4 ++-- lib/foreman_inventory_upload/async/generate_report_job.rb | 5 +++-- lib/foreman_inventory_upload/async/queue_for_upload_job.rb | 4 ++-- lib/tasks/rh_cloud_inventory.rake | 3 ++- .../inventory_upload/api/inventory_controller_test.rb | 5 +++-- 8 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/controllers/api/v2/rh_cloud/inventory_controller.rb b/app/controllers/api/v2/rh_cloud/inventory_controller.rb index 31563c9b..9df6c731 100644 --- a/app/controllers/api/v2/rh_cloud/inventory_controller.rb +++ b/app/controllers/api/v2/rh_cloud/inventory_controller.rb @@ -18,10 +18,12 @@ def download_file api :POST, "/organizations/:organization_id/rh_cloud/report", N_("Start report generation") param :organization_id, Integer, required: true, desc: N_("Set the current organization context for the request") + param :disconnected, :bool, required: false, desc: N_('Generate the report, but do not upload') def generate_report organization_id = params[:organization_id] + disconnected = params[:disconnected] - start_report_generation(organization_id) + start_report_generation(organization_id, disconnected) render json: { action_status: 'success', diff --git a/app/controllers/concerns/inventory_upload/report_actions.rb b/app/controllers/concerns/inventory_upload/report_actions.rb index e40dd367..c8bb8e0a 100644 --- a/app/controllers/concerns/inventory_upload/report_actions.rb +++ b/app/controllers/concerns/inventory_upload/report_actions.rb @@ -10,8 +10,8 @@ def initialize(**params) end end - def start_report_generation(organization_id) - ForemanTasks.async_task(ForemanInventoryUpload::Async::GenerateReportJob, ForemanInventoryUpload.generated_reports_folder, organization_id) + def start_report_generation(organization_id, disconnected) + ForemanTasks.async_task(ForemanInventoryUpload::Async::GenerateReportJob, ForemanInventoryUpload.generated_reports_folder, organization_id, disconnected) end def report_file(organization_id) diff --git a/app/controllers/foreman_inventory_upload/reports_controller.rb b/app/controllers/foreman_inventory_upload/reports_controller.rb index 20bc72e6..37d7e35e 100644 --- a/app/controllers/foreman_inventory_upload/reports_controller.rb +++ b/app/controllers/foreman_inventory_upload/reports_controller.rb @@ -21,8 +21,9 @@ def last def generate organization_id = params[:organization_id] + disconnected = params[:disconnected] - start_report_generation(organization_id) + start_report_generation(organization_id, disconnected) render json: { action_status: 'success', diff --git a/lib/foreman_inventory_upload/async/generate_all_reports_job.rb b/lib/foreman_inventory_upload/async/generate_all_reports_job.rb index e443ffff..61d202a6 100644 --- a/lib/foreman_inventory_upload/async/generate_all_reports_job.rb +++ b/lib/foreman_inventory_upload/async/generate_all_reports_job.rb @@ -32,8 +32,8 @@ def rescue_strategy_for_self Dynflow::Action::Rescue::Fail end - def plan_generate_report(folder, organization) - plan_action(ForemanInventoryUpload::Async::GenerateReportJob, folder, organization.id) + def plan_generate_report(folder, organization, disconnected) + plan_action(ForemanInventoryUpload::Async::GenerateReportJob, folder, organization.id, disconnected) end def logger diff --git a/lib/foreman_inventory_upload/async/generate_report_job.rb b/lib/foreman_inventory_upload/async/generate_report_job.rb index b0bef3fc..89dd0e32 100644 --- a/lib/foreman_inventory_upload/async/generate_report_job.rb +++ b/lib/foreman_inventory_upload/async/generate_report_job.rb @@ -5,7 +5,7 @@ def self.output_label(label) "report_for_#{label}" end - def plan(base_folder, organization_id) + def plan(base_folder, organization_id, disconnected) sequence do super( GenerateReportJob.output_label(organization_id), @@ -17,7 +17,8 @@ def plan(base_folder, organization_id) QueueForUploadJob, base_folder, ForemanInventoryUpload.facts_archive_name(organization_id), - organization_id + organization_id, + disconnected ) end end diff --git a/lib/foreman_inventory_upload/async/queue_for_upload_job.rb b/lib/foreman_inventory_upload/async/queue_for_upload_job.rb index 2bbbf63d..96e9d5be 100644 --- a/lib/foreman_inventory_upload/async/queue_for_upload_job.rb +++ b/lib/foreman_inventory_upload/async/queue_for_upload_job.rb @@ -1,9 +1,9 @@ module ForemanInventoryUpload module Async class QueueForUploadJob < ::Actions::EntryAction - def plan(base_folder, report_file, organization_id) + def plan(base_folder, report_file, organization_id, disconnected) enqueue_task = plan_self(base_folder: base_folder, report_file: report_file) - plan_upload_report(enqueue_task.output[:enqueued_file_name], organization_id) + plan_upload_report(enqueue_task.output[:enqueued_file_name], organization_id) unless disconnected end def run diff --git a/lib/tasks/rh_cloud_inventory.rake b/lib/tasks/rh_cloud_inventory.rake index d7e151e2..bad00621 100644 --- a/lib/tasks/rh_cloud_inventory.rake +++ b/lib/tasks/rh_cloud_inventory.rake @@ -47,7 +47,8 @@ namespace :rh_cloud_inventory do base_folder = ENV['target'] || ForemanInventoryUpload.generated_reports_folder organization_id = ENV['organization_id'] report_file = ForemanInventoryUpload.facts_archive_name(organization_id) - ForemanTasks.sync_task(ForemanInventoryUpload::Async::QueueForUploadJob, base_folder, report_file, organization_id) + disconnected = false + ForemanTasks.sync_task(ForemanInventoryUpload::Async::QueueForUploadJob, base_folder, report_file, organization_id, disconnected) puts "Uploaded #{report_file}" end end diff --git a/test/controllers/inventory_upload/api/inventory_controller_test.rb b/test/controllers/inventory_upload/api/inventory_controller_test.rb index 31c7fca4..b48d31b7 100644 --- a/test/controllers/inventory_upload/api/inventory_controller_test.rb +++ b/test/controllers/inventory_upload/api/inventory_controller_test.rb @@ -6,14 +6,15 @@ class InventoryControllerTest < ActionController::TestCase setup do @test_org = FactoryBot.create(:organization) + @disconnected = false end test 'Starts report generation' do Api::V2::RhCloud::InventoryController.any_instance .expects(:start_report_generation) - .with(@test_org.id.to_s) + .with(@test_org.id.to_s, @disconnected) - post :generate_report, params: { organization_id: @test_org.id } + post :generate_report, params: { organization_id: @test_org.id, disconnected: @disconnected} assert_response :success end