1
|
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
|
def export(csv)
|
18
|
csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL]
|
19
|
@api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource|
|
20
|
compute_resource = @api.resource(:compute_resources).call(:show, {'id' => compute_resource['id']})
|
21
|
|
22
|
name = compute_resource['name']
|
23
|
organizations = export_column(compute_resource, 'organizations', 'name')
|
24
|
locations = export_column(compute_resource, 'locations', 'name')
|
25
|
description = compute_resource['description']
|
26
|
provider = compute_resource['provider']
|
27
|
url = compute_resource['url']
|
28
|
csv << [name, organizations, locations, description, provider, url]
|
29
|
end
|
30
|
end
|
31
|
|
32
|
def import
|
33
|
@existing = {}
|
34
|
@api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource|
|
35
|
@existing[compute_resource['name']] = compute_resource['id'] if compute_resource
|
36
|
end
|
37
|
|
38
|
thread_import do |line|
|
39
|
create_compute_resources_from_csv(line)
|
40
|
end
|
41
|
end
|
42
|
|
43
|
def create_compute_resources_from_csv(line)
|
44
|
count(line[COUNT]).times do |number|
|
45
|
name = namify(line[NAME], number)
|
46
|
params = {
|
47
|
'compute_resource' => {
|
48
|
'name' => name,
|
49
|
'url' => line[URL],
|
50
|
'provider' => line[PROVIDER]
|
51
|
}
|
52
|
}
|
53
|
if !@existing.include? name
|
54
|
print _("Creating compute resource '%{name}'...") % {:name => name} if option_verbose?
|
55
|
id = @api.resource(:compute_resources).call(:create, params)['id']
|
56
|
else
|
57
|
print _("Updating compute resource '%{name}'...") % {:name => name} if option_verbose?
|
58
|
id = @existing[name]
|
59
|
params['id'] = id
|
60
|
@api.resource(:compute_resources).call(:update, params)
|
61
|
end
|
62
|
|
63
|
associate_organizations(id, line[ORGANIZATIONS], 'compute_resource')
|
64
|
associate_locations(id, line[LOCATIONS], 'compute_resource')
|
65
|
|
66
|
puts _("done") if option_verbose?
|
67
|
end
|
68
|
end
|
69
|
end
|
70
|
end
|
71
|
end
|