app/models/katello/glue/candlepin/consumer.rb in katello-2.2.2 vs app/models/katello/glue/candlepin/consumer.rb in katello-2.4.0.rc1

- old
+ new

@@ -1,17 +1,5 @@ -# -# Copyright 2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module Katello module Glue::Candlepin::Consumer SYSTEM = "system" HYPERVISOR = "hypervisor" CANDLEPIN = "candlepin" @@ -22,22 +10,25 @@ base.send :include, LazyAccessor base.send :include, InstanceMethods base.send :extend, ClassMethods base.class_eval do - as_json_hook :consumer_as_json + attr_accessible :cp_type, :owner, :serviceLevel, :installedProducts, :facts, :guestIds, :releaseVer, :autoheal, + :lastCheckin - attr_accessible :cp_type, :owner, :serviceLevel, :installedProducts, :facts, :guestIds, :releaseVer, :autoheal - - lazy_accessor :href, :facts, :cp_type, :href, :idCert, :owner, :lastCheckin, :created, :guestIds, + lazy_accessor :href, :facts, :cp_type, :idCert, :owner, :lastCheckin, :created, :guestIds, :installedProducts, :autoheal, :releaseVer, :serviceLevel, :capabilities, :entitlementStatus, - :initializer => (lambda do |_s| - if uuid - consumer_json = Resources::Candlepin::Consumer.get(uuid) - convert_from_cp_fields(consumer_json) - end - end) + :initializer => :candlepin_consumer_info + + lazy_accessor :candlepin_consumer_info, :initializer => + (lambda do |_s| + if uuid + consumer_json = Resources::Candlepin::Consumer.get(uuid) + convert_from_cp_fields(consumer_json) + end + end) + lazy_accessor :entitlements, :initializer => lambda { |_s| Resources::Candlepin::Consumer.entitlements(uuid) } lazy_accessor :pools, :initializer => lambda { |_s| entitlements.collect { |ent| Resources::Candlepin::Pool.find ent["pool"]["id"] } } lazy_accessor :available_pools, :initializer => lambda { |_s| Resources::Candlepin::Consumer.available_pools(uuid, false) } lazy_accessor :all_available_pools, :initializer => lambda { |_s| Resources::Candlepin::Consumer.available_pools(uuid, true) } lazy_accessor :virtual_host, :initializer => (lambda do |_s| @@ -79,24 +70,10 @@ super(attrs_used_by_model, options) end end - def serializable_hash(options = {}) - hash = super(options) - hash = hash.merge(:serviceLevel => self.serviceLevel) - hash - end - - def consumer_as_json(json = {}) - json['compliance'] = self.compliance - json['registered'] = self.created - json['checkin_time'] = self.checkin_time - json['distribution'] = self.distribution - json - end - def load_from_cp(consumer_json) self.uuid = consumer_json[:uuid] consumer_json[:facts] ||= {'sockets' => 0} convert_from_cp_fields(consumer_json).each do |k, v| instance_variable_set("@#{k}", v) if respond_to?("#{k}=") @@ -125,10 +102,11 @@ Rails.logger.debug e.backtrace.join("\n\t") raise e end def subscribe(pool, quantity = nil) + quantity = quantity.to_i unless quantity.nil? Rails.logger.debug "Subscribing to pool '#{pool}' for : #{name}" Resources::Candlepin::Consumer.consume_entitlement self.uuid, pool, quantity rescue => e Rails.logger.debug e.backtrace.join("\n\t") raise e @@ -242,13 +220,11 @@ facts["cpu.cpu_socket(s)"] = s if @facts end def virtual_guest - v = facts["virt.is_guest"] - return false if (v == false || v.nil?) - return(v == true || v.to_bool) + ::Foreman::Cast.to_bool(facts["virt.is_guest"]) end def virtual_guest=(val) facts["virt.is_guest"] = val if @facts end @@ -376,21 +352,10 @@ end avail_pools.sort! { |a, b| a.poolName <=> b.poolName } avail_pools end - def consumed_entitlements - self.entitlements.collect do |entitlement| - pool = self.get_pool(entitlement['pool']['id']) - entitlement_pool = Katello::Pool.new(pool) - entitlement_pool.cp_id = entitlement['id'] - entitlement_pool.subscription_id = entitlement['pool']['id'] - entitlement_pool.amount = entitlement['quantity'] - entitlement_pool - end - end - def set_content_override(content_label, name, value = nil) Resources::Candlepin::Consumer.update_content_override(self.uuid, content_label, name, value) end def content_overrides @@ -443,20 +408,22 @@ def products all_products = [] self.entitlements.each do |entitlement| - pool = Katello::Pool.find_pool(entitlement['pool']['id']) + pool = Katello::Pool.find_by_cp_id(entitlement['pool']['id']) Katello::Product.where(:cp_id => pool.product_id).each do |product| - if product.is_a? Katello::MarketingProduct - all_products += product.engineering_products - else - all_products << product - end + all_products << product end end return all_products + end + + def find_entitlement(pool_id) + entitlements = self.entitlements.collect { |ent| ent['id'] if ent["pool"]["id"] == pool_id } + entitlements.compact! + entitlements.first end end module ClassMethods def prepopulate!(systems)