Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / operating_systems.rb @ f8ecc788

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 OperatingSystemsCommand < BaseCommand
33

    
34
    FAMILY = 'Family'
35

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

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

    
54
      thread_import do |line|
55
        create_operatingsystems_from_csv(line)
56
      end
57
    end
58

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

    
92
  HammerCLICsv::CsvCommand.subcommand("operating-systems",
93
                                      "import or export operating systems",
94
                                      HammerCLICsv::OperatingSystemsCommand)
95
end