Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / architectures.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
# -= Architectures CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Architecture 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
#
23

    
24
require 'hammer_cli'
25
require 'json'
26
require 'csv'
27

    
28
module HammerCLICsv
29
  class CsvCommand
30
    class ArchitecturesCommand < BaseCommand
31
      command_name 'architectures'
32
      desc         'import or export architectures'
33

    
34
      OPERATINGSYSTEMS = 'Operating Systems'
35

    
36
      def export
37
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
38
          csv << [NAME, COUNT, ORGANIZATIONS, OPERATINGSYSTEMS]
39
          @api.resource(:architectures).call(:index, {:per_page => 999999})['results'].each do |architecture|
40
            name = architecture['name']
41
            count = 1
42
            # TODO: http://projects.theforeman.org/issues/4198
43
            #operatingsystems = architecture['operatingsystem_ids'].collect do |operatingsystem_id|
44
            #  foreman_operatingsystem(:id => operatingsystem_id)
45
            #end.join(',')
46
            operatingsystems = ''
47
            csv << [name, count, operatingsystems]
48
          end
49
        end
50
      end
51

    
52
      def import
53
        @existing = {}
54
        @api.resource(:architectures).call(:index, {:per_page => 999999})['results'].each do |architecture|
55
          @existing[architecture['name']] = architecture['id'] if architecture
56
        end
57

    
58
        thread_import do |line|
59
          create_architectures_from_csv(line)
60
        end
61
      end
62

    
63
      def create_architectures_from_csv(line)
64
        line[COUNT].to_i.times do |number|
65
          name = namify(line[NAME], number)
66
          architecture_id = @existing[name]
67
          operatingsystem_ids = CSV.parse_line(line[OPERATINGSYSTEMS]).collect do |operatingsystem_name|
68
            foreman_operatingsystem(:name => operatingsystem_name)
69
          end
70
          if !architecture_id
71
            print "Creating architecture '#{name}'..." if option_verbose?
72
            architecture_id = @api.resource(:architectures).call(:create, {
73
                               'architecture' => {
74
                                 'name' => name,
75
                                 'operatingsystem_ids' => operatingsystem_ids
76
                               }
77
                             })
78
          else
79
            print "Updating architecture '#{name}'..." if option_verbose?
80
            @api.resource(:architectures).call(:update, {
81
                               'id' => architecture_id,
82
                               'architecture' => {
83
                                 'name' => name,
84
                                 'operatingsystem_ids' => operatingsystem_ids
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