Class: HammerCLICsv::CsvCommand::ComputeResourcesCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/compute_resources.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

#apipie_check_param, #associate_locations, #associate_organizations, #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_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_compute_resources_from_csv(line) ⇒ Object



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

def create_compute_resources_from_csv(line)
  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    params = {
      'compute_resource' => {
        'name' => name,
        'url' => line[URL],
        'provider' => line[PROVIDER]
      }
    }
    if !@existing.include? name
      print "Creating compute resource '#{name}'..." if option_verbose?
      id = @api.resource(:compute_resources).call(:create, params)['id']
    else
      print "Updating compute resource '#{name}'..." if option_verbose?
      id = @existing[name]
      params['id'] = id
      @api.resource(:compute_resources).call(:update, params)
    end

    # Update associated resources
    # TODO: this doesn't work "Environments you cannot remove environments that are used by hosts or inherited."
    #associate_organizations(id, line[ORGANIZATIONS], 'compute_resource')
    #associate_locations(id, line[LOCATIONS], 'compute_resource')

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

#exportObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hammer_cli_csv/compute_resources.rb', line 17

def export
  CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL]
    @api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource|
      compute_resource = @api.resource(:compute_resources).call(:show, {'id' => compute_resource['id']})

      name = compute_resource['name']
      organizations = export_column(compute_resource, 'organizations', 'name')
      locations = export_column(compute_resource, 'locations', 'name')
      description = compute_resource['description']
      provider = compute_resource['provider']
      url = compute_resource['url']
      csv << [name, organizations, locations, description, provider, url]
    end
  end
end

#importObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/hammer_cli_csv/compute_resources.rb', line 34

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

  thread_import do |line|
    create_compute_resources_from_csv(line)
  end
end