Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / host_collections.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
# -= System Groups CSV =-
14
#
15
# Columns
16
#   Name
17
#     - System group name
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "group%d" -> "group1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#   Org Label
23
#   Limit
24
#   Description
25
#
26

    
27
require 'hammer_cli'
28
require 'json'
29
require 'csv'
30

    
31
module HammerCLICsv
32
  class CsvCommand
33
    class HostCollectionsCommand < BaseCommand
34
      command_name 'host-collections'
35
      desc         'import or export host collections'
36

    
37
      ORGANIZATION = 'Organization'
38
      LIMIT = 'Limit'
39
      DESCRIPTION = 'Description'
40

    
41
      def export
42
        CSV.open(option_csv_file, 'wb') do |csv|
43
          csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION]
44
          @api.resource(:organizations)\
45
            .call(:index, {
46
                    'per_page' => 999999
47
                  })['results'].each do |organization|
48
            @api.resource(:host_collections)\
49
              .call(:index, {
50
                      'organization_id' => organization['id']
51
                    }).each do |hostcollection|
52
              puts hostcollection
53
              csv << [hostcollection['name'], 1, organization['id'],
54
                      hostcollection['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'],
55
                      hostcollection['description']]
56
            end
57
          end
58
        end
59
      end
60

    
61
      def import
62
        @existing = {}
63

    
64
        thread_import do |line|
65
          create_hostcollections_from_csv(line)
66
        end
67
      end
68

    
69
      def create_hostcollections_from_csv(line)
70
        if !@existing[line[ORGANIZATION]]
71
          @existing[line[ORGANIZATION]] = {}
72
          @api.resource(:host_collections)\
73
            .call(:index, {
74
                    'per_page' => 999999,
75
                    'organization_id' => foreman_organization(:name => line[ORGANIZATION])
76
                  })['results'].each do |hostcollection|
77
            @existing[line[ORGANIZATION]][hostcollection['name']] = hostcollection['id']
78
          end
79
        end
80

    
81
        line[COUNT].to_i.times do |number|
82
          name = namify(line[NAME], number)
83
          if !@existing[line[ORGANIZATION]].include? name
84
            print "Creating system group '#{name}'..." if option_verbose?
85
            @api.resource(:host_collections)\
86
              .call(:create, {
87
                      'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
88
                      'name' => name,
89
                      'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
90
                      'description' => line[DESCRIPTION]
91
                    })
92
          else
93
            print "Updating system group '#{name}'..." if option_verbose?
94
            @api.resource(:host_collections)\
95
              .call(:update, {
96
                      'organization_id' => line[ORGANIZATION],
97
                      'id' => @existing[line[ORGANIZATION]][name],
98
                      'name' => name,
99
                      'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
100
                      'description' => line[DESCRIPTION]
101
                    })
102
          end
103
          print "done\n" if option_verbose?
104
        end
105
      end
106
    end
107
  end
108
end