Project

General

Profile

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

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

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, {
18
            :per_page => 999999,
19
            :search => option_search
20
        })['results'].each do |user|
21
          if user['organizations']
22
            organizations = CSV.generate do |column|
23
              column << user['organizations'].collect do |organization|
24
                organization['name']
25
              end
26
            end
27
            organizations.delete!("\n")
28
          end
29
          if user['locations']
30
            locations = CSV.generate do |column|
31
              column << user['locations'].collect do |location|
32
                location['name']
33
              end
34
            end
35
            locations.delete!("\n")
36
          end
37
          if user['roles']
38
            roles = CSV.generate do |column|
39
              column << user['roles'].collect do |role|
40
                role['name']
41
              end
42
            end
43
            roles.delete!("\n")
44
          end
45
          admin = user['admin'] ? 'Yes' : 'No'
46
          if user['login'] != 'admin' && !user['login'].start_with?('hidden-')
47
            csv << [user['login'], user['firstname'], user['lastname'], user['mail'],
48
                    organizations, locations, admin, roles]
49
          end
50
        end
51
      end
52

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

    
59
        thread_import do |line|
60
          create_users_from_csv(line)
61
        end
62
      end
63

    
64
      def create_users_from_csv(line)
65
        count(line[COUNT]).times do |number|
66
          name = namify(line[NAME], number)
67

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

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

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

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