Class: Katello::Product

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

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

included, logger

Methods inherited from Model

#destroy!

Constructor Details

#initialize(attrs = nil, options = {}) ⇒ Product

Returns a new instance of Product.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/katello/product.rb', line 67

def initialize(attrs = nil, options = {})
  unless attrs.nil?
    attrs = attrs.with_indifferent_access

    #rename "id" to "cp_id" (activerecord and candlepin variable name conflict)
    if attrs.key?(:id)
      unless attrs.key?(:cp_id)
        attrs[:cp_id] = attrs[:id]
      end
      attrs.delete(:id)
    end

    # ugh. hack-ish. otherwise we have to modify code every time things change on cp side
    attrs = attrs.reject do |k, _v|
      !self.class.column_defaults.keys.member?(k.to_s) && (!respond_to?(:"#{k.to_s}=") rescue true)
    end
  end

  super
end

Class Method Details

.enabledObject



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

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

.find_by_cp_id(cp_id, organization = nil) ⇒ Object



42
43
44
45
46
# File 'app/models/katello/product.rb', line 42

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



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

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

.in_org(organization) ⇒ Object



48
49
50
# File 'app/models/katello/product.rb', line 48

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

.in_orgs(organizations) ⇒ Object



52
53
54
# File 'app/models/katello/product.rb', line 52

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

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


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

def anonymous?
  provider.anonymous_provider?
end

#assign_unique_labelObject



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

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_contentObject



205
206
207
# File 'app/models/katello/product.rb', line 205

def available_content
  self.productContent.find_all { |content| self.repositories.where(:content_id => content.content.id).any? }
end

#cdn_resourceObject



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

def cdn_resource
  return unless (product_certificate = certificate)
  certs = { :ssl_client_cert => OpenSSL::X509::Certificate.new(product_certificate),
            :ssl_client_key => OpenSSL::PKey::RSA.new(key) }
  ::Katello::Resources::CDN::CdnResource.new(provider.repository_url, certs)
end

#custom?Boolean

Returns:

  • (Boolean)


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

def custom?
  provider.custom_provider?
end

#delete_from_env(from_env) ⇒ Object



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

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



178
179
180
# File 'app/models/katello/product.rb', line 178

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#environmentsObject



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

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

#environments_for_view(view) ⇒ Object



191
192
193
194
# File 'app/models/katello/product.rb', line 191

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

#gpg_key_name=(name) ⇒ Object



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

def gpg_key_name=(name)
  if name.blank?
    self.gpg_key = nil
  else
    self.gpg_key = GpgKey.readable.find_by!(:name => name)
  end
end

#library_repositoriesObject



38
39
40
# File 'app/models/katello/product.rb', line 38

def library_repositories
  self.repositories.in_default_view
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

#published_content_viewsObject



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

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

#redhat?Boolean

Returns:

  • (Boolean)


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

def redhat?
  provider.redhat_provider?
end


209
210
211
# File 'app/models/katello/product.rb', line 209

def related_resources
  self.provider
end

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



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

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
132
# 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(:productContent => self.productContent,
                    :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)


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

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

#to_action_inputObject



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

def to_action_input
  super.merge(cp_id: cp_id)
end

#total_package_count(env, view) ⇒ Object



224
225
226
227
228
# File 'app/models/katello/product.rb', line 224

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

#total_puppet_module_count(env, view) ⇒ Object



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

def total_puppet_module_count(env, view)
  repo_ids = view.repos(env).in_product(self).collect { |r| r.pulp_id }
  results = Katello::PuppetModule.legacy_search('', :page_size => 1, :repoids => repo_ids)
  results.empty? ? 0 : results.total
end

#used_by_another_org?Boolean

Returns:

  • (Boolean)


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

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

#user_deletable?Boolean

Returns:

  • (Boolean)


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

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