hammer_cli_csv 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +5 -13
  2. data/config/cli_config.yml +9 -24
  3. data/lib/hammer_cli_csv/activation_keys.rb +220 -95
  4. data/lib/hammer_cli_csv/architectures.rb +7 -11
  5. data/lib/hammer_cli_csv/base.rb +67 -41
  6. data/lib/hammer_cli_csv/compute_profiles.rb +11 -15
  7. data/lib/hammer_cli_csv/compute_resources.rb +11 -15
  8. data/lib/hammer_cli_csv/containers.rb +10 -12
  9. data/lib/hammer_cli_csv/content_hosts.rb +285 -203
  10. data/lib/hammer_cli_csv/content_view_filters.rb +32 -36
  11. data/lib/hammer_cli_csv/content_views.rb +33 -37
  12. data/lib/hammer_cli_csv/csv.rb +17 -1
  13. data/lib/hammer_cli_csv/domains.rb +15 -19
  14. data/lib/hammer_cli_csv/export.rb +13 -5
  15. data/lib/hammer_cli_csv/host_collections.rb +20 -22
  16. data/lib/hammer_cli_csv/host_groups.rb +73 -65
  17. data/lib/hammer_cli_csv/hosts.rb +30 -34
  18. data/lib/hammer_cli_csv/import.rb +24 -10
  19. data/lib/hammer_cli_csv/installation_media.rb +74 -0
  20. data/lib/hammer_cli_csv/job_templates.rb +37 -41
  21. data/lib/hammer_cli_csv/lifecycle_environments.rb +15 -19
  22. data/lib/hammer_cli_csv/locations.rb +4 -6
  23. data/lib/hammer_cli_csv/operating_systems.rb +17 -21
  24. data/lib/hammer_cli_csv/organizations.rb +11 -13
  25. data/lib/hammer_cli_csv/partition_tables.rb +17 -21
  26. data/lib/hammer_cli_csv/products.rb +200 -92
  27. data/lib/hammer_cli_csv/provisioning_templates.rb +19 -23
  28. data/lib/hammer_cli_csv/puppet_environments.rb +8 -12
  29. data/lib/hammer_cli_csv/puppet_facts.rb +20 -24
  30. data/lib/hammer_cli_csv/puppet_reports.rb +47 -51
  31. data/lib/hammer_cli_csv/reports.rb +6 -10
  32. data/lib/hammer_cli_csv/roles.rb +12 -16
  33. data/lib/hammer_cli_csv/settings.rb +8 -6
  34. data/lib/hammer_cli_csv/smart_proxies.rb +9 -13
  35. data/lib/hammer_cli_csv/splice.rb +1 -3
  36. data/lib/hammer_cli_csv/subnets.rb +24 -28
  37. data/lib/hammer_cli_csv/subscriptions.rb +39 -91
  38. data/lib/hammer_cli_csv/sync_plans.rb +20 -24
  39. data/lib/hammer_cli_csv/users.rb +25 -29
  40. data/lib/hammer_cli_csv/utils/subscriptions.rb +130 -0
  41. data/lib/hammer_cli_csv/version.rb +1 -1
  42. data/lib/hammer_cli_csv.rb +2 -2
  43. data/test/csv_test.rb +32 -0
  44. data/test/csv_test_helper.rb +17 -2
  45. data/test/data/content-hosts.csv +3 -2
  46. data/test/data/hosts.csv +0 -11
  47. data/test/data/settings.csv +1 -0
  48. data/test/data/setup/content-hosts.csv +1 -0
  49. data/test/data/setup/content-views.csv +2 -0
  50. data/test/data/setup/lifecycle-environments.csv +5 -0
  51. data/test/data/setup/locations.csv +6 -0
  52. data/test/data/setup/organizations.csv +2 -0
  53. data/test/data/setup/subscriptions.csv +5 -0
  54. data/test/data/subscriptions.csv +15 -17
  55. data/test/export_test.rb +27 -0
  56. data/test/fixtures/vcr_cassettes/apipie.yml +18528 -0
  57. data/test/fixtures/vcr_cassettes/resources/activation_keys_import/create_and_update.yml +38033 -0
  58. data/test/fixtures/vcr_cassettes/resources/content_hosts_export/export.yml +18846 -0
  59. data/test/fixtures/vcr_cassettes/resources/content_hosts_export/export_subscriptions.yml +323 -0
  60. data/test/fixtures/vcr_cassettes/resources/content_hosts_import/create_and_update.yml +41884 -0
  61. data/test/fixtures/vcr_cassettes/resources/content_hosts_import/import_single_line.yml +38248 -0
  62. data/test/fixtures/vcr_cassettes/resources/settings_import/update_settings.yml +18832 -0
  63. data/test/fixtures/vcr_cassettes/resources/settings_import/update_settings_continue.yml +316 -0
  64. data/test/fixtures/vcr_cassettes/resources/subscriptions_import/manifest_does_not_exist.yml +18785 -0
  65. data/test/fixtures/vcr_cassettes/setup/setup_content_views/setup.yml +19090 -0
  66. data/test/fixtures/vcr_cassettes/setup/setup_lifecycle_environments/setup.yml +924 -0
  67. data/test/fixtures/vcr_cassettes/setup/setup_locations/setup.yml +21102 -0
  68. data/test/fixtures/vcr_cassettes/setup/setup_organizations/setup.yml +19278 -0
  69. data/test/fixtures/vcr_cassettes/setup/setup_subscriptions/setup.yml +419 -0
  70. data/test/import_test.rb +20 -25
  71. data/test/resources/activation_keys_test.rb +72 -0
  72. data/test/resources/content_hosts_test.rb +132 -0
  73. data/test/resources/settings_test.rb +81 -0
  74. data/test/resources/subscriptions_test.rb +56 -0
  75. data/test/setup/setup_content_views.rb +17 -0
  76. data/test/setup/setup_lifecycle_environments.rb +17 -0
  77. data/test/setup/setup_locations.rb +17 -0
  78. data/test/setup/setup_organizations.rb +17 -0
  79. data/test/setup/setup_subscriptions.rb +20 -0
  80. data/test/setup/setup_test.rb +10 -0
  81. data/test/test_runner.rb +175 -0
  82. data/test/vcr_setup.rb +54 -0
  83. metadata +77 -13
  84. data/lib/hammer_cli_csv/exception_handler.rb +0 -42
  85. data/lib/hammer_cli_csv/installation_medias.rb +0 -77
  86. data/test/content_hosts_test.rb +0 -61
  87. data/test/settings_test.rb +0 -30
