Project

General

Profile

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

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

1
# Copyright (c) 2013 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
    def initialize(*args)
48
      super(args)
49
      @organization_api = KatelloApi::Resources::Organization.new(@init_options)
50
      @systemgroup_api = KatelloApi::Resources::SystemGroup.new(@init_options)
51
    end
52

    
53
    def execute
54
      csv_export? ? export : import
55

    
56
      HammerCLI::EX_OK
57
    end
58

    
59
    def export
60
      CSV.open(csv_file, 'wb') do |csv|
61
        csv << ['Name', 'Count', 'Org Label', 'Limit', 'Description']
62
        @organization_api.index[0].each do |organization|
63
          @systemgroup_api.index({'organization_id' => organization['label']})[0].each do |systemgroup|
64
            puts systemgroup
65
            csv << [systemgroup['name'], 1, organization['label'],
66
                    systemgroup['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'],
67
                    systemgroup['description']]
68
          end
69
        end
70
      end
71
    end
72

    
73
    def import
74
      @existing = {}
75

    
76
      thread_import do |line|
77
        create_systemgroups_from_csv(line)
78
      end
79
    end
80

    
81
    def create_systemgroups_from_csv(line)
82
      details = parse_systemgroup_csv(line)
83

    
84
      if !@existing[details[:org_label]]
85
        @existing[details[:org_label]] = {}
86
        @systemgroup_api.index({'organization_id' => details[:org_label]})[0].each do |systemgroup|
87
          @existing[details[:org_label]][systemgroup['name']] = systemgroup['id']
88
        end
89
      end
90

    
91
      details[:count].times do |number|
92
        name = namify(details[:name_format], number)
93
        if !@existing[details[:org_label]].include? name
94
          puts "Creating system group '#{name}'" if verbose?
95
          @systemgroup_api.create({
96
                             'organization_id' => details[:org_label],
97
                             'system_group' => {
98
                               'name' => name,
99
                               'max_systems' => (details[:limit] == 'Unlimited') ? -1 : details[:limit],
100
                               'description' => details[:description]
101
                             }
102
                           }, HEADERS)
103
        else
104
          puts "Updating systemgroup '#{name}'" if verbose?
105
          @systemgroup_api.update({
106
                             'organization_id' => details[:org_label],
107
                             'id' => @existing[details[:org_label]][name],
108
                             'system_group' => {
109
                               'name' => name,
110
                               'max_systems' => (details[:limit] == 'Unlimited') ? -1 : details[:limit],
111
                               'description' => details[:description]
112
                             }
113
                           }, HEADERS)
114
        end
115
      end
116
    end
117

    
118
    def parse_systemgroup_csv(line)
119
      keys = [:name_format, :count, :org_label, :limit, :description]
120
      details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0]
121

    
122
      details[:count] = details[:count].to_i
123

    
124
      details
125
    end
126
  end
127

    
128
  HammerCLI::MainCommand.subcommand("csv:systemgroups", "ping the katello server", HammerCLICsv::SystemGroupsCommand)
129
end