Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / organizations.rb @ 561a8ac9

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 'net/http'
31
require 'json'
32
require 'csv'
33

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

    
40
      option %w(--sam), :flag, 'export from SAM-1.3 or SAM-1.4'
41

    
42
      LABEL = 'Label'
43
      DESCRIPTION = 'Description'
44

    
45
      def export
46
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
47
          csv << [NAME, COUNT, LABEL, DESCRIPTION]
48
          if option_sam?
49
            server = option_server || HammerCLI::Settings.get(:csv, :host)
50
            username = option_username || HammerCLI::Settings.get(:csv, :username)
51
            password = option_password || HammerCLI::Settings.get(:csv, :password)
52
            url = "#{server}/api/organizations"
53
            uri = URI(url)
54
            Net::HTTP.start(uri.host, uri.port,
55
                            :use_ssl => uri.scheme == 'https',
56
                            :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
57
              request = Net::HTTP::Get.new uri.request_uri
58
              request.basic_auth(username, password)
59
              response = http.request(request)
60

    
61
              JSON.parse(response.body).each do |organization|
62
                csv << [organization['name'], 1, organization['label'], organization['description']]
63
              end
64
            end
65
          else
66
            @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
67
              csv << [organization['name'], 1, organization['label'], organization['description']]
68
            end
69
          end
70
        end
71
      end
72

    
73
      def import
74
        @existing = {}
75
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
76
          @existing[organization['name']] = organization['id'] if organization
77
        end
78

    
79
        thread_import do |line|
80
          create_organizations_from_csv(line)
81
        end
82
      end
83

    
84
      def create_organizations_from_csv(line)
85
        line[COUNT].to_i.times do |number|
86
          name = namify(line[NAME], number)
87
          label = namify(line[LABEL], number)
88
          if !@existing.include? name
89
            print "Creating organization '#{name}'... " if option_verbose?
90
            @api.resource(:organizations).call(:create, {
91
                                         'name' => name,
92
                                         'label' => label,
93
                                         'description' => line[DESCRIPTION]
94
                                       })
95
          else
96
            print "Updating organization '#{name}'... " if option_verbose?
97
            @api.resource(:organizations).call(:update, {
98
                                         'id' => label,
99
                                         'description' => line[DESCRIPTION]
100
                                       })
101
          end
102
          print "done\n" if option_verbose?
103
        end
104
      end
105
    end
106
  end
107
end