@@ -13,17 +13,13 @@ module HammerCLICsv
13
13
  PENDING = 'Pending'
14
14
  METRICS = 'Metrics'
15
15
 
16
- def export
17
- CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
18
- csv << [NAME]
19
- @api.resource(:reports).call(:index, {
20
- 'per_page' => 999999
21
- })['results'].each do |report|
22
- csv << [report['host_name'], report['metrics'].to_json]
23
- end
16
+ def export(csv)
17
+ csv << [NAME]
18
+ @api.resource(:reports).call(:index, {
19
+ 'per_page' => 999999
20
+ })['results'].each do |report|
21
+ csv << [report['host_name'], report['metrics'].to_json]
24
22
  end
25
-
26
- HammerCLI::EX_OK
27
23
  end
28
24
 
29
25
  def import
@@ -10,25 +10,21 @@ module HammerCLICsv
10
10
  ORGANIZATIONS = 'Organizations'
11
11
  LOCATIONS = 'Locations'
12
12
 
13
- def export
14
- CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
15
- csv << [NAME, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS]
16
- @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
17
- @api.resource(:filters).call(:index, {
18
- 'per_page' => 999999,
19
- 'search' => "role=\"#{role['name']}\""
20
- })['results'].each do |filter|
21
- filter = @api.resource(:filters).call(:show, 'id' => filter['id'])
13
+ def export(csv)
14
+ csv << [NAME, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS]
15
+ @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
16
+ @api.resource(:filters).call(:index, {
17
+ 'per_page' => 999999,
18
+ 'search' => "role=\"#{role['name']}\""
19
+ })['results'].each do |filter|
20
+ filter = @api.resource(:filters).call(:show, 'id' => filter['id'])
22
21
 
