foreman_rh_cloud 3.0.18.1 → 3.0.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -5
  3. data/app/controllers/foreman_inventory_upload/tasks_controller.rb +14 -3
  4. data/app/controllers/foreman_inventory_upload/uploads_settings_controller.rb +8 -0
  5. data/app/controllers/insights_cloud/api/machine_telemetries_controller.rb +17 -7
  6. data/app/controllers/insights_cloud/hits_controller.rb +37 -0
  7. data/app/controllers/insights_cloud/settings_controller.rb +1 -1
  8. data/app/controllers/insights_cloud/tasks_controller.rb +1 -2
  9. data/app/models/insights_client_report_status.rb +58 -0
  10. data/app/models/insights_resolution.rb +1 -1
  11. data/app/models/inventory_sync/inventory_status.rb +6 -0
  12. data/app/models/setting/rh_cloud.rb +5 -5
  13. data/app/services/foreman_rh_cloud/cloud_auth.rb +12 -0
  14. data/app/services/foreman_rh_cloud/cloud_connector.rb +1 -1
  15. data/app/services/foreman_rh_cloud/cloud_request.rb +14 -0
  16. data/app/services/foreman_rh_cloud/cloud_request_forwarder.rb +1 -6
  17. data/app/services/foreman_rh_cloud/remediations_retriever.rb +75 -0
  18. data/app/services/foreman_rh_cloud/template_renderer_helper.rb +22 -0
  19. data/app/subscribers/foreman_rh_cloud/insights_subscriber.rb +9 -0
  20. data/app/views/job_templates/rh_cloud_remediations.erb +14 -0
  21. data/config/routes.rb +2 -1
  22. data/db/migrate/20210404000001_change_resolutions.foreman_rh_cloud.rb +10 -0
  23. data/db/seeds.d/179_ui_notifications.rb +11 -0
  24. data/db/seeds.d/50_job_templates.rb +14 -0
  25. data/lib/foreman_inventory_upload.rb +9 -0
  26. data/lib/foreman_inventory_upload/async/generate_all_reports_job.rb +8 -2
  27. data/lib/foreman_inventory_upload/generators/fact_helpers.rb +19 -0
  28. data/lib/foreman_inventory_upload/generators/queries.rb +3 -2
  29. data/lib/foreman_inventory_upload/generators/slice.rb +6 -6
  30. data/lib/foreman_inventory_upload/generators/tags.rb +8 -6
  31. data/lib/foreman_inventory_upload/scripts/uploader.sh.erb +5 -1
  32. data/lib/foreman_rh_cloud.rb +18 -0
  33. data/lib/foreman_rh_cloud/engine.rb +36 -2
  34. data/lib/foreman_rh_cloud/version.rb +1 -1
  35. data/lib/insights_cloud.rb +12 -0
  36. data/lib/insights_cloud/async/insights_full_sync.rb +43 -38
  37. data/lib/insights_cloud/async/insights_generate_notifications.rb +58 -0
  38. data/lib/insights_cloud/async/insights_resolutions_sync.rb +66 -0
  39. data/lib/insights_cloud/async/insights_rules_sync.rb +15 -24
  40. data/lib/insights_cloud/async/insights_scheduled_sync.rb +1 -1
  41. data/lib/inventory_sync/async/host_result.rb +11 -6
  42. data/lib/inventory_sync/async/inventory_full_sync.rb +25 -41
  43. data/lib/inventory_sync/async/inventory_hosts_sync.rb +34 -0
  44. data/lib/inventory_sync/async/inventory_scheduled_sync.rb +25 -0
  45. data/lib/inventory_sync/async/query_inventory_job.rb +51 -0
  46. data/lib/tasks/insights.rake +4 -12
  47. data/lib/tasks/rh_cloud_inventory.rake +18 -4
  48. data/package.json +2 -2
  49. data/test/factories/insights_factories.rb +22 -0
  50. data/test/factories/inventory_upload_factories.rb +1 -1
  51. data/test/jobs/insights_full_sync_test.rb +28 -15
  52. data/test/jobs/insights_resolutions_sync_test.rb +77 -0
  53. data/test/jobs/insights_rules_sync_test.rb +8 -3
  54. data/test/jobs/inventory_full_sync_test.rb +185 -12
  55. data/test/models/insights_client_report_status_test.rb +77 -0
  56. data/test/unit/rh_cloud_http_proxy_test.rb +4 -4
  57. data/test/unit/services/foreman_rh_cloud/remediations_retriever_test.rb +49 -0
  58. data/test/unit/services/foreman_rh_cloud/template_renderer_helper_test.rb +28 -0
  59. data/test/unit/slice_generator_test.rb +66 -29
  60. data/test/unit/tags_generator_test.rb +10 -0
  61. data/webpack/ForemanInventoryUpload/Components/FullScreenModal/FullScreenModal.js +1 -1
  62. data/webpack/ForemanInventoryUpload/Components/FullScreenModal/__tests__/__snapshots__/FullScreenModal.test.js.snap +1 -1
  63. data/webpack/ForemanInventoryUpload/Components/FullScreenModal/fullScreenModal.scss +14 -16
  64. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilter.js +1 -1
  65. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/__snapshots__/integration.test.js.snap +0 -1
  66. data/webpack/ForemanInventoryUpload/Components/InventorySettings/AdvancedSetting/AdvancedSettingsConstants.js +5 -3
  67. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/PageDescription.js +15 -2
  68. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/__tests__/__snapshots__/PageDescription.test.js.snap +13 -2
  69. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/SyncButtonActions.js +46 -46
  70. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/SyncButtonConstants.js +3 -3
  71. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/SyncButtonSelectors.js +6 -12
  72. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/SyncButtonFixtures.js +1 -9
  73. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/SyncButtonSelectors.test.js +18 -27
  74. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/__snapshots__/SyncButtonSelectors.test.js.snap +1 -16
  75. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/__snapshots__/integrations.test.js.snap +57 -0
  76. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/integrations.test.js +51 -0
  77. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/index.js +2 -5
  78. data/webpack/ForemanInventoryUpload/Components/Terminal/Terminal.js +1 -1
  79. data/webpack/ForemanInventoryUpload/Components/Terminal/__tests__/Terminal.test.js +1 -1
  80. data/webpack/ForemanInventoryUpload/Components/Terminal/__tests__/__snapshots__/Terminal.test.js.snap +2 -2
  81. data/webpack/ForemanInventoryUpload/Components/Terminal/terminal.scss +25 -27
  82. data/webpack/ForemanInventoryUpload/ForemanInventoryUploadReducers.js +0 -2
  83. data/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTable.js +1 -1
  84. data/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableActions.js +19 -19
  85. data/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js +3 -0
  86. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/__snapshots__/InsightsTable.test.js.snap +2 -0
  87. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/__snapshots__/InsightsTableActions.test.js.snap +14 -14
  88. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediateButton.js +60 -0
  89. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediationActions.js +12 -0
  90. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediationHelpers.js +43 -0
  91. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediationModal.js +101 -0
  92. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediationModal.scss +9 -0
  93. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediationModalFooter.js +43 -0
  94. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediationTableConstants.js +38 -0
  95. data/webpack/InsightsCloudSync/Components/RemediationModal/Resolutions.js +55 -0
  96. data/webpack/InsightsCloudSync/Components/RemediationModal/index.js +34 -0
  97. data/webpack/InsightsCloudSync/InsightsCloudSync.js +11 -3
  98. data/webpack/InsightsCloudSync/InsightsCloudSync.scss +5 -0
  99. data/webpack/InsightsCloudSync/InsightsCloudSyncActions.js +44 -20
  100. data/webpack/InsightsCloudSync/InsightsCloudSyncConstants.js +2 -0
  101. data/webpack/InsightsCloudSync/__snapshots__/InsightsCloudSync.test.js.snap +9 -6
  102. data/webpack/InsightsCloudSync/__tests__/__snapshots__/InsightsCloudSyncActions.test.js.snap +11 -7
  103. data/webpack/__mocks__/foremanReact/redux/middlewares/IntervalMiddleware.js +4 -0
  104. data/webpack/common/ForemanTasks/ForemanTasksActions.js +64 -0
  105. data/webpack/common/ForemanTasks/ForemanTasksHelpers.js +7 -0
  106. data/webpack/common/ForemanTasks/index.js +1 -0
  107. data/webpack/{InsightsCloudSync/Components/InsightsTable/components → common/table}/EmptyState.js +0 -0
  108. data/webpack/common/table/helpers.js +7 -0
  109. metadata +60 -26
  110. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/SyncButtonReducer.js +0 -36
  111. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/SyncButtonActions.test.js +0 -31
  112. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/SyncButtonReducer.test.js +0 -26
  113. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/__snapshots__/SyncButtonActions.test.js.snap +0 -98
  114. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/__snapshots__/SyncButtonReducer.test.js.snap +0 -18
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72303b7bc3f1bc2deb3972acde6131cdbe4d1760381d1eb8cdaa5704e3189481
4
- data.tar.gz: 725efab879cee6224007359547068820882319d078089760cac43a8c763df754
3
+ metadata.gz: 6edd44f8ce4af74d7affca479d84e6012b8a8d363bafcab2ef95fb97eaae5eac
4
+ data.tar.gz: 865870711b1216ea512f5bc5c304f46df1f176b568e4fbb39f6e4686efa68cfb
5
5
  SHA512:
