hammer-cli-csv / lib / hammer_cli_csv / environments.rb @ 370106bb
1 |
# Copyright (c) 2013 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 'katello_api'
|
38 |
require 'foreman_api'
|
39 |
require 'json'
|
40 |
require 'csv'
|
41 |
|
42 |
module HammerCLICsv |
43 |
class EnvironmentsCommand < BaseCommand |
44 |
|
45 |
def initialize(*args) |
46 |
super(args)
|
47 |
@environment_api = ForemanApi::Resources::Environment.new(@init_options[:foreman]) |
48 |
end
|
49 |
|
50 |
def execute |
51 |
csv_export? ? export : import |
52 |
|
53 |
HammerCLI::EX_OK |
54 |
end
|
55 |
|
56 |
def export |
57 |
CSV.open(csv_file, 'wb') do |csv| |
58 |
csv << ['Name']
|
59 |
@environment_api.index({}, HEADERS)[0].each do |environment| |
60 |
environment = environment['environment']
|
61 |
name = environment['name']
|
62 |
count = 1
|
63 |
csv << [name, count] |
64 |
end
|
65 |
end
|
66 |
end
|
67 |
|
68 |
def import |
69 |
@existing = {}
|
70 |
@environment_api.index[0].each do |environment| |
71 |
environment = environment['environment']
|
72 |
@existing[environment['name']] = environment['id'] |
73 |
end
|
74 |
|
75 |
thread_import do |line|
|
76 |
create_environments_from_csv(line) |
77 |
end
|
78 |
end
|
79 |
|
80 |
def create_environments_from_csv(line) |
81 |
details = parse_environment_csv(line) |
82 |
|
83 |
details[:count].times do |number| |
84 |
name = namify(details[:name_format], number)
|
85 |
if !@existing.include? name |
86 |
print "Creating environment '#{name}'..." if verbose? |
87 |
@environment_api.create({
|
88 |
'environment' => {
|
89 |
'name' => name
|
90 |
} |
91 |
}, HEADERS)
|
92 |
print "done\n" if verbose? |
93 |
else
|
94 |
print "Updating environment '#{name}'..." if verbose? |
95 |
@environment_api.create({
|
96 |
'id' => @existing["#{name}-#{details[:major]}-#{details[:minor]}"], |
97 |
'environment' => {
|
98 |
'name' => name
|
99 |
} |
100 |
}, HEADERS)
|
101 |
print "done\n" if verbose? |
102 |
end
|
103 |
end
|
104 |
end
|
105 |
|
106 |
def parse_environment_csv(line) |
107 |
keys = [:name_format, :count] |
108 |
details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0] |
109 |
|
110 |
details[:count] = details[:count].to_i |
111 |
|
112 |
details |
113 |
end
|
114 |
end
|
115 |
|
116 |
HammerCLI::MainCommand.subcommand("csv:environments", "ping the katello server", HammerCLICsv::EnvironmentsCommand) |
117 |
end
|