Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / roles.rb @ 561a8ac9

1 587327f4 Tom McKay
# Copyright 2013-2014 Red Hat, Inc.
2 dbd0746f Tom McKay
#
3 587327f4 Tom McKay
# 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 dbd0746f Tom McKay
module HammerCLICsv
13 7bea883f Grant Gainey
  class CsvCommand
14
    class RolesCommand < BaseCommand
15 c02629de Tom McKay
      command_name 'roles'
16
      desc 'import or export roles'
17 dbd0746f Tom McKay
18 c02629de Tom McKay
      RESOURCE = 'Resource'
19
      SEARCH = 'Search'
20
      PERMISSIONS = 'Permissions'
21
      ORGANIZATIONS = 'Organizations'
22
      LOCATIONS = 'Locations'
23 7bea883f Grant Gainey
24
      def export
25
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
26 c02629de Tom McKay
          csv << [NAME, COUNT, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS]
27 7bea883f Grant Gainey
          @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
28
            @api.resource(:filters).call(:index, {
29 c02629de Tom McKay
                                           'per_page' => 999999,
30
                                           'search' => "role=\"#{role['name']}\""
31 7bea883f Grant Gainey
                                })['results'].each do |filter|
32 c02629de Tom McKay
              filter = @api.resource(:filters).call(:show, 'id' => filter['id'])
33
34
              permissions = export_column(filter, 'permissions', 'name')
35
              organizations = export_column(filter, 'organizations', 'name')
36
              locations = export_column(filter, 'locations', 'name')
37
              csv << [role['name'], 1, filter['resource_type'], filter['search'] || '', permissions, organizations, locations]
38 37ef9522 Tom McKay
            end
39
          end
40 dbd0746f Tom McKay
        end
41
42 7bea883f Grant Gainey
        HammerCLI::EX_OK
43 dbd0746f Tom McKay
      end
44
45 7bea883f Grant Gainey
      def import
46
        @existing_roles = {}
47
        @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
48
          @existing_roles[role['name']] = role['id']
49
        end
50 dbd0746f Tom McKay
51 7bea883f Grant Gainey
        @existing_filters = {}
52
        @api.resource(:filters).call(:index, {'per_page' => 999999})['results'].each do |role|
53
          @existing_filters[role['name']] = role['id']
54
        end
55 dbd0746f Tom McKay
56 7bea883f Grant Gainey
        thread_import do |line|
57
          create_roles_from_csv(line)
58 dbd0746f Tom McKay
        end
59 7bea883f Grant Gainey
      end
60 dbd0746f Tom McKay
61 7bea883f Grant Gainey
      def create_roles_from_csv(line)
62 561a8ac9 Tom McKay
63
        permissions = collect_column(line[PERMISSIONS]) do |permission|
64
          foreman_permission(:name => permission)
65
        end
66
        organizations = collect_column(line[ORGANIZATIONS]) do |organization|
67
          foreman_organization(:name => organization)
68
        end
69
        locations = collect_column(line[LOCATIONS]) do |location|
70
          foreman_location(:name => location)
71
        end
72
73 7bea883f Grant Gainey
        line[COUNT].to_i.times do |number|
74
          name = namify(line[NAME], number)
75 c02629de Tom McKay
          search = namify(line[SEARCH], number) if line[SEARCH]
76 dbd0746f Tom McKay
77 7bea883f Grant Gainey
          if !@existing_roles[name]
78
            print "Creating role '#{name}'..." if option_verbose?
79
            role = @api.resource(:roles).call(:create, {
80 c02629de Tom McKay
                                                'name' => name
81
                                              })
82 7bea883f Grant Gainey
            @existing_roles[name] = role['id']
83 bfc065ce Tom McKay
          else
84 7bea883f Grant Gainey
            print "Updating role '#{name}'..." if option_verbose?
85
            @api.resource(:roles).call(:update, {
86 c02629de Tom McKay
                                         'id' => @existing_roles[name]
87
                                       })
88 bfc065ce Tom McKay
          end
89 dbd0746f Tom McKay
90 c02629de Tom McKay
          filter_id = foreman_filter(name, line[RESOURCE], search)
91
          if !filter_id
92
            print " creating filter #{line[RESOURCE]}..."
93 561a8ac9 Tom McKay
            @api.resource(:filters).call(:create, { 'filter' => {
94 c02629de Tom McKay
                                           'role_id' => @existing_roles[name],
95
                                           'search' => search,
96
                                           'organization_ids' => organizations,
97
                                           'location_ids' => locations,
98
                                           'permission_ids' => permissions
99 561a8ac9 Tom McKay
                                         }})
100 c02629de Tom McKay
          else
101
            print " updating filter #{line[RESOURCE]}..."
102
            @api.resource(:filters).call(:update, {
103
                                           'id' => filter_id,
104
                                           'search' => search,
105
                                           'organization_ids' => organizations,
106
                                           'location_ids' => locations,
107
                                           'permission_ids' => permissions
108
                                         })
109 7bea883f Grant Gainey
          end
110
111 c02629de Tom McKay
          puts 'done' if option_verbose?
112 7bea883f Grant Gainey
        end
113 37ef9522 Tom McKay
      end
114 dbd0746f Tom McKay
    end
115
  end
116
end