Class: Katello::Candlepin::MessageHandler

Inherits:
Object
  • Object
show all
Defined in:
app/services/katello/candlepin/message_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ MessageHandler



7
8
9
10
11
# File 'app/services/katello/candlepin/message_handler.rb', line 7

def initialize(message)
  @message = message
  @subscription_facet = ::Katello::Host::SubscriptionFacet.find_by_uuid(consumer_uuid) if consumer_uuid
  @pool = ::Katello::Pool.find_by_cp_id(pool_id) if pool_id
end

Instance Attribute Details

#messageObject (readonly)

Service to handle parsing the messages we receive from candlepin



5
6
7
# File 'app/services/katello/candlepin/message_handler.rb', line 5

def message
  @message
end

#poolObject (readonly)

Service to handle parsing the messages we receive from candlepin



5
6
7
# File 'app/services/katello/candlepin/message_handler.rb', line 5

def pool
  @pool
end

#subscription_facetObject (readonly)

Service to handle parsing the messages we receive from candlepin



5
6
7
# File 'app/services/katello/candlepin/message_handler.rb', line 5

def subscription_facet
  @subscription_facet
end

Instance Method Details

#consumer_uuidObject



41
42
43
# File 'app/services/katello/candlepin/message_handler.rb', line 41

def consumer_uuid
  content['consumerUuid']
end

#contentObject



17
18
19
# File 'app/services/katello/candlepin/message_handler.rb', line 17

def content
  @content ||= JSON.parse(message.content)
end

#create_pool_on_hostObject



60
61
62
63
64
65
66
# File 'app/services/katello/candlepin/message_handler.rb', line 60

def create_pool_on_host
  return if self.subscription_facet.nil? || pool.nil?
  old_host_ids = subscription_facet_host_ids
  ::Katello::SubscriptionFacetPool.where(subscription_facet_id: self.subscription_facet.id,
                                         pool_id: pool.id).first_or_create
  pool.import_audit_record(old_host_ids)
end

#delete_poolObject



90
91
92
93
94
# File 'app/services/katello/candlepin/message_handler.rb', line 90

def delete_pool
  if Katello::Pool.where(:cp_id => pool_id).destroy_all.any?
    Rails.logger.info "Deleted Katello::Pool with cp_id=#{pool_id}"
  end
end

#entity_idObject



25
26
27
# File 'app/services/katello/candlepin/message_handler.rb', line 25

def entity_id
  content['entityId']
end

#event_dataObject



21
22
23
# File 'app/services/katello/candlepin/message_handler.rb', line 21

def event_data
  @event_data ||= (data = content['eventData']) ? JSON.parse(data) : {}
end

#handle_content_access_mode_modifiedObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/services/katello/candlepin/message_handler.rb', line 96

def handle_content_access_mode_modified
  # Ideally the target_name would be the Candlepin Owner key
  # Since it's the displayName, and we don't update that after org creation, there could be a mismatch
  # For now, find the Candlepin Owner displayName from this event, and tie it back to a Katello org based on owner key
  owners = Katello::Resources::Candlepin::Owner.all
  owner = owners.find { |o| o['displayName'] == target_name }

  unless owner
    fail("Candlepin Owner %s could not be found" % target_name)
  end

  org = ::Organization.find_by!(label: owner['key'])
  hosts = org.hosts

  if event_data['contentAccessMode'] == 'org_environment'
    Katello::HostStatusManager.clear_syspurpose_status(hosts)
    Katello::HostStatusManager.update_subscription_status_to_sca(hosts)
  elsif event_data['contentAccessMode'] == 'entitlement'
    cp_consumer_uuids = hosts.joins(:subscription_facet).pluck("#{Katello::Host::SubscriptionFacet.table_name}.uuid")
    cp_consumer_uuids.each do |uuid|
      Katello::Resources::Candlepin::Consumer.compliance(uuid)
      Katello::Resources::Candlepin::Consumer.purpose_compliance(uuid)
    rescue => e
      Rails.logger.error("Error encountered while fetching compliance for consumer #{uuid}: #{e.message}")
    end
  end

  org.simple_content_access?(cached: false)
end

#import_pool(index_hosts = true) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/katello/candlepin/message_handler.rb', line 76

def import_pool(index_hosts = true)
  if pool
    ::Katello::EventQueue.push_event(::Katello::Events::ImportPool::EVENT_TYPE, pool.id)
  else
    begin
      ::Katello::Pool.import_pool(pool_id, index_hosts)
    rescue ActiveRecord::RecordInvalid
      # if we hit this block it's likely that the pool's subscription, product are being created
      # as a result of manifest import/refresh or custom product creation
      Rails.logger.warn("Unable to import pool. It will likely be created by another process.")
    end
  end
end

#pool_idObject



52
53
54
55
56
57
58
# File 'app/services/katello/candlepin/message_handler.rb', line 52

def pool_id
  if subject == 'pool.created' || subject == 'pool.deleted'
    content['entityId']
  elsif subject == 'entitlement.created' ||  subject == 'entitlement.deleted'
    content['referenceId']
  end
end

#reasonsObject



37
38
39
# File 'app/services/katello/candlepin/message_handler.rb', line 37

def reasons
  event_data['reasons']
end

#remove_pool_from_hostObject



68
69
70
71
72
73
74
# File 'app/services/katello/candlepin/message_handler.rb', line 68

def remove_pool_from_host
  return if self.subscription_facet.nil? || pool.nil?
  old_host_ids = subscription_facet_host_ids
  ::Katello::SubscriptionFacetPool.where(subscription_facet_id: self.subscription_facet.id,
                                         pool_id: pool.id).destroy_all
  pool.import_audit_record(old_host_ids)
end

#statusObject



33
34
35
# File 'app/services/katello/candlepin/message_handler.rb', line 33

def status
  event_data['status']
end

#subjectObject



13
14
15
# File 'app/services/katello/candlepin/message_handler.rb', line 13

def subject
  @message.subject
end

#system_purposeObject



45
46
47
48
49
50
# File 'app/services/katello/candlepin/message_handler.rb', line 45

def system_purpose
  if subject == 'system_purpose_compliance.created' && @system_purpose.nil?
    @system_purpose = Katello::Candlepin::SystemPurpose.new(event_data)
  end
  @system_purpose
end

#target_nameObject



29
30
31
# File 'app/services/katello/candlepin/message_handler.rb', line 29

def target_name
  content['targetName']
end