Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / host_collections.rb @ f4fb9c20

1
module HammerCLICsv
2
  class CsvCommand
3
    class HostCollectionsCommand < BaseCommand
4
      command_name 'host-collections'
5
      desc         'import or export host collections'
6

    
7
      ORGANIZATION = 'Organization'
8
      LIMIT = 'Limit'
9
      DESCRIPTION = 'Description'
10

    
11
      def export(csv)
12
        csv << [NAME, ORGANIZATION, LIMIT, DESCRIPTION]
13
        if @server_status['release'] == 'Headpin'
14
          @headpin.get(:organizations).each do |organization|
15
            next if option_organization && organization['name'] != option_organization
16
            @headpin.get("organizations/#{organization['label']}/system_groups").each do |systemgroup|
17
              csv << [systemgroup['name'], organization['name'],
18
                      systemgroup['max_systems'].to_i < 0 ? 'Unlimited' : systemgroup['max_systems'],
19
                      systemgroup['description']]
20
            end
21
          end
22
        else
23
          @api.resource(:organizations).call(:index, {'per_page' => 999999})['results'].each do |organization|
24
            next if option_organization && organization['name'] != option_organization
25
            @api.resource(:host_collections).call(:index, {
26
                'organization_id' => organization['id']
27
            })['results'].each do |hostcollection|
28
              limit = hostcollection['unlimited_content_hosts'] ? 'Unlimited' : hostcollection['max_content_hosts']
29
              csv << [hostcollection['name'], organization['name'],
30
                      limit,
31
                      hostcollection['description']]
32
            end
33
          end
34
        end
35
      end
36

    
37
      def import
38
        @existing = {}
39

    
40
        thread_import do |line|
41
          create_hostcollections_from_csv(line)
42
        end
43
      end
44

    
45
      def create_hostcollections_from_csv(line)
46
        return if option_organization && line[ORGANIZATION] != option_organization
47

    
48
        if !@existing[line[ORGANIZATION]]
49
          @existing[line[ORGANIZATION]] = {}
50
          @api.resource(:host_collections).call(:index, {
51
              'per_page' => 999999,
52
              'organization_id' => foreman_organization(:name => line[ORGANIZATION])
53
          })['results'].each do |hostcollection|
54
            @existing[line[ORGANIZATION]][hostcollection['name']] = hostcollection['id']
55
          end
56
        end
57

    
58
        count(line[COUNT]).times do |number|
59
          name = namify(line[NAME], number)
60
          params =  {
61
                      'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
62
                      'name' => name,
63
                      'unlimited_content_hosts' => (line[LIMIT] == 'Unlimited') ? true : false,
64
                      'max_content_hosts' => (line[LIMIT] == 'Unlimited') ? nil : line[LIMIT].to_i,
65
                      'description' => line[DESCRIPTION]
66
                    }
67
          if !@existing[line[ORGANIZATION]].include? name
68
            print "Creating host collection '#{name}'..." if option_verbose?
69
            @api.resource(:host_collections).call(:create, params)
70
          else
71
            print "Updating host collection '#{name}'..." if option_verbose?
72
            params['id'] = @existing[line[ORGANIZATION]][name]
73
            @api.resource(:host_collections).call(:update, params)
74
          end
75
          print "done\n" if option_verbose?
76
        end
77
      end
78
    end
79
  end
80
end