Class: HammerCLICsv::CsvCommand::ExportCommand

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

Constant Summary collapse

RESOURCES =
%w(
  settings organizations locations puppet_environments operating_systems
  domains architectures partition_tables lifecycle_environments host_collections
  provisioning_templates
  subscriptions activation_keys hosts content_hosts 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)


9
10
11
# File 'lib/hammer_cli_csv/export.rb', line 9

def self.supported?
  true
end

Instance Method Details

#check_server_status(server, username, password) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hammer_cli_csv/export.rb', line 68

def check_server_status(server, username, password)
  url = "#{server}/api/status"
  uri = URI(url)
  nethttp = Net::HTTP.new(uri.host, uri.port)
  nethttp.use_ssl = uri.scheme == 'https'
  nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
  server_status = nethttp.start do |http|
    request = Net::HTTP::Get.new uri.request_uri
    request.basic_auth(username, password)
    response = http.request(request)
    JSON.parse(response.body)
  end

  server_status
end

#executeObject



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

def execute
  @api = api_connection
  skipped_resources = (RESOURCES - SUPPORTED_RESOURCES)

  (RESOURCES - skipped_resources).each do |resource|
    hammer_resource(resource)
  end

  HammerCLI::EX_OK
end

#hammer(context = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/hammer_cli_csv/export.rb', line 45

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

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

#hammer_resource(resource) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hammer_cli_csv/export.rb', line 55

def hammer_resource(resource)
  return if !self.send("option_#{resource}") && !option_dir
  options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
  args = []
  args += %W( --server #{@server} ) if @server
  args += %W( csv #{resource.sub('_', '-')} --export --file #{options_file} )
  args << '-v' if option_verbose?
  args += %W( --organization #{option_organization} ) if option_organization
  args += %W( --threads #{option_threads} ) if option_threads
  puts "Exporting '#{args.join(' ')}'" if option_verbose?
  hammer.run(args)
end