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

#apipie_check_param, #associate_locations, #associate_organizations, #authenticate_request, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, help_columns, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, supported?, #thread_import

Methods included from Utils::Config

#api_connection

Instance Method Details

#create_roles_from_csv(line) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/hammer_cli_csv/roles.rb', line 46

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

  count(line[COUNT]).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, {
          'role' => {
              'name' => name
          }
      })
      @existing_roles[name] = role['id']
    else
      print "Updating role '#{name}'..." if option_verbose?
      # Nothing to update on the role object itself, just filters below
    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.nil? || 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,
          'filter' => {
              'search' => search,
              'unlimited' => search.nil? || search.empty?,
              'organization_ids' => organizations,
              'location_ids' => locations,
              'permission_ids' => permissions
          }
      })
    end

    puts 'done' if option_verbose?
  end
end

#export(csv) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hammer_cli_csv/roles.rb', line 13

def export(csv)
  csv << [NAME, 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'], filter['resource_type'], filter['search'] || '', permissions, organizations, locations]
    end
  end
end

#importObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hammer_cli_csv/roles.rb', line 30

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