6
- metadata.gz: 28d7e810476e8202a62aa7381e8d3ef86cec5f2f6a7bc9580d5a116a543275183201ad573e848f30a65253bee2f20e7b0385f0cc6f0ab90637cfb03398557188
7
- data.tar.gz: 52560a0917aecaed1c9897c593769c19b7dde9348c1b0450cd32fb4998d5c63e4232d671a1b767e33e6dcec960b3f4647bc23d56a80f15c1f18930db8b772409
6
+ metadata.gz: c0f841e299dd1304590bef74ade8c7b10c9f5392ce7bf164f99243c4cf9d1c68d0061dbb83281bc84123539edca21fdbbcd3bbf1f82e3234a0afd6437cca1c0d
7
+ data.tar.gz: ea5d3651236539899e42085000c4f0aa2401229aff79ef22afaee9eef5c277af38054b0e347fc17d6f119267b1b17410d610b6144883b6b19ec1bdf3ac568d98
data/README.md CHANGED
@@ -42,7 +42,7 @@ In UI: Configure -> Insights -> Sync now
42
42
 
43
43
  From command-line:
44
44
 
45
- /usr/sbin/foreman-rake rh_cloud_inventory:sync
45
+ /usr/sbin/foreman-rake rh_cloud_insights:sync
46
46
 
