Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / system_groups.rb @ bfc065ce

1
# Copyright (c) 2013-2014 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
# -= System Groups CSV =-
26
#
27
# Columns
28
#   Name
29
#     - System group name
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "group%d" -> "group1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#   Org Label
35
#   Limit
36
#   Description
37
#
38

    
39
require 'hammer_cli'
40
require 'katello_api'
41
require 'json'
42
require 'csv'
43

    
44
module HammerCLICsv
45
  class SystemGroupsCommand < BaseCommand
46

    
47
    ORGANIZATION = 'Organization'
48
    LIMIT = 'Limit'
49
    DESCRIPTION = 'Description'
50

    
51
    def export
52
      CSV.open(option_csv_file, 'wb') do |csv|
53
        csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION]
54
        @f_organization_api.index[0].each do |organization|
55
          @systemgroup_api.index({'organization_id' => organization['label']})[0].each do |systemgroup|
56
            puts systemgroup
57
            csv << [systemgroup['name'], 1, organization['label'],
58
                    systemgroup['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'],
59
                    systemgroup['description']]
60
          end
61
        end
62
      end
63
    end
64

    
65
    def import
66
      @existing = {}
67

    
68
      thread_import do |line|
69
        create_systemgroups_from_csv(line)
70
      end
71
    end
72

    
73
    def create_systemgroups_from_csv(line)
74
      if !@existing[line[ORGANIZATION]]
75
        @existing[line[ORGANIZATION]] = {}
76
        @k_systemgroup_api.index({'organization_id' => line[ORGANIZATION]})[0]['results'].each do |systemgroup|
77
          @existing[line[ORGANIZATION]][systemgroup['name']] = systemgroup['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
          @k_systemgroup_api.create({
86
                                    'organization_id' => line[ORGANIZATION],
87
                                    'name' => name,
88
                                    'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
89
                                    'description' => line[DESCRIPTION]
90
                                  })
91
        else
92
          print "Updating system group '#{name}'..." if option_verbose?
93
          @k_systemgroup_api.update({
94
                                      'organization_id' => line[ORGANIZATION],
95
                                      'id' => @existing[line[ORGANIZATION]][name],
96
                                      'name' => name,
97
                                      'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
98
                                      'description' => line[DESCRIPTION]
99
                                    })
100
        end
101
    print "done\n" if option_verbose?
102
      end
103
    end
104
  end
105

    
106
  HammerCLI::MainCommand.subcommand("csv:systemgroups", "system groups", HammerCLICsv::SystemGroupsCommand)
107
end