Revision afc8539f
Added by Thomas McKay over 7 years ago
lib/hammer_cli_csv/utils/subscriptions.rb | ||
---|---|---|
SUBS_ACCOUNT = 'Subscription Account'
|
||
SUBS_START = 'Subscription Start'
|
||
SUBS_END = 'Subscription End'
|
||
SUBS_GUESTOF = 'Subscription Guest'
|
||
|
||
def get_all_subscriptions(organization)
|
||
@api.resource(:subscriptions).call(:index, {
|
||
:per_page => 999999,
|
||
'full_results' => true,
|
||
'organization_id' => foreman_organization(:name => organization)
|
||
})['results']
|
||
end
|
||
|
||
def get_subscription(organization, options = {})
|
||
@subscriptions ||= {}
|
||
@subscriptions[organization] ||= {}
|
||
|
||
if options[:name]
|
||
return nil if options[:name].nil? || options[:name].empty?
|
||
options[:id] = @subscriptions[organization][options[:name]]
|
||
if !options[:id]
|
||
results = @api.resource(:subscriptions).call(:index, {
|
||
:per_page => 999999,
|
||
'organization_id' => foreman_organization(:name => organization),
|
||
'search' => "name = \"#{options[:name]}\""
|
||
})
|
||
raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
|
||
raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
|
||
subscription = results['results'][0]
|
||
@subscriptions[organization][options[:name]] = subscription['id']
|
||
options[:id] = @subscriptions[organization][options[:name]]
|
||
raise "Subscription '#{options[:name]}' not found" if !options[:id]
|
||
end
|
||
result = options[:id]
|
||
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
|
||
return nil if options[:id].nil?
|
||
options[:name] = @subscriptions.key(options[:id])
|
||
if !options[:name]
|
||
subscription = @api.resource(:subscriptions).call(:show, {'id' => options[:id]})
|
||
raise "Subscription '#{options[:name]}' not found" if !subscription || subscription.empty?
|
||
options[:name] = subscription['name']
|
||
@subscriptions[options[:name]] = options[:id]
|
||
end
|
||
result = options[:name]
|
||
available_subscriptions = @api.resource(:subscriptions).call(:index, {
|
||
'full_results' => true,
|
||
'organization_id' => organization_id
|
||
})['results']
|
||
end
|
||
|
||
result
|
||
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_by_account(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
|
||
|
||
def matches_by_sku_and_name(matches, line, subscriptions)
|
||
if line[SUBS_SKU]
|
||
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|
|
||
line[SUBS_SKU] == subscription['product_id']
|
||
subs_sku == subscription['product_id']
|
||
end
|
||
raise _("No subscriptions match SKU '%{sku}'") % {:sku => line[SUBS_SKU]} if matches.empty?
|
||
elsif line[SUBS_NAME]
|
||
raise _("No subscriptions match SKU '%{sku}'") % {:sku => subs_sku} if matches.empty?
|
||
elsif subs_name
|
||
matches = subscriptions.select do |subscription|
|
||
line[SUBS_NAME] == subscription['name']
|
||
subs_name == subscription['name']
|
||
end
|
||
end
|
||
debug_subscriptions("matches_by_sku_and_name: #{subs_sku}|#{subs_name}", matches)
|
||
matches
|
||
end
|
||
|
||
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
|
||
|
||
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
|
||
raise _("No subscriptions match name '%{name}'") % {:name => line[SUBS_NAME]} if matches.empty?
|
||
# 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
|
||
|
||
def matches_by_type(matches, line)
|
||
if line[SUBS_TYPE] == 'Red Hat' || line[SUBS_TYPE] == 'Custom'
|
||
matches = matches.select do |subscription|
|
||
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 line[SUBS_TYPE] == 'Red Hat Guest'
|
||
matches = matches.select do |subscription|
|
||
subscription['type'] == 'STACK_DERIVED'
|
||
elsif subs_type == 'Red Hat Guest'
|
||
matches.select! do |subscription|
|
||
!subscription['host'].nil? && !subscription['host'].empty?
|
||
end
|
||
elsif line[SUBS_TYPE] == 'Red Hat Temporary'
|
||
matches = matches.select do |subscription|
|
||
elsif subs_type == 'Red Hat Temporary'
|
||
matches.select! do |subscription|
|
||
subscription['type'] == 'UNMAPPED_GUEST'
|
||
end
|
||
end
|
||
raise _("No subscriptions match type '%{type}'") % {:type => line[SUBS_TYPE]} if matches.empty?
|
||
debug_subscriptions("matches_type: #{subs_type}", matches)
|
||
matches
|
||
end
|
||
|
||
def matches_by_account(matches, line)
|
||
if matches.length > 1 && line[SUBS_ACCOUNT]
|
||
def matches_by_account(matches, subs_account)
|
||
return matches if matches.empty?
|
||
|
||
if matches.length > 1 && subs_account
|
||
refined = matches.select do |subscription|
|
||
line[SUBS_ACCOUNT] == subscription['account_number']
|
||
subs_account == subscription['account_number']
|
||
end
|
||
matches = refined unless refined.empty?
|
||
end
|
||
debug_subscriptions("matches_by_account: #{subs_account}", matches)
|
||
matches
|
||
end
|
||
|
||
def matches_by_contract(matches, line)
|
||
if matches.length > 1 && line[SUBS_CONTRACT]
|
||
def matches_by_contract(matches, subs_contract)
|
||
return matches if matches.empty?
|
||
|
||
if matches.length > 1 && subs_contract
|
||
refined = matches.select do |subscription|
|
||
line[SUBS_CONTRACT] == subscription['contract_number']
|
||
subs_contract == subscription['contract_number']
|
||
end
|
||
matches = refined unless refined.empty?
|
||
end
|
||
debug_subscriptions("matches_by_contract: #{subs_contract}", matches)
|
||
matches
|
||
end
|
||
|
||
def matches_by_quantity(matches, line)
|
||
if line[SUBS_QUANTITY] && line[SUBS_QUANTITY] != 'Automatic'
|
||
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
|
||
refined = matches.select do |subscription|
|
||
subscription['available'] == -1 || line[SUBS_QUANTITY].to_i <= subscription['available']
|
||
subscription['available'] == -1 || subs_quantity <= subscription['available']
|
||
end
|
||
raise _("No '%{name}' subscription with quantity %{quantity} or more available") %
|
||
{:name => matches[0]['name'], :quantity => line[SUBS_QUANTITY]} if refined.empty?
|
||
{:name => matches[0]['name'], :quantity => subs_quantity} if refined.empty?
|
||
matches = refined
|
||
end
|
||
debug_subscriptions("matches_by_quantity: #{subs_quantity}", matches)
|
||
matches
|
||
end
|
||
|
||
def match_with_quantity_to_attach(match, line)
|
||
if line[SUBS_QUANTITY] && line[SUBS_QUANTITY] != 'Automatic' && !line[SUBS_QUANTITY].empty?
|
||
match['quantity'] = line[SUBS_QUANTITY]
|
||
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
|
||
|
||
def subscription_name(subscription)
|
||
if subscription['host'].nil?
|
||
subscription['name']
|
||
else
|
||
"#{subscription['name']} - Guest of #{subscription['host']['name']}"
|
||
end
|
||
end
|
||
|
||
# Subscription amount, SKU, name, contract number, and account number separated by '|'
|
||
# or simply the subscription name.
|
||
def split_subscription_details(details)
|
||
details = details.split('|')
|
||
details.length == 1 ? ['Automatic', nil, details[0], nil, nil] : details
|
||
end
|
||
|
||
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
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #17505, #17052 - refine search for subs in act-keys and content-hosts