Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / permissions.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
#
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, system_groups, 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
#     system_groups - 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 PermissionsCommand < BaseCommand
50

    
51
    def initialize(*args)
52
      super(args)
53
      @role_api = KatelloApi::Resources::Role.new(@init_options)
54
      @permission_api = KatelloApi::Resources::Permission.new(@init_options)
55
    end
56

    
57
    def export
58
      # TODO
59
    end
60

    
61
    def import
62
      csv = get_lines(option_csv_file)[1..-1]
63
      lines_per_thread = csv.length/threads.to_i + 1
64
      splits = []
65

    
66
      @roles = {}
67
      @role_api.index[0].each do |role|
68
          @roles[role['name']] = role['id']
69
      end
70

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

    
78
      puts @verbs
79
      return
80

    
81
      @existing = {}
82

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

    
96
      splits.each do |thread|
97
        thread.join
98
      end
99

    
100
      HammerCLI::EX_OK
101
    end
102

    
103
    def create_permissions_from_csv(line)
104
      details = parse_permission_csv(line)
105

    
106
      puts @permission_api.index({'role_id' => @roles['User System Group']})
107
      # {"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"=>"system_groups", "updated_at"=>"2013-11-07T16:36:56Z"}}
108

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

    
114
      puts @existing
115
      return 1
116

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

    
133
    def parse_permission_csv(line)
134
      keys = [:name_format, :count, :role, :description]
135
      details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0]
136

    
137
      details[:count] = details[:count].to_i
138

    
139
      details
140
    end
141
  end
142

    
143
  HammerCLICsv::CsvCommand.subcommand("permissions",
144
                                      "import or export permissions",
145
                                      HammerCLICsv::PermissionsCommand)
146
end