Class: Katello::UpstreamPool

Inherits:
OpenStruct
  • Object
show all
Defined in:
app/models/katello/upstream_pool.rb

Constant Summary collapse

CP_POOL =
Resources::Candlepin::UpstreamPool

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_fieldsObject



81
82
83
# File 'app/models/katello/upstream_pool.rb', line 81

def all_fields
  kat_to_cp_map.values
end

.base_params(params) ⇒ Object



31
32
33
34
35
36
37
# File 'app/models/katello/upstream_pool.rb', line 31

def base_params(params)
  attachable = ::Foreman::Cast.to_bool(params.delete(:attachable))
  params[:consumer] = upstream_consumer_id if attachable
  params[:sort_by] = 'Product.name'
  params[:order] = 'asc'
  params
end

.fetch_pools(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/katello/upstream_pool.rb', line 6

def fetch_pools(params)
  quantities_only = ::Foreman::Cast.to_bool(params.delete(:quantities_only))
  pool_id_map = Katello::Candlepin::PoolService.upstream_pool_id_map(params.delete(:pool_ids))

  cp_params = request_params(
    base_params: base_params(params),
    extra_params: pool_id_params(pool_id_map.keys),
    included_fields: included_field_params(quantities_only)
  )

  upstream_response = CP_POOL.get(params: cp_params)
  pools = response_to_pools(upstream_response, pool_id_map: pool_id_map)
  total = upstream_response.headers[total_count_header] || pools.count

  respond(pools, total)
end

.included_field_params(quantities_only) ⇒ Object



96
97
98
# File 'app/models/katello/upstream_pool.rb', line 96

def included_field_params(quantities_only)
  quantities_only ? minimal_fields : all_fields
end

.kat_to_cp_mapObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/katello/upstream_pool.rb', line 51

def kat_to_cp_map
  {
    id: 'id',
    active: 'activeSubscription',
    quantity: 'quantity',
    start_date: 'startDate',
    end_date: 'endDate',
    contract_number: 'contractNumber',
    consumed: 'consumed',
    product_name: 'productName',
    product_id: 'productId',
    subscription_id: 'subscriptionId'
  }
end

.map_attributes(pool, pool_id_map: {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/models/katello/upstream_pool.rb', line 66

def map_attributes(pool, pool_id_map: {})
  attributes = {}
  kat_to_cp_map.map do |kat, cp|
    attributes[kat] = pool[cp] if pool[cp]
  end

  attributes[:local_pool_ids] = pool_id_map[attributes[:id]]

  attributes
end

.minimal_fieldsObject



77
78
79
# File 'app/models/katello/upstream_pool.rb', line 77

def minimal_fields
  kat_to_cp_map.values_at(:id, :quantity)
end

.pool_id_params(pool_ids) ⇒ Object



92
93
94
# File 'app/models/katello/upstream_pool.rb', line 92

def pool_id_params(pool_ids)
  pool_ids ? pool_ids.map { |pool| [:poolid, pool] } : []
end

.request_params(base_params: {}, extra_params: [], included_fields: []) ⇒ Object



85
86
87
88
89
90
# File 'app/models/katello/upstream_pool.rb', line 85

def request_params(base_params: {}, extra_params: [], included_fields: [])
  # use extra_params when you have duplicate keys
  # i.e. [[:michael, "bolton"], [:michael, "jordan"]]
  included_fields.map! { |field| [:include, field] }
  RestClient::ParamsArray.new(base_params.to_a + extra_params + included_fields)
end

.respond(pools, total) ⇒ Object



23
24
25
26
27
28
29
# File 'app/models/katello/upstream_pool.rb', line 23

def respond(pools, total)
  {
    pools: pools,
    total: total,
    subtotal: pools.count
  }
end

.response_to_pools(response, pool_id_map: {}) ⇒ Object



43
44
45
46
47
48
49
# File 'app/models/katello/upstream_pool.rb', line 43

def response_to_pools(response, pool_id_map: {})
  pools = JSON.parse(response)
  if pool_id_map.empty?
    pool_id_map = Katello::Candlepin::PoolService.map_upstream_pools_to_local(pools)
  end
  pools.map { |pool| self.new(map_attributes(pool, pool_id_map: pool_id_map)) }
end

.total_count_headerObject



100
101
102
# File 'app/models/katello/upstream_pool.rb', line 100

def total_count_header
  Katello::Resources::Candlepin::TOTAL_COUNT_HEADER
end

.upstream_consumer_idObject



39
40
41
# File 'app/models/katello/upstream_pool.rb', line 39

def upstream_consumer_id
  CP_POOL.upstream_consumer_id
end

Instance Method Details

#availableObject



105
106
107
108
109
# File 'app/models/katello/upstream_pool.rb', line 105

def available
  return -1 if self.quantity == -1
  return 0 unless self.quantity && self.consumed
  self.quantity - self.consumed
end