47
47
  #### Synchronize inventory status
48
48
 
@@ -51,16 +51,32 @@ In UI: Configure -> Inventory Upload -> Sync inventory status
51
51
  From command-line:
52
52
 
53
53
  # all organizations
54
- /usr/sbin/foreman-rake rh_cloud_insights:sync
55
-
54
+ /usr/sbin/foreman-rake rh_cloud_inventory:sync
55
+
56
56
  # specific organization with id 1
57
57
  export organization_id=1
58
- /usr/sbin/foreman-rake rh_cloud_insights:sync
58
+ /usr/sbin/foreman-rake rh_cloud_inventory:sync
59
59
 
60
60
  ## TODO
61
61
 
62
62
  *Todo list here*
63
63
 
64
+ ## Design
65
+
66
+ ### Endpoints
67
+
68
+ | purpose | url | ENV setting for the **bold** part
69
+ | ------------------------------| -------| -----------
70
+ | Inventory uploads | **https://cert.cloud.redhat.com/api/ingress/v1/upload** | SATELLITE_INVENTORY_UPLOAD_URL
71
+ | Query inventory hosts list | **https://cloud.redhat.com** /api/inventory/v1/hosts?tags= | SATELLITE_RH_CLOUD_URL
72
+ | Query insights hits | **https://cloud.redhat.com** /api/insights/v1/export/hits/ | SATELLITE_RH_CLOUD_URL
73
+ | Query insights rules | **https://cloud.redhat.com** /api/insights/v1/rule/?impacting=true&rule_status=enabled&has_playbook=true&limit=&offset= | SATELLITE_RH_CLOUD_URL
74
+ | Query insights resolutions | **https://cloud.redhat.com** /api/remediations/v1/resolutions| SATELLITE_RH_CLOUD_URL
75
+ | Forward insights-client `/static` requests | **https://cloud.redhat.com** /api/static | SATELLITE_RH_CLOUD_URL
76
+ | Forward insights-client legacy `/platform` requests | **https://cert.cloud.redhat.com** /api | SATELLITE_CERT_RH_CLOUD_URL
77
+ | Forward insights-client legacy `/redhat_access/r/insights` requests | **https://cert-api.access.redhat.com** /r/insights | SATELLITE_LEGACY_INSIGHTS_URL
78
+
79
+
64
80
  ## Contributing
