Class: Katello::Resources::Candlepin::Product

Inherits:
CandlepinResource show all
Defined in:
app/lib/katello/resources/candlepin/product.rb

Constant Summary

Constants inherited from HttpResource

HttpResource::REQUEST_MAP

Instance Attribute Summary

Attributes inherited from HttpResource

#json

Class Method Summary collapse

Methods inherited from CandlepinResource

default_headers, fetch_paged, included_list, logger, name_to_key, process_response, raise_rest_client_exception

Methods inherited from HttpResource

#[], #[]=, hash_to_query, #initialize, issue_request, join_path, logger, process_response, raise_rest_client_exception, rest_client

Methods included from Concerns::FilterSensitiveData

#filter_sensitive_data

Constructor Details

This class inherits a constructor from Katello::HttpResource

Class Method Details

.add_content(owner_label, product_id, content_id, enabled) ⇒ Object



65
66
67
# File 'app/lib/katello/resources/candlepin/product.rb', line 65

def add_content(owner_label, product_id, content_id, enabled)
  self.post(join_path(path(owner_label, product_id), "content/#{content_id}?enabled=#{enabled}"), nil, self.default_headers).code.to_i
end

.all(owner_label, included = []) ⇒ Object



6
7
8
9
# File 'app/lib/katello/resources/candlepin/product.rb', line 6

def all(owner_label, included = [])
  products = JSON.parse(Candlepin::CandlepinResource.get(path(owner_label) + "?#{included_list(included)}", self.default_headers).body)
  ::Katello::Util::Data.array_with_indifferent_access products
end

.certificate(id, owner) ⇒ Object



52
53
54
# File 'app/lib/katello/resources/candlepin/product.rb', line 52

def certificate(id, owner)
  self.product_certificate(id, owner).try :[], 'cert'
end

.create(owner_label, attr) ⇒ Object



20
21
22
# File 'app/lib/katello/resources/candlepin/product.rb', line 20

def create(owner_label, attr)
  JSON.parse(self.post(path(owner_label), attr.to_json, self.default_headers).body).with_indifferent_access
end

.create_unlimited_subscription(owner_key, product_id, start_date) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/lib/katello/resources/candlepin/product.rb', line 73

def create_unlimited_subscription(owner_key, product_id, start_date)
  start_date ||= Time.now

  # Subscription-manager (python-rhsm) can't read the certificate with end date beyond
  # 2049 year correctly. Refer the links below for more details:
  # https://bugzilla.redhat.com/show_bug.cgi?id=1789654
  # https://github.com/candlepin/candlepin/blob/5b87865f304555c112982af4fbc83a1c463d37b2/server/src/main/java/org/candlepin/model/UeberCertificateGenerator.java#L247
  end_date = Time.parse('2049-12-01 00:00:00 +0000')

  pool = {
    'startDate' => start_date,
    'endDate' => end_date,
    'quantity' => -1,
    'accountNumber' => '',
    'productId' => product_id,
    'providedProducts' => [],
    'contractNumber' => ''
  }
  JSON.parse(Candlepin::Pool.create(owner_key, pool))
end

.delete_subscriptions(owner_key, product_id) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/lib/katello/resources/candlepin/product.rb', line 98

def delete_subscriptions(owner_key, product_id)
  update_subscriptions = false
  subscriptions = Candlepin::Subscription.get_for_owner owner_key
  subscriptions.each do |s|
    products = ([s['product']] + s['providedProducts'])
    products.each do |p|
      if p['id'] == product_id
        logger.debug "Deleting subscription: " + s.to_json
        Candlepin::Subscription.destroy s['id']
        update_subscriptions = true
      end
    end
  end
  nil
end

.destroy(owner_label, product_id) ⇒ Object



60
61
62
63
# File 'app/lib/katello/resources/candlepin/product.rb', line 60

def destroy(owner_label, product_id)
  fail ArgumentError, "product id has to be specified" unless product_id
  self.delete(path(owner_label, product_id), self.default_headers).code.to_i
end

.find_for_stacking_id(owner_key, stacking_id) ⇒ Object



11
12
13
14
15
16
17
18
# File 'app/lib/katello/resources/candlepin/product.rb', line 11

def find_for_stacking_id(owner_key, stacking_id)
  Resources::Candlepin::Subscription.get_for_owner(owner_key).each do |subscription|
    if subscription['product']['attributes'].any? { |attr| attr['name'] == 'stacking_id' && attr['value'] == stacking_id }
      return subscription['product']
    end
  end
  nil
end

.get(owner_label, id = nil, included = []) ⇒ Object



24
25
26
27
28
29
# File 'app/lib/katello/resources/candlepin/product.rb', line 24

def get(owner_label, id = nil, included = [])
  products_json = super(path(owner_label, id + "/?#{included_list(included)}"), self.default_headers).body
  products = JSON.parse(products_json)
  products = [products] unless id.nil?
  ::Katello::Util::Data.array_with_indifferent_access products
end

.key(id, owner) ⇒ Object



56
57
58
# File 'app/lib/katello/resources/candlepin/product.rb', line 56

def key(id, owner)
  self.product_certificate(id, owner).try :[], 'key'
end

.path(owner_label, id = nil) ⇒ Object



114
115
116
# File 'app/lib/katello/resources/candlepin/product.rb', line 114

def path(owner_label, id = nil)
  "/candlepin/owners/#{owner_label}/products/#{id}"
end

.pools(owner_key, product_id) ⇒ Object



94
95
96
# File 'app/lib/katello/resources/candlepin/product.rb', line 94

def pools(owner_key, product_id)
  Candlepin::Pool.get_for_owner(owner_key).find_all { |pool| pool['productId'] == product_id }
end

.product_certificate(id, owner) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/lib/katello/resources/candlepin/product.rb', line 31

def product_certificate(id, owner)
  included = %w(certificate product.id providedProducts.id
                derivedProvidedProducts.id startDate)
  subscriptions_json = Candlepin::CandlepinResource.get(
    "/candlepin/owners/#{owner}/subscriptions?#{included_list(included)}",
    self.default_headers
  ).body
  subscriptions = JSON.parse(subscriptions_json)

  product_subscription = subscriptions.find do |sub|
    sub['certificate'] && Time.parse(sub['startDate']) < Time.now &&
    (sub["product"]["id"] == id ||
      sub["providedProducts"].any? { |provided| provided["id"] == id } ||
      sub["derivedProvidedProducts"].any? { |provided| provided["id"] == id })
  end

  if product_subscription
    return product_subscription["certificate"]
  end
end

.remove_content(owner_label, product_id, content_id) ⇒ Object



69
70
71
# File 'app/lib/katello/resources/candlepin/product.rb', line 69

def remove_content(owner_label, product_id, content_id)
  self.delete(join_path(path(owner_label, product_id), "content/#{content_id}"), self.default_headers).code.to_i
end

.update(owner_label, attrs) ⇒ Object



118
119
120
# File 'app/lib/katello/resources/candlepin/product.rb', line 118

def update(owner_label, attrs)
  JSON.parse(self.put(path(owner_label, attrs[:id] || attrs['id']), JSON.generate(attrs), self.default_headers).body).with_indifferent_access
end