Project

General

Profile

Revision 6434439d

Added by Thomas McKay almost 6 years ago

fixes #14390 - installation-media i/o

View differences:

lib/hammer_cli_csv.rb
23 23
  require 'hammer_cli_csv/hosts'
24 24
  require 'hammer_cli_csv/host_groups'
25 25
  require 'hammer_cli_csv/import'
26
  require 'hammer_cli_csv/installation_medias'
26
  require 'hammer_cli_csv/installation_media'
27 27
  require 'hammer_cli_csv/job_templates'
28 28
  require 'hammer_cli_csv/lifecycle_environments'
29 29
  require 'hammer_cli_csv/locations'
lib/hammer_cli_csv/installation_media.rb
1
module HammerCLICsv
2
  class CsvCommand
3
    class InstallationMediaCommand < BaseCommand
4
      command_name 'installation-media'
5
      desc         'import or export media'
6

  
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9
      PATH = 'Path'
10
      OSFAMILY = 'OS Family'
11
      OPERATING_SYSTEMS = 'Operating Systems'
12

  
13
      def export
14
        CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
15
          csv << [NAME, ORGANIZATIONS, LOCATIONS, PATH, OSFAMILY, OPERATING_SYSTEMS]
16
          @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
17
            medium = @api.resource(:media).call(:show, :id => medium['id'])
18
            name = medium['name']
19
            organizations = export_column(medium, 'organizations', 'name')
20
            locations = export_column(medium, 'locations', 'name')
21
            count = 1
22
            path = medium['path']
23
            os_family = medium['os_family']
24
            operating_systems = export_column(medium, 'operatingsystems', 'title')
25
            csv << [name, organizations, locations, path, os_family, operating_systems]
26
          end
27
        end
28
      end
29

  
30
      def import
31
        @existing = {}
32
        @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
33
          @existing[medium['name']] = medium['id'] if medium
34
        end
35

  
36
        thread_import do |line|
37
          create_from_csv(line)
38
        end
39
      end
40

  
41
      def create_from_csv(line)
42
        params = {
43
          'medium' => {
44
            'organization_ids' => collect_column(line[ORGANIZATIONS]) do |organization|
45
              foreman_organization(:name => organization)
46
            end,
47
            'location_ids' => collect_column(line[LOCATIONS]) do |location|
48
              foreman_location(:name => location)
49
            end,
50
            'path' => line[PATH],
51
            'os_family' => line[OSFAMILY],
52
            'operatingsystem_ids' => collect_column(line[OPERATING_SYSTEMS]) do |os|
53
              foreman_operatingsystem(:name => os)
54
            end
55
          }
56
        }
57

  
58
        count(line[COUNT]).times do |number|
59
          name = namify(line[NAME], number)
60
          params['medium']['name'] = name
61

  
62
          if !@existing.include? name
63
            print _("Creating installation medium '%{name}'... ") % {:name => name} if option_verbose?
64
            medium = @api.resource(:media).call(:create, params)
65
            @existing[name] = medium['id']
66
          else
67
            print _("Updating installation medium '%{name}'... ") % {:name => name} if option_verbose?
68
            params['id'] = @existing[name]
69
            medium = @api.resource(:media).call(:update, params)
70
          end
71
          puts _('done') if option_verbose?
72
        end
73
      rescue RuntimeError => e
74
        raise "#{e}\n       #{line}"
75
      end
76
    end
77
  end
78
end
lib/hammer_cli_csv/installation_medias.rb
1
module HammerCLICsv
2
  class CsvCommand
3
    class InstallationMediasCommand < BaseCommand
4
      command_name 'installation-medias'
5
      desc         'import or export installation media'
6

  
7
      OSFAMILY = 'OS Family'
8
      PATH = 'Path'
9
      ORGANIZATIONS = 'Organizations'
10

  
11
      def export
12
        CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
13
          csv << [NAME, PATH, OSFAMILY]
14
          @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media|
15
            name = installation_media['name']
16
            count = 1
17
            path = installation_media['path']
18
            os_family = installation_media['os_family']
19
            csv << [name, path, os_family]
20
          end
21
        end
22
      end
23

  
24
      def import
25
        @existing = {}
26
        @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media|
27
          @existing[installation_media['name']] = installation_media['id'] if installation_media
28
        end
29

  
30
        thread_import do |line|
31
          create_installation_medias_from_csv(line)
32
        end
33
      end
34

  
35
      def create_installation_medias_from_csv(line)
36
        count(line[COUNT]).times do |number|
37
          name = namify(line[NAME], number)
38
          if !@existing.include? name
39
            print "Creating installation_media '#{name}'..." if option_verbose?
40
            installation_media_id = @api.resource(:media).call(:create, {
41
                                                       'name' => name
42
                                                     })['id']
43
          else
44
            print "Updating installation_media '#{name}'..." if option_verbose?
45
            installation_media_id = @api.resource(:media).call(:update, {
46
                                                       'id' => @existing[name],
47
                                                       'name' => name
48
                                                     })['id']
49
          end
50

  
51
          # Update associated resources
52
          installation_medias ||= {}
53
          CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
54
            organization_id = foreman_organization(:name => organization)
55
            if installation_medias[organization].nil?
56
              installation_medias[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['installation_medias'].collect do |installation_media|
57
                installation_media['id']
58
              end
59
            end
60
            installation_medias[organization] += [installation_media_id] if !installation_medias[organization].include? installation_media_id
61

  
62
            @api.resource(:organizations).call(:update, {
63
                                                 'id' => organization_id,
64
                                                 'organization' => {
65
                                                   'installation_media_ids' => installation_medias[organization]
66
                                                 }
67
                                               })
68
          end
69

  
70
          print "done\n" if option_verbose?
71
        end
72
      rescue RuntimeError => e
73
        raise "#{e}\n       #{line}"
74
      end
75
    end
76
  end
77
end

Also available in: Unified diff