65
81
 
66
82
  Fork and send a Pull Request. Thanks!
@@ -81,4 +97,3 @@ GNU General Public License for more details.
81
97
 
82
98
  You should have received a copy of the GNU General Public License
83
99
  along with this program. If not, see <http://www.gnu.org/licenses/>.
84
-
@@ -13,12 +13,23 @@ module ForemanInventoryUpload
13
13
  message: N_('Nothing to sync, there are no hosts with subscription for this organization.'),
14
14
  }, status: :method_not_allowed
15
15
  else
16
- host_statuses = InventorySync::Async::InventoryFullSync.perform_now(selected_org)
16
+ task = ForemanTasks.async_task(InventorySync::Async::InventoryFullSync, selected_org)
17
17
  end
18
+ return render json: { message: N_('there was an issue triggering the task') }, status: :internal_server_error unless task
18
19
 
19
20
  render json: {
20
- syncHosts: host_statuses[:sync],
21
- disconnectHosts: host_statuses[:disconnect],
21
+ task: task,
22
+ }, status: :ok
23
+ end
24
+
25
+ def show
26
+ task = ForemanTasks::Task.find_by(id: params[:id])
27
+ return render json: { message: N_('No task was found') }, status: :internal_server_error unless task
28
+
29
+ render json: {
30
+ endedAt: task.ended_at,
31
+ output: task.output,
32
+ result: task.result,
22
33
  }, status: :ok
23
34
  end
24
35
  end
@@ -8,6 +8,7 @@ module ForemanInventoryUpload
8
8
  excludePackagesEnabled: Setting[:exclude_installed_packages],
9
9
  CloudConnectorStatus: ForemanInventoryUpload::UploadsSettingsController.cloud_connector_status,
10
10
  cloudToken: !Setting[:rh_cloud_token].empty?,
11
+ lastSyncTask: last_successful_inventory_sync_task,
11
12
  }, status: :ok
12
13
  end
13
14
 
@@ -22,5 +23,12 @@ module ForemanInventoryUpload
22
23
  return nil unless job
23
24
  { id: job.id, task: ForemanTasks::Task.where(:id => job.task_id).first }
24
25
  end
26
+
27
+ def last_successful_inventory_sync_task
28
+ task = ForemanTasks::Task.where(label: 'InventorySync::Async::InventoryFullSync', result: 'success')
29
+ .reorder('ended_at desc').first
30
+ return nil unless task
31
+ { output: task.output, endedAt: task.ended_at }
32
+ end
25
33
  end
26
34
  end
@@ -10,27 +10,28 @@ module InsightsCloud::Api
10
10
 
11
11
  skip_after_action :log_response_body, :only => [:forward_request]
12
12
  skip_before_action :check_media_type, :only => [:forward_request]
13
+ after_action :update_host_insights_status, only: [:forward_request]
13
14
 
