Class: HammerCLICsv::CsvCommand::UsersCommand

Inherits:
BaseCommand
  • Object
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

#apipie_check_param, #associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, supported?, #thread_import

Methods included from Utils::Config

#api_connection, #credentials, #resource_config

Instance Method Details

#create_user(line, name, roles, organizations, locations) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hammer_cli_csv/users.rb', line 84

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,  # INTERNAL auth
                                 'admin' => line[ADMIN] == 'Yes' ? true : false,
                                 'role_ids' => roles,
                                 'organization_ids' => organizations,
                                 'location_ids' => locations
                               }
                             })
end

#create_users_from_csv(line) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hammer_cli_csv/users.rb', line 61

def create_users_from_csv(line)
  count(line[COUNT]).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
end

#export(csv) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hammer_cli_csv/users.rb', line 15

def export(csv)
  csv << [NAME, 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'], user['firstname'], user['lastname'], user['mail'],
              organizations, locations, admin, roles]
    end
  end
end

#importObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/hammer_cli_csv/users.rb', line 50

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hammer_cli_csv/users.rb', line 102

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