hammer-cli-csv / lib / hammer_cli_csv / puppet_environments.rb @ bfc065ce
1 |
# Copyright (c) 2013-2014 Red Hat
|
---|---|
2 |
#
|
3 |
# MIT License
|
4 |
#
|
5 |
# Permission is hereby granted, free of charge, to any person obtaining
|
6 |
# a copy of this software and associated documentation files (the
|
7 |
# "Software"), to deal in the Software without restriction, including
|
8 |
# without limitation the rights to use, copy, modify, merge, publish,
|
9 |
# distribute, sublicense, and/or sell copies of the Software, and to
|
10 |
# permit persons to whom the Software is furnished to do so, subject to
|
11 |
# the following conditions:
|
12 |
#
|
13 |
# The above copyright notice and this permission notice shall be
|
14 |
# included in all copies or substantial portions of the Software.
|
15 |
#
|
16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20 |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21 |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22 |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23 |
#
|
24 |
#
|
25 |
# -= Environments CSV =-
|
26 |
#
|
27 |
# Columns
|
28 |
# Name
|
29 |
# - Environment name
|
30 |
# - May contain '%d' which will be replaced with current iteration number of Count
|
31 |
# - eg. "os%d" -> "os1"
|
32 |
# Count
|
33 |
# - Number of times to iterate on this line of the CSV file
|
34 |
#
|
35 |
|
36 |
require 'hammer_cli'
|
37 |
require 'json'
|
38 |
require 'csv'
|
39 |
|
40 |
module HammerCLICsv |
41 |
class PuppetEnvironmentsCommand < BaseCommand |
42 |
|
43 |
ORGANIZATIONS = 'Organizations' |
44 |
|
45 |
def export |
46 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
47 |
csv << [NAME, COUNT, ORGANIZATIONS] |
48 |
@f_environment_api.index({:per_page => 999999})[0]['results'].each do |environment| |
49 |
name = environment['name']
|
50 |
count = 1
|
51 |
raise "TODO: organizations"
|
52 |
csv << [name, count] |
53 |
end
|
54 |
end
|
55 |
end
|
56 |
|
57 |
def import |
58 |
@existing = {}
|
59 |
@f_environment_api.index({:per_page => 999999})[0]['results'].each do |environment| |
60 |
@existing[environment['name']] = environment['id'] if environment |
61 |
end
|
62 |
|
63 |
thread_import do |line|
|
64 |
create_environments_from_csv(line) |
65 |
end
|
66 |
end
|
67 |
|
68 |
def create_environments_from_csv(line) |
69 |
line[COUNT].to_i.times do |number| |
70 |
name = namify(line[NAME], number)
|
71 |
if !@existing.include? name |
72 |
print "Creating environment '#{name}'..." if option_verbose? |
73 |
id = @f_environment_api.create({
|
74 |
'environment' => {
|
75 |
'name' => name
|
76 |
} |
77 |
})[0]['environment']['id'] |
78 |
else
|
79 |
print "Updating environment '#{name}'..." if option_verbose? |
80 |
id = @f_environment_api.update({
|
81 |
'id' => @existing[name], |
82 |
'environment' => {
|
83 |
'name' => name
|
84 |
} |
85 |
})[0]['environment']['id'] |
86 |
end
|
87 |
|
88 |
CSV.parse_line(line[ORGANIZATIONS]).each do |organization| |
89 |
@k_organization_api.update({
|
90 |
'id' => foreman_organization(:name => organization), |
91 |
'environment_ids' => [id]
|
92 |
}) |
93 |
end
|
94 |
|
95 |
print "done\n" if option_verbose? |
96 |
end
|
97 |
rescue RuntimeError => e |
98 |
raise "#{e}\n #{line}"
|
99 |
end
|
100 |
end
|
101 |
|
102 |
HammerCLI::MainCommand.subcommand("csv:puppetenvironments", "Import or export puppet environments", HammerCLICsv::PuppetEnvironmentsCommand) |
103 |
end
|