Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / organizations.rb @ a77acc4a

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
# -= Organizations CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Name of the organization.
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "organization%d" -> "organization1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#   Org Label
23
#     - Label of the organization.
24
#     - May contain '%d' which will be replaced with current iteration number of Count
25
#     - eg. "organization%d" -> "organization1"
26
#   Description
27
#
28

    
29
require 'hammer_cli'
30
require 'json'
31
require 'csv'
32

    
33
module HammerCLICsv
34
  class CsvCommand
35
    class OrganizationsCommand < BaseCommand
36
      command_name 'organizations'
37
      desc         'import or export organizations'
38

    
39
      ORGLABEL = 'Org Label'
40
      DESCRIPTION = 'Description'
41

    
42
      def export
43
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
44
          csv << [NAME, COUNT, ORGLABEL, DESCRIPTION]
45
          @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
46
            csv << [organization['name'], 1, organization['label'], organization['description']]
47
          end
48
        end
49
      end
50

    
51
      def import
52
        @existing = {}
53
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
54
          @existing[organization['name']] = organization['id'] if organization
55
        end
56

    
57
        thread_import do |line|
58
          create_organizations_from_csv(line)
59
        end
60
      end
61

    
62
      def create_organizations_from_csv(line)
63
        line[COUNT].to_i.times do |number|
64
          name = namify(line[NAME], number)
65
          label = namify(line[ORGLABEL], number)
66
          if !@existing.include? name
67
            print "Creating organization '#{name}'... " if option_verbose?
68
            @api.resource(:organizations).call(:create, {
69
                                         'name' => name,
70
                                         'label' => label,
71
                                         'description' => line[DESCRIPTION]
72
                                       })
73
          else
74
            print "Updating organization '#{name}'... " if option_verbose?
75
            @api.resource(:organizations).call(:update, {
76
                                         'id' => label,
77
                                         'description' => line[DESCRIPTION]
78
                                       })
79
          end
80
          print "done\n" if option_verbose?
81
        end
82
      end
83
    end
84
  end
85
end