Project

General

Profile

Download (2.99 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / export.rb @ c2496839

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12

    
13
module HammerCLICsv
14
  class CsvCommand
15
    class ExportCommand < HammerCLI::Apipie::Command
16
      command_name 'export'
17
      desc         'export into directory'
18

    
19
      option %w(-v --verbose), :flag, 'be verbose'
20
      option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1
21
      option %w(--server), 'SERVER', 'Server URL'
22
      option %w(-u --username), 'USERNAME', 'Username to access server'
23
      option %w(-p --password), 'PASSWORD', 'Password to access server'
24
      option '--dir', 'DIRECTORY', 'directory to import from'
25

    
26
      RESOURCES = %w( organizations locations puppet_environments operating_systems
27
                      domains architectures partition_tables lifecycle_environments host_collections
28
                      provisioning_templates
29
                      subscriptions activation_keys hosts content_hosts reports roles users )
30
      RESOURCES.each do |resource|
31
        dashed = resource.sub('_', '-')
32
        option "--#{dashed}", 'FILE', "csv file for #{dashed}"
33
      end
34

    
35
      def execute
36
        @api = ApipieBindings::API.new({
37
                                         :uri => option_server || HammerCLI::Settings.get(:csv, :host),
38
                                         :username => option_username || HammerCLI::Settings.get(:csv, :username),
39
                                         :password => option_password || HammerCLI::Settings.get(:csv, :password),
40
                                         :api_version => 2
41
                                       })
42

    
43
        # Swing the hammers
44
        RESOURCES.each do |resource|
45
          hammer_resource(resource)
46
        end
47

    
48
        HammerCLI::EX_OK
49
      end
50

    
51
      def hammer(context = nil)
52
        context ||= {
53
          :interactive => false,
54
          :username => 'admin', # TODO: this needs to come from config/settings
55
          :password => 'changeme' # TODO: this needs to come from config/settings
56
        }
57

    
58
        HammerCLI::MainCommand.new('', context)
59
      end
60

    
61
      def hammer_resource(resource)
62
        return if !self.send("option_#{resource}") && !option_dir
63
        options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
64
        args = %W( csv #{resource.sub('_', '-')} --csv-export --csv-file #{options_file} )
65
        args << '-v' if option_verbose?
66
        args += %W( --threads #{option_threads} )
67
        args += %W( --server #{option_server} ) if option_server
68
        hammer.run(args)
69
      end
70
    end
71
  end
72
end