14
15
  # The method that "proxies" requests over to Cloud
15
16
  def forward_request
16
17
  certs = candlepin_id_cert @organization
17
- cloud_response = ::ForemanRhCloud::CloudRequestForwarder.new.forward_request(request, controller_name, @branch_id, certs)
18
+ @cloud_response = ::ForemanRhCloud::CloudRequestForwarder.new.forward_request(request, controller_name, @branch_id, certs)
18
19
 
19
- if cloud_response.code == 401
20
+ if @cloud_response.code == 401
20
21
  return render json: {
21
22
  :message => 'Authentication to the Insights Service failed.',
22
23
  :headers => {},
23
24
  }, status: :bad_gateway
24
25
  end
25
26
 
26
- if cloud_response.headers[:content_disposition]
27
- return send_data cloud_response, disposition: cloud_response.headers[:content_disposition], type: cloud_response.headers[:content_type]
27
+ if @cloud_response.headers[:content_disposition]
28
+ return send_data @cloud_response, disposition: @cloud_response.headers[:content_disposition], type: @cloud_response.headers[:content_type]
28
29
  end
29
30
 
30
- assign_header(response, cloud_response, :x_resource_count, true)
31
- assign_header(response, cloud_response, :x_rh_insights_request_id, false)
31
+ assign_header(response, @cloud_response, :x_resource_count, true)
32
+ assign_header(response, @cloud_response, :x_rh_insights_request_id, false)
32
33
 
33
- render json: cloud_response, status: cloud_response.code
34
+ render json: @cloud_response, status: @cloud_response.code
34
35
  end
35
36
 
36
37
  def branch_info
@@ -69,5 +70,14 @@ module InsightsCloud::Api
69
70
  @branch_id = cp_owner_id(@organization)
70
71
  return render_message "Branch ID not found for organization #{@organization.title}", :status => 400 unless @branch_id
71
72
  end
73
+
74
+ def update_host_insights_status
75
+ return unless request.path == '/redhat_access/r/insights/platform/ingress/v1/upload' ||
76
+ request.path.include?('/redhat_access/r/insights/uploads/')
77
+
78
+ data = @cloud_response.code.to_s.start_with?('2')
79
+ host_status = @host.get_status(InsightsClientReportStatus)
80
+ host_status.update(reported_at: Time.now.utc, status: host_status.to_status(data: data))
81
+ end
72
82
  end
73
83
  end
@@ -9,6 +9,7 @@ module InsightsCloud
9
9
  hasToken: !Setting[:rh_cloud_token].empty?,
10
10
  hits: hits.map { |hit| hit.attributes.merge(hostname: hit.host&.name, has_playbook: hit.has_playbook?) },
11
11
  itemCount: hits.count,
12
+ isExperimentalMode: Setting[:lab_features],
12
13
  }, status: :ok
13
14
  end
14
15
 
@@ -20,6 +21,21 @@ module InsightsCloud
20
21
  }, status: :ok
21
22
  end
22
23
 
24
+ def resolutions
25
+ if remediation_all_selected_param
26
+ hits = InsightsHit.with_playbook.search_for(params[:query])
27
+ else
28
+ hits = resource_base_search_and_page.where(id: remediation_ids_param)
29
+ end
30
+
31
+ hits.preload(:host, rule: :resolutions)
32
+
33
+ render json: {
34
+ hits: hits.map { |hit| hit.attributes.merge(hostname: hit.host&.name, resolutions: hit.rule.resolutions.map(&:attributes), reboot: hit.rule.reboot_required) },
35
+ itemCount: hits.count,
36
+ }, status: :ok
37
+ end
38
+
23
39
  def model_of_controller
24
40
  ::InsightsHit
25
41
  end
@@ -32,10 +48,31 @@ module InsightsCloud
32
48
  :insights_hits
33
49
  end
34
50
 
51
+ def action_permission
52
+ case params[:action]
53
+ when 'resolutions'
54
+ 'view'
55
+ else
56
+ super
57
+ end
58
+ end
59
+
35
60
  private
