Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / installation_medias.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
module HammerCLICsv
13
  class CsvCommand
14
    class InstallationMediasCommand < BaseCommand
15
      command_name 'installation-medias'
16
      desc         'import or export installation media'
17

    
18
      OSFAMILY = 'OS Family'
19
      PATH = 'Path'
20
      ORGANIZATIONS = 'Organizations'
21

    
22
      def export
23
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
24
          csv << [NAME, COUNT, PATH, OSFAMILY]
25
          @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media|
26
            name = installation_media['name']
27
            count = 1
28
            path = installation_media['path']
29
            os_family = installation_media['os_family']
30
            csv << [name, count, path, os_family]
31
          end
32
        end
33
      end
34

    
35
      def import
36
        @existing = {}
37
        @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media|
38
          @existing[installation_media['name']] = installation_media['id'] if installation_media
39
        end
40

    
41
        thread_import do |line|
42
          create_installation_medias_from_csv(line)
43
        end
44
      end
45

    
46
      def create_installation_medias_from_csv(line)
47
        line[COUNT].to_i.times do |number|
48
          name = namify(line[NAME], number)
49
          if !@existing.include? name
50
            print "Creating installation_media '#{name}'..." if option_verbose?
51
            installation_media_id = @api.resource(:media).call(:create, {
52
                                                       'name' => name
53
                                                     })['id']
54
          else
55
            print "Updating installation_media '#{name}'..." if option_verbose?
56
            installation_media_id = @api.resource(:media).call(:update, {
57
                                                       'id' => @existing[name],
58
                                                       'name' => name
59
                                                     })['id']
60
          end
61

    
62
          # Update associated resources
63
          installation_medias ||= {}
64
          CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
65
            organization_id = foreman_organization(:name => organization)
66
            if installation_medias[organization].nil?
67
              installation_medias[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['installation_medias'].collect do |installation_media|
68
                installation_media['id']
69
              end
70
            end
71
            installation_medias[organization] += [installation_media_id] if !installation_medias[organization].include? installation_media_id
72

    
73
            @api.resource(:organizations).call(:update, {
74
                                                 'id' => organization_id,
75
                                                 'organization' => {
76
                                                   'installation_media_ids' => installation_medias[organization]
77
                                                 }
78
                                               })
79
          end
80

    
81
          print "done\n" if option_verbose?
82
        end
83
      rescue RuntimeError => e
84
        raise "#{e}\n       #{line}"
85
      end
86
    end
87
  end
88
end