Class: HammerCLICsv::CsvCommand::ImportCommand

Inherits:
HammerCLI::Apipie::Command
  • Object
show all
Includes:
Utils::Config
Defined in:
lib/hammer_cli_csv/import.rb

Constant Summary collapse

RESOURCES =
%w(
  settings organizations locations puppet_environments operating_systems
  domains architectures partition_tables lifecycle_environments host_collections
  provisioning_templates
  subscriptions products content_views content_view_filters activation_keys
  hosts content_hosts smart_proxies compute_resources reports roles users
)
SUPPORTED_RESOURCES =
%w(
  settings
)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Config

#api_connection

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/hammer_cli_csv/import.rb', line 11

def self.supported?
  true
end

Instance Method Details

#executeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hammer_cli_csv/import.rb', line 39

def execute
  @api = api_connection

  resources_specified = RESOURCES.collect do |resource|
    resource if self.send("option_#{resource}") || ARGV.include?('--' + resource.gsub('_', '-'))
  end
  resources_specified.compact!
  RESOURCES.each do |resource|
    if resources_specified.include?(resource) || (resources_specified == [] && option_dir)
      hammer_resource(resource)
    end
  end

  HammerCLI::EX_OK
end

#hammer(context = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/hammer_cli_csv/import.rb', line 55

def hammer(context = nil)
  context ||= {
    :interactive => false,
    :username => @username,
    :password => @password
  }

  HammerCLI::MainCommand.new('', context)
end

#hammer_resource(resource) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hammer_cli_csv/import.rb', line 65

def hammer_resource(resource)
  return if !self.send("option_#{resource}") && !option_dir
  options_file = option_dir ? "#{option_dir}/#{resource.gsub('_', '-')}.csv" :  self.send("option_#{resource}")
  unless options_file_exists? options_file
    if option_dir
      return unless SUPPORTED_RESOURCES.include?(resource)
      puts _("Skipping %{resource} because '%{options_file}' does not exist") %
        {:resource => resource, :options_file => options_file} if option_verbose?
      return
    end
    raise "File for #{resource} '#{options_file}' does not exist"
  end
  puts _("Importing %{resource} from '%{options_file}'") %
    {:resource => resource, :options_file => options_file} if option_verbose?

  args = %W( csv #{resource.gsub('_', '-')} --file #{options_file} )
  args << '-v' if option_verbose?
  args += %W( --organization #{option_organization} ) if option_organization
  args += %W( --prefix #{option_prefix} ) if option_prefix
  args += %W( --threads #{option_threads} ) if option_threads
  hammer.run(args)
end