Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / organizations.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
# -= 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
    def initialize(*args)
50
      super(args)
51
      @organization_api = KatelloApi::Resources::Organization.new(@init_options)
52
    end
53

    
54
    def execute
55
      csv_export? ? export : import
56

    
57
      HammerCLI::EX_OK
58
    end
59

    
60
    def export
61
      CSV.open(csv_file, 'wb') do |csv|
62
        csv << ['Name','Count','Org Label', 'Description']
63
        @organization_api.index[0].each do |organization|
64
          csv << [organization['name'], 1, organization['label'], organization['description']]
65
        end
66
      end
67
    end
68

    
69
    def import
70
      @existing = {}
71
      @organization_api.index[0].each do |organization|
72
          @existing[organization['name']] = organization['label']
73
      end
74

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

    
80
    def create_organizations_from_csv(line)
81
      details = parse_organization_csv(line)
82

    
83
      details[:count].times do |number|
84
        name = namify(details[:name_format], number)
85
        label = namify(details[:label_format], number)
86
        if !@existing.include? name
87
          puts "Creating organization '#{name}'" if verbose?
88
          @organization_api.create({
89
                             :organization => {
90
                               :name => name,
91
                               :label => label,
92
                               :description => details[:description]
93
                             }
94
                           }, HEADERS)
95
        else
96
          puts "Updating organization '#{name}'" if verbose?
97
          @organization_api.update({
98
                             'id' => label,
99
                             :organization => {
100
                               :name => name,
101
                               :description => details[:description]
102
                             }
103
                           }, HEADERS)
104
        end
105
      end
106
    end
107

    
108
    def parse_organization_csv(line)
109
      keys = [:name_format, :count, :label_format, :description]
110
      details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0]
111

    
112
      details[:count] = details[:count].to_i
113

    
114
      details
115
    end
116
  end
117

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