Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / import.rb @ a77acc4a

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
      option '--roles', 'FILE', 'source to import roles'
32
      option '--users', 'FILE', 'source to import users'
33
      option '--hosts', 'FILE', 'source to import hosts'
34
      option '--organizations', 'FILE', 'source to import organizations'
35
      option '--locations', 'FILE', 'source to import locations'
36
      option '--puppet-environments', 'FILE', 'source to puppet environments'
37
      option '--operating-systems', 'FILE', 'source to operating systems'
38
      option '--architectures', 'FILE', 'source to architectures'
39
      option '--domains', 'FILE', 'source to domains'
40
      option '--architectures', 'FILE', 'source to architectures'
41
      option '--partition-tables', 'FILE', 'source to partition-tables'
42

    
43
      def ctx
44
        {
45
          :interactive => false,
46
          :username => 'admin',
47
          :password => 'changeme'
48
        }
49
      end
50

    
51
      def hammer(context = nil)
52
        HammerCLI::MainCommand.new('', context || ctx)
53
      end
54

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

    
63
        # Swing the hammers
64
        %w( organizations locations roles users puppet_environments operating_systems
65
            hosts domains architectures partition_tables ).each do |resource|
66
          swing(resource)
67
        end
68

    
69
        HammerCLI::EX_OK
70
      end
71

    
72
      def swing(resource)
73
        return if !self.send("option_#{resource}") && !option_dir
74
        options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
75
        raise "File for #{resource} '#{options_file}' does not exist" unless File.exists? options_file
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