Class: Katello::Host::SubscriptionFacet

Inherits:
Model
  • Object
show all
Defined in:
app/models/katello/host/subscription_facet.rb

Constant Summary collapse

DEFAULT_TYPE =
Glue::Candlepin::Consumer::SYSTEM

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#destroy!

Instance Attribute Details

#factsObject

Returns the value of attribute facts.



16
17
18
# File 'app/models/katello/host/subscription_facet.rb', line 16

def facts
  @facts
end

#hypervisor_guest_uuidsObject

Returns the value of attribute hypervisor_guest_uuids.



16
17
18
# File 'app/models/katello/host/subscription_facet.rb', line 16

def hypervisor_guest_uuids
  @hypervisor_guest_uuids
end

#installed_productsObject

Returns the value of attribute installed_products.



16
17
18
# File 'app/models/katello/host/subscription_facet.rb', line 16

def installed_products
  @installed_products
end

Class Method Details

.find_host(name, organization) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'app/models/katello/host/subscription_facet.rb', line 108

def self.find_host(name, organization)
  hosts = ::Host.where(:name => name)
  return nil if hosts.empty? #no host exists
  if hosts.where("organization_id = #{organization.id} OR organization_id is NULL").empty? #not in the correct org
    #TODO: http://projects.theforeman.org/issues/11532
    fail _("Host is currently registered to a different org, please migrate host to %s.") % organization.name
  end
  hosts.first
end

.find_or_create_host(name, organization, rhsm_params) ⇒ Object



84
85
86
87
88
89
90
# File 'app/models/katello/host/subscription_facet.rb', line 84

def self.find_or_create_host(name, organization, rhsm_params)
  host = find_host(name, organization)
  host = Katello::Host::SubscriptionFacet.new_host_from_facts(rhsm_params[:facts], organization,
                                    Location.default_location) unless host
  host.organization = organization unless host.organization
  host
end

.find_or_create_host_for_hypervisor(name, organization, location = nil) ⇒ Object



92
93
94
95
96
97
98
# File 'app/models/katello/host/subscription_facet.rb', line 92

def self.find_or_create_host_for_hypervisor(name, organization, location = nil)
  location ||= Location.default_location
  host = find_host(name, organization)
  host = ::Host::Managed.new(:name => name, :organization => organization, :location => location,
                      :managed => false) unless host
  host
end

.new_host_from_facts(facts, org, location) ⇒ Object



73
74
75
# File 'app/models/katello/host/subscription_facet.rb', line 73

def self.new_host_from_facts(facts, org, location)
  ::Host::Managed.new(:name => facts['network.hostname'], :organization => org, :location => location, :managed => false)
end

.update_facts(host, rhsm_facts) ⇒ Object



77
78
79
80
81
82
# File 'app/models/katello/host/subscription_facet.rb', line 77

def self.update_facts(host, rhsm_facts)
  return if host.build? || rhsm_facts.nil?
  rhsm_facts[:_type] = RhsmFactName::FACT_TYPE
  rhsm_facts[:_timestamp] = DateTime.now.to_s
  host.import_facts(rhsm_facts)
end

Instance Method Details

#backend_update_needed?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/katello/host/subscription_facet.rb', line 122

def backend_update_needed?
  return true if self.installed_products || self.hypervisor_guest_uuids

  %w(release_version service_level autoheal).each do |method|
    return true if self.send("#{method}_changed?")
  end
  if self.host.content_facet
    return true if (self.host.content_facet.content_view_id_changed? || self.host.content_facet.lifecycle_environment_id_changed?)
  end
  false
end

#candlepin_consumerObject



118
119
120
# File 'app/models/katello/host/subscription_facet.rb', line 118

def candlepin_consumer
  @candlepin_consumer ||= Katello::Candlepin::Consumer.new(self.uuid)
end

#candlepin_environment_idObject



61
62
63
64
65
66
67
# File 'app/models/katello/host/subscription_facet.rb', line 61

def candlepin_environment_id
  if self.host.content_facet
    self.host.content_facet.content_view.cp_environment_id(self.host.content_facet.lifecycle_environment)
  else
    self.host.organization.default_content_view.cp_environment_id(self.host.organization.library)
  end
end

#consumer_attributesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/katello/host/subscription_facet.rb', line 38

def consumer_attributes
  attrs = {
    :autoheal => autoheal,
    :serviceLevel => service_level,
    :releaseVer => release_version,
    :environment => {:id => self.candlepin_environment_id}
  }
  attrs[:facts] = facts if facts
  attrs[:guestIds] = hypervisor_guest_uuids if hypervisor_guest_uuids
  if installed_products
    attrs[:installedProducts] = installed_products.collect do |installed_product|
      product = {
        :productName => installed_product[:product_name],
        :productId => installed_product[:product_id]
      }
      product[:arch] = installed_product[:arch] if installed_product[:arch]
      product[:version] = installed_product[:version] if installed_product[:version]
      product
    end
  end
  attrs
end

#import_database_attributes(consumer_params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/katello/host/subscription_facet.rb', line 25

def import_database_attributes(consumer_params)
  self.autoheal = consumer_params['autoheal'] unless consumer_params['autoheal'].blank?
  self.service_level = consumer_params['serviceLevel'] unless consumer_params['serviceLevel'].blank?
  self.registered_at = consumer_params['created'] unless consumer_params['created'].blank?
  self.last_checkin = consumer_params['lastCheckin'] unless consumer_params['lastCheckin'].blank?

  unless consumer_params['releaseVer'].blank?
    release = consumer_params['releaseVer']
    release = release['releaseVer'] if release.is_a?(Hash)
    self.release_version = release
  end
end

#remove_subscriptions(pools_with_quantities) ⇒ Object



100
101
102
103
104
105
106
# File 'app/models/katello/host/subscription_facet.rb', line 100

def remove_subscriptions(pools_with_quantities)
  entitlements = pools_with_quantities.map do |pool_with_quantities|
    candlepin_consumer.filter_entitlements(pool_with_quantities.pool.cp_id, pool_with_quantities.quantities)
  end

  ForemanTasks.sync_task(Actions::Katello::Host::RemoveSubscriptions, self.host, entitlements.flatten)
end

#update_from_consumer_attributes(consumer_params) ⇒ Object



18
19
20
21
22
23
# File 'app/models/katello/host/subscription_facet.rb', line 18

def update_from_consumer_attributes(consumer_params)
  import_database_attributes(consumer_params)
  self.installed_products = consumer_params['installedProducts'] unless consumer_params['installedProducts'].blank?
  self.hypervisor_guest_uuids = consumer_params['guestIds'] unless consumer_params['hypervisor_guest_uuids'].blank?
  self.facts = consumer_params['facts'] unless consumer_params['facts'].blank?
end

#update_subscription_statusObject



69
70
71
# File 'app/models/katello/host/subscription_facet.rb', line 69

def update_subscription_status
  host.get_status(::Katello::SubscriptionStatus).refresh!
end