Class: HammerCLICsv::CsvCommand::PuppetEnvironmentsCommand

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

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'

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



32
33
34
35
36
37
38
39
40
41
42
43
44
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
75
# File 'lib/hammer_cli_csv/puppet_environments.rb', line 32

def create_environments_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

  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print "Creating environment '#{name}'..." if option_verbose?
      id = @api.resource(:environments).call(:create, {
                                       'environment' => {
                                         'name' => name,
                                         'organization_ids' => organizations
                                       }
                                     })['id']
    else
      print "Updating environment '#{name}'..." if option_verbose?

      environment = @api.resource(:environments).call(:show, {'id' => @existing[name]})
      environment['organizations'].collect do |organization|
        organizations << organization['id']
      end
      organizations.uniq!
      environment['locations'].collect do |location|
        locations << location['id']
      end
      locations.uniq!

      @api.resource(:environments).call(:update, {
                                   'id' => @existing[name],
                                   'environment' => {
                                     'name' => name,
                                     'organization_ids' => organizations,
                                     'location_ids' => locations
                                   }
                                 })
    end

    puts "done" if option_verbose?
  end
end

#export(csv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/hammer_cli_csv/puppet_environments.rb', line 10

def export(csv)
  csv << [NAME, ORGANIZATIONS, LOCATIONS]
  @api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment|
    environment = @api.resource(:environments).call(:show, {:id => environment['id']})
    name = environment['name']
    organizations = export_column(environment, 'organizations', 'name')
    locations = export_column(environment, 'locations', 'name')
    csv << [name, organizations, locations]
  end
end

#importObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/hammer_cli_csv/puppet_environments.rb', line 21

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

  thread_import do |line|
    create_environments_from_csv(line)
  end
end