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

.humanize_class_name(_name = nil) ⇒ Object



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

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

.in_environments(envs) ⇒ Object



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

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

.return_activation_keys_by_id(activation_key_ids) ⇒ Object



198
199
200
201
202
203
204
# File 'app/models/katello/activation_key.rb', line 198

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/katello/activation_key.rb', line 130

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



169
170
171
172
173
174
175
# File 'app/models/katello/activation_key.rb', line 169

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

#check_cvesObject



215
216
217
218
219
220
221
222
# File 'app/models/katello/activation_key.rb', line 215

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



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

def content_view
  single_content_view
end

#content_view_environment_labelsObject



177
178
179
# File 'app/models/katello/activation_key.rb', line 177

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

#content_view_environments=(new_cves) ⇒ Object



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

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



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

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



117
118
119
# File 'app/models/katello/activation_key.rb', line 117

def environment
  single_lifecycle_environment
end

#multi_content_view_environment?Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'app/models/katello/activation_key.rb', line 96

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


165
166
167
# File 'app/models/katello/activation_key.rb', line 165

def related_resources
  self.organization
end

#single_content_viewObject



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

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)


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

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

#single_lifecycle_environmentObject



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

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

#usage_countObject

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



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

def usage_count
  subscription_facet_activation_keys.count
end

#valid_content_override_label?(content_label) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
# File 'app/models/katello/activation_key.rb', line 181

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

#validate_destroyable!Object



206
207
208
209
210
211
212
213
# File 'app/models/katello/activation_key.rb', line 206

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