Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / import.rb @ 8c7ba4af

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
require 'hammer_cli'
13
require 'hammer_cli_csv'
14
require 'hammer_cli_foreman'
15
require 'hammer_cli_katello'
16
require 'json'
17
require 'csv'
18
require 'uri'
19

    
20
module HammerCLICsv
21
  class CsvCommand
22
    class ImportCommand < HammerCLI::Apipie::Command
23
      command_name 'import'
24
      desc         'import by directory'
25

    
26
      option %w(-v --verbose), :flag, 'be verbose'
27
      option %w(--server), 'SERVER', 'Server URL'
28
      option %w(-u --username), 'USERNAME', 'Username to access server'
29
      option %w(-p --password), 'PASSWORD', 'Password to access server'
30
      option '--dir', 'DIRECTORY', 'directory to import from'
31

    
32
      RESOURCES = %w( organizations locations roles users puppet_environments operating_systems
33
            domains architectures partition_tables lifecycle_environments host_collections
34
            subscriptions activation_keys hosts content_hosts reports )
35
      RESOURCES.each do |resource|
36
        dashed = resource.sub('_', '-')
37
        option "--#{dashed}", 'FILE', "csv file for #{dashed}"
38
      end
39

    
40
      def ctx
41
        {
42
          :interactive => false,
43
          :username => 'admin',
44
          :password => 'changeme'
45
        }
46
      end
47

    
48
      def hammer(context = nil)
49
        HammerCLI::MainCommand.new('', context || ctx)
50
      end
51

    
52
      def execute
53
        @api = ApipieBindings::API.new({
54
                                         :uri => option_server || HammerCLI::Settings.get(:csv, :host),
55
                                         :username => option_username || HammerCLI::Settings.get(:csv, :username),
56
                                         :password => option_password || HammerCLI::Settings.get(:csv, :password),
57
                                         :api_version => 2
58
                                       })
59

    
60
        # Swing the hammers
61
        RESOURCES.each do |resource|
62
          hammer_resource(resource)
63
        end
64

    
65
        HammerCLI::EX_OK
66
      end
67

    
68
      def hammer_resource(resource)
69
        return if !self.send("option_#{resource}") && !option_dir
70
        options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
71
        if !File.exists? options_file
72
          return if option_dir
73
          raise "File for #{resource} '#{options_file}' does not exist"
74
        end
75

    
76
        args = %W( csv #{resource.sub('_', '-')} --csv-file #{options_file} )
77
        args << '-v' if option_verbose?
78
        hammer.run(args)
79
      end
80
    end
81
  end
82
end