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



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

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
10
# File 'app/lib/katello/resources/candlepin/product.rb', line 6

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

.certificate(id, owner) ⇒ Object



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

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

.create(owner_label, attr) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

.path(owner_label, id = nil) ⇒ Object



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

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

.pools(owner_key, product_id) ⇒ Object



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

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



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

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



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

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



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

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