hammer_cli_csv 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +15 -0
  2. data/lib/hammer_cli_csv.rb +48 -0
  3. data/lib/hammer_cli_csv/activation_keys.rb +170 -0
  4. data/lib/hammer_cli_csv/architectures.rb +95 -0
  5. data/lib/hammer_cli_csv/base.rb +706 -0
  6. data/lib/hammer_cli_csv/compute_profiles.rb +94 -0
  7. data/lib/hammer_cli_csv/compute_resources.rb +92 -0
  8. data/lib/hammer_cli_csv/content_hosts.rb +357 -0
  9. data/lib/hammer_cli_csv/content_view_filters.rb +158 -0
  10. data/lib/hammer_cli_csv/content_views.rb +86 -0
  11. data/lib/hammer_cli_csv/csv.rb +23 -0
  12. data/lib/hammer_cli_csv/domains.rb +103 -0
  13. data/lib/hammer_cli_csv/exception_handler.rb +53 -0
  14. data/lib/hammer_cli_csv/host_collections.rb +108 -0
  15. data/lib/hammer_cli_csv/hosts.rb +118 -0
  16. data/lib/hammer_cli_csv/import.rb +82 -0
  17. data/lib/hammer_cli_csv/installation_medias.rb +88 -0
  18. data/lib/hammer_cli_csv/lifecycle_environments.rb +116 -0
  19. data/lib/hammer_cli_csv/locations.rb +84 -0
  20. data/lib/hammer_cli_csv/operating_systems.rb +95 -0
  21. data/lib/hammer_cli_csv/organizations.rb +107 -0
  22. data/lib/hammer_cli_csv/partition_tables.rb +98 -0
  23. data/lib/hammer_cli_csv/products.rb +179 -0
  24. data/lib/hammer_cli_csv/provisioning_templates.rb +96 -0
  25. data/lib/hammer_cli_csv/puppet_environments.rb +105 -0
  26. data/lib/hammer_cli_csv/puppet_facts.rb +99 -0
  27. data/lib/hammer_cli_csv/puppet_reports.rb +244 -0
  28. data/lib/hammer_cli_csv/reports.rb +91 -0
  29. data/lib/hammer_cli_csv/roles.rb +115 -0
  30. data/lib/hammer_cli_csv/smart_proxies.rb +88 -0
  31. data/lib/hammer_cli_csv/subnets.rb +121 -0
  32. data/lib/hammer_cli_csv/subscriptions.rb +135 -0
  33. data/lib/hammer_cli_csv/users.rb +133 -0
  34. data/lib/hammer_cli_csv/version.rb +16 -0
  35. data/test/activation_keys_test.rb +17 -0
  36. data/test/apipie_resource_mock.rb +77 -0
  37. data/test/config.template.yml +17 -0
  38. data/test/csv_test_helper.rb +65 -0
  39. data/test/data/activation-keys.csv +119 -0
  40. data/test/data/architectures.csv +5 -0
  41. data/test/data/content-hosts.csv +4 -0
  42. data/test/data/content-view-filters.csv +2 -0
  43. data/test/data/content-views.csv +6 -0
  44. data/test/data/domains.csv +8 -0
  45. data/test/data/host-collections.csv +16 -0
  46. data/test/data/hosts.csv +12 -0
  47. data/test/data/installation-medias.csv +7 -0
  48. data/test/data/lifecycle-environments.csv +6 -0
  49. data/test/data/locations.csv +10 -0
  50. data/test/data/operating-systems.csv +16 -0
  51. data/test/data/organizations.csv +5 -0
  52. data/test/data/partition-tables.csv +187 -0
  53. data/test/data/products.csv +19 -0
  54. data/test/data/puppet-environments.csv +6 -0
  55. data/test/data/puppet-facts.csv +9 -0
  56. data/test/data/reports.csv +4 -0
  57. data/test/data/roles.csv +159 -0
  58. data/test/data/smart-proxies.csv +2 -0
  59. data/test/data/subscriptions.csv +19 -0
  60. data/test/data/users.csv +119 -0
  61. data/test/helpers/command.rb +67 -0
  62. data/test/helpers/resource_disabled.rb +69 -0
  63. data/test/hosts_test.rb +47 -0
  64. data/test/organizations_test.rb +74 -0
  65. data/test/roles_test.rb +39 -0
  66. data/test/setup_test.rb +164 -0
  67. data/test/systems_test.rb +71 -0
  68. data/test/users_test.rb +35 -0
  69. metadata +174 -0