23
- permissions = export_column(filter, 'permissions', 'name')
24
- organizations = export_column(filter, 'organizations', 'name')
25
- locations = export_column(filter, 'locations', 'name')
26
- csv << [role['name'], filter['resource_type'], filter['search'] || '', permissions, organizations, locations]
27
- end
22
+ permissions = export_column(filter, 'permissions', 'name')
23
+ organizations = export_column(filter, 'organizations', 'name')
24
+ locations = export_column(filter, 'locations', 'name')
25
+ csv << [role['name'], filter['resource_type'], filter['search'] || '', permissions, organizations, locations]
28
26
  end
29
27
  end
30
-
31
- HammerCLI::EX_OK
32
28
  end
33
29
 
34
30
  def import
@@ -6,12 +6,14 @@ module HammerCLICsv
6
6
 
7
7
  VALUE = 'Value'
8
8
 
9
- def export
10
- CSV.open(option_file || '/dev/stdout', 'wb') do |csv|
11
- csv << [NAME, VALUE]
12
- @api.resource(:settings).call(:index, {'per_page' => 999999})['results'].each do |setting|
13
- csv << [setting['name'], setting['value']]
14
- end
9
+ def self.supported?
10
+ true
11
+ end
12
+
13
+ def export(csv)
14
+ csv << [NAME, VALUE]
15
+ @api.resource(:settings).call(:index, {'per_page' => 999999})['results'].each do |setting|
16
+ csv << [setting['name'], setting['value']]
15
17
  end
16
18
  end
17
19
 
@@ -9,17 +9,15 @@ module HammerCLICsv
9
9
  URL = 'URL'
10
10
  LIFECYCLE_ENVIRONMENTS = 'Lifecycle Environments'
11
11
 
12
- def export
13
- CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
14
- csv << [NAME, ORGANIZATIONS, LOCATIONS, URL, LIFECYCLE_ENVIRONMENTS]
15
- @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
16
- smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => smart_proxy['id']})
17
- name = smart_proxy['name']
18
- organizations = export_column(smart_proxy, 'organizations', 'name')
19
- locations = export_column(smart_proxy, 'locations', 'name')
20
- url = smart_proxy['url']
21
- csv << [name, organizations, locations, url]
22
- end
12
+ def export(csv)
13
+ csv << [NAME, ORGANIZATIONS, LOCATIONS, URL, LIFECYCLE_ENVIRONMENTS]
14
+ @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
15
+ smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => smart_proxy['id']})
16
+ name = smart_proxy['name']
17
+ organizations = export_column(smart_proxy, 'organizations', 'name')
18
+ locations = export_column(smart_proxy, 'locations', 'name')
19
+ url = smart_proxy['url']
20
+ csv << [name, organizations, locations, url]
23
21
  end
24
22
  end
25
23
 
@@ -63,8 +61,6 @@ module HammerCLICsv
63
61
 
64
62
  print "done\n" if option_verbose?
65
63
  end
66
- rescue RuntimeError => e
67
- raise "#{e}\n #{line}"
68
64
  end
69
65
  end
70
66
  end
@@ -130,8 +130,6 @@ module HammerCLICsv
130
130
  update_host_collections(host_id, line)
131
131
 
132
132
  puts _('done') if option_verbose?
133
- rescue RuntimeError => e
134
- raise "#{e}\n #{line}"
135
133
  end
136
134
 
137
135
  private
@@ -266,7 +264,7 @@ module HammerCLICsv
266
264
  subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
267
265
  (amount, sku, name) = details.split('|')
268
266
  {
269
- :id => katello_subscription(line[ORGANIZATION], :name => name),
267
+ :id => get_subscription(line[ORGANIZATION], :name => name),
270
268
  :quantity => (amount.nil? || amount.empty? || amount == 'Automatic') ? 0 : amount.to_i
271
269
  }
272
270
  end
@@ -19,33 +19,31 @@ module HammerCLICsv
19
19
  DNS_SECONDARY = 'DNS Secondary'
20
20
  VLAN_ID = 'VLAN ID'
21
21
 
