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_purpose_addons(_key, operator, value) ⇒ Object



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

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

.find_by_subscription_id(_key, operator, value) ⇒ Object



159
160
161
162
163
164
# File 'app/models/katello/activation_key.rb', line 159

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



153
154
155
156
157
# File 'app/models/katello/activation_key.rb', line 153

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



149
150
151
# File 'app/models/katello/activation_key.rb', line 149

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

.return_activation_keys_by_id(activation_key_ids) ⇒ Object



166
167
168
169
170
171
172
# File 'app/models/katello/activation_key.rb', line 166

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

#available_releasesObject



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

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

#available_subscriptionsObject



98
99
100
101
102
103
104
# File 'app/models/katello/activation_key.rb', line 98

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/katello/activation_key.rb', line 114

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



129
130
131
132
133
134
135
# File 'app/models/katello/activation_key.rb', line 129

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



74
75
76
77
78
79
80
# File 'app/models/katello/activation_key.rb', line 74

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



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

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


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

def related_resources
  self.organization
end

#subscribe_to_pool(pool_id, quantity = 1) ⇒ Object



137
138
139
140
141
# File 'app/models/katello/activation_key.rb', line 137

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



143
144
145
146
147
# File 'app/models/katello/activation_key.rb', line 143

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



82
83
84
# File 'app/models/katello/activation_key.rb', line 82

def usage_count
  subscription_facet_activation_keys.count
end

#valid_content_override_label?(content_label) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#validate_destroyable!Object



180
181
182
183
184
185
186
187
188
189
# File 'app/models/katello/activation_key.rb', line 180

def validate_destroyable!
  unless organization.being_deleted?
    if 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
  end
  true
end