Class: Katello::Product

Inherits:
Model
  • Object
show all
Includes:
ForemanTasks::Concerns::ActionSubject, Glue::Pulp::Repos, Authorization::Product, Ext::LabelFromName, Glue, Glue::Candlepin::Product
Defined in:
app/models/katello/product.rb

Defined Under Namespace

Classes: Jail

Constant Summary

Constants included from Glue::Candlepin::Product

Glue::Candlepin::Product::PRODUCT_ATTRS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ext::LabelFromName

included, #label_not_changed, #setup_label_from_name

Methods included from Authorization::Product

#deletable?, #editable?, #readable?, #syncable?

Methods included from Glue

logger

Methods included from Glue::Candlepin::Product

custom_product_id?, engineering_product_id?, import_redhat_product_from_cp, included

Methods inherited from Model

#destroy!

Class Method Details

.enabledObject



79
80
81
82
# File 'app/models/katello/product.rb', line 79

def self.enabled
  self.where("#{Product.table_name}.id in (?) or #{Product.table_name}.id in (?)",
             Product.redhat.joins(:root_repositories => :repositories).select(:id), Product.custom.select(:id))
end

.find_by_cp_id(cp_id, organization = nil) ⇒ Object



54
55
56
57
58
# File 'app/models/katello/product.rb', line 54

def self.find_by_cp_id(cp_id, organization = nil)
  query = self.where(:cp_id => cp_id).readonly(false)
  query = query.in_org(organization) if organization
  query.first
end

.humanize_class_name(_name = nil) ⇒ Object



235
236
237
# File 'app/models/katello/product.rb', line 235

def self.humanize_class_name(_name = nil)
  _("Product and Repositories")
end

.in_org(organization) ⇒ Object



60
61
62
# File 'app/models/katello/product.rb', line 60

def self.in_org(organization)
  where(:organization_id => organization.id)
end

.in_orgs(organizations) ⇒ Object



64
65
66
# File 'app/models/katello/product.rb', line 64

def self.in_orgs(organizations)
  where(:organization_id => organizations)
end

.subscribableObject



73
74
75
76
77
# File 'app/models/katello/product.rb', line 73

def self.subscribable
  joins("LEFT OUTER JOIN #{Katello::RootRepository.table_name} repo ON repo.product_id = #{self.table_name}.id")
    .where("repo.content_type IN (?) OR repo IS NULL", RootRepository::SUBSCRIBABLE_TYPES)
    .group("#{self.table_name}.id, repo.product_id")
end

.unused_product_idObject



239
240
241
242
243
244
245
246
# File 'app/models/katello/product.rb', line 239

def self.unused_product_id
  id = (SecureRandom.random_number * (10**12 - 10**11) + 10**11).to_i # guarantee that we get 12 digits
  if ::Katello::Product.find_by(:cp_id => id)
    unused_product_id
  else
    id
  end
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'app/models/katello/product.rb', line 156

def anonymous?
  provider.anonymous_provider?
end

#assign_unique_labelObject



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

def assign_unique_label
  self.label = Util::Model.labelize(self.name) if self.label.blank?

  # if the object label is already being used in this org, append the id to make it unique
  if Product.all_in_org(self.organization).where("#{Katello::Product.table_name}.label = ?", self.label).count > 0
    self.label = self.label + "_" + self.cp_id unless self.cp_id.blank?
  end
end

#available_content(content_view_version_id = nil) ⇒ Object



206
207
208
209
210
# File 'app/models/katello/product.rb', line 206

def available_content(content_view_version_id = nil)
  root_repos = self.root_repositories.subscribable
  root_repos = root_repos.join(:repositories).where(:content_view_version_id => content_view_version_id) if content_view_version_id
  self.product_contents.joins(:content).where("#{Katello::Content.table_name}.cp_content_id" => root_repos.select(:content_id))
end

#cdn_resourceObject



220
221
222
223
224
225
226
227
# File 'app/models/katello/product.rb', line 220

def cdn_resource
  return if self.certificate.nil?

  @cdn_resource ||= ::Katello::Resources::CDN::CdnResource.create(
    product: self,
    cdn_configuration: self.organization.cdn_configuration
  )
end

#custom?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/katello/product.rb', line 141

def custom?
  !redhat?
end

#delete_from_env(from_env) ⇒ Object



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