36
61
 
37
62
  def host_id_param
38
63
  params.require(:host_id)
39
64
  end
65
+
66
+ def remediation_request_params
67
+ params.permit(remediations: [:hit_id, :remediation_id]).require(:remediations)
68
+ end
69
+
70
+ def remediation_ids_param
71
+ params.require(:ids).map(&:to_i)
72
+ end
73
+
74
+ def remediation_all_selected_param
75
+ ActiveModel::Type::Boolean.new.cast(params[:isAllSelected])
76
+ end
40
77
  end
41
78
  end
@@ -13,7 +13,7 @@ module InsightsCloud
13
13
  token = Setting::RhCloud.find_by_name("rh_cloud_token")
14
14
  token.value = params.require(:value)
15
15
  token.save!
16
- InsightsCloud::Async::InsightsFullSync.perform_now
16
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync)
17
17
  redirect_to(:controller => "hits", :action => 'index')
18
18
  end
19
19
 
@@ -1,8 +1,7 @@
1
1
  module InsightsCloud
2
2
  class TasksController < ::ApplicationController
3
3
  def create
4
- job = InsightsCloud::Async::InsightsFullSync.perform_later
5
- task = ForemanTasks::Task.find_by(external_id: job.provider_job_id)
4
+ task = ForemanTasks.async_task(InsightsCloud::Async::InsightsFullSync)
6
5
 
