Class: HammerCLICsv::CsvCommand::RolesCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/roles.rb

Constant Summary collapse

RESOURCE =
'Resource'
SEARCH =
'Search'
PERMISSIONS =
'Permissions'
ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_roles_from_csv(line) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hammer_cli_csv/roles.rb', line 61

def create_roles_from_csv(line)

  permissions = collect_column(line[PERMISSIONS]) do |permission|
    foreman_permission(:name => permission)
  end
  organizations = collect_column(line[ORGANIZATIONS]) do |organization|
    foreman_organization(:name => organization)
  end
  locations = collect_column(line[LOCATIONS]) do |location|
    foreman_location(:name => location)
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    search = line[SEARCH] ? namify(line[SEARCH], number) : nil

    if !@existing_roles[name]
      print "Creating role '#{name}'..." if option_verbose?
      role = @api.resource(:roles).call(:create, {
                                          'name' => name
                                        })
      @existing_roles[name] = role['id']
    else
      print "Updating role '#{name}'..." if option_verbose?
      @api.resource(:roles).call(:update, {
                                   'id' => @existing_roles[name]
                                 })
    end

    filter_id = foreman_filter(name, line[RESOURCE], search)
    if !filter_id
      print " creating filter #{line[RESOURCE]}..." if option_verbose?
      @api.resource(:filters)\
        .call(:create, { 'filter' => {
                  'role_id' => @existing_roles[name],
                  'search' => search,
                  'unlimited' => search.empty?,
                  'organization_ids' => organizations,
                  'location_ids' => locations,
                  'permission_ids' => permissions
                }})
    else
      print " updating filter #{line[RESOURCE]}..."
      @api.resource(:filters)\
        .call(:update, {
                'id' => filter_id,
                'search' => search,
                'unlimited' => search.empty?,
                'organization_ids' => organizations,
                'location_ids' => locations,
                'permission_ids' => permissions
              })
    end

    puts 'done' if option_verbose?
  end
end

#exportObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hammer_cli_csv/roles.rb', line 24

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS]
    @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role|
      @api.resource(:filters).call(:index, {
                                     'per_page' => 999999,
                                     'search' => "role=\"#{role['name']}\""
                          })['results'].each do |filter|
        filter = @api.resource(:filters).call(:show, 'id' => filter['id'])

        permissions = export_column(filter, 'permissions', 'name')
        organizations = export_column(filter, 'organizations', 'name')
        locations = export_column(filter, 'locations', 'name')
        csv << [role['name'], 1, filter['resource_type'], filter['search'] || '', permissions, organizations, locations]
      end
    end
  end

  HammerCLI::EX_OK
end

#importObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hammer_cli_csv/roles.rb', line 45

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

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

  thread_import do |line|
    create_roles_from_csv(line)
  end
end