Class: HammerCLICsv::CsvCommand::ProvisioningTemplatesCommand

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

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
KIND =
'Kind'
SOURCE =
'Source'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #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_templates_from_csv(line) ⇒ Object



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

def create_templates_from_csv(line)
  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)
    if !@existing.include? name
      print "Creating provisioning template '#{name}'..." if option_verbose?
      id = @api.resource(:config_templates)
        .call(:create, {
                'name' => name,
                'snippet' => line[KIND] == 'snippet',
                'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
                'organizations' => organizations,
                'locations' => locations
              })['id']
    else
      print "Updating provisioning template '#{name}'..." if option_verbose?
      id = @api.resource(:config_template)
        .call(:update, {
                'id' => @existing[name],
                'name' => name,
                'organizations' => organizations,
                'locations' => locations
              })['id']
    end
    @existing[name] = id

    print "done\n" if option_verbose?
  end
rescue RuntimeError => e
  raise "#{e}\n       #{line[NAME]}"
end

#exportObject



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

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, KIND, SOURCE]
    @api.resource(:config_templates)
      .call(:index, {
              :per_page => 999999
            })['results'].each do |template_id|
      template = @api.resource(:config_templates).call(:show, {:id => template_id['id']})
      name = template['name']
      count = 1
      kind = template['snippet'] ? 'snippet' : template['template_kind_name']
      organizations = export_column(template, 'organizations', 'name')
      locations = export_column(template, 'locations', 'name')
      unless name == 'Boot disk iPXE - generic host' || name == 'Boot disk iPXE - host'
        csv << [name, count, organizations, locations, kind, template['template']]
      end
    end
  end
end

#importObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hammer_cli_csv/provisioning_templates.rb', line 43

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

  thread_import do |line|
    create_templates_from_csv(line)
  end
end