22
- def export
23
- CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
24
- csv << [NAME, ORGANIZATIONS, LOCATIONS, NETWORK, NETWORK_MASK,
25
- NETWORK_FROM, NETWORK_TO, DOMAINS, GATEWAY, DHCP_PROXY, TFTP_PROXY, DNS_PROXY,
26
- DNS_PRIMARY, DNS_SECONDARY, VLAN_ID]
27
- @api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
28
- subnet = @api.resource(:subnets).call(:show, {'id' => subnet['id']})
22
+ def export(csv)
23
+ csv << [NAME, ORGANIZATIONS, LOCATIONS, NETWORK, NETWORK_MASK,
24
+ NETWORK_FROM, NETWORK_TO, DOMAINS, GATEWAY, DHCP_PROXY, TFTP_PROXY, DNS_PROXY,
25
+ DNS_PRIMARY, DNS_SECONDARY, VLAN_ID]
26
+ @api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
27
+ subnet = @api.resource(:subnets).call(:show, {'id' => subnet['id']})
29
28
 
30
- name = subnet['name']
31
- organizations = export_column(subnet, 'organizations', 'name')
32
- locations = export_column(subnet, 'locations', 'name')
33
- network = subnet['network']
34
- network_mask = subnet['mask']
35
- network_from = subnet['from']
36
- network_to = subnet['to']
37
- domains = export_column(subnet, 'domains', 'name')
38
- gateway = subnet['gateway']
39
- dhcp_proxy = (subnet['dhcp'] && subnet['dhcp'].key?('name')) ? subnet['dhcp']['name'] : ''
40
- tftp_proxy = (subnet['tftp'] && subnet['tftp'].key?('name')) ? subnet['tftp']['name'] : ''
41
- dns_proxy = (subnet['dns'] && subnet['dns'].key?('name')) ? subnet['dns']['name'] : ''
42
- dns_primary = subnet['dns_primary']
43
- dns_secondary = subnet['dns_secondary']
44
- vlan_id = subnet['vlanid']
45
- csv << [name, organizations, locations, network, network_mask,
46
- network_from, network_to, domains, gateway, dhcp_proxy, tftp_proxy, dns_proxy,
47
- dns_primary, dns_secondary, vlan_id]
48
- end
29
+ name = subnet['name']
30
+ organizations = export_column(subnet, 'organizations', 'name')
31
+ locations = export_column(subnet, 'locations', 'name')
32
+ network = subnet['network']
33
+ network_mask = subnet['mask']
34
+ network_from = subnet['from']
35
+ network_to = subnet['to']
36
+ domains = export_column(subnet, 'domains', 'name')
37
+ gateway = subnet['gateway']
38
+ dhcp_proxy = (subnet['dhcp'] && subnet['dhcp'].key?('name')) ? subnet['dhcp']['name'] : ''
39
+ tftp_proxy = (subnet['tftp'] && subnet['tftp'].key?('name')) ? subnet['tftp']['name'] : ''
40
+ dns_proxy = (subnet['dns'] && subnet['dns'].key?('name')) ? subnet['dns']['name'] : ''
41
+ dns_primary = subnet['dns_primary']
42
+ dns_secondary = subnet['dns_secondary']
43
+ vlan_id = subnet['vlanid']
44
+ csv << [name, organizations, locations, network, network_mask,
45
+ network_from, network_to, domains, gateway, dhcp_proxy, tftp_proxy, dns_proxy,
46
+ dns_primary, dns_secondary, vlan_id]
49
47
  end
50
48
  end
51
49
 
@@ -100,8 +98,6 @@ module HammerCLICsv
100
98
 
101
99
  print "done\n" if option_verbose?
102
100
  end
103
- rescue RuntimeError => e
104
- raise "#{e}\n #{line}"
105
101
  end
106
102
  end
107
103
  end
@@ -4,110 +4,60 @@ module HammerCLICsv
4
4
  command_name 'subscriptions'
5
5
  desc 'import or export subscriptions'
6
6
 
7
+ def self.supported?
8
+ true
9
+ end
10
+
7
11
  ORGANIZATION = 'Organization'
8
12
  MANIFEST = 'Manifest File'
9
- CONTENT_SET = 'Content Set'
10
- ARCH = 'Arch'
11
- RELEASE = 'Release'
13
+ SUBSCRIPTION = 'Subscription Name'
14
+ QUANTITY = 'Quantity'
15
+ SKU = 'Product SKU'
16
+ CONTRACT = 'Contract Number'
17
+ ACCOUNT = 'Account Number'
12
18
 
