Class: Katello::Resources::Candlepin::ActivationKey

Inherits:
CandlepinResource show all
Defined in:
app/lib/katello/resources/candlepin/activation_key.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_pools(id, pool_id, quantity) ⇒ Object



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

def add_pools(id, pool_id, quantity)
  cppath = join_path(path(id), "pools/#{pool_id}")
  quantity = Integer(quantity) rescue nil
  cppath += "?quantity=#{quantity}" if quantity && quantity > 0
  pool = self.post(cppath, {}, self.default_headers)
  JSON.parse(pool).with_indifferent_access
end

.add_product(id, product_id) ⇒ Object



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

def add_product(id, product_id)
  cppath = join_path(path(id), "product/#{product_id}")
  product = self.post(cppath, {}, self.default_headers)
  JSON.parse(product).with_indifferent_access
end

.content_overrides(id) ⇒ Object



73
74
75
76
# File 'app/lib/katello/resources/candlepin/activation_key.rb', line 73

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(name, owner_key, auto_attach, service_level, release_version, purpose_role, purpose_usage, purpose_addons) ⇒ Object

rubocop:disable Metrics/ParameterLists



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

def create(name, owner_key, auto_attach, service_level, release_version, purpose_role, purpose_usage, purpose_addons)
  url = "/candlepin/owners/#{owner_key}/activation_keys"
  params = {
    name: name,
    autoAttach: auto_attach,
    serviceLevel: service_level,
    releaseVer: release_version,
    role: purpose_role,
    usage: purpose_usage,
    addOns: purpose_addons
  }
  response = self.post(url, params.to_json, self.default_headers)
  JSON.parse(response.body).with_indifferent_access
end

.destroy(id) ⇒ Object



34
35
36
37
# File 'app/lib/katello/resources/candlepin/activation_key.rb', line 34

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

.get(id = nil, params = '', owner = nil) ⇒ Object



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

def get(id = nil, params = '', owner = nil)
  akeys_json = super(path(id, owner) + params, self.default_headers).body
  akeys = JSON.parse(akeys_json)
  akeys = [akeys] unless id.nil?
  ::Katello::Util::Data.array_with_indifferent_access akeys
end

.key_pools(id) ⇒ Object



43
44
45
46
47
# File 'app/lib/katello/resources/candlepin/activation_key.rb', line 43

def key_pools(id)
  kp_json = Candlepin::CandlepinResource.get(join_path(path(id), "pools"), self.default_headers).body
  key_pools = JSON.parse(kp_json)
  ::Katello::Util::Data.array_with_indifferent_access key_pools
end

.path(id = nil, owner_id = nil) ⇒ Object



105
106
107
108
109
110
111
# File 'app/lib/katello/resources/candlepin/activation_key.rb', line 105

def path(id = nil, owner_id = nil)
  if owner_id
    "/candlepin/owners/#{owner_id}/activation_keys/#{id}"
  else
    "/candlepin/activation_keys/#{id}"
  end
end

.pools(owner_key) ⇒ Object



39
40
41
# File 'app/lib/katello/resources/candlepin/activation_key.rb', line 39

def pools(owner_key)
  Candlepin::Owner.pools(owner_key)
end

.remove_pools(id, pool_id) ⇒ Object



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

def remove_pools(id, pool_id)
  pool = self.delete(join_path(path(id), "pools/#{pool_id}"), self.default_headers)
  JSON.parse(pool).with_indifferent_access
end

.remove_product(id, product_id) ⇒ Object



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

def remove_product(id, product_id)
  product = self.delete(join_path(path(id), "product/#{product_id}"), self.default_headers)
  JSON.parse(product).with_indifferent_access
end

.update(id, release_version, service_level, auto_attach, purpose_role, purpose_usage, purpose_addons) ⇒ Object



29
30
31
32
# File 'app/lib/katello/resources/candlepin/activation_key.rb', line 29

def update(id, release_version, service_level, auto_attach, purpose_role, purpose_usage, purpose_addons)
  attrs = { :releaseVer => release_version, :serviceLevel => service_level, :autoAttach => auto_attach, :role => purpose_role, :usage => purpose_usage, :addOns => purpose_addons }.delete_if { |_k, v| v.nil? }
  JSON.parse(self.put(path(id), attrs.to_json, self.default_headers).body).with_indifferent_access
end

.update_content_overrides(id, content_overrides) ⇒ Object

expected params id : ID of the Activation Key content_overrides => Array of content override hashes



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/lib/katello/resources/candlepin/activation_key.rb', line 81

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