hammer-cli-csv / lib / hammer_cli_csv / users.rb @ 0c7641a0
1 |
# Copyright (c) 2013 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 |
# Login
|
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 |
# First Name
|
35 |
# Last Name
|
36 |
# Email
|
37 |
#
|
38 |
|
39 |
require 'hammer_cli'
|
40 |
require 'katello_api'
|
41 |
require 'json'
|
42 |
require 'csv'
|
43 |
|
44 |
module HammerCLICsv |
45 |
class UsersCommand < BaseCommand |
46 |
|
47 |
FIRSTNAME = 'First Name' |
48 |
LASTNAME = 'Last Name' |
49 |
EMAIL = 'Email' |
50 |
|
51 |
def execute |
52 |
super
|
53 |
csv_export? ? export : import |
54 |
|
55 |
HammerCLI::EX_OK |
56 |
end
|
57 |
|
58 |
def export |
59 |
CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
60 |
csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL] |
61 |
@f_user_api.index({:per_page => 999999}, HEADERS)[0].each do |user| |
62 |
csv << [user['login'], 1, user['firstname'], user['lastname'], user['mail']] |
63 |
end
|
64 |
end
|
65 |
end
|
66 |
|
67 |
def import |
68 |
@existing = {}
|
69 |
@f_user_api.index({:per_page => 999999}, HEADERS)[0].each do |user| |
70 |
@existing[user['login']] = user['id'] |
71 |
end
|
72 |
|
73 |
thread_import do |line|
|
74 |
create_users_from_csv(line) |
75 |
end
|
76 |
end
|
77 |
|
78 |
def create_users_from_csv(line) |
79 |
line[COUNT].to_i.times do |number| |
80 |
name = namify(line[NAME], number)
|
81 |
if !@existing.include? name |
82 |
print "Creating user '#{name}'... " if verbose? |
83 |
@f_user_api.create({
|
84 |
'user' => {
|
85 |
'login' => name,
|
86 |
'firstname' => line[FIRSTNAME], |
87 |
'lastname' => line[LASTNAME], |
88 |
'mail' => line[EMAIL], |
89 |
'password' => 'admin', |
90 |
'auth_source_id' => 1, # INTERNAL auth |
91 |
} |
92 |
}, HEADERS)
|
93 |
else
|
94 |
print "Updating user '#{name}'... " if verbose? |
95 |
@f_user_api.update({
|
96 |
'id' => @existing[name], |
97 |
'user' => {
|
98 |
'login' => name,
|
99 |
'firstname' => line[FIRSTNAME], |
100 |
'lastname' => line[LASTNAME], |
101 |
'mail' => line[EMAIL], |
102 |
'password' => 'admin' |
103 |
} |
104 |
}, HEADERS)
|
105 |
end
|
106 |
print "done\n" if verbose? |
107 |
end
|
108 |
rescue RuntimeError => e |
109 |
raise RuntimeError.new("#{e}\n #{line}") |
110 |
end
|
111 |
end
|
112 |
|
113 |
HammerCLI::MainCommand.subcommand("csv:users", "ping the katello server", HammerCLICsv::UsersCommand) |
114 |
end
|