forked from netbox-community/Device-Type-Library-Import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nb-dt-import.py
279 lines (259 loc) · 13.4 KB
/
nb-dt-import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from git import Repo, exc, RemoteProgress
from collections import Counter
import yaml, pynetbox, glob, argparse, os, settings
parser = argparse.ArgumentParser(description='Import Netbox Device Types')
parser.add_argument('--vendor', nargs='+', help="List of vendors to import eg. apc cisco")
parser.add_argument('--url','--git', default='https://github.com/netbox-community/devicetype-library.git', help="Git URL with valid Device Type YAML files")
args = parser.parse_args()
cwd = os.getcwd()
counter = Counter(added=0,updated=0,manufacturer=0)
nbUrl = settings.NETBOX_URL
nbToken = settings.NETBOX_TOKEN
def update_package(path: str):
try:
repo = Repo(path)
if repo.remotes.origin.url.endswith('.git'):
repo.remotes.origin.pull()
print(f"Pulled Repo {repo.remotes.origin.url}")
except exc.InvalidGitRepositoryError:
pass
def getFiles(vendors=None):
files = []
discoveredVendors = []
if vendors:
for r, d, f in os.walk('./repo/device-types/'):
for folder in d:
for vendor in vendors:
if vendor.lower() == folder.lower():
discoveredVendors.append({'name': folder, 'slug': folder.lower()})
files.extend(glob.glob('./repo/device-types/' + folder + '/*.yaml'))
else:
for r, d, f in os.walk('./repo/device-types/'):
for folder in d:
if folder.lower() != "Testing":
discoveredVendors.append({'name': folder, 'slug': folder.lower()})
files.extend(glob.glob('./repo/device-types/[!Testing]*/*.yaml'))
return files, discoveredVendors
def readYAMl(files):
deviceTypes = []
manufacturers = []
for file in files:
with open(file, 'r') as stream:
try:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
continue
manufacturer = data['manufacturer']
data['manufacturer'] = {}
data['manufacturer']['name'] = manufacturer
data['manufacturer']['slug'] = manufacturer.lower().replace(" ", "")
deviceTypes.append(data)
manufacturers.append(manufacturer)
return deviceTypes
def createManufacturers(vendors, nb):
for vendor in vendors:
try:
manGet = nb.dcim.manufacturers.get(name=vendor["name"])
if manGet:
print(f'Manufacturer Exists: {manGet.name} - {manGet.id}')
else:
manSuccess = nb.dcim.manufacturers.create(vendor)
print(f'Manufacturer Created: {manSuccess.name} - {manSuccess.id}')
counter.update({'manufacturer':1})
except pynetbox.RequestError as e:
print(e.error)
def createInterfaces(interfaces, deviceType, nb):
for interface in interfaces:
interface['device_type'] = deviceType
try:
ifGet = nb.dcim.interface_templates.get(devicetype_id=deviceType, name=interface["name"])
if ifGet:
print(f'Interface Template Exists: {ifGet.name} - {ifGet.type} - {ifGet.device_type.id} - {ifGet.id}')
else:
ifSuccess = nb.dcim.interface_templates.create(interface)
print(f'Interface Template Created: {ifSuccess.name} - {ifSuccess.type} - {ifSuccess.device_type.id} - {ifSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createConsolePorts(consoleports, deviceType, nb):
for consoleport in consoleports:
consoleport['device_type'] = deviceType
try:
cpGet = nb.dcim.console_port_templates.get(devicetype_id=deviceType, name=consoleport["name"])
if cpGet:
print(f'Console Port Template Exists: {cpGet.name} - {cpGet.type} - {cpGet.device_type.id} - {cpGet.id}')
else:
cpSuccess = nb.dcim.console_port_templates.create(consoleport)
print(f'Console Port Created: {cpSuccess.name} - {cpSuccess.type} - {cpSuccess.device_type.id} - {cpSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createPowerPorts(powerports, deviceType, nb):
for powerport in powerports:
powerport['device_type'] = deviceType
try:
ppGet = nb.dcim.power_port_templates.get(devicetype_id=deviceType, name=powerport["name"])
if ppGet:
print(f'Power Port Template Exists: {ppGet.name} - {ppGet.type} - {ppGet.device_type.id} - {ppGet.id}')
else:
ppSuccess = nb.dcim.power_port_templates.create(powerport)
print(f'Power Port Created: {ppSuccess.name} - {ppSuccess.type} - {ppSuccess.device_type.id} - {ppSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createConsoleServerPorts(consoleserverports, deviceType, nb):
for csport in consoleserverports:
csport['device_type'] = deviceType
try:
cspGet = nb.dcim.console_server_port_templates.get(devicetype_id=deviceType, name=csport["name"])
if cspGet:
print(f'Console Server Port Template Exists: {cspGet.name} - {cspGet.type} - {cspGet.device_type.id} - {cspGet.id}')
else:
cspSuccess = nb.dcim.console_server_port_templates.create(csport)
print(f'Console Server Port Created: {cspSuccess.name} - {cspSuccess.type} - {cspSuccess.device_type.id} - {cspSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createFrontPorts(frontports, deviceType, nb):
for frontport in frontports:
frontport['device_type'] = deviceType
try:
fpGet = nb.dcim.front_port_templates.get(devicetype_id=deviceType, name=frontport["name"])
if fpGet:
print(f'Front Port Template Exists: {fpGet.name} - {fpGet.type} - {fpGet.device_type.id} - {fpGet.id}')
else:
rpGet = nb.dcim.rear_port_templates.get(devicetype_id=deviceType, name=frontport["rear_port"])
if rpGet:
frontport['rear_port'] = rpGet.id
fpSuccess = nb.dcim.front_port_templates.create(frontport)
print(f'Front Port Created: {fpSuccess.name} - {fpSuccess.type} - {fpSuccess.device_type.id} - {fpSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createRearPorts(rearports, deviceType, nb):
for rearport in rearports:
rearport['device_type'] = deviceType
try:
rpGet = nb.dcim.rear_port_templates.get(devicetype_id=deviceType, name=rearport["name"])
if rpGet:
print(f'Rear Port Template Exists: {rpGet.name} - {rpGet.type} - {rpGet.device_type.id} - {rpGet.id}')
else:
rpSuccess = nb.dcim.rear_port_templates.create(rearport)
print(f'Rear Port Created: {rpSuccess.name} - {rpSuccess.type} - {rpSuccess.device_type.id} - {rpSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createDeviceBays(devicebays, deviceType, nb):
for devicebay in devicebays:
devicebay['device_type'] = deviceType
try:
dbGet = nb.dcim.device_bay_templates.get(devicetype_id=deviceType, name=devicebay["name"])
if dbGet:
print(f'Device Bay Template Exists: {dbGet.name} - {dbGet.device_type.id} - {dbGet.id}')
else:
dbSuccess = nb.dcim.device_bay_templates.create(devicebay)
print(f'Device Bay Created: {dbSuccess.name} - {dbSuccess.device_type.id} - {dbSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createPowerOutlets(poweroutlets, deviceType, nb):
for poweroutlet in poweroutlets:
try:
poGet = nb.dcim.power_outlet_templates.get(devicetype_id=deviceType, name=poweroutlet["name"])
if poGet:
print(f'Power Outlet Template Exists: {poGet.name} - {poGet.type} - {poGet.device_type.id} - {poGet.id}')
else:
try:
ppGet = nb.dcim.power_port_templates.get(devicetype_id=deviceType)
if ppGet:
poweroutlet["power_port"] = ppGet.id
poweroutlet["device_type"] = deviceType
poSuccess = nb.dcim.power_outlet_templates.create(poweroutlet)
print(f'Power Outlet Created: {poSuccess.name} - {poSuccess.type} - {poSuccess.device_type.id} - {poSuccess.id}')
counter.update({'updated':1})
except:
poweroutlet["device_type"] = deviceType
poSuccess = nb.dcim.power_outlet_templates.create(poweroutlet)
print(f'Power Outlet Created: {poSuccess.name} - {poSuccess.type} - {poSuccess.device_type.id} - {poSuccess.id}')
counter.update({'updated':1})
except pynetbox.RequestError as e:
print(e.error)
def createDeviceTypes(deviceTypes, nb):
for deviceType in deviceTypes:
try:
dtGet = nb.dcim.device_types.get(model=deviceType["model"])
if dtGet:
print(f'Device Type Exists: {dtGet.manufacturer.name} - {dtGet.model} - {dtGet.id}')
if "interfaces" in deviceType:
createInterfaces(deviceType["interfaces"], dtGet.id, nb)
if "power-ports" in deviceType:
createPowerPorts(deviceType["power-ports"], dtGet.id, nb)
if "power-port" in deviceType:
createPowerPorts(deviceType["power-port"], dtGet.id, nb)
if "console-ports" in deviceType:
createConsolePorts(deviceType["console-ports"], dtGet.id, nb)
if "power-outlets" in deviceType:
createPowerOutlets(deviceType["power-outlets"], dtGet.id, nb)
if "console-server-ports" in deviceType:
createConsoleServerPorts(deviceType["console-server-ports"], dtGet.id, nb)
if "rear-ports" in deviceType:
createRearPorts(deviceType["rear-ports"], dtGet.id, nb)
if "front-ports" in deviceType:
createFrontPorts(deviceType["front-ports"], dtGet.id, nb)
if "device-bays" in deviceType:
createDeviceBays(deviceType["device-bays"], dtGet.id, nb)
else:
dtSuccess = nb.dcim.device_types.create(deviceType)
counter.update({'added':1})
print(f'Device Type Created: {dtSuccess.manufacturer.name} - {dtSuccess.model} - {dtSuccess.id}')
if "interfaces" in deviceType:
createInterfaces(deviceType["interfaces"], dtSuccess.id, nb)
if "power-ports" in deviceType:
createPowerPorts(deviceType["power-ports"], dtSuccess.id, nb)
if "power-port" in deviceType:
createPowerPorts(deviceType["power-port"], dtSuccess.id, nb)
if "console-ports" in deviceType:
createConsolePorts(deviceType["console-ports"], dtSuccess.id, nb)
if "power-outlets" in deviceType:
createPowerOutlets(deviceType["power-outlets"], dtSuccess.id, nb)
if "console-server-ports" in deviceType:
createConsoleServerPorts(deviceType["console-server-ports"], dtSuccess.id, nb)
if "rear-ports" in deviceType:
createRearPorts(deviceType["rear-ports"], dtSuccess.id, nb)
if "front-ports" in deviceType:
createFrontPorts(deviceType["front-ports"], dtSuccess.id, nb)
if "device-bays" in deviceType:
createDeviceBays(deviceType["device-bays"], dtSuccess.id, nb)
except pynetbox.RequestError as e:
print(e.error)
try:
if os.path.isdir('./repo'):
print(f"Package devicetype-library is already installed, updating {os.path.join(cwd, 'repo')}")
update_package('./repo')
else:
repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo'))
print(f"Package Installed {repo.remotes.origin.url}")
except exc.GitCommandError as error:
print("Couldn't clone {} ({})".format(args.url, error))
nb = pynetbox.api(nbUrl, token=nbToken)
if args.vendor is None:
print("No Vendor Specified, Gathering All Device-Types")
files, vendors = getFiles()
print(str(len(vendors)) + " Vendors Found")
print(str(len(files)) + " Device-Types Found")
deviceTypes = readYAMl(files)
createManufacturers(vendors, nb)
createDeviceTypes(deviceTypes, nb)
else:
print("Vendor Specified, Gathering All Matching Device-Types")
files, vendors = getFiles(args.vendor)
print(str(len(vendors)) + " Vendors Found")
print(str(len(files)) + " Device-Types Found")
deviceTypes = readYAMl(files)
createManufacturers(vendors, nb)
createDeviceTypes(deviceTypes, nb)
print('---')
print('{} devices created'.format(counter['added']))
print('{} interfaces/ports updated'.format(counter['updated']))
print('{} manufacturers created'.format(counter['manufacturer']))