hammer-cli-csv / lib / hammer_cli_csv / puppet_environments.rb @ 561a8ac9
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 |
# -= Environments CSV =-
|
14 |
#
|
15 |
# Columns
|
16 |
# Name
|
17 |
# - Environment 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 PuppetEnvironmentsCommand < BaseCommand |
31 |
command_name 'puppet-environments'
|
32 |
desc 'import or export puppet environments'
|
33 |
|
34 |
ORGANIZATIONS = 'Organizations' |
35 |
|
36 |
def export |
37 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
38 |
csv << [NAME, COUNT, ORGANIZATIONS] |
39 |
@api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment| |
40 |
name = environment['name']
|
41 |
count = 1
|
42 |
csv << [name, count] |
43 |
raise 'TODO: organizations'
|
44 |
end
|
45 |
end
|
46 |
end
|
47 |
|
48 |
def import |
49 |
@existing = {}
|
50 |
@api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment| |
51 |
@existing[environment['name']] = environment['id'] if environment |
52 |
end
|
53 |
|
54 |
thread_import do |line|
|
55 |
create_environments_from_csv(line) |
56 |
end
|
57 |
end
|
58 |
|
59 |
def create_environments_from_csv(line) |
60 |
line[COUNT].to_i.times do |number| |
61 |
name = namify(line[NAME], number)
|
62 |
if !@existing.include? name |
63 |
print "Creating environment '#{name}'..." if option_verbose? |
64 |
id = @api.resource(:environments).call(:create, { |
65 |
'environment' => {
|
66 |
'name' => name
|
67 |
} |
68 |
})['id']
|
69 |
else
|
70 |
print "Updating environment '#{name}'..." if option_verbose? |
71 |
id = @api.resource(:environments).call(:update, { |
72 |
'id' => @existing[name], |
73 |
'environment' => {
|
74 |
'name' => name
|
75 |
} |
76 |
})['environment']['id'] |
77 |
end
|
78 |
|
79 |
# Update associated resources
|
80 |
# TODO: Bug #4738: organization json does not include puppet environments
|
81 |
# http://projects.theforeman.org/issues/4738#change-15319
|
82 |
# Update below to match style of domains
|
83 |
organization_ids = CSV.parse_line(line[ORGANIZATIONS]).collect do |organization| |
84 |
foreman_organization(:name => organization)
|
85 |
end
|
86 |
organization_ids += @api.resource(:environments).call(:show, {'id' => id})['organizations'].collect do |organization| |
87 |
organization['id']
|
88 |
end
|
89 |
organization_ids.uniq! |
90 |
|
91 |
@api.resource(:environments).call(:update, { |
92 |
'id' => id,
|
93 |
'environment' => {
|
94 |
'organization_ids' => organization_ids
|
95 |
} |
96 |
}) |
97 |
|
98 |
print "done\n" if option_verbose? |
99 |
end
|
100 |
rescue RuntimeError => e |
101 |
raise "#{e}\n #{line}"
|
102 |
end
|
103 |
end
|
104 |
end
|
105 |
end
|