Project

General

Profile

Download (4.65 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / users.rb @ d76bdbc0

1
module HammerCLICsv
2
  class CsvCommand
3
    class UsersCommand < BaseCommand
4
      command_name 'users'
5
      desc         'import or export users'
6

    
7
      FIRSTNAME = 'First Name'
8
      LASTNAME = 'Last Name'
9
      EMAIL = 'Email'
10
      ORGANIZATIONS = 'Organizations'
11
      LOCATIONS = 'Locations'
12
      ADMIN = 'Administrator'
13
      ROLES = 'Roles'
14

    
15
      def export(csv)
16
        csv << [NAME, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ADMIN, ROLES]
17
        @api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user|
18
          if user['organizations']
19
            organizations = CSV.generate do |column|
20
              column << user['organizations'].collect do |organization|
21
                organization['name']
22
              end
23
            end
24
            organizations.delete!("\n")
25
          end
26
          if user['locations']
27
            locations = CSV.generate do |column|
28
              column << user['locations'].collect do |location|
29
                location['name']
30
              end
31
            end
32
            locations.delete!("\n")
33
          end
34
          if user['roles']
35
            roles = CSV.generate do |column|
36
              column << user['roles'].collect do |role|
37
                role['name']
38
              end
39
            end
40
            roles.delete!("\n")
41
          end
42
          admin = user['admin'] ? 'Yes' : 'No'
43
          if user['login'] != 'admin' && !user['login'].start_with?('hidden-')
44
            csv << [user['login'], user['firstname'], user['lastname'], user['mail'],
45
                    organizations, locations, admin, roles]
46
          end
47
        end
48
      end
49

    
50
      def import
51
        @existing = {}
52
        @api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user|
53
          @existing[user['login']] = user['id'] if user
54
        end
55

    
56
        thread_import do |line|
57
          create_users_from_csv(line)
58
        end
59
      end
60

    
61
      def create_users_from_csv(line)
62
        count(line[COUNT]).times do |number|
63
          name = namify(line[NAME], number)
64

    
65
          roles = collect_column(line[ROLES]) do |role|
66
            foreman_role(:name => role)
67
          end
68
          organizations = collect_column(line[ORGANIZATIONS]) do |organization|
69
            foreman_organization(:name => organization)
70
          end
71
          locations = collect_column(line[LOCATIONS]) do |location|
72
            foreman_location(:name => location)
73
          end
74

    
75
          if !@existing.include? name
76
            create_user(line, name, roles, organizations, locations)
77
          else
78
            update_user(line, name, roles, organizations, locations)
79
          end
80
          print "done\n" if option_verbose?
81
        end
82
      end
83

    
84
      def create_user(line, name, roles, organizations, locations)
85
        print "Creating user '#{name}'... " if option_verbose?
86
        @api.resource(:users).call(:create, {
87
                                     'user' => {
88
                                       'login' => name,
89
                                       'firstname' => line[FIRSTNAME],
90
                                       'lastname' => line[LASTNAME],
91
                                       'mail' => line[EMAIL],
92
                                       'password' => 'redhat',
93
                                       'auth_source_id' => 1,  # INTERNAL auth
94
                                       'admin' => line[ADMIN] == 'Yes' ? true : false,
95
                                       'role_ids' => roles,
96
                                       'organization_ids' => organizations,
97
                                       'location_ids' => locations
98
                                     }
99
                                   })
100
      end
101

    
102
      def update_user(line, name, roles, organizations, locations)
103
        print "Updating user '#{name}'... " if option_verbose?
104
        @api.resource(:users).call(:update, {
105
                                     'id' => @existing[name],
106
                                     'user' => {
107
                                       'login' => name,
108
                                       'firstname' => line[FIRSTNAME],
109
                                       'lastname' => line[LASTNAME],
110
                                       'password' => 'redhat',
111
                                       'mail' => line[EMAIL],
112
                                       'role_ids' => roles,
113
                                       'admin' => line[ADMIN] == 'Yes' ? true : false,
114
                                       'organization_ids' => organizations,
115
                                       'location_ids' => locations
116
                                     }
117
                                   })
118
      end
119
    end
120
    autoload_subcommands
121
  end
122
end