hammer-cli-csv / lib / hammer_cli_csv / host_collections.rb @ a77acc4a
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).call(:index, {'per_page' => 999999})['results'].each do |organization| |
45 |
@api.resource(:host_collections).call(:index, {'organization_id' => organization['id']}).each do |hostcollection| |
46 |
puts hostcollection |
47 |
csv << [hostcollection['name'], 1, organization['id'], |
48 |
hostcollection['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'], |
49 |
hostcollection['description']]
|
50 |
end
|
51 |
end
|
52 |
end
|
53 |
end
|
54 |
|
55 |
def import |
56 |
@existing = {}
|
57 |
|
58 |
thread_import do |line|
|
59 |
create_hostcollections_from_csv(line) |
60 |
end
|
61 |
end
|
62 |
|
63 |
def create_hostcollections_from_csv(line) |
64 |
if !@existing[line[ORGANIZATION]] |
65 |
@existing[line[ORGANIZATION]] = {} |
66 |
@api.resource(:host_collections).call(:index, { |
67 |
'per_page' => 999999, |
68 |
'organization_id' => foreman_organization(:name => line[ORGANIZATION]) |
69 |
})['results'].each do |hostcollection| |
70 |
@existing[line[ORGANIZATION]][hostcollection['name']] = hostcollection['id'] |
71 |
end
|
72 |
end
|
73 |
|
74 |
line[COUNT].to_i.times do |number| |
75 |
name = namify(line[NAME], number)
|
76 |
if !@existing[line[ORGANIZATION]].include? name |
77 |
print "Creating system group '#{name}'..." if option_verbose? |
78 |
@api.resource(:host_collections).call(:create, { |
79 |
'organization_id' => foreman_organization(:name => line[ORGANIZATION]), |
80 |
'name' => name,
|
81 |
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT], |
82 |
'description' => line[DESCRIPTION] |
83 |
}) |
84 |
else
|
85 |
print "Updating system group '#{name}'..." if option_verbose? |
86 |
@api.resource(:host_collections).call(:update, { |
87 |
'organization_id' => line[ORGANIZATION], |
88 |
'id' => @existing[line[ORGANIZATION]][name], |
89 |
'name' => name,
|
90 |
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT], |
91 |
'description' => line[DESCRIPTION] |
92 |
}) |
93 |
end
|
94 |
print "done\n" if option_verbose? |
95 |
end
|
96 |
end
|
97 |
end
|
98 |
end
|
99 |
end
|