Skip to content

Commit b0c6358

Browse files
committed
address comments and rubocop
1 parent e5d7001 commit b0c6358

File tree

5 files changed

+92
-34
lines changed

5 files changed

+92
-34
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module ForemanInventoryUpload
2+
module Async
3+
class CreateMissingInsightsFacets < ::Actions::EntryAction
4+
def plan(organization_id)
5+
plan_self(organization_id: organization_id)
6+
end
7+
8+
def finalize
9+
organization = ::Organization.find(input[:organization_id])
10+
hosts_without_facets = ::ForemanInventoryUpload::Generators::Queries.for_org(organization, hosts_query: 'null? insights_uuid')
11+
facet_count = hosts_without_facets.count
12+
hosts_without_facets.each do |batch|
13+
facets = batch.pluck(:id, 'katello_subscription_facets.uuid').map do |host_id, uuid|
14+
{
15+
host_id: host_id,
16+
uuid: uuid,
17+
}
18+
end
19+
# We don't need to validate the facets here as we create the necessary fields.
20+
# rubocop:disable Rails/SkipsModelValidations
21+
InsightsFacet.upsert_all(facets, unique_by: :host_id) unless facets.empty?
22+
# rubocop:enable Rails/SkipsModelValidations
23+
end
24+
Rails.logger.info "Missing Insights facets created: #{facet_count}" unless facet_count.zero?
25+
end
26+
end
27+
end
28+
end

lib/foreman_inventory_upload/async/generate_single_host_report.rb renamed to lib/foreman_inventory_upload/async/generate_host_report.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
module ForemanInventoryUpload
22
module Async
3-
class GenerateSingleHostReport < ::Actions::EntryAction
3+
class GenerateHostReport < ::Actions::EntryAction
44
def plan(base_folder, organization_id, filter)
55
plan_self(
66
base_folder: base_folder,
77
organization_id: organization_id,
8-
filter: filter,
8+
filter: filter
99
)
1010
input[:target] = File.join(base_folder, ForemanInventoryUpload.facts_archive_name(input[:organization_id], input[:filter]))
11+
end
12+
13+
def run
1114
archived_report_generator = ForemanInventoryUpload::Generators::ArchivedReport.new(input[:target])
1215
archived_report_generator.render(organization: input[:organization_id], filter: input[:filter])
1316
end
14-
17+
1518
def finalize
1619
output[:result] = "Generated #{input[:target]} for organization id #{input[:organization_id]}"
1720
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module ForemanInventoryUpload
2+
module Async
3+
class HostInventoryReportJob < ::Actions::EntryAction
4+
def plan(base_folder, organization_id, hosts_filter = "")
5+
sequence do
6+
plan_action(
7+
GenerateHostReport,
8+
base_folder,
9+
organization_id,
10+
hosts_filter
11+
)
12+
13+
plan_action(
14+
QueueForUploadJob,
15+
base_folder,
16+
ForemanInventoryUpload.facts_archive_name(organization_id, hosts_filter),
17+
organization_id
18+
)
19+
20+
if ForemanRhCloud.with_iop_smart_proxy?
21+
plan_action(
22+
CreateMissingInsightsFacets,
23+
organization_id
24+
)
25+
end
26+
end
27+
end
28+
29+
def humanized_name
30+
_("Host inventory report job")
31+
end
32+
33+
def organization_id
34+
input[:organization_id]
35+
end
36+
end
37+
end
38+
end

lib/foreman_inventory_upload/async/single_host_report_job.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module ForemanInventoryUpload
22
module Async
3-
class SingleHostReportJob < ::Actions::EntryAction
3+
class SingleHostReportJob < HostInventoryReportJob
44
def plan(base_folder, organization_id, host_id)
55
input[:host_id] = host_id
66
sequence do
77
plan_action(
8-
GenerateSingleHostReport,
8+
GenerateHostReport,
99
base_folder,
1010
organization_id,
11-
hosts_filter,
11+
hosts_filter
1212
)
1313

1414
plan_action(
@@ -17,6 +17,11 @@ def plan(base_folder, organization_id, host_id)
1717
ForemanInventoryUpload.facts_archive_name(organization_id, hosts_filter),
1818
organization_id
1919
)
20+
21+
plan_action(
22+
CreateMissingInsightsFacets,
23+
organization_id
24+
)
2025
end
2126
end
2227

@@ -27,7 +32,7 @@ def hostname(host_id)
2732

2833
def humanized_name
2934
hostname_result = hostname(input[:host_id])
30-
hostname_result.present? ? _("Single-host report job for host %s" % hostname_result) : _("Single-host report job")
35+
hostname_result.present? ? format(_("Single-host report job for host %s"), hostname_result) : _("Single-host report job")
3136
end
3237

3338
def organization_id

lib/tasks/rh_cloud_inventory.rake

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ namespace :rh_cloud_inventory do
2323
end
2424
end
2525
desc 'Generate inventory report to be sent to Red Hat cloud'
26-
task generate: :environment do
27-
organizations = [ENV['organization_id']]
26+
task generate: [:environment, 'dynflow:client'] do
27+
organization_ids = [ENV['organization_id']]
2828
base_folder = ENV['target'] || Dir.pwd
2929
filter = ENV['hosts_filter']
3030

@@ -34,37 +34,21 @@ namespace :rh_cloud_inventory do
3434
puts "Using #{base_folder} for the output"
3535
end
3636

37-
if organizations.empty?
37+
if organization_ids.empty?
3838
puts "Must specify organization_id"
3939
return
4040
end
4141

4242
User.as_anonymous_admin do
43-
organizations.each do |organization|
44-
target = File.join(base_folder, ForemanInventoryUpload.facts_archive_name(organization, filter))
45-
archived_report_generator = ForemanInventoryUpload::Generators::ArchivedReport.new(target, Logger.new(STDOUT))
46-
archived_report_generator.render(organization: organization, filter: filter)
47-
puts "Successfully generated #{target} for organization id #{organization}"
48-
puts "Check the Uploading tab for report uploading status." if Setting[:subscription_connection_enabled]
49-
50-
next unless ForemanRhCloud.with_iop_smart_proxy?
51-
52-
puts 'Creating missing insights facets'
53-
hosts_without_facets = ForemanInventoryUpload::Generators::Queries.for_org(organization, hosts_query: 'null? insights_uuid')
54-
hosts_without_facets.each do |batch|
55-
facets = batch.pluck(:id, 'katello_subscription_facets.uuid').map do |host_id, uuid|
56-
{
57-
host_id: host_id,
58-
uuid: uuid,
59-
}
60-
end
61-
# We don't need to validate the facets here as we create the necessary fields.
62-
# rubocop:disable Rails/SkipsModelValidations
63-
InsightsFacet.upsert_all(facets, unique_by: :host_id) unless facets.empty?
64-
# rubocop:enable Rails/SkipsModelValidations
65-
end
66-
puts 'Missing Insights facets created'
43+
organization_ids.each do |organization_id|
44+
ForemanTasks.sync_task(
45+
ForemanInventoryUpload::Async::HostInventoryReportJob,
46+
base_folder,
47+
organization_id,
48+
filter
49+
)
6750
end
51+
puts "Check the Uploading tab for report uploading status." if Setting[:subscription_connection_enabled]
6852
end
6953
end
7054
desc 'Upload generated inventory report to Red Hat cloud'

0 commit comments

Comments
 (0)