Class: HammerCLICsv::CsvCommand::UsersCommand
- Inherits:
-
BaseCommand
- Object
- HammerCLI::Apipie::Command
- BaseCommand
- HammerCLICsv::CsvCommand::UsersCommand
show all
- Defined in:
- lib/hammer_cli_csv/users.rb
Constant Summary
collapse
- FIRSTNAME =
'First Name'
- LASTNAME =
'Last Name'
- EMAIL =
'Email'
- ORGANIZATIONS =
'Organizations'
- LOCATIONS =
'Locations'
- ADMIN =
'Administrator'
- ROLES =
'Roles'
Constants inherited
from BaseCommand
BaseCommand::COUNT, BaseCommand::NAME
Instance Method Summary
collapse
Methods inherited from BaseCommand
#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import
Instance Method Details
#create_user(line, name, roles, organizations, locations) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/hammer_cli_csv/users.rb', line 99
def create_user(line, name, roles, organizations, locations)
print "Creating user '#{name}'... " if option_verbose?
@api.resource(:users).call(:create, {
'user' => {
'login' => name,
'firstname' => line[FIRSTNAME],
'lastname' => line[LASTNAME],
'mail' => line[EMAIL],
'password' => 'redhat',
'auth_source_id' => 1,
'admin' => line[ADMIN] == 'Yes' ? true : false,
'role_ids' => roles,
'organization_ids' => organizations,
'location_ids' => locations
}
})
end
|
#create_users_from_csv(line) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/hammer_cli_csv/users.rb', line 74
def create_users_from_csv(line)
line[COUNT].to_i.times do |number|
name = namify(line[NAME], number)
roles = collect_column(line[ROLES]) do |role|
foreman_role(:name => role)
end
organizations = collect_column(line[ORGANIZATIONS]) do |organization|
foreman_organization(:name => organization)
end
locations = collect_column(line[LOCATIONS]) do |location|
foreman_location(:name => location)
end
if !@existing.include? name
create_user(line, name, roles, organizations, locations)
else
update_user(line, name, roles, organizations, locations)
end
print "done\n" if option_verbose?
end
rescue RuntimeError => e
raise "#{e}\n #{line}"
end
|
#export ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/hammer_cli_csv/users.rb', line 26
def export
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ADMIN, ROLES]
@api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user|
if user['organizations']
organizations = CSV.generate do |column|
column << user['organizations'].collect do |organization|
organization['name']
end
end
organizations.delete!("\n")
end
if user['locations']
locations = CSV.generate do |column|
column << user['locations'].collect do |location|
location['name']
end
end
locations.delete!("\n")
end
if user['roles']
roles = CSV.generate do |column|
column << user['roles'].collect do |role|
role['name']
end
end
roles.delete!("\n")
end
admin = user['admin'] ? 'Yes' : 'No'
if user['login'] != 'admin' && !user['login'].start_with?('hidden-')
csv << [user['login'], 1, user['firstname'], user['lastname'], user['mail'],
organizations, locations, admin, roles]
end
end
end
end
|
#import ⇒ Object
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/hammer_cli_csv/users.rb', line 63
def import
@existing = {}
@api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user|
@existing[user['login']] = user['id'] if user
end
thread_import do |line|
create_users_from_csv(line)
end
end
|
#update_user(line, name, roles, organizations, locations) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/hammer_cli_csv/users.rb', line 117
def update_user(line, name, roles, organizations, locations)
print "Updating user '#{name}'... " if option_verbose?
@api.resource(:users).call(:update, {
'id' => @existing[name],
'user' => {
'login' => name,
'firstname' => line[FIRSTNAME],
'lastname' => line[LASTNAME],
'password' => 'redhat',
'mail' => line[EMAIL],
'role_ids' => roles,
'admin' => line[ADMIN] == 'Yes' ? true : false,
'organization_ids' => organizations,
'location_ids' => locations
}
})
end
|