7
6
  render json: {
8
7
  task: task,
@@ -0,0 +1,58 @@
1
+ class InsightsClientReportStatus < HostStatus::Status
2
+ REPORT_INTERVAL = 48.hours
3
+
4
+ REPORTING = 0 # host_registration_insights = true & getting data
5
+ NO_REPORT = 1 # host_registration_insights = true & not getting data
6
+ NOT_MANAGED = 2 # host_registration_insights = false
7
+ NOT_MANAGED_WITH_DATA = 3 # host_registration_insights = false & getting data
8
+
9
+ def self.status_name
10
+ N_('Insights')
11
+ end
12
+
13
+ def to_label(_options = {})
14
+ case status
15
+ when REPORTING
16
+ N_('Reporting')
17
+ when NO_REPORT
18
+ N_('Not reporting')
19
+ when NOT_MANAGED
20
+ N_('Not reporting (not set by registration)')
21
+ when NOT_MANAGED_WITH_DATA
22
+ N_('Reporting (not set by registration)')
23
+ end
24
+ end
25
+
26
+ def to_global(_options = {})
27
+ case status
28
+ when REPORTING
29
+ ::HostStatus::Global::OK
30
+ when NO_REPORT
31
+ ::HostStatus::Global::ERROR
32
+ when NOT_MANAGED
33
+ ::HostStatus::Global::OK
34
+ when NOT_MANAGED_WITH_DATA
35
+ ::HostStatus::Global::WARN
36
+ end
37
+ end
38
+
39
+ def to_status(data: false)
40
+ if insights_param
41
+ return REPORTING if data
42
+ return in_interval? ? REPORTING : NO_REPORT
43
+ end
44
+
45
+ data ? NOT_MANAGED_WITH_DATA : NOT_MANAGED
46
+ end
47
+
48
+ private
49
+
50
+ def in_interval?
51
+ return false unless reported_at
52
+ (Time.now.utc - reported_at).to_i < REPORT_INTERVAL.to_i
53
+ end
54
+
55
+ def insights_param
56
+ host.host_params_hash.dig('host_registration_insights', :value)
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  class InsightsResolution < ApplicationRecord
2
- belongs_to :rule, class_name: 'InsightsRule', foreign_key: 'rule_id', primary_key: 'rule_id', inverse_of: :resolutions
2
+ belongs_to :rule, class_name: 'InsightsRule', primary_key: 'rule_id', inverse_of: :resolutions
3
3
  end
@@ -26,5 +26,11 @@ module InventorySync
26
26
  N_('Successfully uploaded to your RH cloud inventory')
27
27
  end
28
28
  end
29
+
30
+ def to_status(options = {})
31
+ # this method used to calculate status.
32
+ # Since the calculation is done externally we should return the previously calculated status
33
+ status
34
+ end
29
35
  end
30
36
  end
@@ -14,12 +14,12 @@ class Setting::RhCloud < Setting
14
14
  def self.default_settings
15
15
  return unless ActiveRecord::Base.connection.table_exists?('settings')
16
16
  [
17
- set('allow_auto_inventory_upload', N_('Allow automatic upload of the host inventory to the Red Hat cloud'), true, N_('Allow automatic inventory uploads')),
18
- set('allow_auto_insights_sync', N_('Allow recommendations synchronization from Red Hat cloud'), false, N_('Allow recommendations synchronization')),
19
- set('obfuscate_inventory_hostnames', N_('Obfuscate host names sent to Red Hat cloud'), false, N_('Obfuscate host names')),
20
- set('obfuscate_inventory_ips', N_('Obfuscate ip addresses sent to Red Hat cloud'), false, N_('Obfuscate IPs')),
17
+ set('allow_auto_inventory_upload', N_('Enable automatic upload of your host inventory to the Red Hat cloud'), true, N_('Automatic inventory upload')),
18
+ set('allow_auto_insights_sync', N_('Enable automatic synchronization of Insights recommendations from the Red Hat cloud'), false, N_('Synchronize recommendations Automatically')),
19
+ set('obfuscate_inventory_hostnames', N_('Obfuscate host names sent to the Red Hat cloud'), false, N_('Obfuscate host names')),
20
+ set('obfuscate_inventory_ips', N_('Obfuscate ipv4 addresses sent to the Red Hat cloud'), false, N_('Obfuscate host ipv4 addresses')),
21
21
  set('rh_cloud_token', N_('Authentication token to Red Hat cloud services. Used to authenticate requests to cloud APIs'), nil, N_('Red Hat Cloud token'), nil, encrypted: true),
22
- set('exclude_installed_packages', N_('Exclude installed packages from Red Hat cloud inventory report'), false, N_("Don't upload installed packages")),
22
+ set('exclude_installed_packages', N_('Exclude installed packages from being uploaded to the Red Hat cloud'), false, N_("Exclude installed Packages")),
23
23
  set('include_parameter_tags', N_('Should import include parameter tags from Foreman?'), false, N_('Include parameters in insights-client reports')),
24
24
  ]
25
25
  end
@@ -2,6 +2,8 @@ module ForemanRhCloud
2
2
  module CloudAuth
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ include CloudRequest
6
+
5
7
  def rh_credentials
6
8
  @rh_credentials ||= query_refresh_token
7
9
  end
@@ -24,5 +26,15 @@ module ForemanRhCloud
24
26
  Foreman::Logging.exception('Unable to authenticate using rh_cloud_token setting', e)
25
27
  raise ::Foreman::WrappedException.new(e, N_('Unable to authenticate using rh_cloud_token setting'))
26
28
  end
29
+
30
+ def execute_cloud_request(params)
31
+ final_params = {
32
+ headers: {
33
+ Authorization: "Bearer #{rh_credentials}",
34
+ },
35
+ }.deep_merge(params)
36
+
37
+ super(final_params)
38
+ end
27
39
  end
28
40
  end
@@ -59,7 +59,7 @@ module ForemanRhCloud
59
59
  end
60
60
 
61
61
  def foreman_host
62
- ::Host.unscoped.friendly.find(::SmartProxy.default_capsule.name)
62
+ ForemanRhCloud.foreman_host
63
63
  end
64
64
  end
65
65
  end
@@ -0,0 +1,14 @@
1
+ module ForemanRhCloud
2
+ module CloudRequest
3
+ extend ActiveSupport::Concern
4
+
5
+ def execute_cloud_request(params)
6
+ final_params = {
7
+ verify_ssl: ForemanRhCloud.verify_ssl_method,
8
+ proxy: ForemanRhCloud.transformed_http_proxy_string(logger: logger),
9
+ }.deep_merge(params)
10
+
11
+ RestClient::Request.execute(final_params)
12
+ end
13
+ end
14
+ end
@@ -2,7 +2,7 @@ require 'rest-client'
2
2
 
3
3
  module ForemanRhCloud
4
4
  class CloudRequestForwarder
5
- include ::ForemanRhCloud::CloudAuth
5
+ include ForemanRhCloud::CloudRequest
6
6
 
7
7
  def forward_request(original_request, controller_name, branch_id, certs)
8
8
  forward_params = prepare_forward_params(original_request, branch_id)
@@ -22,7 +22,6 @@ module ForemanRhCloud
22
22
  def prepare_request_opts(original_request, forward_payload, forward_params, certs)
23
23
  base_params = {
24
24
  method: original_request.method,
25
- verify_ssl: ForemanRhCloud.verify_ssl_method,
26
25
  payload: forward_payload,
27
26
  headers: {
28
27
  params: forward_params,
@@ -33,10 +32,6 @@ module ForemanRhCloud
33
32
  base_params.merge(path_params(original_request.path, certs))
34
33
  end
35
34
 
36
- def execute_cloud_request(request_opts)
37
- RestClient::Request.execute request_opts
38
- end
39
-
40
35
  def prepare_forward_payload(original_request, controller_name)
41
36
  forward_payload = original_request.request_parameters[controller_name]
42
37
 
@@ -0,0 +1,75 @@
1
+ module ForemanRhCloud
2
+ class RemediationsRetriever
3
+ include CloudAuth
4
+
5
+ attr_reader :logger
6
+
7
+ def initialize(hit_remediation_pairs, logger: Logger.new(IO::NULL))
8
+ @hit_remediation_pairs = hit_remediation_pairs
9
+ @logger = logger
10
+
11
+ logger.debug("Querying playbook for #{hit_remediation_pairs}")
12
+ end
13
+
14
+ def create_playbook
15
+ response = query_playbook
16
+
17
+ logger.debug("Got playbook response: #{response.body}")
18
+
19
+ response.body
20
+ end
21
+
22
+ private
23
+
24
+ def hit_ids
25
+ @hit_remediation_pairs.map { |pair| pair["hit_id"] }
26
+ end
27
+
28
+ def remediation_ids
29
+ @hit_remediation_pairs.map { |pair| pair["resolution_id"] }
30
+ end
31
+
32
+ def hits
33
+ @hits ||= Hash[
34
+ InsightsHit.joins(:insights_facet).where(id: hit_ids).pluck(:id, 'insights_facets.uuid')
35
+ ]
36
+ end
37
+
38
+ def pairs_by_remediation_id
39
+ @hit_remediation_pairs.group_by { |pair| pair["resolution_id"] }
40
+ end
41
+
42
+ def remediations
43
+ @remediations ||= Hash[
44
+ InsightsResolution.where(id: remediation_ids).pluck(:id, :resolution_type, :rule_id).map do |id, resolution_type, rule_id|
45
+ [id, {resolution_type: resolution_type, rule_id: rule_id}]
46
+ end
47
+ ]
48
+ end
49
+
50
+ def playbook_request
51
+ {
52
+ issues: pairs_by_remediation_id.map do |remediation_id, pairs|
53
+ {
54
+ resolution: remediations[remediation_id][:resolution_type],
55
+ id: InsightsCloud.remediation_rule_id(remediations[remediation_id][:rule_id]),
56
+ systems: pairs.map do |pair|
57
+ hits[pair["hit_id"]]
58
+ end,
59
+ }
60
+ end,
61
+ }
62
+ end
63
+
64
+ def query_playbook
65
+ execute_cloud_request(
66
+ method: :post,
67
+ url: InsightsCloud.playbook_url,
68
+ headers: {
69
+ content_type: :json,
70
+ },
71
+ payload: playbook_request.to_json
72
+ )
73
+ end
74
+ end
75
+ end