Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / roles.rb @ d76bdbc0

1
module HammerCLICsv
2
  class CsvCommand
3
    class RolesCommand < BaseCommand
4
      command_name 'roles'
5
      desc 'import or export roles'
6

    
7
      RESOURCE = 'Resource'
8
      SEARCH = 'Search'
9
      PERMISSIONS = 'Permissions'
10
      ORGANIZATIONS = 'Organizations'
11
      LOCATIONS = 'Locations'
12

    
13
      def export(csv)
14
        csv << [NAME, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS]
15
        @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
16
          @api.resource(:filters).call(:index, {
17
              'per_page' => 999999,
18
              'search' => "role=\"#{role['name']}\""
19
          })['results'].each do |filter|
20
            filter = @api.resource(:filters).call(:show, 'id' => filter['id'])
21

    
22
            permissions = export_column(filter, 'permissions', 'name')
23
            organizations = export_column(filter, 'organizations', 'name')
24
            locations = export_column(filter, 'locations', 'name')
25
            csv << [role['name'], filter['resource_type'], filter['search'] || '', permissions, organizations, locations]
26
          end
27
        end
28
      end
29

    
30
      def import
31
        @existing_roles = {}
32
        @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
33
          @existing_roles[role['name']] = role['id']
34
        end
35

    
36
        @existing_filters = {}
37
        @api.resource(:filters).call(:index, {'per_page' => 999999})['results'].each do |role|
38
          @existing_filters[role['name']] = role['id']
39
        end
40

    
41
        thread_import do |line|
42
          create_roles_from_csv(line)
43
        end
44
      end
45

    
46
      def create_roles_from_csv(line)
47

    
48
        permissions = collect_column(line[PERMISSIONS]) do |permission|
49
          foreman_permission(:name => permission)
50
        end
51
        organizations = collect_column(line[ORGANIZATIONS]) do |organization|
52
          foreman_organization(:name => organization)
53
        end
54
        locations = collect_column(line[LOCATIONS]) do |location|
55
          foreman_location(:name => location)
56
        end
57

    
58
        count(line[COUNT]).times do |number|
59
          name = namify(line[NAME], number)
60
          search = line[SEARCH] ? namify(line[SEARCH], number) : nil
61

    
62
          if !@existing_roles[name]
63
            print "Creating role '#{name}'..." if option_verbose?
64
            role = @api.resource(:roles).call(:create, {
65
                'role' => {
66
                    'name' => name
67
                }
68
            })
69
            @existing_roles[name] = role['id']
70
          else
71
            print "Updating role '#{name}'..." if option_verbose?
72
            # Nothing to update on the role object itself, just filters below
73
          end
74

    
75
          filter_id = foreman_filter(name, line[RESOURCE], search)
76
          if !filter_id
77
            print " creating filter #{line[RESOURCE]}..." if option_verbose?
78
            @api.resource(:filters).call(:create, { 'filter' => {
79
                'role_id' => @existing_roles[name],
80
                'search' => search,
81
                'unlimited' => search.nil? || search.empty?,
82
                'organization_ids' => organizations,
83
                'location_ids' => locations,
84
                'permission_ids' => permissions
85
            }})
86
          else
87
            print " updating filter #{line[RESOURCE]}..."
88
            @api.resource(:filters).call(:update, {
89
                'id' => filter_id,
90
                'filter' => {
91
                    'search' => search,
92
                    'unlimited' => search.nil? || search.empty?,
93
                    'organization_ids' => organizations,
94
                    'location_ids' => locations,
95
                    'permission_ids' => permissions
96
                }
97
            })
98
          end
99

    
100
          puts 'done' if option_verbose?
101
        end
102
      end
103
    end
104
  end
105
end