13
- def export
14
- CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
15
- csv << [NAME, ORGANIZATION, MANIFEST, CONTENT_SET, ARCH, RELEASE]
16
- @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
17
- next if option_organization && organization['name'] != option_organization
18
- @api.resource(:products).call(:index, {
19
- 'per_page' => 999999,
20
- 'organization_id' => organization['id'],
21
- 'enabled' => true
22
- })['results'].each do |product|
23
- if product['redhat']
24
- name = product['name']
25
- @api.resource(:repository_sets).call(:index, {
26
- 'per_page' => 999999,
27
- 'organization_id' => organization['id'],
28
- 'product_id' => product['id']
29
- })['results'].each do |repository_set|
30
- content_set = repository_set['name']
31
- repository_set['repositories'].each do |repository|
32
- name_split = repository['name'].split(' ')
33
- arch = name_split[-2]
34
- release = name_split[-1]
35
- csv << [name, organization['name'], nil, content_set, arch, release]
36
- end
37
- end
38
- end
39
- end
40
- end
19
+ def export(csv)
20
+ csv << [NAME, ORGANIZATION, MANIFEST, SUBSCRIPTION, QUANTITY, SKU, CONTRACT, ACCOUNT]
21
+ @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
22
+ next if option_organization && organization['name'] != option_organization
23
+ organization = @api.resource(:organizations).call(:show, {'id' => organization['id']})
24
+ export_manifest(csv, organization)
25
+ export_subscriptions(csv, organization)
41
26
  end
42
27
  end
43
28
 
44
- def import
45
- thread_import do |line|
46
- if line[MANIFEST] && !line[MANIFEST].empty?
47
- import_manifest_from_csv(line)
48
- else
49
- enable_products_from_csv(line)
50
- end
51
- end
52
- end
53
-
54
- # FIXME: TODO remove this rubocop
55
- # rubocop:disable CyclomaticComplexity
56
- def enable_products_from_csv(line)
57
- organization = line[ORGANIZATION] || option_organization
58
- raise "Organization is required in either input CSV or by option --organization" if organization.nil? || organization.empty?
59
- line[ORGANIZATION] = organization
60
- return if option_organization && line[ORGANIZATION] != option_organization
29
+ def export_manifest(csv, organization)
30
+ info = organization['owner_details']['upstreamConsumer']
31
+ return if info.nil?
61
32
 
62
- results = @api.resource(:products).call(:index, {
63
- 'per_page' => 999999,
64
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
65
- 'name' => line[NAME]
66
- })['results']
67
- raise "No match for product '#{line[NAME]}'" if results.length == 0
68
- raise "Multiple matches for product '#{line[NAME]}'" if results.length != 1
69
- product = @api.resource(:products).call(:show, {'id' => results[0]['id']})
33
+ csv << ["# Manifest Name", organization['name'], info['name']]
34
+ csv << ["# Manifest URL", organization['name'], "https://#{info['webUrl']}#{info['uuid']}"]
35
+ end
70
36
 
71
- results = @api.resource(:repository_sets).call(:index, {
37
+ def export_subscriptions(csv, organization)
38
+ @api.resource(:subscriptions).call(:index, {
72
39
  'per_page' => 999999,
73
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
74
- 'product_id' => product['id'],
75
- 'name' => line[CONTENT_SET]
76
- })['results']
77
- raise "No match for content set '#{line[CONTENT_SET]}'" if results.length == 0
78
- raise "Multiple matches for content set '#{line[CONTENT_SET]}'" if results.length != 1
79
- repository_set = results[0]
80
-
81
- repository = repository_set['repositories'].find do |repo|
82
- if line[RELEASE].nil? || line[RELEASE].empty?
83
- repo['name'].end_with?("#{line[ARCH]}")
84
- else
85
- repo['name'].end_with?("#{line[ARCH]} #{line[RELEASE]}")
86
- end
40
+ 'organization_id' => organization['id']
41
+ })['results'].each do |subscription|
42
+ next if subscription['product_id'].to_i != 0 # Red Hat subs do not have number SKU
43
+ name = subscription['host'].nil? ? "# Subscription" : "# Guest Subscription for Host '#{subscription['host']['name']}'"
44
+ quantity = subscription['quantity'] < 0 ? "Unlimited" : subscription['quantity']
45
+ csv << [name, organization['name'], nil, subscription['name'],
46
+ quantity, subscription['product_id'], subscription['contract_number'],
47
+ subscription['account_number'],
48
+ subscription['start_date'], subscription['end_date']]
87
49
  end
