1 |
317e335f
|
Tom McKay
|
require 'hammer_cli'
|
2 |
|
|
require 'json'
|
3 |
|
|
require 'csv'
|
4 |
|
|
|
5 |
|
|
module HammerCLICsv
|
6 |
|
|
class CsvCommand
|
7 |
|
|
class ComputeResourcesCommand < BaseCommand
|
8 |
|
|
command_name 'compute-resources'
|
9 |
|
|
desc 'import or export compute resources'
|
10 |
|
|
|
11 |
|
|
ORGANIZATIONS = 'Organizations'
|
12 |
|
|
LOCATIONS = 'Locations'
|
13 |
|
|
DESCRIPTION = 'Description'
|
14 |
|
|
PROVIDER = 'Provider'
|
15 |
|
|
URL = 'URL'
|
16 |
|
|
|
17 |
c06f1783
|
Tom McKay
|
def export(csv)
|
18 |
|
|
csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL]
|
19 |
6498291e
|
Tom McKay
|
@api.resource(:compute_resources).call(:index, {
|
20 |
|
|
:per_page => 999999,
|
21 |
|
|
:search => option_search
|
22 |
|
|
})['results'].each do |compute_resource|
|
23 |
c06f1783
|
Tom McKay
|
compute_resource = @api.resource(:compute_resources).call(:show, {'id' => compute_resource['id']})
|
24 |
317e335f
|
Tom McKay
|
|
25 |
c06f1783
|
Tom McKay
|
name = compute_resource['name']
|
26 |
|
|
organizations = export_column(compute_resource, 'organizations', 'name')
|
27 |
|
|
locations = export_column(compute_resource, 'locations', 'name')
|
28 |
|
|
description = compute_resource['description']
|
29 |
|
|
provider = compute_resource['provider']
|
30 |
|
|
url = compute_resource['url']
|
31 |
|
|
csv << [name, organizations, locations, description, provider, url]
|
32 |
317e335f
|
Tom McKay
|
end
|
33 |
|
|
end
|
34 |
|
|
|
35 |
|
|
def import
|
36 |
|
|
@existing = {}
|
37 |
|
|
@api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource|
|
38 |
|
|
@existing[compute_resource['name']] = compute_resource['id'] if compute_resource
|
39 |
|
|
end
|
40 |
|
|
|
41 |
|
|
thread_import do |line|
|
42 |
|
|
create_compute_resources_from_csv(line)
|
43 |
|
|
end
|
44 |
|
|
end
|
45 |
|
|
|
46 |
|
|
def create_compute_resources_from_csv(line)
|
47 |
02387fb5
|
Tom McKay
|
count(line[COUNT]).times do |number|
|
48 |
317e335f
|
Tom McKay
|
name = namify(line[NAME], number)
|
49 |
5146ac68
|
Tom McKay
|
params = {
|
50 |
|
|
'compute_resource' => {
|
51 |
|
|
'name' => name,
|
52 |
|
|
'url' => line[URL],
|
53 |
|
|
'provider' => line[PROVIDER]
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
317e335f
|
Tom McKay
|
if !@existing.include? name
|
57 |
a872bfc1
|
Tom McKay
|
print _("Creating compute resource '%{name}'...") % {:name => name} if option_verbose?
|
58 |
5146ac68
|
Tom McKay
|
id = @api.resource(:compute_resources).call(:create, params)['id']
|
59 |
317e335f
|
Tom McKay
|
else
|
60 |
a872bfc1
|
Tom McKay
|
print _("Updating compute resource '%{name}'...") % {:name => name} if option_verbose?
|
61 |
5146ac68
|
Tom McKay
|
id = @existing[name]
|
62 |
|
|
params['id'] = id
|
63 |
|
|
@api.resource(:compute_resources).call(:update, params)
|
64 |
317e335f
|
Tom McKay
|
end
|
65 |
|
|
|
66 |
a872bfc1
|
Tom McKay
|
associate_organizations(id, line[ORGANIZATIONS], 'compute_resource')
|
67 |
|
|
associate_locations(id, line[LOCATIONS], 'compute_resource')
|
68 |
317e335f
|
Tom McKay
|
|
69 |
a872bfc1
|
Tom McKay
|
puts _("done") if option_verbose?
|
70 |
317e335f
|
Tom McKay
|
end
|
71 |
|
|
end
|
72 |
|
|
end
|
73 |
|
|
end
|
74 |
|
|
end |