Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / permissions.rb @ a77acc4a

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
#
13
# -= Users CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Login name of the user.
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "user%d" -> "user1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#   Role
23
#   Description
24
#   Category
25
#     - organizations, environments, activation_keys, host_collections, providers, users, roles,
26
#       content_view_definitions, content_views, all
27
#   Verbs
28
#     organizations - gpg, redhat_products, delete_distributors, delete_systems, manage_nodes,
29
#                     update_distributors, update, update_systems, read_distributors, read,
30
#                     read_systems, register_distributors, register_systems, sync
31
#     environments - manage_changesets, delete_changesets, update_distributors, update_systems,
32
#                    promote_changesets, read_changesets, read_distributors, read_contents, read_systems,
33
#                    register_distributors, register_systems, delete_distributors, delete_systems
34
#     activation_keys - manage_all, read_all
35
#     host_collections - create, delete, delete_systems, update, update_systems, read, read_systems
36
#     providers - create, delete, update, read
37
#     users - create, delete, update, read
38
#     roles - create, delete, update, read
39
#     content_view_definitions - create, delete, update, publish, read
40
#     content_views - promote, read, subscribe
41
#     all
42
#
43

    
44
require 'hammer_cli'
45
require 'json'
46
require 'csv'
47

    
48
module HammerCLICsv
49
  class CsvCommand
50
    class PermissionsCommand < BaseCommand
51
      command_name 'permissions'
52
      desc         'import or export permissions'
53

    
54
      def initialize(*args)
55
        super(args)
56
        @role_api = KatelloApi::Resources::Role.new(@init_options)
57
        @permission_api = KatelloApi::Resources::Permission.new(@init_options)
58
      end
59

    
60
      def export
61
        # TODO
62
      end
63

    
64
      def import
65
        csv = get_lines(option_csv_file)[1..-1]
66
        lines_per_thread = csv.length / threads.to_i + 1
67
        splits = []
68

    
69
        @roles = {}
70
        @role_api.index[0].each do |role|
71
            @roles[role['name']] = role['id']
72
        end
73

    
74
        @verbs = {}
75
        puts @role_api.available_verbs[0]
76
        return HammerCLI::EX_OK
77
        @role_api.available_verbs[0].each do |verb|
78
            @verbs[verb['name']] = verb['id']
79
        end
80

    
81
        puts @verbs
82
        return
83

    
84
        @existing = {}
85

    
86
        threads.to_i.times do |current_thread|
87
          start_index = ((current_thread) * lines_per_thread).to_i
88
          finish_index = ((current_thread + 1) * lines_per_thread).to_i
89
          lines = csv[start_index...finish_index].clone
90
          splits << Thread.new do
91
            lines.each do |line|
92
              if line.index('#') != 0
93
                create_permissions_from_csv(line)
94
              end
95
            end
96
          end
97
        end
98

    
99
        splits.each do |thread|
100
          thread.join
101
        end
102

    
103
        HammerCLI::EX_OK
104
      end
105

    
106
      def create_permissions_from_csv(line)
107
        details = parse_permission_csv(line)
108

    
109
        puts @permission_api.index({'role_id' => @roles['User System Group']})
110
        # {"all_tags"=>false, "all_verbs"=>false, "created_at"=>"2013-11-11T02:31:23Z", "description"=>"and it's description!", "id"=>12, "name"=>"Accounting System Group Modify Systems", "organization_id"=>2, "resource_type_id"=>5, "role_id"=>124, "updated_at"=>"2013-11-11T02:31:23Z", "tags"=>[{"created_at"=>"2013-11-11T02:31:23Z", "formatted"=>{"name"=>6, "display_name"=>"Accounting"}, "id"=>2, "permission_id"=>12, "tag_id"=>6, "updated_at"=>"2013-11-11T02:31:23Z"}], "verbs"=>[{"created_at"=>"2013-11-07T19:44:45Z", "id"=>7, "updated_at"=>"2013-11-07T19:44:45Z", "verb"=>"update_systems"}], "resource_type"=>{"created_at"=>"2013-11-07T16:36:56Z", "id"=>5, "name"=>"host_collections", "updated_at"=>"2013-11-07T16:36:56Z"}}
111

    
112
        @existing[@roles[details[:role]]] ||= {}
113
        @permission_api.index({'role_id' => @roles[details[:role]]}).each do |permission|
114
            @existing[@roles[details[:role]]][permission['name']] = permission['id']
115
        end
116

    
117
        puts @existing
118
        return 1
119

    
120
        details[:count].times do |number|
121
          name = namify(details[:name_format], number)
122
          if !@existing.include? name
123
            @permission_api.create({
124
                               :permission => {
125
                                 :name => name,
126
                                 :email => details[:email],
127
                                 :password => 'admin'
128
                               }
129
                             }, {'Accept' => 'version=2,application/json'})
130
          else
131
            puts "Skip existing permission '#{name}'"
132
          end
133
        end
134
      end
135

    
136
      def parse_permission_csv(line)
137
        keys = [:name_format, :count, :role, :description]
138
        details = CSV.parse(line).collect { |a| Hash[keys.zip(a)] }[0]
139

    
140
        details[:count] = details[:count].to_i
141

    
142
        details
143
      end
144
    end
145
  end
146
end