hammer-cli-csv / lib / hammer_cli_csv / users.rb @ 561a8ac9
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 UsersCommand < BaseCommand |
15 |
command_name 'users'
|
16 |
desc 'import or export users'
|
17 |
|
18 |
FIRSTNAME = 'First Name' |
19 |
LASTNAME = 'Last Name' |
20 |
EMAIL = 'Email' |
21 |
ORGANIZATIONS = 'Organizations' |
22 |
LOCATIONS = 'Locations' |
23 |
ROLES = 'Roles' |
24 |
|
25 |
def export |
26 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
27 |
csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ROLES] |
28 |
@api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user| |
29 |
if user['organizations'] |
30 |
organizations = CSV.generate do |column| |
31 |
column << user['organizations'].collect do |organization| |
32 |
organization['name']
|
33 |
end
|
34 |
end
|
35 |
organizations.delete!("\n")
|
36 |
end
|
37 |
if user['locations'] |
38 |
locations = CSV.generate do |column| |
39 |
column << user['locations'].collect do |location| |
40 |
location['name']
|
41 |
end
|
42 |
end
|
43 |
locations.delete!("\n")
|
44 |
end
|
45 |
if user['roles'] |
46 |
roles = CSV.generate do |column| |
47 |
column << user['roles'].collect do |role| |
48 |
role['name']
|
49 |
end
|
50 |
end
|
51 |
roles.delete!("\n")
|
52 |
end
|
53 |
if user['login'] != 'admin' && !user['login'].start_with?('hidden-') |
54 |
csv << [user['login'], 1, user['firstname'], user['lastname'], user['mail'], |
55 |
organizations, locations, roles] |
56 |
end
|
57 |
end
|
58 |
end
|
59 |
end
|
60 |
|
61 |
def import |
62 |
@existing = {}
|
63 |
@api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user| |
64 |
@existing[user['login']] = user['id'] if user |
65 |
end
|
66 |
|
67 |
thread_import do |line|
|
68 |
create_users_from_csv(line) |
69 |
end
|
70 |
end
|
71 |
|
72 |
def create_users_from_csv(line) |
73 |
line[COUNT].to_i.times do |number| |
74 |
name = namify(line[NAME], number)
|
75 |
|
76 |
roles = collect_column(line[ROLES]) do |role| |
77 |
foreman_role(:name => role)
|
78 |
end
|
79 |
organizations = collect_column(line[ORGANIZATIONS]) do |organization| |
80 |
foreman_organization(:name => organization)
|
81 |
end
|
82 |
locations = collect_column(line[LOCATIONS]) do |location| |
83 |
foreman_location(:name => location)
|
84 |
end
|
85 |
|
86 |
if !@existing.include? name |
87 |
create_user(line, name, roles, organizations, locations) |
88 |
else
|
89 |
update_user(line, name, roles, organizations, locations) |
90 |
end
|
91 |
print "done\n" if option_verbose? |
92 |
end
|
93 |
rescue RuntimeError => e |
94 |
raise "#{e}\n #{line}"
|
95 |
end
|
96 |
|
97 |
def create_user(line, name, roles, organizations, locations) |
98 |
print "Creating user '#{name}'... " if option_verbose? |
99 |
@api.resource(:users).call(:create, { |
100 |
'user' => {
|
101 |
'login' => name,
|
102 |
'firstname' => line[FIRSTNAME], |
103 |
'lastname' => line[LASTNAME], |
104 |
'mail' => line[EMAIL], |
105 |
'password' => 'redhat', |
106 |
'auth_source_id' => 1, # INTERNAL auth |
107 |
'role_ids' => roles,
|
108 |
'organization_ids' => organizations,
|
109 |
'location_ids' => locations
|
110 |
} |
111 |
}) |
112 |
end
|
113 |
|
114 |
def update_user(line, name, roles, organizations, locations) |
115 |
print "Updating user '#{name}'... " if option_verbose? |
116 |
@api.resource(:users).call(:update, { |
117 |
'id' => @existing[name], |
118 |
'user' => {
|
119 |
'login' => name,
|
120 |
'firstname' => line[FIRSTNAME], |
121 |
'lastname' => line[LASTNAME], |
122 |
'password' => 'redhat', |
123 |
'mail' => line[EMAIL], |
124 |
'role_ids' => roles,
|
125 |
'organization_ids' => organizations,
|
126 |
'location_ids' => locations
|
127 |
} |
128 |
}) |
129 |
end
|
130 |
end
|
131 |
autoload_subcommands |
132 |
end
|
133 |
end
|