Class: HammerCLICsv::CsvCommand::LocationsCommand

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

Constant Summary collapse

PARENT =
'Parent Location'

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



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/locations.rb', line 30

def create_locations_from_csv(line)
  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    location_id = @existing[name]
    if !location_id
      print "Creating location '#{name}'... " if option_verbose?
      @api.resource(:locations).call(:create, {
                                       'location' => {
                                         'name' => name,
                                         'parent_id' => foreman_location(:name => line[PARENT])
                                       }
                                     })
    else
      print "Updating location '#{name}'... " if option_verbose?
      @api.resource(:locations).call(:update, {
                                       'id' => location_id,
                                       'location' => {
                                         'parent_id' => foreman_location(:name => line[PARENT])
                                       }
                                     })
    end
    print "done\n" if option_verbose?
  end
end

#export(csv) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/hammer_cli_csv/locations.rb', line 9

def export(csv)
  csv << [NAME, PARENT]
  @api.resource(:locations).call(:index, {
      :per_page => 999999,
      :search => option_search
  })['results'].each do |location|
    csv << [location['name'], '']
  end
end

#importObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/hammer_cli_csv/locations.rb', line 19

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

  thread_import do |line|
    create_locations_from_csv(line)
  end
end