50
+ end
88
51
 
89
- if repository.nil?
90
- print "Enabling repository #{line[CONTENT_SET]} #{line[ARCH]} #{line[RELEASE]}..." if option_verbose?
91
- product_content = product['product_content'].find do |content|
92
- content['content']['name'] == line[CONTENT_SET]
52
+ def import
53
+ thread_import do |line|
54
+ if line[MANIFEST] && !line[MANIFEST].empty?
55
+ import_manifest(line)
93
56
  end
94
- raise "No match for content set '#{line[CONTENT_SET]}'" if !product_content
95
-
96
- params = {
97
- 'id' => product_content['content']['id'],
98
- 'product_id' => product['id'],
99
- 'basearch' => line[ARCH]
100
- }
101
- params['releasever'] = line[RELEASE] unless line[RELEASE].nil? || line[RELEASE].empty?
102
-
103
- @api.resource(:repository_sets).call(:enable, params)
104
- puts 'done' if option_verbose?
105
- else
106
- puts "Repository #{repository['name']} already enabled" if option_verbose?
107
57
  end
108
58
  end
109
59
 
110
- def import_manifest_from_csv(line)
60
+ def import_manifest(line)
111
61
  return if option_organization && line[ORGANIZATION] != option_organization
112
62
  args = %W{
113
63
  --server #{ @server } --username #{ @username } --password #{ @password }
@@ -116,8 +66,6 @@ module HammerCLICsv
116
66
  }
117
67
  hammer.run(args)
118
68
 
119
- rescue RuntimeError => e
120
- raise "#{e}\n #{line}"
121
69
  end
122
70
  end
123
71
  end
@@ -11,32 +11,30 @@ module HammerCLICsv
11
11
  INTERVAL = 'Interval'
12
12
  PRODUCTS = 'Products'
13
13
 
14
- def export
15
- CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
16
- csv << [NAME, ORGANIZATION, DESCRIPTION, ENABLED, STARTDATE, INTERVAL, PRODUCTS]
14
+ def export(csv)
15
+ csv << [NAME, ORGANIZATION, DESCRIPTION, ENABLED, STARTDATE, INTERVAL, PRODUCTS]
17
16
 
18
- @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
19
- next if option_organization && organization['name'] != option_organization
17
+ @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
18
+ next if option_organization && organization['name'] != option_organization
20
19
 
21
- @api.resource(:sync_plans).call(:index, {
22
- 'per_page' => 999999,
23
- 'organization_id' => foreman_organization(:name => organization['name'])
24
- })['results'].each do |sync_plan|
25
- name = sync_plan['name']
26
- organization_name = organization['name']
27
- description = sync_plan['description']
28
- enabled = sync_plan['enabled'] ? 'Yes' : 'No'
29
- start_date = sync_plan['sync_date']
30
- interval = sync_plan['interval']
31
- products = CSV.generate do |column|
32
- column << sync_plan['products'].collect do |product|
33
- product['name']
34
- end
20
+ @api.resource(:sync_plans).call(:index, {
21
+ 'per_page' => 999999,
22
+ 'organization_id' => foreman_organization(:name => organization['name'])
23
+ })['results'].each do |sync_plan|
24
+ name = sync_plan['name']
25
+ organization_name = organization['name']
26
+ description = sync_plan['description']
27
+ enabled = sync_plan['enabled'] ? 'Yes' : 'No'
28
+ start_date = sync_plan['sync_date']
29
+ interval = sync_plan['interval']
30
+ products = CSV.generate do |column|
31
+ column << sync_plan['products'].collect do |product|
32
+ product['name']
35
33
  end
36
- products.delete!("\n")
37
- csv << [name, organization_name, description, enabled, start_date, interval,
38
- products]
39
34
  end
35
+ products.delete!("\n")
36
+ csv << [name, organization_name, description, enabled, start_date, interval,
37
+ products]
40
38
  end
41
39
  end
42
40
  end
@@ -89,8 +87,6 @@ module HammerCLICsv
89
87
  puts "done" if option_verbose?
90
88
  end
91
89
 
92
- rescue RuntimeError => e
93
- raise "#{e}\n #{line}"
94
90
  end
95
91
 
96
92
  private
