Class: Katello::ActivationKey

Inherits:
Model
  • Object
show all
Includes:
ForemanTasks::Concerns::ActionSubject, Glue::Candlepin::ActivationKey, Katello::Authorization::ActivationKey, Glue
Defined in:
app/models/katello/activation_key.rb

Instance Method Summary collapse

Methods included from Katello::Authorization::ActivationKey

#deletable?, #editable?, #readable?

Methods included from Glue

included, logger

Methods inherited from Model

#destroy!

Instance Method Details

#available_contentObject



109
110
111
# File 'app/models/katello/activation_key.rb', line 109

def available_content
  self.products ? self.products.map(&:available_content).flatten.uniq { |product| product.content.id } : []
end

#available_releasesObject



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

def available_releases
  if self.environment
    self.environment.available_releases
  else
    self.organization.library.available_releases
  end
end

#available_subscriptionsObject



89
90
91
92
93
94
# File 'app/models/katello/activation_key.rb', line 89

def available_subscriptions
  all_pools = self.get_pools.map { |pool| pool["id"] }
  added_pools = self.get_key_pools.map { |pool| pool["id"] }
  available_pools = all_pools - added_pools
  Pool.where(:cp_id => available_pools)
end

#calculate_consumption(product, pools, _allocate) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/katello/activation_key.rb', line 117

def calculate_consumption(product, pools, _allocate)
  pools = pools.sort_by { |pool| [pool.start_date, pool.cp_id] }
  consumption = {}

  if product.provider.redhat_provider?
    pools.each do |pool|
      consumption[pool] ||= 0
      consumption[pool] += 1
    end
  else
    consumption[pools.first] = 1
  end
  return consumption
end

#copy(new_name) ⇒ Object



178
179
180
181
182
183
184
# File 'app/models/katello/activation_key.rb', line 178

def copy(new_name)
  new_key = ActivationKey.new
  new_key.name = new_name
  new_key.attributes = self.attributes.slice("description", "environment_id", "organization_id", "content_view_id", "max_hosts", "unlimited_hosts")
  new_key.host_collection_ids = self.host_collection_ids
  new_key
end

#environment_existsObject



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

def environment_exists
  if environment_id && environment.nil?
    errors.add(:environment, _("ID: %s doesn't exist ") % environment_id)
  elsif !environment.nil? && environment.organization != self.organization
    errors.add(:environment, _("name: %s doesn't exist ") % environment.name)
  end
end

#productsObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/katello/activation_key.rb', line 96

def products
  all_products = []

  cp_pools = self.get_key_pools
  if cp_pools
    pools = cp_pools.collect { |cp_pool| Pool.find_by(:cp_id => cp_pool['id']) }
    pools.each do |pool|
      all_products << pool.subscription.products
    end
  end
  all_products.flatten!
end


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

def related_resources
  self.organization
end

#subscribe_system(system) ⇒ Object

subscribe to each product according the entitlements remaining TODO: break up method rubocop:disable MethodLength



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/models/katello/activation_key.rb', line 135

def subscribe_system(system)
  already_subscribed = []
  begin
    # sanity check before we start subscribing
    self.pools.each do |pool|
      fail _("Pool %s has no product associated") % pool.cp_id unless pool.product_id
      fail _("Unable to determine quantity for pool %s") % pool.cp_id unless pool.quantity
    end

    allocate = system.sockets.to_i
    Rails.logger.debug "Number of sockets for registration: #{allocate}"
    fail _("Number of sockets must be higher than 0 for system %s") % system.name if allocate <= 0

    # we sort just to make the order deterministig.
    self.pools.group_by(&:product_id).sort_by(&:first).each do |product_id, pools|
      product = Product.find_by_cp_id(product_id, self.organization)
      consumption = calculate_consumption(product, pools, allocate)

      Rails.logger.debug "Autosubscribing pools: #{consumption.map { |pool, amount| "#{pool.cp_id} => #{amount}" }.join(", ")}"
      consumption.each do |pool, amount|
        Rails.logger.debug "Subscribing #{system.name} to product: #{product_id}, consuming pool #{pool.cp_id} of amount: #{amount}"
        if entitlements_array = system.subscribe(pool.cp_id, amount)
          # store for possible rollback
          entitlements_array.each do |ent|
            already_subscribed << ent['id']
          end
        end
      end
    end
  rescue => e
    Rails.logger.error "Autosubscription failed, rolling back: #{already_subscribed.inspect}"
    already_subscribed.each do |entitlement_id|
      begin
        Rails.logger.debug "Rolling back: #{entitlement_id}"
        system.unsubscribe(entitlement_id)
      rescue => re
        Rails.logger.fatal "Rollback failed, skipping: #{re.message}"
      end
    end
    raise e
  end
end

#subscribe_to_pool(pool_id, quantity = 1) ⇒ Object



186
187
188
189
190
# File 'app/models/katello/activation_key.rb', line 186

def subscribe_to_pool(pool_id, quantity = 1)
  self.subscribe(pool_id, quantity)
rescue RestClient::ResourceNotFound, RestClient::BadRequest => e
  raise JSON.parse(e.response)['displayMessage']
end

#subscriptionsObject



85
86
87
# File 'app/models/katello/activation_key.rb', line 85

def subscriptions
  self.pools
end

#unsubscribe_from_pool(pool_id) ⇒ Object



192
193
194
195
196
# File 'app/models/katello/activation_key.rb', line 192

def unsubscribe_from_pool(pool_id)
  self.unsubscribe(pool_id)
rescue RestClient::ResourceNotFound, RestClient::BadRequest => e
  raise JSON.parse(e.response)['displayMessage']
end

#usage_countObject



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

def usage_count
  subscription_facet_activation_keys.count
end

#valid_content_label?(content_label) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/katello/activation_key.rb', line 113

def ()
  self.available_content.map(&:content).any? { |content| content.label ==  }
end