Project

General

Profile

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

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

1
# Copyright (c) 2013-2014 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24

    
25
require 'hammer_cli'
26
require 'katello_api'
27
require 'json'
28
require 'csv'
29

    
30
module HammerCLICsv
31
  class UsersCommand < BaseCommand
32

    
33
    FIRSTNAME = 'First Name'
34
    LASTNAME = 'Last Name'
35
    EMAIL = 'Email'
36
    ORGANIZATIONS = 'Organizations'
37
    LOCATIONS = 'Locations'
38
    ROLES = 'Roles'
39

    
40
    def export
41
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
42
        csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ROLES]
43
        @f_user_api.index({:per_page => 999999})[0]['results'].each do |user|
44
          organizations = CSV.generate do |column|
45
            column << user['organizations'].collect do |organization|
46
              organization['name']
47
            end
48
          end.delete!("\n") if user['organizations']
49
          locations = CSV.generate do |column|
50
            column << user['locations'].collect do |location|
51
              location['name']
52
            end
53
          end.delete!("\n") if user['locations']
54
          roles = CSV.generate do |column|
55
            column << user['roles'].collect do |role|
56
              role['name']
57
            end
58
          end.delete!("\n") if user['roles']
59
          if user['login'] != 'admin' && !user['login'].start_with?('hidden-')
60
            csv << [user['login'], 1, user['firstname'], user['lastname'], user['mail'], organizations, locations, roles]
61
          end
62
        end
63
      end
64
    end
65

    
66
    def import
67
      @existing = {}
68
      @f_user_api.index({:per_page => 999999})[0]['results'].each do |user|
69
        @existing[user['login']] = user['id'] if user
70
      end
71

    
72
      thread_import do |line|
73
        create_users_from_csv(line)
74
      end
75
    end
76

    
77
    def create_users_from_csv(line)
78
      line[COUNT].to_i.times do |number|
79
        name = namify(line[NAME], number)
80

    
81
        roles = CSV.parse_line(line[ROLES], {:skip_blanks => true}).collect do |role|
82
          foreman_role(:name => namify(role, number))
83
        end if line[ROLES]
84
        organizations = CSV.parse_line(line[ORGANIZATIONS], {:skip_blanks => true}).collect do |organization|
85
          foreman_organization(:name => organization)
86
        end if line[ORGANIZATIONS]
87
        locations = CSV.parse_line(line[LOCATIONS], {:skip_blanks => true}).collect do |location|
88
          foreman_location(:name => location)
89
        end if line[LOCATIONS]
90

    
91
        if !@existing.include? name
92
          print "Creating user '#{name}'... " if option_verbose?
93
          @f_user_api.create({
94
                               'user' => {
95
                                 'login' => name,
96
                                 'firstname' => line[FIRSTNAME],
97
                                 'lastname' => line[LASTNAME],
98
                                 'mail' => line[EMAIL],
99
                                 'password' => 'changeme',
100
                                 'auth_source_id' => 1,  # INTERNAL auth
101
                                 'organization_ids' => organizations,
102
                                 'location_ids' => locations,
103
                                 'role_ids' => roles
104
                               }
105
                             })
106
        else
107
          print "Updating user '#{name}'... " if option_verbose?
108
          @f_user_api.update({
109
                               'id' => @existing[name],
110
                               'user' => {
111
                                 'login' => name,
112
                                 'firstname' => line[FIRSTNAME],
113
                                 'lastname' => line[LASTNAME],
114
                                 'mail' => line[EMAIL],
115
                                 'password' => 'changeme',
116
                                 'organization_ids' => organizations,
117
                                 'location_ids' => locations,
118
                                 'role_ids' => roles
119
                               }
120
                             })
121
        end
122
        print "done\n" if option_verbose?
123
      end
124
    rescue RuntimeError => e
125
      raise "#{e}\n       #{line}"
126
    end
127
  end
128

    
129
  HammerCLI::MainCommand.subcommand("csv:users", "import or export users as CSV", HammerCLICsv::UsersCommand)
130
end