Class: HammerCLICsv::CsvCommand::ComputeProfilesCommand

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

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
DESCRIPTION =
'Description'
PROVIDER =
'Provider'
URL =
'URL'

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



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

def create_compute_profiles_from_csv(line)
  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print "Creating compute profile '#{name}'..." if option_verbose?
      id = @api.resource(:compute_profiles)\
        .call(:create, {
                'compute_profile' => {
                  'name' => name,
                  'url' => line[URL]
                }
              })['id']
    else
      print "Updating compute profile '#{name}'..." if option_verbose?
      id = @api.resource(:compute_profiles)\
        .call(:update, {
                'id' => @existing[name],
                'compute_profile' => {
                  'name' => name,
                  'url' => line[URL]
                }
              })['compute_profile']['id']
    end

    # Update associated profiles
    associate_organizations(id, line[ORGANIZATIONS], 'compute_profile')
    associate_locations(id, line[LOCATIONS], 'compute_profile')

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

#exportObject



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

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL]
    @api.resource(:compute_profiles).call(:index, {:per_page => 999999})['results'].each do |compute_profile|
      puts compute_profile
      compute_profile = @api.resource(:compute_profiles).call(:show, {'id' => compute_profile['id']})
      name = compute_profile['name']
      count = 1
      organizations = export_column(compute_profile, 'organizations', 'name')
      locations = export_column(compute_profile, 'locations', 'name')
      description = compute_profile['description']
      provider = compute_profile['provider']
      url = compute_profile['url']
      csv << [name, count, organizations, locations, description, provider, url]
    end
  end
end

#importObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/hammer_cli_csv/compute_profiles.rb', line 48

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

  thread_import do |line|
    create_compute_profiles_from_csv(line)
  end
end