def delete_from_env(from_env)
  @orchestration_for = :delete
  delete_repos(repos(from_env))
  if from_env.products.include? self
    self.environments.delete(from_env)
  end
  save!
end

#delete_repos(repos) ⇒ Object



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

def delete_repos(repos)
  repos.each { |repo| repo.destroy }
end

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  !self.provider.redhat_provider? || self.repositories.present?
end

#environmentsObject



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

def environments
  KTEnvironment.where(:organization_id => organization.id).
    where("library = ? OR id IN (?)", true, repositories.map(&:environment_id))
end

#environments_for_view(view) ⇒ Object



188
189
190
191
# File 'app/models/katello/product.rb', line 188

def environments_for_view(view)
  versions = view.versions.select { |version| version.products.include?(self) }
  versions.collect { |v| v.environments }.flatten
end

#library_repositoriesObject



50
51
52
# File 'app/models/katello/product.rb', line 50

def library_repositories
  self.repositories.in_default_view
end

#orphaned?Boolean

Returns:

  • (Boolean)


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

def orphaned?
  self.pool_products.empty?
end

#plan_nameObject



115
116
117
118
# File 'app/models/katello/product.rb', line 115

def plan_name
  return sync_plan.name if sync_plan
  N_('None')
end

#product_content_by_id(content_id) ⇒ Object



202
203
204
# File 'app/models/katello/product.rb', line 202

def product_content_by_id(content_id)
  product_contents.joins(:content).where("#{Katello::Content.table_name}.cp_content_id = ?", content_id).first
end

#published_content_view_versionsObject



150
151
152
153
154
# File 'app/models/katello/product.rb', line 150

def published_content_view_versions
  Katello::ContentViewVersion.joins(:content_view).joins(:repositories => :root).
      where("#{Katello::ContentView.table_name}.default" => false).
      where("#{Katello::RootRepository.table_name}.product_id" => self.id).order(:content_view_id)
end

#published_content_viewsObject



145
146
147
148
# File 'app/models/katello/product.rb', line 145

def published_content_views
  Katello::ContentView.non_default.joins(:content_view_versions => {:repositories => :root}).
      where("#{Katello::RootRepository.table_name}.product_id" => self.id)
end

#redhat?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/models/katello/product.rb', line 133

def redhat?
  provider.redhat_provider?
end


212
213
214
# File 'app/models/katello/product.rb', line 212

def related_resources
  self.provider
end

#repos(env, content_view = nil, include_feedless = true) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/katello/product.rb', line 90

def repos(env, content_view = nil, include_feedless = true)
  if content_view.nil?
    if !env.library?
      fail "No content view specified for the repos call in a " \
                      "Non library environment #{env.inspect}"
    else
      content_view = env.default_content_view
    end
  end

  # cache repos so we can cache lazy_accessors
  @repo_cache ||= {}
  @repo_cache[env.id] ||= content_view.repos_in_product(env, self)

  repositories = @repo_cache[env.id]
  repositories = repositories.has_url unless include_feedless
  repositories
end

#serializable_hash(options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/katello/product.rb', line 120

def serializable_hash(options = {})
  options = {} if options.nil?

  hash = super(options.merge(:except => [:cp_id, :id]))
  hash = hash.merge(:multiplier => self.multiplier,
                    :attributes => self.attrs,
                    :id => self.cp_id,
                    :sync_plan_name => self.sync_plan ? self.sync_plan.name : nil,
                    :sync_state => self.sync_state,
                    :last_sync => self.last_sync)
  hash
end

#syncable_content?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'app/models/katello/product.rb', line 198

def syncable_content?
  repositories.any?(&:url?)
end

#to_action_inputObject



216
217
218
# File 'app/models/katello/product.rb', line 216

def to_action_input
  super.merge(cp_id: cp_id)
end

#total_package_count(env, view) ⇒ Object



229
230
231
232
233
# File 'app/models/katello/product.rb', line 229

def total_package_count(env, view)
  repo_ids = view.repos(env).in_product(self).collect { |r| r.pulp_id }
  result = Katello::Package.legacy_search('*', 0, 1, repo_ids)
  result.length > 0 ? result.total : 0
end

#used_by_another_org?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/models/katello/product.rb', line 160

def used_by_another_org?
  self.class.where(["cp_id = ? AND id != ?", cp_id, id]).count > 0
end

#user_deletable?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/models/katello/product.rb', line 137

def user_deletable?
  self.published_content_views.empty? && !self.redhat?
end