Class: HammerCLICsv::CsvCommand::SyncPlansCommand

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

Constant Summary collapse

ORGANIZATION =
'Organization'
DESCRIPTION =
'Description'
ENABLED =
'Enabled'
STARTDATE =
'Start Date'
INTERVAL =
'Interval'
PRODUCTS =
'Products'

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_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_content_hosts_from_csv(line) ⇒ Object



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
101
102
103
104
105
106
107
108
109
# File 'lib/hammer_cli_csv/sync_plans.rb', line 67

def create_content_hosts_from_csv(line)
  return if option_organization && line[ORGANIZATION] != option_organization

  if !@existing[line[ORGANIZATION]]
    @existing[line[ORGANIZATION]] = {}
    @api.resource(:sync_plans).call(:index, {
        'per_page' => 999999,
        'organization_id' => foreman_organization(:name => line[ORGANIZATION])
    })['results'].each do |sync_plan|
      @existing[line[ORGANIZATION]][sync_plan['name']] = sync_plan['id']
    end
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    if !@existing[line[ORGANIZATION]].include? name
      print "Creating sync plan '#{name}'..." if option_verbose?
      @api.resource(:sync_plans).call(:create, {
          'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
          'name' => name,
          'description' => line[DESCRIPTION],
          'enabled' => line[ENABLED] == 'Yes' ? true : false,
          'sync_date' => line[STARTDATE],
          'interval' => line[INTERVAL],
          'products' => products(line)
      })
    else
      print "Updating sync plan '#{name}'..." if option_verbose?
      # TODO
      # @api.resource(:host_collections).call(:update, {
      #     'organization_id' => line[ORGANIZATION],
      #     'id' => @existing[line[ORGANIZATION]][name],
      #     'name' => name,
      #     'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
      #     'description' => line[DESCRIPTION]
      # })
    end
    puts "done" if option_verbose?
  end

rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



28
29
30
31
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
# File 'lib/hammer_cli_csv/sync_plans.rb', line 28

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, ENABLED, STARTDATE, INTERVAL, PRODUCTS]

    @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
      next if option_organization && organization['name'] != option_organization

      @api.resource(:sync_plans).call(:index, {
           'per_page' => 999999,
           'organization_id' => foreman_organization(:name => organization['name'])
      })['results'].each do |sync_plan|
        name = sync_plan['name']
        count = 1
        organization_name = organization['name']
        description = sync_plan['description']
        enabled = sync_plan['enabled'] ? 'Yes' : 'No'
        start_date = sync_plan['sync_date']
        interval = sync_plan['interval']
        products = CSV.generate do |column|
          column << sync_plan['products'].collect do |product|
            product['name']
          end
        end
        products.delete!("\n")
        csv << [name, count, organization_name, description, enabled, start_date, interval,
                products]
      end
    end
  end
end

#importObject



59
60
61
62
63
64
65
# File 'lib/hammer_cli_csv/sync_plans.rb', line 59

def import
  @existing = {}

  thread_import do |line|
    create_content_hosts_from_csv(line)
  end
end