@@ -0,0 +1,91 @@
1
+ # Copyright 2013-2014 Red Hat, Inc.
2
+ #
3
+ # This software is licensed to you under the GNU General Public
4
+ # License as published by the Free Software Foundation; either version
5
+ # 2 of the License (GPLv2) or (at your option) any later version.
6
+ # There is NO WARRANTY for this software, express or implied,
7
+ # including the implied warranties of MERCHANTABILITY,
8
+ # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
+ # have received a copy of GPLv2 along with this software; if not, see
10
+ # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11
+
12
+ module HammerCLICsv
13
+ class CsvCommand
14
+ class ReportsCommand < BaseCommand
15
+ command_name 'reports'
16
+ desc 'import or export reports'
17
+
18
+ TIME = 'Time'
19
+ APPLIED = 'Applied'
20
+ RESTARTED = 'Restarted'
21
+ FAILED = 'Failed'
22
+ FAILED_RESTARTS = 'Failed Restarts'
23
+ SKIPPED = 'Skipped'
24
+ PENDING = 'Pending'
25
+ METRICS = 'Metrics'
26
+
27
+ def export
28
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
29
+ csv << [NAME, COUNT]
30
+ @api.resource(:reports)
31
+ .call(:index, {
32
+ 'per_page' => 999999
33
+ })['results'].each do |report|
34
+ csv << [report['host_name'], 1, report['metrics'].to_json]
35
+ end
36
+ end
37
+
38
+ HammerCLI::EX_OK
39
+ end
40
+
41
+ def import
42
+ @existing_reports = {}
43
+ @api.resource(:reports)
44
+ .call(:index, {
45
+ 'per_page' => 999999
46
+ })['results'].each do |report|
47
+ @existing_reports[report['name']] = report['id']
48
+ end
49
+
50
+ thread_import do |line|
51
+ create_reports_from_csv(line)
52
+ end
53
+ end
54
+
55
+ def create_reports_from_csv(line)
56
+ line[COUNT].to_i.times do |number|
57
+ name = namify(line[NAME], number)
58
+
59
+ if !@existing_reports[name]
60
+ print "Creating report '#{name}'..." if option_verbose?
61
+ reported_at = line[TIME] || Time.now
62
+ report = @api.resource(:reports)
63
+ .call(:create, {
64
+ 'host' => name,
65
+ 'reported_at' => reported_at,
66
+ 'status' => {
67
+ 'applied' => line[APPLIED],
68
+ 'restarted' => line[RESTARTED],
69
+ 'failed' => line[FAILED],
70
+ 'failed_restarts' => line[FAILED_RESTARTS],
71
+ 'skipped' => line[SKIPPED],
72
+ 'pending' => line[PENDING]
73
+ },
74
+ 'metrics' => JSON.parse(line[METRICS]),
75
+ 'logs' => []
76
+ })
77
+ @existing_reports[name] = report['id']
78
+ else
79
+ print "Updating report '#{name}'..." if option_verbose?
80
+ @api.resource(:reports)
81
+ .call(:update, {
82
+ 'id' => @existing_reports[name]
83
+ })
84
+ end
85
+
86
+ puts 'done' if option_verbose?
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,115 @@
1
+ # Copyright 2013-2014 Red Hat, Inc.
2
+ #
3
+ # This software is licensed to you under the GNU General Public
4
+ # License as published by the Free Software Foundation; either version
5
+ # 2 of the License (GPLv2) or (at your option) any later version.
6
+ # There is NO WARRANTY for this software, express or implied,
7
+ # including the implied warranties of MERCHANTABILITY,
8
+ # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
+ # have received a copy of GPLv2 along with this software; if not, see
10
+ # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11
+
12
+ module HammerCLICsv
13
+ class CsvCommand
14
+ class RolesCommand < BaseCommand
15
+ command_name 'roles'
16
+ desc 'import or export roles'
17
+
18
+ RESOURCE = 'Resource'
19
+ SEARCH = 'Search'
20
+ PERMISSIONS = 'Permissions'
21
+ ORGANIZATIONS = 'Organizations'
22
+ LOCATIONS = 'Locations'
23
+
24
+ def export
25
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
26
+ csv << [NAME, COUNT, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS]
27
+ @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
28
+ @api.resource(:filters).call(:index, {
29
+ 'per_page' => 999999,
30
+ 'search' => "role=\"#{role['name']}\""
31
+ })['results'].each do |filter|
32
+ filter = @api.resource(:filters).call(:show, 'id' => filter['id'])
33
+
34
+ permissions = export_column(filter, 'permissions', 'name')
35
+ organizations = export_column(filter, 'organizations', 'name')
36
+ locations = export_column(filter, 'locations', 'name')
37
+ csv << [role['name'], 1, filter['resource_type'], filter['search'] || '', permissions, organizations, locations]
38
+ end
39
+ end
40
+ end
41
+
42
+ HammerCLI::EX_OK
43
+ end
44
+
45
+ def import
46
+ @existing_roles = {}
47
+ @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
48
+ @existing_roles[role['name']] = role['id']
49
+ end
50
+
51
+ @existing_filters = {}
52
+ @api.resource(:filters).call(:index, {'per_page' => 999999})['results'].each do |role|
53
+ @existing_filters[role['name']] = role['id']
54
+ end
55
+
56
+ thread_import do |line|
57
+ create_roles_from_csv(line)
58
+ end
59
+ end
60
+
61
+ def create_roles_from_csv(line)
62
+ line[COUNT].to_i.times do |number|
63
+ name = namify(line[NAME], number)
64
+ search = namify(line[SEARCH], number) if line[SEARCH]
65
+
66
+ if !@existing_roles[name]
67
+ print "Creating role '#{name}'..." if option_verbose?
68
+ role = @api.resource(:roles).call(:create, {
69
+ 'name' => name
70
+ })
71
+ @existing_roles[name] = role['id']
72
+ else
73
+ print "Updating role '#{name}'..." if option_verbose?
74
+ @api.resource(:roles).call(:update, {
75
+ 'id' => @existing_roles[name]
76
+ })
77
+ end
78
+
79
+ permissions = collect_column(line[PERMISSIONS]) do |permission|
80
+ foreman_permission(:name => permission)
81
+ end
82
+ organizations = collect_column(line[ORGANIZATIONS]) do |organization|
83
+ foreman_organization(:name => organization)
84
+ end
85
+ locations = collect_column(line[LOCATIONS]) do |location|
86
+ foreman_location(:name => location)
87
+ end
88
+
89
+ filter_id = foreman_filter(name, line[RESOURCE], search)
90
+ if !filter_id
91
+ print " creating filter #{line[RESOURCE]}..."
92
+ @api.resource(:filters).call(:create, {
93
+ 'role_id' => @existing_roles[name],
94
+ 'search' => search,
95
+ 'organization_ids' => organizations,
96
+ 'location_ids' => locations,
97
+ 'permission_ids' => permissions
98
+ })
99
+ else
100
+ print " updating filter #{line[RESOURCE]}..."
101
+ @api.resource(:filters).call(:update, {
102
+ 'id' => filter_id,
103
+ 'search' => search,
104
+ 'organization_ids' => organizations,
105
+ 'location_ids' => locations,
106
+ 'permission_ids' => permissions
107
+ })
108
+ end
109
+
110
+ puts 'done' if option_verbose?
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,88 @@
1
+ # Copyright 2013-2014 Red Hat, Inc.
2
+ #
3
+ # This software is licensed to you under the GNU General Public
4
+ # License as published by the Free Software Foundation; either version
5
+ # 2 of the License (GPLv2) or (at your option) any later version.
6
+ # There is NO WARRANTY for this software, express or implied,
7
+ # including the implied warranties of MERCHANTABILITY,
8
+ # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
+ # have received a copy of GPLv2 along with this software; if not, see
10
+ # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11
+
12
+ require 'hammer_cli'
13
+ require 'json'
14
+ require 'csv'
15
+
16
+ module HammerCLICsv
17
+ class CsvCommand
18
+ class SmartProxiesCommand < BaseCommand
19
+ command_name 'smart-proxies'
20
+ desc 'import or export smart proxies'
21
+
22
+ ORGANIZATIONS = 'Organizations'
23
+ LOCATIONS = 'Locations'
24
+ URL = 'URL'
25
+ LIFECYCLE_ENVIRONMENTS = 'Lifecycle Environments'
26
+
27
+ def export
28
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
29
+ csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, URL, LIFECYCLE_ENVIRONMENTS]
30
+ @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
31
+ smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => smart_proxy['id']})
32
+ name = smart_proxy['name']
33
+ count = 1
34
+ organizations = export_column(smart_proxy, 'organizations', 'name')
35
+ locations = export_column(smart_proxy, 'locations', 'name')
36
+ url = smart_proxy['url']
37
+ csv << [name, count, organizations, locations, url]
38
+ end
39
+ end
40
+ end
41
+
42
+ def import
43
+ @existing = {}
44
+ @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
45
+ @existing[smart_proxy['url']] = smart_proxy['id'] if smart_proxy
46
+ end
47
+
48
+ thread_import do |line|
49
+ create_smart_proxies_from_csv(line)
50
+ end
51
+ end
52
+
53
+ def create_smart_proxies_from_csv(line)
54
+ line[COUNT].to_i.times do |number|
55
+ name = namify(line[NAME], number)
56
+ if !@existing.include? line[URL]
57
+ print "Creating smart proxy '#{name}'..." if option_verbose?
58
+ id = @api.resource(:smart_proxies)
59
+ .call(:create, {
60
+ 'smart_proxy' => {
61
+ 'name' => name,
62
+ 'url' => line[URL]
63
+ }
64
+ })['id']
65
+ else
66
+ print "Updating smart proxy '#{name}'..." if option_verbose?
67
+ id = @api.resource(:smart_proxies)
68
+ .call(:update, {
69
+ 'id' => @existing[name],
70
+ 'smart_proxy' => {
71
+ 'name' => name,
72
+ 'url' => line[URL]
73
+ }
74
+ })['smart_proxy']['id']
75
+ end
76
+
77
+ # Update associated resources
78
+ associate_organizations(id, line[ORGANIZATIONS], 'smart_proxy')
79
+ associate_locations(id, line[LOCATIONS], 'smart_proxy')
80
+
81
+ print "done\n" if option_verbose?
82
+ end
83
+ rescue RuntimeError => e
84
+ raise "#{e}\n #{line}"
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,121 @@
1
+ # Copyright 2013-2014 Red Hat, Inc.
2
+ #
3
+ # This software is licensed to you under the GNU General Public
4
+ # License as published by the Free Software Foundation; either version
5
+ # 2 of the License (GPLv2) or (at your option) any later version.
6
+ # There is NO WARRANTY for this software, express or implied,
7
+ # including the implied warranties of MERCHANTABILITY,
8
+ # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
+ # have received a copy of GPLv2 along with this software; if not, see
10
+ # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11
+
12
+ require 'hammer_cli'
13
+ require 'json'
14
+ require 'csv'
15
+
16
+ module HammerCLICsv
17
+ class CsvCommand
18
+ class SubnetsCommand < BaseCommand
19
+ command_name 'subnets'
20
+ desc 'import or export subnets'
21
+
22
+ ORGANIZATIONS = 'Organizations'
23
+ LOCATIONS = 'Locations'
24
+ NETWORK = 'Network'
25
+ NETWORK_MASK = 'Network Mask'
26
+ NETWORK_FROM = 'From'
27
+ NETWORK_TO = 'To'
28
+ DOMAINS = 'Domains'
29
+ GATEWAY = 'Gateway'
30
+ DHCP_PROXY = 'DHCP Proxy'
31
+ TFTP_PROXY = 'TFTP Proxy'
32
+ DNS_PROXY = 'DNS Proxy'
33
+ DNS_PRIMARY = 'DNS Primary'
34
+ DNS_SECONDARY = 'DNS Secondary'
35
+ VLAN_ID = 'VLAN ID'
36
+
37
+ def export
38
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
39
+ csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, NETWORK, NETWORK_MASK,
40
+ NETWORK_FROM, NETWORK_TO, DOMAINS, GATEWAY, DHCP_PROXY, TFTP_PROXY, DNS_PROXY,
41
+ DNS_PRIMARY, DNS_SECONDARY, VLAN_ID]
42
+ @api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
43
+ subnet = @api.resource(:subnets).call(:show, {'id' => subnet['id']})
44
+
45
+ name = subnet['name']
46
+ count = 1
47
+ organizations = export_column(subnet, 'organizations', 'name')
48
+ locations = export_column(subnet, 'locations', 'name')
49
+ network = subnet['network']
50
+ network_mask = subnet['mask']
51
+ network_from = subnet['from']
52
+ network_to = subnet['to']
53
+ domains = export_column(subnet, 'domains', 'name')
54
+ gateway = subnet['gateway']
55
+ dhcp_proxy = (subnet['dhcp'] && subnet['dhcp'].key?('name')) ? subnet['dhcp']['name'] : ''
56
+ tftp_proxy = (subnet['tftp'] && subnet['tftp'].key?('name')) ? subnet['tftp']['name'] : ''
57
+ dns_proxy = (subnet['dns'] && subnet['dns'].key?('name')) ? subnet['dns']['name'] : ''
58
+ dns_primary = subnet['dns_primary']
59
+ dns_secondary = subnet['dns_secondary']
60
+ vlan_id = subnet['vlanid']
61
+ csv << [name, count, organizations, locations, network, network_mask,
62
+ network_from, network_to, domains, gateway, dhcp_proxy, tftp_proxy, dns_proxy,
63
+ dns_primary, dns_secondary, vlan_id]
64
+ end
65
+ end
66
+ end
67
+
68
+ def import
69
+ @existing = {}
70
+ @api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
71
+ @existing[subnet['name']] = subnet['id'] if subnet
72
+ end
73
+
74
+ thread_import do |line|
75
+ create_subnets_from_csv(line)
76
+ end
77
+ end
78
+
79
+ def create_subnets_from_csv(line)
80
+ line[DOMAINS] = (CSV.parse_line(line[DOMAINS]) || []).collect do |domain|
81
+ foreman_domain(:name => domain)
82
+ end
83
+
84
+ line[COUNT].to_i.times do |number|
85
+ name = namify(line[NAME], number)
86
+ if !@existing.include? name
87
+ print "Creating subnet '#{name}'..." if option_verbose?
88
+ id = @api.resource(:subnets)
89
+ .call(:create, {
90
+ 'subnet' => {
91
+ 'name' => name
92
+ }
93
+ })['id']
94
+ else
95
+ print "Updating subnet '#{name}'..." if option_verbose?
96
+ id = @api.resource(:subnets)
97
+ .call(:update, {
98
+ 'id' => @existing[name],
99
+ 'subnet' => {
100
+ 'name' => name,
101
+ 'network' => line[NETWORK],
102
+ 'mask' => line[NETWORK_MASK],
103
+ 'from' => line[NETWORK_FROM],
104
+ 'to' => line[NETWORK_TO],
105
+ 'domain_ids' => line[DOMAINS]
106
+ }
107
+ })['id']
108
+ end
109
+
110
+ # Update associated resources
111
+ associate_organizations(id, line[ORGANIZATIONS], 'subnet')
112
+ associate_locations(id, line[LOCATIONS], 'subnet')
113
+
114
+ print "done\n" if option_verbose?
115
+ end
116
+ rescue RuntimeError => e
117
+ raise "#{e}\n #{line}"
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,135 @@
1
+ # Copyright 2013-2014 Red Hat, Inc.
2
+ #
3
+ # This software is licensed to you under the GNU General Public
4
+ # License as published by the Free Software Foundation; either version
5
+ # 2 of the License (GPLv2) or (at your option) any later version.
6
+ # There is NO WARRANTY for this software, express or implied,
7
+ # including the implied warranties of MERCHANTABILITY,
8
+ # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
+ # have received a copy of GPLv2 along with this software; if not, see
10
+ # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11
+
12
+ #
13
+ # -= Systems CSV =-
14
+ #
15
+ # Columns
16
+ # Name
17
+ # - System name
18
+ # - May contain '%d' which will be replaced with current iteration number of Count
19
+ # - eg. "os%d" -> "os1"
20
+ # Count
21
+ # - Number of times to iterate on this line of the CSV file
22
+
23
+ require 'hammer_cli'
24
+ require 'json'
25
+ require 'csv'
26
+ require 'uri'
27
+
28
+ module HammerCLICsv
29
+ class CsvCommand
30
+ class SubscriptionsCommand < BaseCommand
31
+ command_name 'subscriptions'
32
+ desc 'import or export subscriptions'
33
+
34
+ ORGANIZATION = 'Organization'
35
+ MANIFEST = 'Manifest File'
36
+ CONTENT_SET = 'Content Set'
37
+ ARCH = 'Arch'
38
+ RELEASE = 'Release'
39
+
40
+ def export
41
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
42
+ csv << [NAME, COUNT, ORGANIZATION, MANIFEST, CONTENT_SET, ARCH, RELEASE]
43
+ @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
44
+ @api.resource(:products).call(:index, {
45
+ 'per_page' => 999999,
46
+ 'organization_id' => foreman_organization(:name => organization['name']),
47
+ 'enabled' => true
48
+ })['results'].each do |product|
49
+ if product['provider']['name'] == 'Red Hat'
50
+ product['product_content'].each do |product_content|
51
+ if product_content['enabled']
52
+ puts product_content
53
+ content_set = product_content['content']['name']
54
+ release = '?????'
55
+ arches = product_content['content']['arches']
56
+ if arches.nil?
57
+ csv << [product['name'], 1, organization['name'], nil, content_set, nil, release]
58
+ else
59
+ arches.split(',').each do |arch|
60
+ csv << [product['name'], 1, organization['name'], nil, content_set, arch, release]
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ def import
72
+ thread_import do |line|
73
+ if line[MANIFEST] && !line[MANIFEST].empty?
74
+ import_manifest_from_csv(line)
75
+ else
76
+ enable_products_from_csv(line)
77
+ end
78
+ end
79
+ end
80
+
81
+ def enable_products_from_csv(line)
82
+ results = @api.resource(:products).call(:index, {
83
+ 'per_page' => 999999,
84
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
85
+ 'name' => line[NAME]
86
+ })['results']
87
+ raise "No match for product '#{line[NAME]}'" if results.length == 0
88
+ raise "Multiple matches for product '#{line[NAME]}'" if results.length != 1
89
+ product = results[0]
90
+
91
+ results = @api.resource(:repository_sets).call(:index, {
92
+ 'per_page' => 999999,
93
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
94
+ 'product_id' => product['id'],
95
+ 'name' => line[CONTENT_SET]
96
+ })['results']
97
+ raise "No match for content set '#{line[CONTENT_SET]}'" if results.length == 0
98
+ raise "Multiple matches for content set '#{line[CONTENT_SET]}'" if results.length != 1
99
+ repository_set = results[0]
100
+
101
+ repository = repository_set['repositories'].find do |repo|
102
+ repo['name'].end_with?("#{line[ARCH]} #{line[RELEASE]}")
103
+ end
104
+
105
+ if repository.nil?
106
+ print "Enabling repository #{line[CONTENT_SET]} #{line[ARCH]} #{line[RELEASE]}..." if option_verbose?
107
+ product_content = product['product_content'].find do |content|
108
+ content['content']['name'] == line[CONTENT_SET]
109
+ end
110
+ raise "No match for content set '#{line[CONTENT_SET]}'" if !product_content
111
+
112
+ @api.resource(:repository_sets).call(:enable, {
113
+ 'id' => product_content['content']['id'],
114
+ 'product_id' => product['id'],
115
+ 'basearch' => line[ARCH],
116
+ 'releasever' => line[RELEASE]
117
+ })
118
+ puts 'done' if option_verbose?
119
+ else
120
+ puts "Repository #{repository['name']} already enabled" if option_verbose?
121
+ end
122
+ end
123
+
124
+ def import_manifest_from_csv(line)
125
+ # TODO: --server needs to come from config/settings
126
+ args = %W{ subscription upload --file #{ line[MANIFEST] }
127
+ --organization-id #{ foreman_organization(:name => line[ORGANIZATION]) } }
128
+ hammer.run(args)
129
+
130
+ rescue RuntimeError => e
131
+ raise "#{e}\n #{line}"
132
+ end
133
+ end
134
+ end
135
+ end