Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / organizations.rb @ 0c7641a0

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
# -= Organizations CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Name of the organization.
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "organization%d" -> "organization1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#   Org Label
35
#     - Label of the organization.
36
#     - May contain '%d' which will be replaced with current iteration number of Count
37
#     - eg. "organization%d" -> "organization1"
38
#   Description
39
#
40

    
41
require 'hammer_cli'
42
require 'katello_api'
43
require 'json'
44
require 'csv'
45

    
46
module HammerCLICsv
47
  class OrganizationsCommand < BaseCommand
48

    
49
    ORGLABEL = 'Org Label'
50
    DESCRIPTION = 'Description'
51

    
52
    def execute
53
      super
54
      csv_export? ? export : import
55
      HammerCLI::EX_OK
56
    end
57

    
58
    def export
59
      CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
60
        csv << [NAME, COUNT, ORGLABEL, DESCRIPTION]
61
        @f_organization_api.index({:per_page => 999999}, HEADERS)[0].each do |organization|
62
          organization = organization['organization']
63
          csv << [organization['name'], 1, '', '']
64
        end
65
      end
66
    end
67

    
68
    def import
69
      @existing = {}
70
      @f_organization_api.index({:per_page => 999999}, HEADERS)[0].each do |organization|
71
          @existing[organization['name']] = organization['label']
72
      end
73

    
74
      thread_import do |line|
75
        create_organizations_from_csv(line)
76
      end
77
    end
78

    
79
    def create_organizations_from_csv(line)
80
      line[COUNT].to_i.times do |number|
81
        name = namify(line[NAME], number)
82
        label = namify(line[ORGLABEL], number)
83
        if !@existing.include? name
84
          print "Creating organization '#{name}'... " if verbose?
85
          @f_organization_api.create({
86
                             'organization' => {
87
                               'name' => name,
88
                               #'label' => label,
89
                               #'description' => line[DESCRIPTION]
90
                             }
91
                           }, HEADERS)
92
        else
93
          print "Updating organization '#{name}'... " if verbose?
94
          puts "\nWARNING: Updating organizations is not supported since the entire organization data must be passed in\n"
95
          # @f_organization_api.update({
96
          #                    'id' => name,
97
          #                    'organization' => {
98
          #                      'name' => name,
99
          #                      #'description' => line[DESCRIPTION]
100
          #                    }
101
          #                  }, HEADERS)
102
        end
103
        print "done\n" if verbose?
104
      end
105
    end
106
  end
107

    
108
  HammerCLI::MainCommand.subcommand("csv:organizations", "ping the katello server", HammerCLICsv::OrganizationsCommand)
109
end