Class: Katello::Resources::Candlepin::Consumer

Inherits:
CandlepinResource show all
Extended by:
ConsumerResource
Defined in:
app/lib/katello/resources/candlepin/consumer.rb

Constant Summary

Constants inherited from HttpResource

HttpResource::REQUEST_MAP

Instance Attribute Summary

Attributes inherited from HttpResource

#json

Class Method Summary collapse

Methods included from ConsumerResource

path

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

.all_uuidsObject



8
9
10
11
12
13
14
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 8

def all_uuids
  cp_consumers = Organization.all.map do |org|
    ::Katello::Resources::Candlepin::Consumer.get('owner' => org.label, :include_only => [:uuid])
  end
  cp_consumers.flatten!
  cp_consumers.map { |consumer| consumer["uuid"] }
end

.async_hypervisors(owner:, reporter_id:, raw_json:) ⇒ Object



47
48
49
50
51
52
53
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 47

def async_hypervisors(owner:, reporter_id:, raw_json:)
  url = "/candlepin/hypervisors/#{owner}?reporter_id=#{reporter_id}"
  headers = self.default_headers
  headers['content-type'] = 'text/plain'
  response = self.post(url, raw_json, headers)
  JSON.parse(response).with_indifferent_access
end

.available_pools(owner_label, uuid, listall: false) ⇒ Object



96
97
98
99
100
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 96

def available_pools(owner_label, uuid, listall: false)
  url = Resources::Candlepin::Pool.path(nil, owner_label) + "?consumer=#{uuid}&listall=#{listall}&add_future=true"
  response = Candlepin::CandlepinResource.get(url, self.default_headers).body
  JSON.parse(response)
end

.checkin(uuid, checkin_date) ⇒ Object



91
92
93
94
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 91

def checkin(uuid, checkin_date)
  checkin_date ||= Time.now
  self.put(path(uuid), {:lastCheckin => checkin_date}.to_json, self.default_headers).body
end

.compliance(uuid) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 153

def compliance(uuid)
  response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'compliance'), self.default_headers(uuid)).body
  if response.present?
    json = JSON.parse(response).with_indifferent_access
    if json['reasons']
      json['reasons'].sort! { |x, y| x['attributes']['name'] <=> y['attributes']['name'] }
    else
      json['reasons'] = []
    end
    json
  else
    return nil
  end
end

.consume_entitlement(uuid, pool, quantity = nil) ⇒ Object



123
124
125
126
127
128
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 123

def consume_entitlement(uuid, pool, quantity = nil)
  uri = join_path(path(uuid), 'entitlements') + "?pool=#{pool}"
  uri += "&quantity=#{quantity}" if quantity && quantity > 0
  response = self.post(uri, "", self.default_headers).body
  response.blank? ? [] : JSON.parse(response)
end

.content_overrides(id) ⇒ Object



175
176
177
178
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 175

def content_overrides(id)
  result = Candlepin::CandlepinResource.get(join_path(path(id), 'content_overrides'), self.default_headers).body
  ::Katello::Util::Data.array_with_indifferent_access(JSON.parse(result))
end

.create(env_ids, parameters, activation_key_cp_ids, org) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 37

def create(env_ids, parameters, activation_key_cp_ids, org)
  parameters['installedProducts'] ||= [] #if installed products is nil, candlepin won't attach custom products
  parameters['environments'] = env_ids.map { |cp_id| { id: cp_id } }
  url = "/candlepin/consumers/?owner=#{org.label}"
  url += "&activation_keys=" + activation_key_cp_ids.join(",") if activation_key_cp_ids.length > 0

  response = self.post(url, parameters.to_json, self.default_headers).body
  JSON.parse(response).with_indifferent_access
end

.destroy(uuid) ⇒ Object



82
83
84
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 82

def destroy(uuid)
  self.delete(path(uuid), User.cp_oauth_header).code.to_i
end

.entitlements(uuid) ⇒ Object



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

def entitlements(uuid)
  response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'entitlements'), self.default_headers).body
  ::Katello::Util::Data.array_with_indifferent_access JSON.parse(response)
end

.export(uuid) ⇒ Object



107
108
109
110
111
112
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 107

def export(uuid)
  # Export is a zip file
  headers = self.default_headers
  headers['accept'] = 'application/zip'
  Candlepin::CandlepinResource.get(join_path(path(uuid), 'export'), headers)
end

