hammer-cli-csv / lib / hammer_cli_csv / roles.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 |
# -= Users CSV =-
|
14 |
#
|
15 |
# Columns
|
16 |
# Name
|
17 |
# - Login name of the user.
|
18 |
# - May contain '%d' which will be replaced with current iteration number of Count
|
19 |
# - eg. "user%d" -> "user1"
|
20 |
# Count
|
21 |
# - Number of times to iterate on this line of the CSV file
|
22 |
# Description
|
23 |
#
|
24 |
|
25 |
require 'hammer_cli'
|
26 |
require 'json'
|
27 |
require 'csv'
|
28 |
|
29 |
module HammerCLICsv |
30 |
class RolesCommand < BaseCommand |
31 |
|
32 |
ROLE = "Role" |
33 |
FILTER = "Filter" |
34 |
PERMISSIONS = "Permissions" |
35 |
ORGANIZATIONS = "Organizations" |
36 |
LOCATIONS = "Locations" |
37 |
|
38 |
def export |
39 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| |
40 |
csv << [NAME, COUNT, FILTER, PERMISSIONS, ORGANIZATIONS, LOCATIONS] |
41 |
@api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role| |
42 |
@api.resource(:filters).call(:index, { |
43 |
'per_page' => 999999, |
44 |
'search' => "role=\"#{role['name']}\"" |
45 |
})['results'].each do |filter| |
46 |
if filter['search'] && filter['search'] != '' |
47 |
permissions = CSV.generate do |column| |
48 |
column << filter['permissions'].collect do |permission| |
49 |
permission['name']
|
50 |
end
|
51 |
end.delete!("\n") |
52 |
organizations = CSV.generate do |column| |
53 |
column << filter['organizations'].collect do |organization| |
54 |
organization['name']
|
55 |
end
|
56 |
end.delete!("\n") |
57 |
locations = CSV.generate do |column| |
58 |
column << filter['locations'].collect do |location| |
59 |
location['name']
|
60 |
end
|
61 |
end.delete!("\n") |
62 |
csv << [role['name'], 1, filter['search'], permissions, organizations, locations] |
63 |
end
|
64 |
end
|
65 |
end
|
66 |
end
|
67 |
|
68 |
HammerCLI::EX_OK |
69 |
end
|
70 |
|
71 |
def import |
72 |
@existing_roles = {}
|
73 |
@api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role| |
74 |
@existing_roles[role['name']] = role['id'] |
75 |
end
|
76 |
|
77 |
@existing_filters = {}
|
78 |
@api.resource(:filters).call(:index, {'per_page' => 999999})['results'].each do |role| |
79 |
@existing_filters[role['name']] = role['id'] |
80 |
end
|
81 |
|
82 |
thread_import do |line|
|
83 |
create_roles_from_csv(line) |
84 |
end
|
85 |
end
|
86 |
|
87 |
def create_roles_from_csv(line) |
88 |
line[COUNT].to_i.times do |number| |
89 |
name = namify(line[NAME], number)
|
90 |
filter = namify(line[FILTER], number) if line[FILTER] |
91 |
|
92 |
if !@existing_roles[name] |
93 |
print "Creating role '#{name}'..." if option_verbose? |
94 |
role = @api.resource(:roles).call(:create, { |
95 |
'name' => name
|
96 |
}) |
97 |
@existing_roles[name] = role['id'] |
98 |
else
|
99 |
print "Updating role '#{name}'..." if option_verbose? |
100 |
@api.resource(:roles).call(:update, { |
101 |
'id' => @existing_roles[name] |
102 |
}) |
103 |
end
|
104 |
|
105 |
permissions = CSV.parse_line(line[PERMISSIONS], {:skip_blanks => true}).collect do |permission| |
106 |
foreman_permission(:name => permission)
|
107 |
end if line[PERMISSIONS] |
108 |
organizations = CSV.parse_line(line[ORGANIZATIONS], {:skip_blanks => true}).collect do |organization| |
109 |
foreman_organization(:name => organization)
|
110 |
end if line[ORGANIZATIONS] |
111 |
locations = CSV.parse_line(line[LOCATIONS], {:skip_blanks => true}).collect do |location| |
112 |
foreman_location(:name => location)
|
113 |
end if line[LOCATIONS] |
114 |
|
115 |
if filter
|
116 |
filter_id = foreman_filter(name, :name => filter)
|
117 |
if !filter_id
|
118 |
@api.resource(:filters).call(:create, { |
119 |
'role_id' => @existing_roles[name], |
120 |
'search' => filter,
|
121 |
'organization_ids' => organizations || [],
|
122 |
'location_ids' => locations || [],
|
123 |
'permission_ids' => permissions || []
|
124 |
}) |
125 |
else
|
126 |
@api.resource(:filters).call(:update, { |
127 |
'id' => filter_id,
|
128 |
'search' => filter,
|
129 |
'organization_ids' => organizations || [],
|
130 |
'location_ids' => locations || [],
|
131 |
'permission_ids' => permissions || []
|
132 |
}) |
133 |
end
|
134 |
end
|
135 |
|
136 |
puts "done" if option_verbose? |
137 |
end
|
138 |
end
|
139 |
end
|
140 |
|
141 |
HammerCLICsv::CsvCommand.subcommand("roles", |
142 |
"import or export roles",
|
143 |
HammerCLICsv::RolesCommand) |
144 |
end
|