Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / roles.rb @ 9e11b4c5

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
module HammerCLICsv
13
  class CsvCommand
14
    class RolesCommand < BaseCommand
15
      command_name 'roles'
16
      desc 'import or export roles'
17

    
18
      RESOURCE = 'Resource'
19
      SEARCH = 'Search'
20
      PERMISSIONS = 'Permissions'
21
      ORGANIZATIONS = 'Organizations'
22
      LOCATIONS = 'Locations'
23

    
24
      def export
25
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
26
          csv << [NAME, COUNT, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS]
27
          @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
28
            @api.resource(:filters).call(:index, {
29
                'per_page' => 999999,
30
                'search' => "role=\"#{role['name']}\""
31
            })['results'].each do |filter|
32
              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
            end
39
          end
40
        end
41

    
42
        HammerCLI::EX_OK
43
      end
44

    
45
      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

    
51
        @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

    
56
        thread_import do |line|
57
          create_roles_from_csv(line)
58
        end
59
      end
60

    
61
      def create_roles_from_csv(line)
62

    
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
        line[COUNT].to_i.times do |number|
74
          name = namify(line[NAME], number)
75
          search = line[SEARCH] ? namify(line[SEARCH], number) : nil
76

    
77
          if !@existing_roles[name]
78
            print "Creating role '#{name}'..." if option_verbose?
79
            role = @api.resource(:roles).call(:create, {
80
                'role' => {
81
                    'name' => name
82
                }
83
            })
84
            @existing_roles[name] = role['id']
85
          else
86
            print "Updating role '#{name}'..." if option_verbose?
87
            # Nothing to update on the role object itself, just filters below
88
          end
89

    
90
          filter_id = foreman_filter(name, line[RESOURCE], search)
91
          if !filter_id
92
            print " creating filter #{line[RESOURCE]}..." if option_verbose?
93
            @api.resource(:filters).call(:create, { 'filter' => {
94
                'role_id' => @existing_roles[name],
95
                'search' => search,
96
                'unlimited' => search.nil? || search.empty?,
97
                'organization_ids' => organizations,
98
                'location_ids' => locations,
99
                'permission_ids' => permissions
100
            }})
101
          else
102
            print " updating filter #{line[RESOURCE]}..."
103
            @api.resource(:filters).call(:update, {
104
                'id' => filter_id,
105
                'filter' => {
106
                    'search' => search,
107
                    'unlimited' => search.nil? || search.empty?,
108
                    'organization_ids' => organizations,
109
                    'location_ids' => locations,
110
                    'permission_ids' => permissions
111
                }
112
            })
113
          end
114

    
115
          puts 'done' if option_verbose?
116
        end
117
      end
118
    end
119
  end
120
end