Class: Katello::ActivationKey

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

Defined Under Namespace

Classes: Jail

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Katello::Authorization::ActivationKey

#deletable?, #editable?, #readable?

Methods included from Glue

logger

Methods included from Glue::Candlepin::ActivationKey

included

Methods inherited from Model

#destroy!

Class Method Details

.find_by_subscription_id(_key, operator, value) ⇒ Object



253
254
255
256
257
258
# File 'app/models/katello/activation_key.rb', line 253

def self.find_by_subscription_id(_key, operator, value)
  # What we refer to as "subscriptions" is really Pools, so we search based on Pool id.
  conditions = sanitize_sql_for_conditions(["#{Katello::Pool.table_name}.id #{operator} ?", value_to_sql(operator, value)])
  activation_keys = ::Katello::ActivationKey.joins(:pools).where(conditions)
  return_activation_keys_by_id(activation_keys.pluck(:id))
end

.find_by_subscription_name(_key, operator, value) ⇒ Object



247
248
249
250
251
# File 'app/models/katello/activation_key.rb', line 247

def self.find_by_subscription_name(_key, operator, value)
  conditions = sanitize_sql_for_conditions(["#{Katello::Subscription.table_name}.name #{operator} ?", value_to_sql(operator, value)])
  activation_keys = ::Katello::ActivationKey.joins(pools: :subscription).where(conditions)
  return_activation_keys_by_id(activation_keys.pluck(:id))
end

.humanize_class_name(_name = nil) ⇒ Object



243
244
245
# File 'app/models/katello/activation_key.rb', line 243

def self.humanize_class_name(_name = nil)
  _("Activation Keys")
end

.in_environments(envs) ⇒ Object



88
89
90
# File 'app/models/katello/activation_key.rb', line 88

def self.in_environments(envs)
  with_environments(envs)
end

.return_activation_keys_by_id(activation_key_ids) ⇒ Object



260
261
262
263
264
265
266
# File 'app/models/katello/activation_key.rb', line 260

def self.return_activation_keys_by_id(activation_key_ids)
  if activation_key_ids.empty?
    {:conditions => "1=0"}
  else
    {:conditions => "#{Katello::ActivationKey.table_name}.id IN (#{activation_key_ids.join(',')})"}
  end
end

Instance Method Details

#assign_single_environment(content_view_id: nil, lifecycle_environment_id: nil, environment_id: nil, content_view: nil, lifecycle_environment: nil, environment: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



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
# File 'app/models/katello/activation_key.rb', line 136

def assign_single_environment(
  content_view_id: nil, lifecycle_environment_id: nil, environment_id: nil,
  content_view: nil, lifecycle_environment: nil, environment: nil
)
  lifecycle_environment_id ||= environment_id || lifecycle_environment&.id || environment&.id || self.single_lifecycle_environment&.id
  content_view_id ||= content_view&.id || self.single_content_view&.id

  unless lifecycle_environment_id
    fail _("Lifecycle environment must be specified")
  end

  unless content_view_id
    fail _("Content view must be specified")
  end

  content_view_environment = ::Katello::ContentViewEnvironment
    .where(:content_view_id => content_view_id, :environment_id => lifecycle_environment_id)
    .first_or_create do |cve|
    Rails.logger.info("ContentViewEnvironment not found for content view '#{cve.content_view_name}' and environment '#{cve.environment&.name}'; creating a new one.")
  end
  fail _("Unable to create ContentViewEnvironment. Check the logs for more information.") if content_view_environment.nil?

  if self.content_view_environments.include?(content_view_environment)
    Rails.logger.info("Activation key '#{name}' already has the content view environment '#{content_view_environment.content_view_name}' and environment '#{content_view_environment.environment&.name}'.")
  else
    self.content_view_environments = [content_view_environment]
  end
end

#available_releasesObject



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

def available_releases
  releases = self.content_view_environments.flat_map do |cve|
    cve.content_view.version(cve.lifecycle_environment).available_releases
  end
  return self.organization.library.available_releases if releases.blank?
  releases
end

#available_subscriptionsObject



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

def available_subscriptions
  all_pools = self.get_pools.map { |pool| pool["id"] }
  added_pools = self.pools.pluck(:cp_id)
  available_pools = all_pools - added_pools
  Pool.where(:cp_id => available_pools,
             :subscription_id => Subscription.subscribable)
