Project

General

Profile

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

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

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
      rescue RuntimeError => e
83
        raise "#{e}\n       #{line}"
84
      end
85

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

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