Skip to content

Commit

Permalink
Add ability to generate report without uploading (theforeman#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris1984 authored and ShimShtein committed Nov 1, 2023
1 parent 7489c09 commit 2fe2a6b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/controllers/api/v2/rh_cloud/inventory_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/inventory_upload/report_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/foreman_inventory_upload/async/generate_report_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/foreman_inventory_upload/async/queue_for_upload_job.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/rh_cloud_inventory.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2fe2a6b

Please sign in to comment.