Module: Glue::Candlepin::Pool::InstanceMethods

Defined in:
app/models/katello/glue/candlepin/pool.rb

Instance Method Summary collapse

Instance Method Details

#create_activation_key_associationsObject



227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/models/katello/glue/candlepin/pool.rb', line 227

def create_activation_key_associations
  keys = Rails.cache.fetch("#{organization.label}/activation_keys_id_pool_id", expires_in: 2.minutes) do
    Resources::Candlepin::ActivationKey.get(nil, "?include=id&include=pools.pool.id", organization.label)
  end
  activation_key_ids = keys.collect do |key|
    key['id'] if key['pools'].present? && key['pools'].any? { |pool| pool['pool'].try(:[], 'id') == cp_id }
  end
  related_keys = ::Katello::ActivationKey.where(:cp_id => activation_key_ids.compact)
  related_keys.each do |key|
    Katello::PoolActivationKey.where(:activation_key_id => key.id, :pool_id => self.id).first_or_create
  end
end

#create_product_associationsObject

rubocop:enable Metrics/MethodLength,Metrics/AbcSize



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/katello/glue/candlepin/pool.rb', line 180

def create_product_associations
  products = self.backend_data["providedProducts"] + self.backend_data["derivedProvidedProducts"]
  cp_product_ids = products.map { |product| product["productId"] }
  cp_product_ids << self.subscription.cp_id if self.subscription

  cp_product_ids.each do |cp_id|
    product = ::Katello::Product.where(:cp_id => cp_id, :organization_id => self.organization.id)
    if product.any?
      ::Katello::Util::Support.active_record_retry do
        ::Katello::PoolProduct.where(:pool_id => self.id, :product_id => product.first.id).first_or_create
      end
    end
  end
end

#hostsObject



223
224
225
# File 'app/models/katello/glue/candlepin/pool.rb', line 223

def hosts
  ::Host.where(:id => self.subscription_facets.pluck(:host_id))
end

#import_data(index_hosts_and_activation_keys = false) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity



134
135
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/katello/glue/candlepin/pool.rb', line 134

def import_data(index_hosts_and_activation_keys = false)
  pool_attributes = {}.with_indifferent_access
  pool_json = self.backend_data

  product_attributes = pool_json["productAttributes"] + pool_json["attributes"]
  product_attributes.map { |attr| pool_attributes[attr["name"].underscore.to_sym] = attr["value"] }

  %w(accountNumber contractNumber quantity startDate endDate accountNumber consumed).each do |json_attribute|
    pool_attributes[json_attribute.underscore] = pool_json[json_attribute]
  end
  pool_attributes[:pool_type] = pool_json["type"] if pool_json.key?("type")
  pool_attributes[:upstream_pool_id] = pool_json["upstreamPoolId"] if pool_json.key?("upstreamPoolId")

  if pool_attributes.key?(:multi_entitlement)
    pool_attributes[:multi_entitlement] = pool_attributes[:multi_entitlement] == "yes"
  end

  if pool_attributes.key?(:virtual)
    pool_attributes[:virt_only] = pool_attributes["virtual"] == 'true'
  end

  if pool_attributes.key?("requires_host")
    pool_attributes[:hypervisor_id] = ::Katello::Host::SubscriptionFacet.find_by(:uuid => pool_attributes["requires_host"])
                                                                        .try(:host_id)
  end

  if pool_attributes.key?(:unmapped_guests_only) && pool_attributes[:unmapped_guests_only] == 'true'
    pool_attributes[:unmapped_guest] = true
  end

  if subscription.try(:redhat?)
    pool_attributes[:virt_who] = pool_attributes['virt_limit'] != "0" && pool_attributes['virt_limit'].present?
  else
    pool_attributes[:virt_who] = false
  end

  pool_attributes['stack_id'] = pool_json['stackId']
  exceptions = pool_attributes.keys.map(&:to_sym) - self.attribute_names.map(&:to_sym)
  self.update(pool_attributes.except!(*exceptions))
  self.save!
  self.create_activation_key_associations if index_hosts_and_activation_keys
  self.create_product_associations
  self.import_hosts if index_hosts_and_activation_keys
end

#import_hostsObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/models/katello/glue/candlepin/pool.rb', line 195

def import_hosts
  sub_facet_ids_from_cp, host_ids_from_cp = Katello::Host::SubscriptionFacet.where('uuid in (?)', consumer_uuids).pluck(:id, :host_id).transpose
  sub_facet_ids_from_cp ||= []
  host_ids_from_cp ||= []

  sub_facet_ids_from_pool_table, host_ids_from_pool_table = self.subscription_facets.pluck(:id, :host_id).transpose
  sub_facet_ids_from_pool_table ||= []
  host_ids_from_pool_table ||= []

  entries_to_add = sub_facet_ids_from_cp - sub_facet_ids_from_pool_table
  facet_pool_data = entries_to_add.map { |sub_facet_id| { pool_id: self.id, subscription_facet_id: sub_facet_id } }
  Katello::SubscriptionFacetPool.import(facet_pool_data) unless facet_pool_data.empty?

  entries_to_remove = sub_facet_ids_from_pool_table - sub_facet_ids_from_cp
  self.subscription_facet_pools.where(subscription_facet_id: entries_to_remove).delete_all
  self.import_audit_record(host_ids_from_pool_table, host_ids_from_cp)
end

#import_lazy_attributesObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/katello/glue/candlepin/pool.rb', line 97

def import_lazy_attributes
  json = self.class.candlepin_data(self.cp_id, true)

  return {} if json.blank?

  pool_attributes = json["attributes"] + json["productAttributes"]
  json["virt_only"] = false
  pool_attributes.each do |attr|
    json[attr["name"]] = attr["value"]
    case attr["name"]
    when 'virt_limit'
      json["virt_limit"] = attr['value'].to_i
    when 'support_type'
      json['support_type'] = attr['value']
    end
  end

  json["calculatedAttributes"]&.each do |key|
    json["suggested_quantity"] = json["calculatedAttributes"]["suggested_quantity"].to_i if key == 'suggested_quantity'
  end

  json["product_id"] = json["productId"] if json["productId"]
  json["upstream_entitlement_id"] = json["upstreamEntitlementId"]

  subscription.backend_data["attributes"].map { |attr| json[attr["name"].underscore.to_sym] = attr["value"] }
  json
end

#import_managed_associationsObject



213
214
215
216
217
218
219
220
221
# File 'app/models/katello/glue/candlepin/pool.rb', line 213

def import_managed_associations
  Katello::Logging.time("Imported host associations") do
    import_hosts
  end

  Katello::Logging.time("Imported activation key associations") do
    create_activation_key_associations
  end
end

#provider?(organization) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'app/models/katello/glue/candlepin/pool.rb', line 125

def provider?(organization)
  providers = self.subscription.products.collect do |provider|
    Katello::Provider.where(:id => provider.provider_id, :organization_id => organization.id).first
  end
  providers.any?
end