Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / architectures.rb @ 0c7641a0

1
# Copyright (c) 2013 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24
#
25
# -= Architectures CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Architecture name
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "os%d" -> "os1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#
35

    
36
require 'hammer_cli'
37
require 'foreman_api'
38
require 'json'
39
require 'csv'
40

    
41
module HammerCLICsv
42
  class ArchitecturesCommand < BaseCommand
43

    
44
    OPERATINGSYSTEMS = 'Operating Systems'
45

    
46
    def execute
47
      super
48
      csv_export? ? export : import
49
      HammerCLI::EX_OK
50
    end
51

    
52
    def export
53
      CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
54
        csv << [NAME, COUNT, OPERATINGSYSTEMS]
55
        @f_architecture_api.index({:per_page => 999999}, HEADERS)[0].each do |architecture|
56
          name = architecture['name']
57
          count = 1
58
          operatingsystems = architecture['operatingsystem_ids'].collect do |operatingsystem_id|
59
            foreman_operatingsystem(:id => operatingsystem_id)
60
          end.join(',')
61
          csv << [name, count, operatingsystems]
62
        end
63
      end
64
    end
65

    
66
    def import
67
      @existing = {}
68
      @f_architecture_api.index({:per_page => 999999}, HEADERS)[0].each do |architecture|
69
        @existing[architecture['name']] = architecture['id'] if architecture
70
      end
71

    
72
      thread_import do |line|
73
        create_architectures_from_csv(line)
74
      end
75
    end
76

    
77
    def create_architectures_from_csv(line)
78
      line[COUNT].to_i.times do |number|
79
        name = namify(line[NAME], number)
80
        architecture_id = @existing[name]
81
        if !architecture_id
82
          print "Creating architecture '#{name}'..." if verbose?
83
          architecture_id = @f_architecture_api.create({
84
                             'architecture' => {
85
                               'name' => name
86
                             }
87
                           }, HEADERS)
88
        else
89
          print "Updating architecture '#{name}'..." if verbose?
90
          @f_architecture_api.update({
91
                             'id' => architecture_id,
92
                             'architecture' => {
93
                               'name' => name
94
                             }
95
                           }, HEADERS)
96
        end
97

    
98
=begin
99
        # Update operating systems refered to
100
        CSV.parse_line(line[OPERATINGSYSTEMS]).each do |operatingsystem_name|
101
          operatingsystem = @f_operatingsystem_api.show({
102
                                                          'id' => foreman_operatingsystem(:name => operatingsystem_name)
103
                                                        }, HEADERS)[0]
104
          architecture_ids = operatingsystem['architectures'].collect { |a| a['id'] }
105
          if !architecture_ids.include? architecture_id
106
            architecture_ids << architecture_id
107
            @f_operatingsystem_api.update({
108
                                            'id' => operatingsystem['id'],
109
                                            'operatingsystem' => {
110
                                              'architecture_ids' => architecture_ids
111
                                            }
112
                                          }, HEADERS)
113
          end
114
        end
115
=end
116

    
117
        print "done\n" if verbose?
118
      end
119
    rescue RuntimeError => e
120
      raise RuntimeError.new("#{e}\n       #{line}")
121
    end
122
  end
123

    
124
  HammerCLI::MainCommand.subcommand("csv:architectures", "ping the katello server", HammerCLICsv::ArchitecturesCommand)
125
end