hammer_cli_csv 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/hammer_cli_csv.rb +48 -0
- data/lib/hammer_cli_csv/activation_keys.rb +170 -0
- data/lib/hammer_cli_csv/architectures.rb +95 -0
- data/lib/hammer_cli_csv/base.rb +706 -0
- data/lib/hammer_cli_csv/compute_profiles.rb +94 -0
- data/lib/hammer_cli_csv/compute_resources.rb +92 -0
- data/lib/hammer_cli_csv/content_hosts.rb +357 -0
- data/lib/hammer_cli_csv/content_view_filters.rb +158 -0
- data/lib/hammer_cli_csv/content_views.rb +86 -0
- data/lib/hammer_cli_csv/csv.rb +23 -0
- data/lib/hammer_cli_csv/domains.rb +103 -0
- data/lib/hammer_cli_csv/exception_handler.rb +53 -0
- data/lib/hammer_cli_csv/host_collections.rb +108 -0
- data/lib/hammer_cli_csv/hosts.rb +118 -0
- data/lib/hammer_cli_csv/import.rb +82 -0
- data/lib/hammer_cli_csv/installation_medias.rb +88 -0
- data/lib/hammer_cli_csv/lifecycle_environments.rb +116 -0
- data/lib/hammer_cli_csv/locations.rb +84 -0
- data/lib/hammer_cli_csv/operating_systems.rb +95 -0
- data/lib/hammer_cli_csv/organizations.rb +107 -0
- data/lib/hammer_cli_csv/partition_tables.rb +98 -0
- data/lib/hammer_cli_csv/products.rb +179 -0
- data/lib/hammer_cli_csv/provisioning_templates.rb +96 -0
- data/lib/hammer_cli_csv/puppet_environments.rb +105 -0
- data/lib/hammer_cli_csv/puppet_facts.rb +99 -0
- data/lib/hammer_cli_csv/puppet_reports.rb +244 -0
- data/lib/hammer_cli_csv/reports.rb +91 -0
- data/lib/hammer_cli_csv/roles.rb +115 -0
- data/lib/hammer_cli_csv/smart_proxies.rb +88 -0
- data/lib/hammer_cli_csv/subnets.rb +121 -0
- data/lib/hammer_cli_csv/subscriptions.rb +135 -0
- data/lib/hammer_cli_csv/users.rb +133 -0
- data/lib/hammer_cli_csv/version.rb +16 -0
- data/test/activation_keys_test.rb +17 -0
- data/test/apipie_resource_mock.rb +77 -0
- data/test/config.template.yml +17 -0
- data/test/csv_test_helper.rb +65 -0
- data/test/data/activation-keys.csv +119 -0
- data/test/data/architectures.csv +5 -0
- data/test/data/content-hosts.csv +4 -0
- data/test/data/content-view-filters.csv +2 -0
- data/test/data/content-views.csv +6 -0
- data/test/data/domains.csv +8 -0
- data/test/data/host-collections.csv +16 -0
- data/test/data/hosts.csv +12 -0
- data/test/data/installation-medias.csv +7 -0
- data/test/data/lifecycle-environments.csv +6 -0
- data/test/data/locations.csv +10 -0
- data/test/data/operating-systems.csv +16 -0
- data/test/data/organizations.csv +5 -0
- data/test/data/partition-tables.csv +187 -0
- data/test/data/products.csv +19 -0
- data/test/data/puppet-environments.csv +6 -0
- data/test/data/puppet-facts.csv +9 -0
- data/test/data/reports.csv +4 -0
- data/test/data/roles.csv +159 -0
- data/test/data/smart-proxies.csv +2 -0
- data/test/data/subscriptions.csv +19 -0
- data/test/data/users.csv +119 -0
- data/test/helpers/command.rb +67 -0
- data/test/helpers/resource_disabled.rb +69 -0
- data/test/hosts_test.rb +47 -0
- data/test/organizations_test.rb +74 -0
- data/test/roles_test.rb +39 -0
- data/test/setup_test.rb +164 -0
- data/test/systems_test.rb +71 -0
- data/test/users_test.rb +35 -0
- metadata +174 -0
@@ -0,0 +1,158 @@
|
|
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
|
+
# NOTE:
|
13
|
+
# rpm -qa --queryformat "%{NAME}|=|%{VERSION}-%{RELEASE},"
|
14
|
+
|
15
|
+
module HammerCLICsv
|
16
|
+
class CsvCommand
|
17
|
+
class ContentViewFiltersCommand < BaseCommand
|
18
|
+
command_name 'content-view-filters'
|
19
|
+
desc 'import or export content-view-filters'
|
20
|
+
|
21
|
+
CONTENTVIEW = 'Content View'
|
22
|
+
ORGANIZATION = 'Organization'
|
23
|
+
TYPE = 'Type'
|
24
|
+
DESCRIPTION = 'Description'
|
25
|
+
REPOSITORIES = 'Repositories'
|
26
|
+
RULES = 'Rules'
|
27
|
+
|
28
|
+
def export
|
29
|
+
# TODO
|
30
|
+
end
|
31
|
+
|
32
|
+
def import
|
33
|
+
@existing_filters = {}
|
34
|
+
|
35
|
+
thread_import do |line|
|
36
|
+
create_filters_from_csv(line)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def create_filters_from_csv(line)
|
41
|
+
@existing_filters[line[ORGANIZATION]] ||= {}
|
42
|
+
if !@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]]
|
43
|
+
@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
|
44
|
+
@api.resource(:content_view_filters)
|
45
|
+
.call(:index, {
|
46
|
+
'per_page' => 999999,
|
47
|
+
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
|
48
|
+
})['results'].each do |filter|
|
49
|
+
@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][filter['name']] = filter['id'] if filter
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
repository_ids = collect_column(line[REPOSITORIES]) do |repository|
|
54
|
+
katello_repository(line[ORGANIZATION], :name => repository)
|
55
|
+
end
|
56
|
+
|
57
|
+
line[COUNT].to_i.times do |number|
|
58
|
+
name = namify(line[NAME], number)
|
59
|
+
|
60
|
+
filter_id = @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][name]
|
61
|
+
if !filter_id
|
62
|
+
print "Creating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
|
63
|
+
filter_id = @api.resource(:content_view_filters)
|
64
|
+
.call(:create, {
|
65
|
+
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
|
66
|
+
'name' => name,
|
67
|
+
'description' => line[DESCRIPTION],
|
68
|
+
'type' => filter_type(line[TYPE]),
|
69
|
+
'inclusion' => filter_inclusion?(line[TYPE]),
|
70
|
+
'repository_ids' => repository_ids
|
71
|
+
})['id']
|
72
|
+
@existing_filters[line[ORGANIZATION]][name] = filter_id
|
73
|
+
else
|
74
|
+
print "Updating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
|
75
|
+
@api.resource(:content_view_filters)
|
76
|
+
.call(:update, {
|
77
|
+
'id' => filter_id,
|
78
|
+
'description' => line[DESCRIPTION],
|
79
|
+
'type' => filter_type(line[TYPE]),
|
80
|
+
'inclusion' => filter_inclusion?(line[TYPE]),
|
81
|
+
'repository_ids' => repository_ids
|
82
|
+
})
|
83
|
+
end
|
84
|
+
|
85
|
+
@existing_rules ||= {}
|
86
|
+
@existing_rules[line[ORGANIZATION]] ||= {}
|
87
|
+
@existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
|
88
|
+
@api.resource(:content_view_filter_rules)
|
89
|
+
.call(:index, {
|
90
|
+
'per_page' => 999999,
|
91
|
+
'content_view_filter_id' => filter_id
|
92
|
+
})['results'].each do |rule|
|
93
|
+
@existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
|
94
|
+
end
|
95
|
+
|
96
|
+
collect_column(line[RULES]) do |rule|
|
97
|
+
name, type, version = rule.split('|')
|
98
|
+
params = {
|
99
|
+
'content_view_filter_id' => filter_id,
|
100
|
+
'name' => name
|
101
|
+
}
|
102
|
+
if type == '='
|
103
|
+
params['type'] = 'equal',
|
104
|
+
params['version'] = version
|
105
|
+
elsif type == '<'
|
106
|
+
params['type'] = 'less',
|
107
|
+
params['max_version'] = version
|
108
|
+
elsif type == '>'
|
109
|
+
params['type'] = 'greater',
|
110
|
+
params['min_version'] = version
|
111
|
+
elsif type == '-'
|
112
|
+
params['type'] = 'range',
|
113
|
+
min_version, max_version = version.split(',')
|
114
|
+
params['min_version'] = min_version
|
115
|
+
params['max_version'] = max_version
|
116
|
+
else
|
117
|
+
raise "Unknown type '#{type}' from '#{line[RULES]}'"
|
118
|
+
end
|
119
|
+
|
120
|
+
rule = @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][name]
|
121
|
+
if !rule
|
122
|
+
print "creating rule '#{rule}'..." if option_verbose?
|
123
|
+
rule = @api.resource(:content_view_filter_rules).call(:create, params)
|
124
|
+
@existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
|
125
|
+
else
|
126
|
+
print "updating rule '#{rule}'..." if option_verbose?
|
127
|
+
params['id'] = rule['id']
|
128
|
+
@api.resource(:content_view_filter_rules).call(:update, params)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
puts 'done' if option_verbose?
|
133
|
+
end
|
134
|
+
|
135
|
+
rescue RuntimeError => e
|
136
|
+
raise "#{e}\n #{line}"
|
137
|
+
end
|
138
|
+
|
139
|
+
private
|
140
|
+
|
141
|
+
def filter_type(type)
|
142
|
+
if type.split[1] == 'RPM'
|
143
|
+
'rpm'
|
144
|
+
else
|
145
|
+
'unknown'
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def filter_inclusion?(type)
|
150
|
+
if type.split[0] == 'Include'
|
151
|
+
true
|
152
|
+
else
|
153
|
+
false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,86 @@
|
|
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 ContentViewsCommand < BaseCommand
|
15
|
+
command_name 'content-views'
|
16
|
+
desc 'import or export content-views'
|
17
|
+
|
18
|
+
ORGANIZATION = 'Organization'
|
19
|
+
DESCRIPTION = 'Description'
|
20
|
+
COMPOSITE = 'Composite'
|
21
|
+
REPOSITORIES = 'Repositories'
|
22
|
+
|
23
|
+
def export
|
24
|
+
# TODO
|
25
|
+
end
|
26
|
+
|
27
|
+
def import
|
28
|
+
@existing_contentviews = {}
|
29
|
+
|
30
|
+
thread_import do |line|
|
31
|
+
create_contentviews_from_csv(line)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def create_contentviews_from_csv(line)
|
36
|
+
if !@existing_contentviews[line[ORGANIZATION]]
|
37
|
+
@existing_contentviews[line[ORGANIZATION]] ||= {}
|
38
|
+
@api.resource(:content_views)
|
39
|
+
.call(:index, {
|
40
|
+
'per_page' => 999999,
|
41
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
42
|
+
'nondefault' => true
|
43
|
+
})['results'].each do |contentview|
|
44
|
+
@existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
repository_ids = collect_column(line[REPOSITORIES]) do |repository|
|
49
|
+
katello_repository(line[ORGANIZATION], :name => repository)
|
50
|
+
end
|
51
|
+
|
52
|
+
line[COUNT].to_i.times do |number|
|
53
|
+
name = namify(line[NAME], number)
|
54
|
+
composite = line[COMPOSITE] == 'Yes' ? true : false
|
55
|
+
|
56
|
+
contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
|
57
|
+
if !contentview_id
|
58
|
+
print "Creating content view '#{name}'..." if option_verbose?
|
59
|
+
contentview_id = @api.resource(:content_views)
|
60
|
+
.call(:create, {
|
61
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
62
|
+
'name' => name,
|
63
|
+
'label' => labelize(name),
|
64
|
+
'description' => line[DESCRIPTION],
|
65
|
+
'composite' => composite,
|
66
|
+
'repository_ids' => repository_ids
|
67
|
+
})['id']
|
68
|
+
@existing_contentviews[line[ORGANIZATION]][name] = contentview_id
|
69
|
+
else
|
70
|
+
print "Updating content view '#{name}'..." if option_verbose?
|
71
|
+
@api.resource(:content_views)
|
72
|
+
.call(:update, {
|
73
|
+
'id' => contentview_id,
|
74
|
+
'description' => line[DESCRIPTION],
|
75
|
+
'repository_ids' => repository_ids
|
76
|
+
})
|
77
|
+
end
|
78
|
+
puts 'done' if option_verbose?
|
79
|
+
end
|
80
|
+
|
81
|
+
rescue RuntimeError => e
|
82
|
+
raise "#{e}\n #{line}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
# Copyright 2013-2014 Red Hat, Inc.
|
3
|
+
#
|
4
|
+
# This software is licensed to you under the GNU General Public
|
5
|
+
# License as published by the Free Software Foundation; either version
|
6
|
+
# 2 of the License (GPLv2) or (at your option) any later version.
|
7
|
+
# There is NO WARRANTY for this software, express or implied,
|
8
|
+
# including the implied warranties of MERCHANTABILITY,
|
9
|
+
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
|
10
|
+
# have received a copy of GPLv2 along with this software; if not, see
|
11
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
|
12
|
+
|
13
|
+
require 'hammer_cli'
|
14
|
+
require 'hammer_cli/exit_codes'
|
15
|
+
|
16
|
+
module HammerCLICsv
|
17
|
+
class CsvCommand < HammerCLI::AbstractCommand
|
18
|
+
end
|
19
|
+
|
20
|
+
HammerCLI::MainCommand.subcommand('csv',
|
21
|
+
_('import to, or export from a running foretello server'),
|
22
|
+
HammerCLICsv::CsvCommand)
|
23
|
+
end
|
@@ -0,0 +1,103 @@
|
|
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
|
+
# -= Domains CSV =-
|
14
|
+
#
|
15
|
+
# Columns
|
16
|
+
# Name
|
17
|
+
# - Domain 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
|
+
|
24
|
+
require 'hammer_cli'
|
25
|
+
require 'json'
|
26
|
+
require 'csv'
|
27
|
+
|
28
|
+
module HammerCLICsv
|
29
|
+
class CsvCommand
|
30
|
+
class DomainsCommand < BaseCommand
|
31
|
+
command_name 'domains'
|
32
|
+
desc 'import or export domains'
|
33
|
+
|
34
|
+
FULLNAME = 'Full Name'
|
35
|
+
ORGANIZATIONS = 'Organizations'
|
36
|
+
|
37
|
+
def export
|
38
|
+
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
39
|
+
csv << [NAME, COUNT, FULLNAME]
|
40
|
+
@api.resource(:domains).call(:index, {:per_page => 999999})['results'].each do |domain|
|
41
|
+
puts domain
|
42
|
+
name = domain['name']
|
43
|
+
count = 1
|
44
|
+
fullname = domain['fullname']
|
45
|
+
csv << [name, count, fullname]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def import
|
51
|
+
@existing = {}
|
52
|
+
@api.resource(:domains).call(:index, {:per_page => 999999})['results'].each do |domain|
|
53
|
+
@existing[domain['name']] = domain['id'] if domain
|
54
|
+
end
|
55
|
+
|
56
|
+
thread_import do |line|
|
57
|
+
create_domains_from_csv(line)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_domains_from_csv(line)
|
62
|
+
line[COUNT].to_i.times do |number|
|
63
|
+
name = namify(line[NAME], number)
|
64
|
+
if !@existing.include? name
|
65
|
+
print "Creating domain '#{name}'..." if option_verbose?
|
66
|
+
domain_id = @api.resource(:domains).call(:create, {
|
67
|
+
'name' => name
|
68
|
+
})['id']
|
69
|
+
else
|
70
|
+
print "Updating domain '#{name}'..." if option_verbose?
|
71
|
+
domain_id = @api.resource(:domains).call(:update, {
|
72
|
+
'id' => @existing[name],
|
73
|
+
'name' => name
|
74
|
+
})['id']
|
75
|
+
end
|
76
|
+
|
77
|
+
# Update associated resources
|
78
|
+
domains ||= {}
|
79
|
+
CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
|
80
|
+
organization_id = foreman_organization(:name => organization)
|
81
|
+
if domains[organization].nil?
|
82
|
+
domains[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['domains'].collect do |domain|
|
83
|
+
domain['id']
|
84
|
+
end
|
85
|
+
end
|
86
|
+
domains[organization] += [domain_id] if !domains[organization].include? domain_id
|
87
|
+
|
88
|
+
@api.resource(:organizations).call(:update, {
|
89
|
+
'id' => organization_id,
|
90
|
+
'organization' => {
|
91
|
+
'domain_ids' => domains[organization]
|
92
|
+
}
|
93
|
+
})
|
94
|
+
end
|
95
|
+
|
96
|
+
print "done\n" if option_verbose?
|
97
|
+
end
|
98
|
+
rescue RuntimeError => e
|
99
|
+
raise "#{e}\n #{line}"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,53 @@
|
|
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/exception_handler'
|
13
|
+
|
14
|
+
module HammerCLICsv
|
15
|
+
class ExceptionHandler < HammerCLI::ExceptionHandler
|
16
|
+
def mappings
|
17
|
+
super + [
|
18
|
+
[Exception, :handle_csv_exception],
|
19
|
+
[RestClient::Forbidden, :handle_forbidden],
|
20
|
+
[RestClient::UnprocessableEntity, :handle_unprocessable_entity],
|
21
|
+
[ArgumentError, :handle_argument_error]
|
22
|
+
]
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def handle_csv_exception(e)
|
28
|
+
$stderr.puts e.message
|
29
|
+
log_full_error e
|
30
|
+
HammerCLI::EX_DATAERR
|
31
|
+
end
|
32
|
+
|
33
|
+
def handle_unprocessable_entity(e)
|
34
|
+
response = JSON.parse(e.response)
|
35
|
+
response = response[response.keys[0]]
|
36
|
+
|
37
|
+
print_error response['full_messages']
|
38
|
+
HammerCLI::EX_DATAERR
|
39
|
+
end
|
40
|
+
|
41
|
+
def handle_argument_error(e)
|
42
|
+
print_error e.message
|
43
|
+
log_full_error e
|
44
|
+
HammerCLI::EX_USAGE
|
45
|
+
end
|
46
|
+
|
47
|
+
def handle_forbidden(e)
|
48
|
+
print_error 'Forbidden - server refused to process the request'
|
49
|
+
log_full_error e
|
50
|
+
HammerCLI::EX_NOPERM
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,108 @@
|
|
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
|
+
# -= System Groups CSV =-
|
14
|
+
#
|
15
|
+
# Columns
|
16
|
+
# Name
|
17
|
+
# - System group name
|
18
|
+
# - May contain '%d' which will be replaced with current iteration number of Count
|
19
|
+
# - eg. "group%d" -> "group1"
|
20
|
+
# Count
|
21
|
+
# - Number of times to iterate on this line of the CSV file
|
22
|
+
# Org Label
|
23
|
+
# Limit
|
24
|
+
# Description
|
25
|
+
#
|
26
|
+
|
27
|
+
require 'hammer_cli'
|
28
|
+
require 'json'
|
29
|
+
require 'csv'
|
30
|
+
|
31
|
+
module HammerCLICsv
|
32
|
+
class CsvCommand
|
33
|
+
class HostCollectionsCommand < BaseCommand
|
34
|
+
command_name 'host-collections'
|
35
|
+
desc 'import or export host collections'
|
36
|
+
|
37
|
+
ORGANIZATION = 'Organization'
|
38
|
+
LIMIT = 'Limit'
|
39
|
+
DESCRIPTION = 'Description'
|
40
|
+
|
41
|
+
def export
|
42
|
+
CSV.open(option_csv_file, 'wb') do |csv|
|
43
|
+
csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION]
|
44
|
+
@api.resource(:organizations)
|
45
|
+
.call(:index, {
|
46
|
+
'per_page' => 999999
|
47
|
+
})['results'].each do |organization|
|
48
|
+
@api.resource(:host_collections)
|
49
|
+
.call(:index, {
|
50
|
+
'organization_id' => organization['id']
|
51
|
+
}).each do |hostcollection|
|
52
|
+
puts hostcollection
|
53
|
+
csv << [hostcollection['name'], 1, organization['id'],
|
54
|
+
hostcollection['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'],
|
55
|
+
hostcollection['description']]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def import
|
62
|
+
@existing = {}
|
63
|
+
|
64
|
+
thread_import do |line|
|
65
|
+
create_hostcollections_from_csv(line)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_hostcollections_from_csv(line)
|
70
|
+
if !@existing[line[ORGANIZATION]]
|
71
|
+
@existing[line[ORGANIZATION]] = {}
|
72
|
+
@api.resource(:host_collections)
|
73
|
+
.call(:index, {
|
74
|
+
'per_page' => 999999,
|
75
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION])
|
76
|
+
})['results'].each do |hostcollection|
|
77
|
+
@existing[line[ORGANIZATION]][hostcollection['name']] = hostcollection['id']
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
line[COUNT].to_i.times do |number|
|
82
|
+
name = namify(line[NAME], number)
|
83
|
+
if !@existing[line[ORGANIZATION]].include? name
|
84
|
+
print "Creating system group '#{name}'..." if option_verbose?
|
85
|
+
@api.resource(:host_collections)
|
86
|
+
.call(:create, {
|
87
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
88
|
+
'name' => name,
|
89
|
+
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
|
90
|
+
'description' => line[DESCRIPTION]
|
91
|
+
})
|
92
|
+
else
|
93
|
+
print "Updating system group '#{name}'..." if option_verbose?
|
94
|
+
@api.resource(:host_collections)
|
95
|
+
.call(:update, {
|
96
|
+
'organization_id' => line[ORGANIZATION],
|
97
|
+
'id' => @existing[line[ORGANIZATION]][name],
|
98
|
+
'name' => name,
|
99
|
+
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
|
100
|
+
'description' => line[DESCRIPTION]
|
101
|
+
})
|
102
|
+
end
|
103
|
+
print "done\n" if option_verbose?
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|