Revision 2f22006a
Added by Thomas McKay almost 7 years ago
lib/hammer_cli_csv/content_hosts.rb | ||
---|---|---|
end
|
||
|
||
option %w(--itemized-subscriptions), :flag, _('Export one subscription per row, only process update subscriptions on import')
|
||
option %w(--clear-subscriptions), :flag, _('When processing --itemized-subscriptions, clear existing subscriptions first')
|
||
option %w(--columns), 'COLUMN_NAMES', _('Comma separated list of column names to export')
|
||
|
||
SEARCH = 'Search'
|
||
... | ... | |
end
|
||
|
||
def export(csv)
|
||
raise _("--clear-subscriptions option only relevant during import") if option_clear_subscriptions?
|
||
|
||
if option_itemized_subscriptions?
|
||
export_itemized_subscriptions csv
|
||
else
|
||
... | ... | |
raise _("--columns option only relevant with --export") unless option_columns.nil?
|
||
|
||
@existing = {}
|
||
@visited = {}
|
||
@hypervisor_guests = {}
|
||
@all_subscriptions = {}
|
||
|
||
... | ... | |
|
||
print(_("Updating subscriptions for content host '%{name}'...") % {:name => name}) if option_verbose?
|
||
host = @api.resource(:hosts).call(:show, {:id => @existing[name]})
|
||
update_subscriptions(host, line, false)
|
||
remove_existing = clear_subscriptions?(host['name'])
|
||
update_subscriptions(host, line, remove_existing)
|
||
puts _('done') if option_verbose?
|
||
end
|
||
|
||
... | ... | |
'full_results' => true
|
||
})['results']
|
||
if remove_existing && existing_subscriptions.length != 0
|
||
print _("clearing existing subscriptions...") if option_verbose?
|
||
existing_subscriptions.map! do |existing_subscription|
|
||
{:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}
|
||
subs = [{:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}]
|
||
@api.resource(:host_subscriptions).call(:remove_subscriptions, {
|
||
'host_id' => host['id'],
|
||
'subscriptions' => subs
|
||
})
|
||
end
|
||
@api.resource(:host_subscriptions).call(:remove_subscriptions, {
|
||
'host_id' => host['id'],
|
||
'subscriptions' => existing_subscriptions
|
||
})
|
||
existing_subscriptions = []
|
||
end
|
||
|
||
... | ... | |
# http://projects.theforeman.org/issues/6307
|
||
total = @api.resource(:hosts).call(:index, {
|
||
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
||
'search' => option_search,
|
||
'per_page' => 1
|
||
})['total'].to_i
|
||
})['subtotal'].to_i
|
||
(total / 20 + 1).to_i.times do |page|
|
||
@api.resource(:hosts).call(:index, {
|
||
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
||
'search' => option_search,
|
||
'page' => page + 1,
|
||
'per_page' => 20
|
||
})['results'].each do |host|
|
||
... | ... | |
hypervisor = namify(hypervisor, 1) if !hypervisor.nil? && !hypervisor.empty?
|
||
hypervisor
|
||
end
|
||
|
||
def clear_subscriptions?(name)
|
||
if option_clear_subscriptions? && !@visited.include?(name)
|
||
@visited[name] = true
|
||
true
|
||
else
|
||
false
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
test/resources/content_hosts_test.rb | ||
---|---|---|
csv content-hosts [OPTIONS]
|
||
|
||
Options:
|
||
--clear-subscriptions When processing --itemized-subscriptions, clear existing subscriptions first
|
||
--columns COLUMN_NAMES Comma separated list of column names to export
|
||
--continue-on-error Continue processing even if individual resource error
|
||
--export Export current data instead of importing
|
||
... | ... | |
stop_vcr
|
||
end
|
||
|
||
def test_export_with_clear_subscriptions
|
||
start_vcr
|
||
set_user 'admin'
|
||
|
||
stdout,stderr = capture {
|
||
hammer.run(%W{--reload-cache csv content-hosts --export --clear-subscriptions})
|
||
}
|
||
assert_equal "Error: --clear-subscriptions option only relevant during import\n", stderr
|
||
assert_equal stdout, ''
|
||
|
||
stop_vcr
|
||
end
|
||
|
||
def test_export_with_columns
|
||
start_vcr
|
||
set_user 'admin'
|
||
... | ... | |
hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
|
||
}
|
||
assert_equal '', stderr
|
||
assert_equal "Updating content host 'testaaa0'...done\nUpdating content host 'testaaa1'...done\n", stdout
|
||
assert_equal "Updating content host 'testaaa0'...clearing existing subscriptions...done\nUpdating content host 'testaaa1'...clearing existing subscriptions...done\n", stdout
|
||
|
||
|
||
%w{testaaa0 testaaa1 testbbb0 testbbb1 testbbb2}.each do |hostname|
|
||
... | ... | |
stop_vcr
|
||
end
|
||
|
||
# import a single line, then import again w/ clearing
|
||
def test_import_single_line_clear_subs
|
||
start_vcr
|
||
set_user 'admin'
|
||
|
||
hostname = 'tester1'
|
||
|
||
file = Tempfile.new('content_hosts_test')
|
||
file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
|
||
file.write("#{hostname},Test Corporation,Library,Default Organization View,\"\",Yes,,RHEL 7.2,x86_64,2,3882752,1,\"\",\"69|Red Hat Enterprise Linux Server,290|Red Hat OpenShift Enterprise\",\"\"\"1|RH00001|Red Hat Enterprise Linux for Virtual Datacenters, Premium\"\"\"\n")
|
||
file.rewind
|
||
|
||
stdout,stderr = capture {
|
||
hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
|
||
}
|
||
assert_equal '', stderr
|
||
assert_equal "Creating content host '#{hostname}'...done\n", stdout
|
||
|
||
file = Tempfile.new('content_hosts_test')
|
||
file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
|
||
file.write("#{hostname},Test Corporation,Library,Default Organization View,\"\",Yes,,RHEL 7.2,x86_64,2,3882752,1,\"\",\"69|Red Hat Enterprise Linux Server,290|Red Hat OpenShift Enterprise\",\"\"\"|RH00004|Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)\"\"\"\n")
|
||
file.rewind
|
||
|
||
stdout,stderr = capture {
|
||
hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path} --clear-subscriptions})
|
||
}
|
||
assert_equal '', stderr
|
||
assert_equal "Updating content host '#{hostname}'...clearing existing subscriptions...done\n", stdout
|
||
|
||
stdout,stderr = capture {
|
||
hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --itemized-subscriptions --search name=#{hostname}})
|
||
}
|
||
assert_equal '', stderr
|
||
lines = stdout.split("\n")
|
||
lines.select! { |line| line.match(/tester1.*/) }
|
||
assert_equal 1, lines.length
|
||
assert_match(/.*Test Corporation,Library,Default Organization View,"",Yes,,RHEL 7.2,x86_64,2,3882752,1.*/, lines[0])
|
||
assert_match(/.*Red Hat Enterprise Linux Server, Standard.*/, lines[0])
|
||
host_delete(hostname)
|
||
|
||
stop_vcr
|
||
end
|
||
|
||
def test_columns_config
|
||
start_vcr
|
||
set_user 'admin'
|
Also available in: Unified diff
fixes #18864 - option to clear subscriptions first