Sha256: cc65df499689e07fb65e2b3663519b44102d653cf9091b9c398ae66e1244cbb7

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module ForemanRhCloud
  class HitRemediationsRetriever < RemediationsRetriever
    def initialize(hit_remediation_pairs, logger: Logger.new(IO::NULL))
      super(logger: logger)
      @hit_remediation_pairs = hit_remediation_pairs
      logger.debug("Querying playbook for #{hit_remediation_pairs}")
    end

    private

    def hit_ids
      @hit_remediation_pairs.map { |pair| pair["hit_id"] }
    end

    def remediation_ids
      @hit_remediation_pairs.map { |pair| pair["resolution_id"] }
    end

    def hits
      @hits ||= Hash[
        InsightsHit.joins(:insights_facet).where(id: hit_ids).pluck(:id, 'insights_facets.uuid')
      ]
    end

    def pairs_by_remediation_id
      @hit_remediation_pairs.group_by { |pair| pair["resolution_id"] }
    end

    def remediations
      @remediations ||= Hash[
        InsightsResolution.where(id: remediation_ids).pluck(:id, :resolution_type, :rule_id).map do |id, resolution_type, rule_id|
          [id, {resolution_type: resolution_type, rule_id: rule_id}]
        end
      ]
    end

    def playbook_request
      {
        issues: pairs_by_remediation_id.map do |remediation_id, pairs|
          {
            resolution: remediations[remediation_id][:resolution_type],
            id: InsightsCloud.remediation_rule_id(remediations[remediation_id][:rule_id]),
            systems: pairs.map do |pair|
              hits[pair["hit_id"]]
            end,
          }
        end,
      }
    end

    def playbook_url
      InsightsCloud.playbook_url
    end

    def headers
      super
    end

    def payload
      playbook_request.to_json
    end

    def method
      :post
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
foreman_rh_cloud-5.0.35 app/services/foreman_rh_cloud/hit_remediations_retriever.rb
foreman_rh_cloud-5.0.34 app/services/foreman_rh_cloud/hit_remediations_retriever.rb
foreman_rh_cloud-5.0.33 app/services/foreman_rh_cloud/hit_remediations_retriever.rb