@@ -12,39 +12,37 @@ module HammerCLICsv
12
12
  ADMIN = 'Administrator'
13
13
  ROLES = 'Roles'
14
14
 
15
- def export
16
- CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
17
- csv << [NAME, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ADMIN, ROLES]
18
- @api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user|
19
- if user['organizations']
20
- organizations = CSV.generate do |column|
21
- column << user['organizations'].collect do |organization|
22
- organization['name']
23
- end
15
+ def export(csv)
16
+ csv << [NAME, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ADMIN, ROLES]
17
+ @api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user|
18
+ if user['organizations']
19
+ organizations = CSV.generate do |column|
20
+ column << user['organizations'].collect do |organization|
21
+ organization['name']
24
22
  end
25
- organizations.delete!("\n")
26
23
  end
27
- if user['locations']
28
- locations = CSV.generate do |column|
29
- column << user['locations'].collect do |location|
30
- location['name']
31
- end
24
+ organizations.delete!("\n")
25
+ end
26
+ if user['locations']
27
+ locations = CSV.generate do |column|
28
+ column << user['locations'].collect do |location|
29
+ location['name']
32
30
  end
33
- locations.delete!("\n")
34
31
  end
35
- if user['roles']
36
- roles = CSV.generate do |column|
37
- column << user['roles'].collect do |role|
38
- role['name']
39
- end
32
+ locations.delete!("\n")
33
+ end
34
+ if user['roles']
35
+ roles = CSV.generate do |column|
36
+ column << user['roles'].collect do |role|
37
+ role['name']
40
38
  end
41
- roles.delete!("\n")
42
- end
43
- admin = user['admin'] ? 'Yes' : 'No'
44
- if user['login'] != 'admin' && !user['login'].start_with?('hidden-')
45
- csv << [user['login'], user['firstname'], user['lastname'], user['mail'],
46
- organizations, locations, admin, roles]
47
39
  end
40
+ roles.delete!("\n")
41
+ end
42
+ admin = user['admin'] ? 'Yes' : 'No'
43
+ if user['login'] != 'admin' && !user['login'].start_with?('hidden-')
44
+ csv << [user['login'], user['firstname'], user['lastname'], user['mail'],
45
+ organizations, locations, admin, roles]
48
46
  end
49
47
  end
50
48
  end
@@ -81,8 +79,6 @@ module HammerCLICsv
81
79
  end
82
80
  print "done\n" if option_verbose?
83
81
  end
84
- rescue RuntimeError => e
85
- raise "#{e}\n #{line}"
86
82
  end
87
83
 
88
84
  def create_user(line, name, roles, organizations, locations)
