hammer-cli-csv / lib / hammer_cli_csv / roles.rb @ 90d0b5c8
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 |
module HammerCLICsv |
13 |
class CsvCommand |
14 |
class RolesCommand < BaseCommand |
15 |
command_name 'roles'
|
16 |
desc 'import or export roles'
|
17 |
|
18 |
RESOURCE = 'Resource' |
19 |
SEARCH = 'Search' |
20 |
PERMISSIONS = 'Permissions' |
21 |
ORGANIZATIONS = 'Organizations' |
22 |
LOCATIONS = 'Locations' |
23 |
|
24 |
def export |
25 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| |
26 |
csv << [NAME, COUNT, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS] |
27 |
@api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role| |
28 |
@api.resource(:filters).call(:index, { |
29 |
'per_page' => 999999, |
30 |
'search' => "role=\"#{role['name']}\"" |
31 |
})['results'].each do |filter| |
32 |
filter = @api.resource(:filters).call(:show, 'id' => filter['id']) |
33 |
|
34 |
permissions = export_column(filter, 'permissions', 'name') |
35 |
organizations = export_column(filter, 'organizations', 'name') |
36 |
locations = export_column(filter, 'locations', 'name') |
37 |
csv << [role['name'], 1, filter['resource_type'], filter['search'] || '', permissions, organizations, locations] |
38 |
end
|
39 |
end
|
40 |
end
|
41 |
|
42 |
HammerCLI::EX_OK |
43 |
end
|
44 |
|
45 |
def import |
46 |
@existing_roles = {}
|
47 |
@api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role| |
48 |
@existing_roles[role['name']] = role['id'] |
49 |
end
|
50 |
|
51 |
@existing_filters = {}
|
52 |
@api.resource(:filters).call(:index, {'per_page' => 999999})['results'].each do |role| |
53 |
@existing_filters[role['name']] = role['id'] |
54 |
end
|
55 |
|
56 |
thread_import do |line|
|
57 |
create_roles_from_csv(line) |
58 |
end
|
59 |
end
|
60 |
|
61 |
def create_roles_from_csv(line) |
62 |
|
63 |
permissions = collect_column(line[PERMISSIONS]) do |permission| |
64 |
foreman_permission(:name => permission)
|
65 |
end
|
66 |
organizations = collect_column(line[ORGANIZATIONS]) do |organization| |
67 |
foreman_organization(:name => organization)
|
68 |
end
|
69 |
locations = collect_column(line[LOCATIONS]) do |location| |
70 |
foreman_location(:name => location)
|
71 |
end
|
72 |
|
73 |
line[COUNT].to_i.times do |number| |
74 |
name = namify(line[NAME], number)
|
75 |
search = line[SEARCH] ? namify(line[SEARCH], number) : nil |
76 |
|
77 |
if !@existing_roles[name] |
78 |
print "Creating role '#{name}'..." if option_verbose? |
79 |
role = @api.resource(:roles).call(:create, { |
80 |
'name' => name
|
81 |
}) |
82 |
@existing_roles[name] = role['id'] |
83 |
else
|
84 |
print "Updating role '#{name}'..." if option_verbose? |
85 |
@api.resource(:roles).call(:update, { |
86 |
'id' => @existing_roles[name] |
87 |
}) |
88 |
end
|
89 |
|
90 |
filter_id = foreman_filter(name, line[RESOURCE], search)
|
91 |
if !filter_id
|
92 |
print " creating filter #{line[RESOURCE]}..."
|
93 |
@api.resource(:filters).call(:create, { 'filter' => { |
94 |
'role_id' => @existing_roles[name], |
95 |
'search' => search,
|
96 |
'organization_ids' => organizations,
|
97 |
'location_ids' => locations,
|
98 |
'permission_ids' => permissions
|
99 |
}}) |
100 |
else
|
101 |
print " updating filter #{line[RESOURCE]}..."
|
102 |
@api.resource(:filters).call(:update, { |
103 |
'id' => filter_id,
|
104 |
'search' => search,
|
105 |
'organization_ids' => organizations,
|
106 |
'location_ids' => locations,
|
107 |
'permission_ids' => permissions
|
108 |
}) |
109 |
end
|
110 |
|
111 |
puts 'done' if option_verbose? |
112 |
end
|
113 |
end
|
114 |
end
|
115 |
end
|
116 |
end
|