Class: HammerCLICsv::CsvCommand::PartitionTablesCommand

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

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
OSFAMILY =
'OS Family'
OPERATINGSYSTEMS =
'Operating Systems'
LAYOUT =
'Layout'

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_ptables_from_csv(line) ⇒ Object



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
# File 'lib/hammer_cli_csv/partition_tables.rb', line 66

def create_ptables_from_csv(line)
  params = {
    'ptable' => {
      'os_family' => line[OSFAMILY],
      'layout' => line[LAYOUT]
    }
  }
  # Check for backwards compatibility
  if apipie_check_param(:ptable, :create, 'ptable[operatingsystem_ids]')
    operatingsystems = collect_column(line[OPERATINGSYSTEMS]) do |operatingsystem|
      foreman_operatingsystem(:name => operatingsystem)
    end
    params['ptable']['operatingsystem_ids'] = operatingsystems
  end
  if apipie_check_param(:ptable, :create, 'ptable[organization_ids]')
    organizations = collect_column(line[ORGANIZATIONS]) do |organization|
      foreman_organization(:name => organization)
    end
    params['ptable']['organization_ids'] = organizations
  end
  if apipie_check_param(:ptable, :create, 'ptable[location_ids]')
    locations = collect_column(line[LOCATIONS]) do |location|
      foreman_location(:name => location)
    end
    params['ptable']['location_ids'] = locations
  end

  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    params['ptable']['name'] = name
    if !@existing.include? name
      print "Creating partition-table '#{name}'... " if option_verbose?
      @api.resource(:ptables).call(:create, params)
    else
      print "Updating partition-table '#{name}'..." if option_verbose?
      params['id'] = @existing[name]
      @api.resource(:ptables).call(:update, params)
    end
    print "done\n" if option_verbose?
  end
end

#export(csv) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hammer_cli_csv/partition_tables.rb', line 13

def export(csv)
  # TODO: partition-tables do not return their organizations or locations
  # http://projects.theforeman.org/issues/11175
  organizations_map = {}
  @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
    ptables = @api.resource(:ptables).call(:index, {
        'organization_id' => organization['id'],
        'search' => option_search
    })['results'].each do |ptable|
      organizations_map[ptable['name']] ||= []
      organizations_map[ptable['name']] << organization['name']
    end
  end
  locations_map = {}
  @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location|
    ptables = @api.resource(:ptables).call(:index, {'location_id' => location['id']})['results'].each do |ptable|
      locations_map[ptable['name']] ||= []
      locations_map[ptable['name']] << location['name']
    end
  end

  csv << [NAME, ORGANIZATIONS, LOCATIONS, OSFAMILY, OPERATINGSYSTEMS, LAYOUT]
  @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable|
    ptable = @api.resource(:ptables).call(:show, {'id' => ptable['id']})
    name = ptable['name']
    osfamily = ptable['os_family']
    layout = ptable['layout']
    operatingsystems = export_column(ptable, 'operatingsystems', 'title')

    organizations = CSV.generate do |column|
      column << organizations_map[name] if organizations_map[name]
    end
    organizations.delete!("\n")
    locations = CSV.generate do |column|
      column << locations_map[name] if locations_map[name]
    end
    locations.delete!("\n")

    csv << [name, organizations, locations, osfamily, operatingsystems, layout]
  end
end

#importObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/hammer_cli_csv/partition_tables.rb', line 55

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

  thread_import do |line|
    create_ptables_from_csv(line)
  end
end