1
|
module HammerCLICsv
|
2
|
class CsvCommand
|
3
|
class SmartProxiesCommand < BaseCommand
|
4
|
command_name 'smart-proxies'
|
5
|
desc 'import or export smart proxies'
|
6
|
|
7
|
ORGANIZATIONS = 'Organizations'
|
8
|
LOCATIONS = 'Locations'
|
9
|
URL = 'URL'
|
10
|
LIFECYCLE_ENVIRONMENTS = 'Lifecycle Environments'
|
11
|
|
12
|
def export(csv)
|
13
|
csv << [NAME, ORGANIZATIONS, LOCATIONS, URL, LIFECYCLE_ENVIRONMENTS]
|
14
|
@api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
|
15
|
smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => smart_proxy['id']})
|
16
|
name = smart_proxy['name']
|
17
|
organizations = export_column(smart_proxy, 'organizations', 'name')
|
18
|
locations = export_column(smart_proxy, 'locations', 'name')
|
19
|
url = smart_proxy['url']
|
20
|
csv << [name, organizations, locations, url]
|
21
|
end
|
22
|
end
|
23
|
|
24
|
def import
|
25
|
@existing = {}
|
26
|
@api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
|
27
|
@existing[smart_proxy['url']] = smart_proxy['id'] if smart_proxy
|
28
|
end
|
29
|
|
30
|
thread_import do |line|
|
31
|
create_smart_proxies_from_csv(line)
|
32
|
end
|
33
|
end
|
34
|
|
35
|
def create_smart_proxies_from_csv(line)
|
36
|
count(line[COUNT]).times do |number|
|
37
|
name = namify(line[NAME], number)
|
38
|
id = @existing[line[URL]]
|
39
|
if id.nil?
|
40
|
print "Creating smart proxy '#{name}'..." if option_verbose?
|
41
|
id = @api.resource(:smart_proxies).call(:create, {
|
42
|
'smart_proxy' => {
|
43
|
'name' => name,
|
44
|
'url' => line[URL]
|
45
|
}
|
46
|
})['id']
|
47
|
else
|
48
|
print "Updating smart proxy '#{name}'..." if option_verbose?
|
49
|
@api.resource(:smart_proxies).call(:update, {
|
50
|
'id' => id,
|
51
|
'smart_proxy' => {
|
52
|
'name' => name,
|
53
|
'url' => line[URL]
|
54
|
}
|
55
|
})
|
56
|
end
|
57
|
|
58
|
|
59
|
associate_organizations(id, line[ORGANIZATIONS], 'smart_proxy')
|
60
|
associate_locations(id, line[LOCATIONS], 'smart_proxy')
|
61
|
|
62
|
print "done\n" if option_verbose?
|
63
|
end
|
64
|
end
|
65
|
end
|
66
|
end
|
67
|
end
|