Project

General

Profile

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

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

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 OrganizationsCommand < BaseCommand
35
    ORGLABEL = 'Org Label'
36
    DESCRIPTION = 'Description'
37

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

    
47
    def import
48
      @existing = {}
49
      @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
50
        @existing[organization['name']] = organization['label'] if organization
51
      end
52

    
53
      thread_import do |line|
54
        create_organizations_from_csv(line)
55
      end
56
    end
57

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

    
81
  HammerCLICsv::CsvCommand.subcommand('organizations',
82
                                      'import or export organizations',
83
                                      HammerCLICsv::OrganizationsCommand)
84
end