.get(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 16

def get(params)
  if params.is_a?(String)
    JSON.parse(super(path(params), self.default_headers).body).with_indifferent_access
  else
    includes = params.key?(:include_only) ? "&" + included_list(params.delete(:include_only)) : ""
    fetch_paged do |page_add|
      response = super(path + hash_to_query(params) + includes + "&#{page_add}", self.default_headers).body
      JSON.parse(response).map(&:with_indifferent_access)
    end
  end
end

.get_all_with_facts(uuids) ⇒ Object



29
30
31
32
33
34
35
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 29

def get_all_with_facts(uuids)
  consumers = []
  uuids.each do |uuid|
    consumers << get(uuid)
  end
  consumers
end

.hypervisors_heartbeat(owner:, reporter_id:) ⇒ Object



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

def hypervisors_heartbeat(owner:, reporter_id:)
  url = "/candlepin/hypervisors/#{owner}/heartbeat?reporter_id=#{reporter_id}"
  response = self.put(url, {}.to_json, self.default_headers).body
  JSON.parse(response).with_indifferent_access
end

.purpose_compliance(uuid) ⇒ Object



168
169
170
171
172
173
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 168

def purpose_compliance(uuid)
  response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'purpose_compliance'), self.default_headers(uuid)).body
  if response.present?
    JSON.parse(response).with_indifferent_access
  end
end

.refresh_entitlements(uuid) ⇒ Object



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

def refresh_entitlements(uuid)
  self.post(join_path(path(uuid), 'entitlements'), "", self.default_headers).body
end

.regenerate_identity_certificates(uuid) ⇒ Object



102
103
104
105
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 102

def regenerate_identity_certificates(uuid)
  response = self.post(path(uuid), {}, self.default_headers).body
  JSON.parse(response).with_indifferent_access
end

.register_hypervisors(params) ⇒ Object



61
62
63
64
65
66
67
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 61

def register_hypervisors(params)
  url = "/candlepin/hypervisors"
  url << "?owner=#{params[:owner]}&env=#{params[:env]}"
  attrs = params.except(:owner, :env)
  response = self.post(url, attrs.to_json, self.default_headers).body
  JSON.parse(response).with_indifferent_access
end

.remove_entitlement(uuid, ent_id) ⇒ Object



130
131
132
133
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 130

def remove_entitlement(uuid, ent_id)
  uri = join_path(path(uuid), 'entitlements') + "/#{ent_id}"
  self.delete(uri, self.default_headers).code.to_i
end

.serials(uuid) ⇒ Object



86
87
88
89
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 86

def serials(uuid)
  response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'certificates/serials'))
  JSON.parse(response.body)
end

.update(uuid, params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 69

def update(uuid, params)
  if params.empty?
    true
  else
    if params.key?(:environment) && params[:environment].key?(:id)
      params[:environments] = [{"id": params[:environment][:id]}]
    end
    self.put(path(uuid), params.to_json, self.default_headers).body
  end
  # consumer update doesn't return any data atm
  # JSON.parse(response).with_indifferent_access
end

.update_content_overrides(id, content_overrides) ⇒ Object

expected params id : UUID of the consumer content_overrides => Array of entitlement hashes objects



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 183

def update_content_overrides(id, content_overrides)
  attrs_to_delete = []
  attrs_to_update = []
  content_overrides.each do |content_override|
    if content_override[:value]
      attrs_to_update << content_override
    else
      attrs_to_delete << content_override
    end
  end

  if attrs_to_update.present?
    result = Candlepin::CandlepinResource.put(join_path(path(id), 'content_overrides'),
                                              attrs_to_update.to_json, self.default_headers)
  end
  if attrs_to_delete.present?
    client = Candlepin::CandlepinResource.rest_client(Net::HTTP::Delete, :delete,
                                                      join_path(path(id), 'content_overrides'))
    client.options[:payload] = attrs_to_delete.to_json
    result = client.delete({:accept => :json, :content_type => :json}.merge(User.cp_oauth_header))
  end
  ::Katello::Util::Data.array_with_indifferent_access(JSON.parse(result))
end

.virtual_guests(uuid) ⇒ Object



135
136
137
138
139
140
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 135

def virtual_guests(uuid)
  response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'guests'), self.default_headers).body
  ::Katello::Util::Data.array_with_indifferent_access JSON.parse(response)
rescue RestClient::Exception
  return []
end

.virtual_host(uuid) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'app/lib/katello/resources/candlepin/consumer.rb', line 142

def virtual_host(uuid)
  response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'host'), self.default_headers).body
  if response.present?
    JSON.parse(response).with_indifferent_access
  else
    return nil
  end
rescue RestClient::Exception
  return nil
end