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

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

Instance Attribute Summary

Attributes inherited from HttpResource

#json

Class Method Summary collapse

Methods inherited from CandlepinResource

default_headers, logger, name_to_key

Methods inherited from HttpResource

#[], #[]=, create_thing, delete, hash_to_query, #initialize, join_path, logger, post, print_debug_info, process_response, put, raise_rest_client_exception, rest_client, url_encode

Constructor Details

This class inherits a constructor from Katello::HttpResource

Class Method Details

.available_pools(uuid, listall = false) ⇒ Object



148
149
150
151
152
# File 'app/lib/katello/resources/candlepin.rb', line 148

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

.checkin(uuid, checkin_date) ⇒ Object



143
144
145
146
# File 'app/lib/katello/resources/candlepin.rb', line 143

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

.compliance(uuid) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/lib/katello/resources/candlepin.rb', line 216

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



176
177
178
179
180
181
# File 'app/lib/katello/resources/candlepin.rb', line 176

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



240
241
242
243
# File 'app/lib/katello/resources/candlepin.rb', line 240

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_id, parameters, activation_key_cp_ids) ⇒ Object



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

def create(env_id, parameters, activation_key_cp_ids)
  parameters['installedProducts'] ||= [] #if installed products is nil, candlepin won't attach custom products
  url = "/candlepin/environments/#{url_encode(env_id)}/consumers/"
  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



134
135
136
# File 'app/lib/katello/resources/candlepin.rb', line 134

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

.entitlements(uuid) ⇒ Object



167
168
169
170
# File 'app/lib/katello/resources/candlepin.rb', line 167

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

.events(uuid) ⇒ Object



231
232
233
234
235
236
237
238
# File 'app/lib/katello/resources/candlepin.rb', line 231

def events(uuid)
  response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'events'), self.default_headers).body
  if response.present?
    ::Katello::Util::Data.array_with_indifferent_access JSON.parse(response)
  else
    return []
  end
end

.export(uuid) ⇒ Object



159
160
161
162
163
164
165
# File 'app/lib/katello/resources/candlepin.rb', line 159

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

.get(params) ⇒ Object



98
99
100
101
102
103
104
105
# File 'app/lib/katello/resources/candlepin.rb', line 98

def get(params)
  if params.is_a?(String)
    JSON.parse(super(path(params), self.default_headers).body).with_indifferent_access
  else
    response = super(path + hash_to_query(params), self.default_headers).body
    JSON.parse(response)
  end
end

.path(id = nil) ⇒ Object



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

def path(id = nil)
  "/candlepin/consumers/#{id}"
end

.refresh_entitlements(uuid) ⇒ Object



172
173
174
# File 'app/lib/katello/resources/candlepin.rb', line 172

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

.regenerate_identity_certificates(uuid) ⇒ Object



154
155
156
157
# File 'app/lib/katello/resources/candlepin.rb', line 154

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



116
117
118
119
120
121
122
# File 'app/lib/katello/resources/candlepin.rb', line 116

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_certificate(uuid, serial_id) ⇒ Object



193
194
195
196
# File 'app/lib/katello/resources/candlepin.rb', line 193

def remove_certificate(uuid, serial_id)
  uri = join_path(path(uuid), 'certificates') + "/#{serial_id}"
  self.delete(uri, self.default_headers).code.to_i
end

.remove_entitlement(uuid, ent_id) ⇒ Object



183
184
185
186
# File 'app/lib/katello/resources/candlepin.rb', line 183

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

.remove_entitlements(uuid) ⇒ Object



188
189
190
191
# File 'app/lib/katello/resources/candlepin.rb', line 188

def remove_entitlements(uuid)
  uri = join_path(path(uuid), 'entitlements')
  self.delete(uri, self.default_headers).code.to_i
end

.serials(uuid) ⇒ Object



138
139
140
141
# File 'app/lib/katello/resources/candlepin.rb', line 138

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

.update(uuid, params) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'app/lib/katello/resources/candlepin.rb', line 124

def update(uuid, params)
  if params.empty?
    true
  else
    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_override(id, content_label, name, value = nil) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/lib/katello/resources/candlepin.rb', line 245

def update_content_override(id, , name, value = nil)
  attrs = [{ :contentLabel => , :name => name }]
  if value
    attrs[0][:value] = value
    result = Candlepin::CandlepinResource.put(join_path(path(id), 'content_overrides'),
                                              attrs.to_json, self.default_headers)
  else
    client = Candlepin::CandlepinResource.rest_client(Net::HTTP::Delete, :delete,
                                                      join_path(path(id), 'content_overrides'))
    client.options[:payload] = attrs.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



198
199
200
201
202
203
# File 'app/lib/katello/resources/candlepin.rb', line 198

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
  return []
end

.virtual_host(uuid) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'app/lib/katello/resources/candlepin.rb', line 205

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
  return nil
end