Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / operating_systems.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
# -= Operating Systems CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Operating system name
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "os%d" -> "os1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#   Major
23
#   Minor
24
#   Family
25
#
26

    
27
require 'hammer_cli'
28
require 'json'
29
require 'csv'
30

    
31
module HammerCLICsv
32
  class CsvCommand
33
    class OperatingSystemsCommand < BaseCommand
34
      command_name 'operating-systems'
35
      desc         'import or export operating systems'
36

    
37
      FAMILY = 'Family'
38

    
39
      def export
40
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
41
          csv << [NAME, COUNT, FAMILY]
42
          @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
43
            name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
44
            count = 1
45
            family = operatingsystem['family']
46
            csv << [name, count, family]
47
          end
48
        end
49
      end
50

    
51
      def import
52
        @existing = {}
53
        @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
54
          @existing[build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])] = operatingsystem['id'] if operatingsystem
55
        end
56

    
57
        thread_import do |line|
58
          create_operatingsystems_from_csv(line)
59
        end
60
      end
61

    
62
      def create_operatingsystems_from_csv(line)
63
        line[COUNT].to_i.times do |number|
64
          name = namify(line[NAME], number)
65
          (osname, major, minor) = split_os_name(name)
66
          if !@existing.include? name
67
            print "Creating operating system '#{name}'..." if option_verbose?
68
            @api.resource(:operatingsystems).call(:create, {
69
                                            'operatingsystem' => {
70
                                              'name' => osname,
71
                                              'major' => major,
72
                                              'minor' => minor,
73
                                              'family' => line[FAMILY]
74
                                            }
75
                                          })
76
          else
77
            print "Updating operating system '#{name}'..." if option_verbose?
78
            @api.resource(:operatingsystems).call(:update, {
79
                                            'id' => @existing[name],
80
                                            'operatingsystem' => {
81
                                              'name' => osname,
82
                                              'major' => major,
83
                                              'minor' => minor,
84
                                              'family' => line[FAMILY]
85
                                            }
86
                                          })
87
          end
88
          print "done\n" if option_verbose?
89
        end
90
      rescue RuntimeError => e
91
        raise "#{e}\n       #{line}"
92
      end
93
    end
94
  end
95
end