Sha256: 70aba4f4c0eb4d2960dc40efe1db8ae7b37468242ec27981ebdaf08362d62b7e

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

require 'rest-client'

module InventorySync
  module Async
    class QueryInventoryJob < ::Actions::EntryAction
      include ActiveSupport::Callbacks
      include ::ForemanRhCloud::CloudAuth

      define_callbacks :iteration, :step

      def run
        run_callbacks :iteration do
          page = 1
          loop do
            api_response = query_inventory(page)
            results = HostResult.new(api_response)
            logger.debug("Downloaded cloud inventory data: #{results.percentage}%")

            run_callbacks :step do
              results
            end

            page += 1
            break if results.last?
          end
        end
      end

      private

      def query_inventory(page = 1)
        hosts_inventory_response = RestClient::Request.execute(
          method: :get,
          url: ForemanInventoryUpload.inventory_export_url,
          verify_ssl: ForemanRhCloud.verify_ssl_method,
          proxy: ForemanRhCloud.transformed_http_proxy_string(logger: logger),
          headers: {
            Authorization: "Bearer #{rh_credentials}",
            params: {
              per_page: 100,
              page: page,
            },
          }
        )

        JSON.parse(hosts_inventory_response)
      end

      def logger
        action_logger
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
foreman_rh_cloud-4.0.21.1 lib/inventory_sync/async/query_inventory_job.rb
foreman_rh_cloud-3.0.21.1 lib/inventory_sync/async/query_inventory_job.rb
foreman_rh_cloud-3.0.21 lib/inventory_sync/async/query_inventory_job.rb
foreman_rh_cloud-3.0.20 lib/inventory_sync/async/query_inventory_job.rb
foreman_rh_cloud-3.0.19 lib/inventory_sync/async/query_inventory_job.rb