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