Project

General

Profile

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

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

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12
module HammerCLICsv
13
  class UsersCommand < BaseCommand
14
    FIRSTNAME = 'First Name'
15
    LASTNAME = 'Last Name'
16
    EMAIL = 'Email'
17
    ORGANIZATIONS = 'Organizations'
18
    LOCATIONS = 'Locations'
19
    ROLES = 'Roles'
20

    
21
    def export
22
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
23
        csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ROLES]
24
        @api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user|
25
          if user['organizations']
26
            organizations = CSV.generate do |column|
27
              column << user['organizations'].collect do |organization|
28
                organization['name']
29
              end
30
            end
31
            organizations.delete!("\n")
32
          end
33
          if user['locations']
34
            locations = CSV.generate do |column|
35
              column << user['locations'].collect do |location|
36
                location['name']
37
              end
38
            end
39
            locations.delete!("\n")
40
          end
41
          if user['roles']
42
            roles = CSV.generate do |column|
43
              column << user['roles'].collect do |role|
44
                role['name']
45
              end
46
            end
47
            roles.delete!("\n")
48
          end
49
          if user['login'] != 'admin' && !user['login'].start_with?('hidden-')
50
            csv << [user['login'], 1, user['firstname'], user['lastname'], user['mail'],
51
                    organizations, locations, roles]
52
          end
53
        end
54
      end
55
    end
56

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

    
63
      thread_import do |line|
64
        create_users_from_csv(line)
65
      end
66
    end
67

    
68
    def create_users_from_csv(line)
69
      line[COUNT].to_i.times do |number|
70
        name = namify(line[NAME], number)
71

    
72
        roles = CSV.parse_line(line[ROLES], {:skip_blanks => true}).collect do |role|
73
          foreman_role(:name => namify(role, number))
74
        end if line[ROLES] && !line[ROLES].empty?
75
        organizations = CSV.parse_line(line[ORGANIZATIONS], {:skip_blanks => true}).collect do |organization|
76
          foreman_organization(:name => organization)
77
        end if line[ORGANIZATIONS] && !line[ORGANIZATIONS].empty?
78
        locations = CSV.parse_line(line[LOCATIONS], {:skip_blanks => true}).collect do |location|
79
          foreman_location(:name => location)
80
        end if line[LOCATIONS] && !line[LOCATIONS].empty?
81

    
82
        if !@existing.include? name
83
          create_user(line, name, roles, organizations, locations)
84
        else
85
          update_user(line, name, roles, organizations, locations)
86
        end
87
        print "done\n" if option_verbose?
88
      end
89
    rescue RuntimeError => e
90
      raise "#{e}\n       #{line}"
91
    end
92

    
93
    def create_user(line, name, roles, organizations, locations)
94
      print "Creating user '#{name}'... " if option_verbose?
95
      @api.resource(:users).call(:create, {
96
                                   'user' => {
97
                                     'login' => name,
98
                                     'firstname' => line[FIRSTNAME],
99
                                     'lastname' => line[LASTNAME],
100
                                     'mail' => line[EMAIL],
101
                                     'password' => 'changeme',
102
                                     'auth_source_id' => 1,  # INTERNAL auth
103
                                     'organization_ids' => organizations,
104
                                     'location_ids' => locations,
105
                                     'role_ids' => roles
106
                                   }
107
                                 })
108
    end
109

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

    
128
  HammerCLICsv::CsvCommand.subcommand('users',
129
                                      'import or export users',
130
                                      HammerCLICsv::UsersCommand)
131
end