Class: HammerCLICsv::CsvCommand::DomainsCommand

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

Constant Summary collapse

FULLNAME =
'Full Name'
ORGANIZATIONS =
'Organizations'

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



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
94
95
96
97
98
99
100
# File 'lib/hammer_cli_csv/domains.rb', line 61

def create_domains_from_csv(line)
  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print "Creating domain '#{name}'..." if option_verbose?
      domain_id = @api.resource(:domains).call(:create, {
                                                 'name' => name
                                               })['id']
    else
      print "Updating domain '#{name}'..." if option_verbose?
      domain_id = @api.resource(:domains).call(:update, {
                                                 'id' => @existing[name],
                                                 'name' => name
                                               })['id']
    end

    # Update associated resources
    domains ||= {}
    CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
      organization_id = foreman_organization(:name => organization)
      if domains[organization].nil?
        domains[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['domains'].collect do |domain|
          domain['id']
        end
      end
      domains[organization] += [domain_id] if !domains[organization].include? domain_id

      @api.resource(:organizations).call(:update, {
                                           'id' => organization_id,
                                           'organization' => {
                                             'domain_ids' => domains[organization]
                                           }
                                         })
    end

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

#exportObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hammer_cli_csv/domains.rb', line 37

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, COUNT, FULLNAME]
    @api.resource(:domains).call(:index, {:per_page => 999999})['results'].each do |domain|
      puts domain
      name = domain['name']
      count = 1
      fullname = domain['fullname']
      csv << [name, count, fullname]
    end
  end
end

#importObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/hammer_cli_csv/domains.rb', line 50

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

  thread_import do |line|
    create_domains_from_csv(line)
  end
end