Project

General

Profile

Revision bfc065ce

Added by Thomas McKay over 8 years ago

roles-test - wip

roles-test - users now get roles, orgs, and locations

View differences:

lib/hammer_cli_csv/users.rb
21 21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
#
24
#
25
# -= Users CSV =-
26
#
27
# Columns
28
#   Login
29
#     - Login name of the user.
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "user%d" -> "user1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#   First Name
35
#   Last Name
36
#   Email
37
#
38 24

  
39 25
require 'hammer_cli'
40 26
require 'katello_api'
......
47 33
    FIRSTNAME = 'First Name'
48 34
    LASTNAME = 'Last Name'
49 35
    EMAIL = 'Email'
36
    ORGANIZATIONS = 'Organizations'
37
    LOCATIONS = 'Locations'
38
    ROLES = 'Roles'
50 39

  
51 40
    def export
52 41
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
53
        csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL]
42
        csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ROLES]
54 43
        @f_user_api.index({:per_page => 999999})[0]['results'].each do |user|
55
          csv << [user['login'], 1, user['firstname'], user['lastname'], user['mail']]
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
56 62
        end
57 63
      end
58 64
    end
......
71 77
    def create_users_from_csv(line)
72 78
      line[COUNT].to_i.times do |number|
73 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

  
74 91
        if !@existing.include? name
75 92
          print "Creating user '#{name}'... " if option_verbose?
76 93
          @f_user_api.create({
......
79 96
                                 'firstname' => line[FIRSTNAME],
80 97
                                 'lastname' => line[LASTNAME],
81 98
                                 'mail' => line[EMAIL],
82
                                 'password' => 'admin',
99
                                 'password' => 'changeme',
83 100
                                 'auth_source_id' => 1,  # INTERNAL auth
101
                                 'organization_ids' => organizations,
102
                                 'location_ids' => locations,
103
                                 'role_ids' => roles
84 104
                               }
85 105
                             })
86 106
        else
......
92 112
                                 'firstname' => line[FIRSTNAME],
93 113
                                 'lastname' => line[LASTNAME],
94 114
                                 'mail' => line[EMAIL],
95
                                 'password' => 'admin'
115
                                 'password' => 'changeme',
116
                                 'organization_ids' => organizations,
117
                                 'location_ids' => locations,
118
                                 'role_ids' => roles
96 119
                               }
97 120
                             })
98 121
        end

Also available in: Unified diff