hammer-cli-csv / lib / hammer_cli_csv / compute_resources.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 |
require 'hammer_cli'
|
13 |
require 'json'
|
14 |
require 'csv'
|
15 |
|
16 |
module HammerCLICsv |
17 |
class CsvCommand |
18 |
class ComputeResourcesCommand < BaseCommand |
19 |
command_name 'compute-resources'
|
20 |
desc 'import or export compute resources'
|
21 |
|
22 |
ORGANIZATIONS = 'Organizations' |
23 |
LOCATIONS = 'Locations' |
24 |
DESCRIPTION = 'Description' |
25 |
PROVIDER = 'Provider' |
26 |
URL = 'URL' |
27 |
|
28 |
def export |
29 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
30 |
csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL] |
31 |
@api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource| |
32 |
compute_resource = @api.resource(:compute_resources).call(:show, {'id' => compute_resource['id']}) |
33 |
|
34 |
name = compute_resource['name']
|
35 |
count = 1
|
36 |
organizations = export_column(compute_resource, 'organizations', 'name') |
37 |
locations = export_column(compute_resource, 'locations', 'name') |
38 |
description = compute_resource['description']
|
39 |
provider = compute_resource['provider']
|
40 |
url = compute_resource['url']
|
41 |
csv << [name, count, organizations, locations, description, provider, url] |
42 |
end
|
43 |
end
|
44 |
end
|
45 |
|
46 |
def import |
47 |
@existing = {}
|
48 |
@api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource| |
49 |
@existing[compute_resource['name']] = compute_resource['id'] if compute_resource |
50 |
end
|
51 |
|
52 |
thread_import do |line|
|
53 |
create_compute_resources_from_csv(line) |
54 |
end
|
55 |
end
|
56 |
|
57 |
def create_compute_resources_from_csv(line) |
58 |
line[COUNT].to_i.times do |number| |
59 |
name = namify(line[NAME], number)
|
60 |
if !@existing.include? name |
61 |
print "Creating compute resource '#{name}'..." if option_verbose? |
62 |
id = @api.resource(:compute_resources)\ |
63 |
.call(:create, {
|
64 |
'compute_resource' => {
|
65 |
'name' => name,
|
66 |
'url' => line[URL] |
67 |
} |
68 |
})['id']
|
69 |
else
|
70 |
print "Updating compute resource '#{name}'..." if option_verbose? |
71 |
id = @api.resource(:compute_resources)\ |
72 |
.call(:update, {
|
73 |
'id' => @existing[name], |
74 |
'compute_resource' => {
|
75 |
'name' => name,
|
76 |
'url' => line[URL] |
77 |
} |
78 |
})['compute_resource']['id'] |
79 |
end
|
80 |
|
81 |
# Update associated resources
|
82 |
associate_organizations(id, line[ORGANIZATIONS], 'compute_resource') |
83 |
associate_locations(id, line[LOCATIONS], 'compute_resource') |
84 |
|
85 |
print "done\n" if option_verbose? |
86 |
end
|
87 |
rescue RuntimeError => e |
88 |
raise "#{e}\n #{line}"
|
89 |
end
|
90 |
end
|
91 |
end
|
92 |
end
|