1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
require 'hammer_cli'
|
13
|
require 'json'
|
14
|
require 'csv'
|
15
|
|
16
|
module HammerCLICsv
|
17
|
class CsvCommand
|
18
|
class SubnetsCommand < BaseCommand
|
19
|
command_name 'subnets'
|
20
|
desc 'import or export subnets'
|
21
|
|
22
|
ORGANIZATIONS = 'Organizations'
|
23
|
LOCATIONS = 'Locations'
|
24
|
NETWORK = 'Network'
|
25
|
NETWORK_MASK = 'Network Mask'
|
26
|
NETWORK_FROM = 'From'
|
27
|
NETWORK_TO = 'To'
|
28
|
DOMAINS = 'Domains'
|
29
|
GATEWAY = 'Gateway'
|
30
|
DHCP_PROXY = 'DHCP Proxy'
|
31
|
TFTP_PROXY = 'TFTP Proxy'
|
32
|
DNS_PROXY = 'DNS Proxy'
|
33
|
DNS_PRIMARY = 'DNS Primary'
|
34
|
DNS_SECONDARY = 'DNS Secondary'
|
35
|
VLAN_ID = 'VLAN ID'
|
36
|
|
37
|
def export
|
38
|
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
39
|
csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, NETWORK, NETWORK_MASK,
|
40
|
NETWORK_FROM, NETWORK_TO, DOMAINS, GATEWAY, DHCP_PROXY, TFTP_PROXY, DNS_PROXY,
|
41
|
DNS_PRIMARY, DNS_SECONDARY, VLAN_ID]
|
42
|
@api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
|
43
|
subnet = @api.resource(:subnets).call(:show, {'id' => subnet['id']})
|
44
|
|
45
|
name = subnet['name']
|
46
|
count = 1
|
47
|
organizations = export_column(subnet, 'organizations', 'name')
|
48
|
locations = export_column(subnet, 'locations', 'name')
|
49
|
network = subnet['network']
|
50
|
network_mask = subnet['mask']
|
51
|
network_from = subnet['from']
|
52
|
network_to = subnet['to']
|
53
|
domains = export_column(subnet, 'domains', 'name')
|
54
|
gateway = subnet['gateway']
|
55
|
dhcp_proxy = (subnet['dhcp'] && subnet['dhcp'].key?('name')) ? subnet['dhcp']['name'] : ''
|
56
|
tftp_proxy = (subnet['tftp'] && subnet['tftp'].key?('name')) ? subnet['tftp']['name'] : ''
|
57
|
dns_proxy = (subnet['dns'] && subnet['dns'].key?('name')) ? subnet['dns']['name'] : ''
|
58
|
dns_primary = subnet['dns_primary']
|
59
|
dns_secondary = subnet['dns_secondary']
|
60
|
vlan_id = subnet['vlanid']
|
61
|
csv << [name, count, organizations, locations, network, network_mask,
|
62
|
network_from, network_to, domains, gateway, dhcp_proxy, tftp_proxy, dns_proxy,
|
63
|
dns_primary, dns_secondary, vlan_id]
|
64
|
end
|
65
|
end
|
66
|
end
|
67
|
|
68
|
def import
|
69
|
@existing = {}
|
70
|
@api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
|
71
|
@existing[subnet['name']] = subnet['id'] if subnet
|
72
|
end
|
73
|
|
74
|
thread_import do |line|
|
75
|
create_subnets_from_csv(line)
|
76
|
end
|
77
|
end
|
78
|
|
79
|
def create_subnets_from_csv(line)
|
80
|
line[DOMAINS] = (CSV.parse_line(line[DOMAINS]) || []).collect do |domain|
|
81
|
foreman_domain(:name => domain)
|
82
|
end
|
83
|
|
84
|
line[COUNT].to_i.times do |number|
|
85
|
name = namify(line[NAME], number)
|
86
|
if !@existing.include? name
|
87
|
print "Creating subnet '#{name}'..." if option_verbose?
|
88
|
id = @api.resource(:subnets)\
|
89
|
.call(:create, {
|
90
|
'subnet' => {
|
91
|
'name' => name
|
92
|
}
|
93
|
})['id']
|
94
|
else
|
95
|
print "Updating subnet '#{name}'..." if option_verbose?
|
96
|
id = @api.resource(:subnets)\
|
97
|
.call(:update, {
|
98
|
'id' => @existing[name],
|
99
|
'subnet' => {
|
100
|
'name' => name,
|
101
|
'network' => line[NETWORK],
|
102
|
'mask' => line[NETWORK_MASK],
|
103
|
'from' => line[NETWORK_FROM],
|
104
|
'to' => line[NETWORK_TO],
|
105
|
'domain_ids' => line[DOMAINS]
|
106
|
}
|
107
|
})['id']
|
108
|
end
|
109
|
|
110
|
|
111
|
associate_organizations(id, line[ORGANIZATIONS], 'subnet')
|
112
|
associate_locations(id, line[LOCATIONS], 'subnet')
|
113
|
|
114
|
print "done\n" if option_verbose?
|
115
|
end
|
116
|
rescue RuntimeError => e
|
117
|
raise "#{e}\n #{line}"
|
118
|
end
|
119
|
end
|
120
|
end
|
121
|
end
|