Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / import.rb @ 561a8ac9

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 ImportCommand < HammerCLI::Apipie::Command
16
      command_name 'import'
17
      desc         'import by 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
        if !File.exists? options_file
65
          return if option_dir
66
          raise "File for #{resource} '#{options_file}' does not exist"
67
        end
68

    
69
        args = %W( csv #{resource.sub('_', '-')} --csv-file #{options_file} )
70
        args << '-v' if option_verbose?
71
        args += %W( --threads #{option_threads} )
72
        hammer.run(args)
73
      end
74
    end
75
  end
76
end