end

#calculate_consumption(product, pools, _allocate) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/katello/activation_key.rb', line 207

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

#check_cvesObject



277
278
279
280
281
282
283
284
# File 'app/models/katello/activation_key.rb', line 277

def check_cves
  cves_not_in_org = self.content_view_environments.any? do |cve|
    cve.content_view.organization != cve.environment.organization ||
      self.organization != cve.content_view.organization
  end

  errors.add(:base, _("Cannot add content view environments from a different organization")) if cves_not_in_org
end

#content_viewObject



119
120
121
# File 'app/models/katello/activation_key.rb', line 119

def content_view
  single_content_view
end

#content_view_environment_labelsObject



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

def content_view_environment_labels
  content_view_environments.map(&:label).join(',')
end

#content_view_environments=(new_cves) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'app/models/katello/activation_key.rb', line 92

def content_view_environments=(new_cves)
  if new_cves.length > 1 && !Setting['allow_multiple_content_views']
    fail ::Katello::Errors::MultiEnvironmentNotSupportedError,
    _("Assigning an activation key to multiple content view environments is not enabled. To enable, set the allow_multiple_content_views setting.")
  end
  super(new_cves)
  Katello::ContentViewEnvironmentActivationKey.reprioritize_for_activation_key(self, new_cves)
  self.content_view_environments.reload unless self.new_record?
end

#copy(new_name) ⇒ Object



222
223
224
225
226
227
228
229
# File 'app/models/katello/activation_key.rb', line 222

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

#environmentObject



123
124
125
# File 'app/models/katello/activation_key.rb', line 123

def environment
  single_lifecycle_environment
end

#hostsObject



171
172
173
# File 'app/models/katello/activation_key.rb', line 171

def hosts
  subscription_facets.map(&:host)
end

#multi_content_view_environment?Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'app/models/katello/activation_key.rb', line 102

def multi_content_view_environment?
  # returns false if there are no content view environments
  content_view_environments.size > 1
end

#productsObject



199
200
201
# File 'app/models/katello/activation_key.rb', line 199

def products
  Katello::Product.distinct.joins(:pools => :activation_keys).where("#{Katello::ActivationKey.table_name}.id" => self.id).enabled.sort
end


175
176
177
# File 'app/models/katello/activation_key.rb', line 175

def related_resources
  self.organization
end

#single_content_viewObject



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

def single_content_view
  if multi_content_view_environment?
    Rails.logger.warn _("Activation key %s has more than one content view. Use #content_views instead.") % name
  end
  content_view_environments&.first&.content_view
end

#single_content_view_environment?Boolean

Returns:

  • (Boolean)


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

def single_content_view_environment?
  # also returns false if there are no content view environments
  content_view_environments.size == 1
end

#single_lifecycle_environmentObject



127
128
129
130
131
132
# File 'app/models/katello/activation_key.rb', line 127

def single_lifecycle_environment
  if multi_content_view_environment?
    Rails.logger.warn _("Activation key %s has more than one lifecycle environment. Use #lifecycle_environments instead.") % name
  end
  content_view_environments&.first&.lifecycle_environment
end

#subscribe_to_pool(pool_id, quantity = 1) ⇒ Object



231
232
233
234
235
# File 'app/models/katello/activation_key.rb', line 231

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

#unsubscribe_from_pool(pool_id) ⇒ Object



237
238
239
240
241
# File 'app/models/katello/activation_key.rb', line 237

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

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



167
168
169
# File 'app/models/katello/activation_key.rb', line 167

def usage_count
  subscription_facet_activation_keys.count
end

#valid_content_override_label?(content_label) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
# File 'app/models/katello/activation_key.rb', line 203

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

#validate_destroyable!Object



268
269
270
271
272
273
274
275
# File 'app/models/katello/activation_key.rb', line 268

def validate_destroyable!
  if !organization.being_deleted? && Parameter.where(name: 'kt_activation_keys').pluck(:value).any? { |value| value.split(",").include?(name) }
    fail _("This activation key is associated to one or more Hosts/Hostgroups. "\
            "Search and unassociate Hosts/Hostgroups using params.kt_activation_keys ~ \"%{name}\" "\
            "before deleting." % {name: name})
  end
  true
end