hammer-cli-csv / lib / hammer_cli_csv / system_groups.rb @ f8ecc788
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 SystemGroupsCommand < BaseCommand |
33 |
|
34 |
ORGANIZATION = 'Organization' |
35 |
LIMIT = 'Limit' |
36 |
DESCRIPTION = 'Description' |
37 |
|
38 |
def export |
39 |
CSV.open(option_csv_file, 'wb') do |csv| |
40 |
csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION] |
41 |
@api.resource(:organizations).call(:index, {'per_page' => 999999})['results'].each do |organization| |
42 |
@api.resource(:system_groups).call(:index, {'organization_id' => organization['label']}).each do |systemgroup| |
43 |
puts systemgroup |
44 |
csv << [systemgroup['name'], 1, organization['label'], |
45 |
systemgroup['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'], |
46 |
systemgroup['description']]
|
47 |
end
|
48 |
end
|
49 |
end
|
50 |
end
|
51 |
|
52 |
def import |
53 |
@existing = {}
|
54 |
|
55 |
thread_import do |line|
|
56 |
create_systemgroups_from_csv(line) |
57 |
end
|
58 |
end
|
59 |
|
60 |
def create_systemgroups_from_csv(line) |
61 |
if !@existing[line[ORGANIZATION]] |
62 |
@existing[line[ORGANIZATION]] = {} |
63 |
@api.resource(:system_groups).call(:index, {'organization_id' => line[ORGANIZATION]})['results'].each do |systemgroup| |
64 |
@existing[line[ORGANIZATION]][systemgroup['name']] = systemgroup['id'] |
65 |
end
|
66 |
end
|
67 |
|
68 |
line[COUNT].to_i.times do |number| |
69 |
name = namify(line[NAME], number)
|
70 |
if !@existing[line[ORGANIZATION]].include? name |
71 |
print "Creating system group '#{name}'..." if option_verbose? |
72 |
@api.resource(:system_groups).call(:create, { |
73 |
'organization_id' => line[ORGANIZATION], |
74 |
'name' => name,
|
75 |
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT], |
76 |
'description' => line[DESCRIPTION] |
77 |
}) |
78 |
else
|
79 |
print "Updating system group '#{name}'..." if option_verbose? |
80 |
@api.resource(:system_groups).call(:update, { |
81 |
'organization_id' => line[ORGANIZATION], |
82 |
'id' => @existing[line[ORGANIZATION]][name], |
83 |
'name' => name,
|
84 |
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT], |
85 |
'description' => line[DESCRIPTION] |
86 |
}) |
87 |
end
|
88 |
print "done\n" if option_verbose? |
89 |
end
|
90 |
end
|
91 |
end
|
92 |
|
93 |
HammerCLICsv::CsvCommand.subcommand("system-groups", |
94 |
"import or export system groups",
|
95 |
HammerCLICsv::SystemGroupsCommand) |
96 |
end
|