hammer-cli-csv / lib / hammer_cli_csv / organizations.rb @ c2496839
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 |
LABEL = 'Label' |
41 |
DESCRIPTION = 'Description' |
42 |
|
43 |
def export |
44 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
45 |
csv << [NAME, COUNT, LABEL, DESCRIPTION] |
46 |
|
47 |
if @server_status['release'] == 'Headpin' |
48 |
server = option_server || HammerCLI::Settings.get(:csv, :host) |
49 |
username = option_username || HammerCLI::Settings.get(:csv, :username) |
50 |
password = option_password || HammerCLI::Settings.get(:csv, :password) |
51 |
url = "#{server}/api/organizations"
|
52 |
uri = URI(url) |
53 |
Net::HTTP.start(uri.host, uri.port, |
54 |
:use_ssl => uri.scheme == 'https', |
55 |
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http| |
56 |
request = Net::HTTP::Get.new uri.request_uri |
57 |
request.basic_auth(username, password) |
58 |
response = http.request(request) |
59 |
|
60 |
JSON.parse(response.body).each do |organization| |
61 |
csv << [organization['name'], 1, organization['label'], organization['description']] |
62 |
end
|
63 |
end
|
64 |
else
|
65 |
@api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization| |
66 |
csv << [organization['name'], 1, organization['label'], organization['description']] |
67 |
end
|
68 |
end
|
69 |
end
|
70 |
end
|
71 |
|
72 |
def import |
73 |
@existing = {}
|
74 |
@api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization| |
75 |
@existing[organization['name']] = organization['id'] if organization |
76 |
end
|
77 |
|
78 |
thread_import do |line|
|
79 |
create_organizations_from_csv(line) |
80 |
end
|
81 |
end
|
82 |
|
83 |
def create_organizations_from_csv(line) |
84 |
line[COUNT].to_i.times do |number| |
85 |
name = namify(line[NAME], number)
|
86 |
label = namify(line[LABEL], number)
|
87 |
if !@existing.include? name |
88 |
print "Creating organization '#{name}'... " if option_verbose? |
89 |
@api.resource(:organizations).call(:create, { |
90 |
'name' => name,
|
91 |
'label' => label,
|
92 |
'description' => line[DESCRIPTION] |
93 |
}) |
94 |
else
|
95 |
print "Updating organization '#{name}'... " if option_verbose? |
96 |
@api.resource(:organizations).call(:update, { |
97 |
'id' => label,
|
98 |
'description' => line[DESCRIPTION] |
99 |
}) |
100 |
end
|
101 |
print "done\n" if option_verbose? |
102 |
end
|
103 |
end
|
104 |
end
|
105 |
end
|
106 |
end
|