Module: HammerCLICsv::Utils::Subscriptions

Included in:
CsvCommand::ActivationKeysCommand, CsvCommand::ContentHostsCommand, CsvCommand::SubscriptionsCommand
Defined in:
lib/hammer_cli_csv/utils/subscriptions.rb

Constant Summary collapse

SUBSCRIPTIONS =
'Subscriptions'
SUBS_NAME =
'Subscription Name'
SUBS_TYPE =
'Subscription Type'
SUBS_QUANTITY =
'Subscription Quantity'
SUBS_SKU =
'Subscription SKU'
SUBS_CONTRACT =
'Subscription Contract'
SUBS_ACCOUNT =
'Subscription Account'
SUBS_START =
'Subscription Start'
SUBS_END =
'Subscription End'
SUBS_GUESTOF =
'Subscription Guest'

Instance Method Summary collapse

Instance Method Details

#debug_subscriptions(description, subscriptions) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 203

def debug_subscriptions(description, subscriptions)
  logger.debug(description)
  subscriptions.each do |subscription|
    logger.debug "#{subscription['quantity_consumed']}"\
                 "|#{subscription['product_id']}"\
                 "|#{subscription['product_name']}"\
                 "|#{subscription['contract_number']}|#{subscription['account_number']}"
  end
end

#get_all_subscriptions(organization) ⇒ Object



15
16
17
18
19
20
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 15

def get_all_subscriptions(organization)
  @api.resource(:subscriptions).call(:index, {
      'full_results' => true,
      'organization_id' => foreman_organization(:name => organization)
  })['results']
end

#get_matching_subscriptions(organization_id, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 22

def get_matching_subscriptions(organization_id, options = {})
  logger.debug("get_matching_subscriptions: #{options}")
  if options[:host]
    available_subscriptions = @api.resource(:subscriptions).call(:index, {
      'full_results' => true,
      'organization_id' => organization_id,
      'host_id' => options[:host]['id'],
      'available_for' => 'host',
      'match_host' => true
    })['results']
  elsif options[:activation_key]
    available_subscriptions = @api.resource(:subscriptions).call(:index, {
      'full_results' => true,
      'organization_id' => organization_id,
      'activation_key_id' => options[:activation_key]['id'],
      'available_for' => 'activation_key'
    })['results']
  else
    available_subscriptions = @api.resource(:subscriptions).call(:index, {
        'full_results' => true,
        'organization_id' => organization_id
    })['results']
  end

  debug_subscriptions('available_subscriptions', available_subscriptions)
  matches = matches_by_sku_and_name([], options[:sku], options[:name], available_subscriptions)
  matches = matches_by_type(matches, options[:type])
  matches = matches_by_hypervisor(matches, options[:hypervisor])
  matches = (matches, options[:account])
  matches = matches_by_contract(matches, options[:contract])
  matches = matches_by_sla(matches, options[:sla])
  matches = matches_by_quantity(matches, options[:quantity]) unless options[:activation_key]

  matches
end

#match_with_quantity_to_attach(match, subs_quantity) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 179

def match_with_quantity_to_attach(match, subs_quantity)
  if subs_quantity && subs_quantity != 'Automatic' && !subs_quantity.empty?
    match['quantity'] = subs_quantity
  else
    match['quantity'] = -1
  end
  match
end

#matches_by_account(matches, subs_account) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 136

def (matches, )
  return matches if matches.empty?

  if matches.length > 1 && 
    refined = matches.select do |subscription|
       == subscription['account_number']
    end
    matches = refined unless refined.empty?
  end
  debug_subscriptions("matches_by_account: #{}", matches)
  matches
end

#matches_by_contract(matches, subs_contract) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 149

def matches_by_contract(matches, subs_contract)
  return matches if matches.empty?

  if matches.length > 1 && subs_contract
    refined = matches.select do |subscription|
      subs_contract == subscription['contract_number']
    end
    matches = refined unless refined.empty?
  end
  debug_subscriptions("matches_by_contract: #{subs_contract}", matches)
  matches
