Sha256: 77760e0a1aadcc0702f87e2bc1b2282cfc25af3b64ade589ca4340942dcad14a

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 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

    def organization
      InsightsHit.find(@hit_remediation_pairs.first['hit_id']).host.organization
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
foreman_rh_cloud-10.0.3 app/services/foreman_rh_cloud/hit_remediations_retriever.rb
foreman_rh_cloud-11.0.3 app/services/foreman_rh_cloud/hit_remediations_retriever.rb
foreman_rh_cloud-11.0.2 app/services/foreman_rh_cloud/hit_remediations_retriever.rb
foreman_rh_cloud-11.0.1 app/services/foreman_rh_cloud/hit_remediations_retriever.rb
foreman_rh_cloud-11.0.0 app/services/foreman_rh_cloud/hit_remediations_retriever.rb
foreman_rh_cloud-10.0.2 app/services/foreman_rh_cloud/hit_remediations_retriever.rb