Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit 2f22006

Browse files
author
Tom McKay
committed
fixes #18864 - option to clear subscriptions first
1 parent ac187d3 commit 2f22006

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

lib/hammer_cli_csv/content_hosts.rb

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def self.supported?
1212
end
1313

1414
option %w(--itemized-subscriptions), :flag, _('Export one subscription per row, only process update subscriptions on import')
15+
option %w(--clear-subscriptions), :flag, _('When processing --itemized-subscriptions, clear existing subscriptions first')
1516
option %w(--columns), 'COLUMN_NAMES', _('Comma separated list of column names to export')
1617

1718
SEARCH = 'Search'
@@ -86,6 +87,8 @@ def column_headers
8687
end
8788

8889
def export(csv)
90+
raise _("--clear-subscriptions option only relevant during import") if option_clear_subscriptions?
91+
8992
if option_itemized_subscriptions?
9093
export_itemized_subscriptions csv
9194
else
@@ -151,6 +154,7 @@ def import
151154
raise _("--columns option only relevant with --export") unless option_columns.nil?
152155

153156
@existing = {}
157+
@visited = {}
154158
@hypervisor_guests = {}
155159
@all_subscriptions = {}
156160

@@ -214,7 +218,8 @@ def update_itemized_subscriptions(name, line)
214218

215219
print(_("Updating subscriptions for content host '%{name}'...") % {:name => name}) if option_verbose?
216220
host = @api.resource(:hosts).call(:show, {:id => @existing[name]})
217-
update_subscriptions(host, line, false)
221+
remove_existing = clear_subscriptions?(host['name'])
222+
update_subscriptions(host, line, remove_existing)
218223
puts _('done') if option_verbose?
219224
end
220225

@@ -347,13 +352,14 @@ def update_subscriptions(host, line, remove_existing)
347352
'full_results' => true
348353
})['results']
349354
if remove_existing && existing_subscriptions.length != 0
355+
print _("clearing existing subscriptions...") if option_verbose?
350356
existing_subscriptions.map! do |existing_subscription|
351-
{:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}
357+
subs = [{:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}]
358+
@api.resource(:host_subscriptions).call(:remove_subscriptions, {
359+
'host_id' => host['id'],
360+
'subscriptions' => subs
361+
})
352362
end
353-
@api.resource(:host_subscriptions).call(:remove_subscriptions, {
354-
'host_id' => host['id'],
355-
'subscriptions' => existing_subscriptions
356-
})
357363
existing_subscriptions = []
358364
end
359365

@@ -425,11 +431,13 @@ def update_existing(line)
425431
# http://projects.theforeman.org/issues/6307
426432
total = @api.resource(:hosts).call(:index, {
427433
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
434+
'search' => option_search,
428435
'per_page' => 1
429-
})['total'].to_i
436+
})['subtotal'].to_i
430437
(total / 20 + 1).to_i.times do |page|
431438
@api.resource(:hosts).call(:index, {
432439
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
440+
'search' => option_search,
433441
'page' => page + 1,
434442
'per_page' => 20
435443
})['results'].each do |host|
@@ -599,6 +607,15 @@ def hypervisor_from_line(line)
599607
hypervisor = namify(hypervisor, 1) if !hypervisor.nil? && !hypervisor.empty?
600608
hypervisor
601609
end
610+
611+
def clear_subscriptions?(name)
612+
if option_clear_subscriptions? && !@visited.include?(name)
613+
@visited[name] = true
614+
true
615+
else
616+
false
617+
end
618+
end
602619
end
603620
end
604621
end

test/resources/content_hosts_test.rb

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def test_usage
1717
csv content-hosts [OPTIONS]
1818
1919
Options:
20+
--clear-subscriptions When processing --itemized-subscriptions, clear existing subscriptions first
2021
--columns COLUMN_NAMES Comma separated list of column names to export
2122
--continue-on-error Continue processing even if individual resource error
2223
--export Export current data instead of importing
@@ -56,6 +57,19 @@ def test_usage
5657
stop_vcr
5758
end
5859

60+
def test_export_with_clear_subscriptions
61+
start_vcr
62+
set_user 'admin'
63+
64+
stdout,stderr = capture {
65+
hammer.run(%W{--reload-cache csv content-hosts --export --clear-subscriptions})
66+
}
67+
assert_equal "Error: --clear-subscriptions option only relevant during import\n", stderr
68+
assert_equal stdout, ''
69+
70+
stop_vcr
71+
end
72+
5973
def test_export_with_columns
6074
start_vcr
6175
set_user 'admin'
@@ -186,7 +200,7 @@ def test_import_search
186200
hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
187201
}
188202
assert_equal '', stderr
189-
assert_equal "Updating content host 'testaaa0'...done\nUpdating content host 'testaaa1'...done\n", stdout
203+
assert_equal "Updating content host 'testaaa0'...clearing existing subscriptions...done\nUpdating content host 'testaaa1'...clearing existing subscriptions...done\n", stdout
190204

191205

192206
%w{testaaa0 testaaa1 testbbb0 testbbb1 testbbb2}.each do |hostname|
@@ -196,6 +210,49 @@ def test_import_search
196210
stop_vcr
197211
end
198212

213+
# import a single line, then import again w/ clearing
214+
def test_import_single_line_clear_subs
215+
start_vcr
216+
set_user 'admin'
217+
218+
hostname = 'tester1'
219+
220+
file = Tempfile.new('content_hosts_test')
221+
file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
222+
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")
223+
file.rewind
224+
225+
stdout,stderr = capture {
226+
hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
227+
}
228+
assert_equal '', stderr
229+
assert_equal "Creating content host '#{hostname}'...done\n", stdout
230+
231+
file = Tempfile.new('content_hosts_test')
232+
file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
233+
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")
234+
file.rewind
235+
236+
stdout,stderr = capture {
237+
hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path} --clear-subscriptions})
238+
}
239+
assert_equal '', stderr
240+
assert_equal "Updating content host '#{hostname}'...clearing existing subscriptions...done\n", stdout
241+
242+
stdout,stderr = capture {
243+
hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --itemized-subscriptions --search name=#{hostname}})
244+
}
245+
assert_equal '', stderr
246+
lines = stdout.split("\n")
247+
lines.select! { |line| line.match(/tester1.*/) }
248+
assert_equal 1, lines.length
249+
assert_match(/.*Test Corporation,Library,Default Organization View,"",Yes,,RHEL 7.2,x86_64,2,3882752,1.*/, lines[0])
250+
assert_match(/.*Red Hat Enterprise Linux Server, Standard.*/, lines[0])
251+
host_delete(hostname)
252+
253+
stop_vcr
254+
end
255+
199256
def test_columns_config
200257
start_vcr
201258
set_user 'admin'

0 commit comments

Comments
 (0)