end

#matches_by_hypervisor(matches, subs_hypervisor) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 75

def matches_by_hypervisor(matches, subs_hypervisor)
  return matches if matches.empty?

  if !subs_hypervisor.nil? && !subs_hypervisor.empty?
    matches.select! do |subscription|
      !subscription['host'].nil? && subscription['host']['name'] == subs_hypervisor
    end
    if matches.empty? && subs_hypervisor =~ /virt-who-/
      subs_hypervisor = subs_hypervisor.split('-')[2..-2].join('-')
      matches.select! do |subscription|
        !subscription['host'].nil? && subscription['host']['name'] == subs_hypervisor
      end
    end
  else
    matches.select! do |subscription|
      subscription['host'].nil?
    end
  end
  debug_subscriptions("matches_by_hypervisor: #{subs_hypervisor}", matches)
  matches
end

#matches_by_quantity(matches, subs_quantity) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 162

def matches_by_quantity(matches, subs_quantity)
  return matches if matches.empty?

  matches.select! do |subscription|
    subscription['available'] != 0
  end

  if !subs_quantity.nil? && !subs_quantity.empty? && subs_quantity != 'Automatic'
    subs_quantity = subs_quantity.to_i
    matches.select! do |subscription|
      subscription['available'] < 0 || subs_quantity <= subscription['available']
    end
  end
  debug_subscriptions("matches_by_quantity: #{subs_quantity}", matches)
  matches
end

#matches_by_sku_and_name(matches, subs_sku, subs_name, subscriptions) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 58

def matches_by_sku_and_name(matches, subs_sku, subs_name, subscriptions)
  return matches if subscriptions.empty?

  if subs_sku
    matches = subscriptions.select do |subscription|
      subs_sku == subscription['product_id']
    end
    raise _("No subscriptions match SKU '%{sku}'") % {:sku => subs_sku} if matches.empty?
  elsif subs_name
    matches = subscriptions.select do |subscription|
      subs_name == subscription['name']
    end
  end
  debug_subscriptions("matches_by_sku_and_name: #{subs_sku}|#{subs_name}", matches)
  matches
end

#matches_by_sla(matches, subs_sla) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 97

def matches_by_sla(matches, subs_sla)
  return matches if matches.empty?

  if !subs_sla.nil? && !subs_sla.empty?
    found = matches.select do |subscription|
      subscription['sla'] == subs_sla
    end
    # Fallback to subscriptions w/o sla set
    if found.empty?
      found = matches.select do |subscription|
        subscription['sla'].nil? || subscription['sla'].empty?
      end
    end
    matches = found
  end
  debug_subscriptions("matches_by_sla: #{subs_sla}", matches)
  matches
end

#matches_by_type(matches, subs_type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 116

def matches_by_type(matches, subs_type)
  return matches if matches.empty?

  if subs_type == 'Red Hat' || subs_type == 'Custom'
    matches.select! do |subscription|
      subscription['type'] == 'NORMAL'
    end
  elsif subs_type == 'Red Hat Guest'
    matches.select! do |subscription|
      !subscription['host'].nil? && !subscription['host'].empty?
    end
  elsif subs_type == 'Red Hat Temporary'
    matches.select! do |subscription|
      subscription['type'] == 'UNMAPPED_GUEST'
    end
  end
  debug_subscriptions("matches_type: #{subs_type}", matches)
  matches
end

#split_subscription_details(details) ⇒ Object

Subscription amount, SKU, name, contract number, and account number separated by ‘|’ or simply the subscription name.



198
199
200
201
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 198

def split_subscription_details(details)
  details = details.split('|')
  details.length == 1 ? ['Automatic', nil, details[0], nil, nil] : details
end

#subscription_name(subscription) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/hammer_cli_csv/utils/subscriptions.rb', line 188

def subscription_name(subscription)
  if subscription['host'].nil?
    subscription['name']
  else
    "#{subscription['name']} - Guest of #{subscription['host']['name']}"
  end
end