@@ -0,0 +1,130 @@
1
+ module HammerCLICsv
2
+ module Utils
3
+ module Subscriptions
4
+ SUBSCRIPTIONS = 'Subscriptions'
5
+ SUBS_NAME = 'Subscription Name'
6
+ SUBS_TYPE = 'Subscription Type'
7
+ SUBS_QUANTITY = 'Subscription Quantity'
8
+ SUBS_SKU = 'Subscription SKU'
9
+ SUBS_CONTRACT = 'Subscription Contract'
10
+ SUBS_ACCOUNT = 'Subscription Account'
11
+ SUBS_START = 'Subscription Start'
12
+ SUBS_END = 'Subscription End'
13
+
14
+ def get_all_subscriptions(organization)
15
+ @api.resource(:subscriptions).call(:index, {
16
+ :per_page => 999999,
17
+ 'organization_id' => foreman_organization(:name => organization)
18
+ })['results']
19
+ end
20
+
21
+ def get_subscription(organization, options = {})
22
+ @subscriptions ||= {}
23
+ @subscriptions[organization] ||= {}
24
+
25
+ if options[:name]
26
+ return nil if options[:name].nil? || options[:name].empty?
27
+ options[:id] = @subscriptions[organization][options[:name]]
28
+ if !options[:id]
29
+ results = @api.resource(:subscriptions).call(:index, {
30
+ :per_page => 999999,
31
+ 'organization_id' => foreman_organization(:name => organization),
32
+ 'search' => "name = \"#{options[:name]}\""
33
+ })
34
+ raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
35
+ raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
36
+ subscription = results['results'][0]
37
+ @subscriptions[organization][options[:name]] = subscription['id']
38
+ options[:id] = @subscriptions[organization][options[:name]]
39
+ raise "Subscription '#{options[:name]}' not found" if !options[:id]
40
+ end
41
+ result = options[:id]
42
+ else
43
+ return nil if options[:id].nil?
44
+ options[:name] = @subscriptions.key(options[:id])
45
+ if !options[:name]
46
+ subscription = @api.resource(:subscriptions).call(:show, {'id' => options[:id]})
47
+ raise "Subscription '#{options[:name]}' not found" if !subscription || subscription.empty?
48
+ options[:name] = subscription['name']
49
+ @subscriptions[options[:name]] = options[:id]
50
+ end
51
+ result = options[:name]
52
+ end
53
+
54
+ result
55
+ end
56
+
57
+ def matches_by_sku_and_name(matches, line, subscriptions)
58
+ if line[SUBS_SKU]
59
+ matches = subscriptions.select do |subscription|
60
+ line[SUBS_SKU] == subscription['product_id']
61
+ end
62
+ raise _("No subscriptions match SKU '%{sku}'") % {:sku => line[SUBS_SKU]} if matches.empty?
63
+ elsif line[SUBS_NAME]
64
+ matches = subscriptions.select do |subscription|
65
+ line[SUBS_NAME] == subscription['name']
66
+ end
67
+ raise _("No subscriptions match name '%{name}'") % {:name => line[SUBS_NAME]} if matches.empty?
68
+ end
69
+ matches
70
+ end
71
+
72
+ def matches_by_type(matches, line)
73
+ if line[SUBS_TYPE] == 'Red Hat' || line[SUBS_TYPE] == 'Custom'
74
+ matches = matches.select do |subscription|
75
+ subscription['type'] == 'NORMAL'
76
+ end
77
+ elsif line[SUBS_TYPE] == 'Red Hat Guest'
78
+ matches = matches.select do |subscription|
79
+ subscription['type'] == 'STACK_DERIVED'
80
+ end
81
+ elsif line[SUBS_TYPE] == 'Red Hat Temporary'
82
+ matches = matches.select do |subscription|
83
+ subscription['type'] == 'UNMAPPED_GUEST'
84
+ end
85
+ end
86
+ raise _("No subscriptions match type '%{type}'") % {:type => line[SUBS_TYPE]} if matches.empty?
87
+ matches
88
+ end
89
+
90
+ def matches_by_account(matches, line)
91
+ if matches.length > 1 && line[SUBS_ACCOUNT]
92
+ refined = matches.select do |subscription|
93
+ line[SUBS_ACCOUNT] == subscription['account_number']
94
+ end
95
+ matches = refined unless refined.empty?
96
+ end
97
+ matches
98
+ end
99
+
100
+ def matches_by_contract(matches, line)
101
+ if matches.length > 1 && line[SUBS_CONTRACT]
102
+ refined = matches.select do |subscription|
103
+ line[SUBS_CONTRACT] == subscription['contract_number']
104
+ end
105
+ matches = refined unless refined.empty?
106
+ end
107
+ matches
108
+ end
109
+
110
+ def matches_by_quantity(matches, line)
111
+ if line[SUBS_QUANTITY] && line[SUBS_QUANTITY] != 'Automatic'
112
+ refined = matches.select do |subscription|
113
+ subscription['available'] == -1 || line[SUBS_QUANTITY].to_i <= subscription['available']
114
+ end
115
+ raise _("No '%{name}' subscription with quantity %{quantity} or more available") %
116
+ {:name => matches[0]['name'], :quantity => line[SUBS_QUANTITY]} if refined.empty?
117
+ matches = refined
118
+ end
119
+ matches
120
+ end
121
+
122
+ # Subscription amount, SKU, name, contract number, and account number separated by '|'
123
+ # or simply the subscription name.
124
+ def split_subscription_details(details)
125
+ details = details.split('|')
126
+ details.length == 1 ? ['Automatic', nil, details[0], nil, nil] : details
127
+ end
128
+ end
129
+ end
130
+ end
@@ -1,5 +1,5 @@
1
1
  module HammerCLICsv
2
2
  def self.version
3
- @version ||= Gem::Version.new('2.0.0')
3
+ @version ||= Gem::Version.new('2.1.0')
4
4
  end
5
5
  end