bulk_import.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. from django import forms
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.core.exceptions import ValidationError
  4. from dcim.models import Device, Interface, Site
  5. from ipam.choices import *
  6. from ipam.constants import *
  7. from ipam.models import *
  8. from netbox.forms import NetBoxModelCSVForm
  9. from tenancy.models import Tenant
  10. from utilities.forms import CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, SlugField
  11. from virtualization.models import VirtualMachine, VMInterface
  12. __all__ = (
  13. 'AggregateCSVForm',
  14. 'ASNCSVForm',
  15. 'FHRPGroupCSVForm',
  16. 'IPAddressCSVForm',
  17. 'IPRangeCSVForm',
  18. 'L2VPNCSVForm',
  19. 'L2VPNTerminationCSVForm',
  20. 'PrefixCSVForm',
  21. 'RIRCSVForm',
  22. 'RoleCSVForm',
  23. 'RouteTargetCSVForm',
  24. 'ServiceCSVForm',
  25. 'ServiceTemplateCSVForm',
  26. 'VLANCSVForm',
  27. 'VLANGroupCSVForm',
  28. 'VRFCSVForm',
  29. )
  30. class VRFCSVForm(NetBoxModelCSVForm):
  31. tenant = CSVModelChoiceField(
  32. queryset=Tenant.objects.all(),
  33. required=False,
  34. to_field_name='name',
  35. help_text='Assigned tenant'
  36. )
  37. class Meta:
  38. model = VRF
  39. fields = ('name', 'rd', 'tenant', 'enforce_unique', 'description', 'comments', 'tags')
  40. class RouteTargetCSVForm(NetBoxModelCSVForm):
  41. tenant = CSVModelChoiceField(
  42. queryset=Tenant.objects.all(),
  43. required=False,
  44. to_field_name='name',
  45. help_text='Assigned tenant'
  46. )
  47. class Meta:
  48. model = RouteTarget
  49. fields = ('name', 'tenant', 'description', 'comments', 'tags')
  50. class RIRCSVForm(NetBoxModelCSVForm):
  51. slug = SlugField()
  52. class Meta:
  53. model = RIR
  54. fields = ('name', 'slug', 'is_private', 'description', 'tags')
  55. help_texts = {
  56. 'name': 'RIR name',
  57. }
  58. class AggregateCSVForm(NetBoxModelCSVForm):
  59. rir = CSVModelChoiceField(
  60. queryset=RIR.objects.all(),
  61. to_field_name='name',
  62. help_text='Assigned RIR'
  63. )
  64. tenant = CSVModelChoiceField(
  65. queryset=Tenant.objects.all(),
  66. required=False,
  67. to_field_name='name',
  68. help_text='Assigned tenant'
  69. )
  70. class Meta:
  71. model = Aggregate
  72. fields = ('prefix', 'rir', 'tenant', 'date_added', 'description', 'comments', 'tags')
  73. class ASNCSVForm(NetBoxModelCSVForm):
  74. rir = CSVModelChoiceField(
  75. queryset=RIR.objects.all(),
  76. to_field_name='name',
  77. help_text='Assigned RIR'
  78. )
  79. tenant = CSVModelChoiceField(
  80. queryset=Tenant.objects.all(),
  81. required=False,
  82. to_field_name='name',
  83. help_text='Assigned tenant'
  84. )
  85. class Meta:
  86. model = ASN
  87. fields = ('asn', 'rir', 'tenant', 'description', 'comments', 'tags')
  88. class RoleCSVForm(NetBoxModelCSVForm):
  89. slug = SlugField()
  90. class Meta:
  91. model = Role
  92. fields = ('name', 'slug', 'weight', 'description', 'tags')
  93. class PrefixCSVForm(NetBoxModelCSVForm):
  94. vrf = CSVModelChoiceField(
  95. queryset=VRF.objects.all(),
  96. to_field_name='name',
  97. required=False,
  98. help_text='Assigned VRF'
  99. )
  100. tenant = CSVModelChoiceField(
  101. queryset=Tenant.objects.all(),
  102. required=False,
  103. to_field_name='name',
  104. help_text='Assigned tenant'
  105. )
  106. site = CSVModelChoiceField(
  107. queryset=Site.objects.all(),
  108. required=False,
  109. to_field_name='name',
  110. help_text='Assigned site'
  111. )
  112. vlan_group = CSVModelChoiceField(
  113. queryset=VLANGroup.objects.all(),
  114. required=False,
  115. to_field_name='name',
  116. help_text="VLAN's group (if any)"
  117. )
  118. vlan = CSVModelChoiceField(
  119. queryset=VLAN.objects.all(),
  120. required=False,
  121. to_field_name='vid',
  122. help_text="Assigned VLAN"
  123. )
  124. status = CSVChoiceField(
  125. choices=PrefixStatusChoices,
  126. help_text='Operational status'
  127. )
  128. role = CSVModelChoiceField(
  129. queryset=Role.objects.all(),
  130. required=False,
  131. to_field_name='name',
  132. help_text='Functional role'
  133. )
  134. class Meta:
  135. model = Prefix
  136. fields = (
  137. 'prefix', 'vrf', 'tenant', 'site', 'vlan_group', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized',
  138. 'description', 'comments', 'tags',
  139. )
  140. def __init__(self, data=None, *args, **kwargs):
  141. super().__init__(data, *args, **kwargs)
  142. if data:
  143. # Limit VLAN queryset by assigned site and/or group (if specified)
  144. params = {}
  145. if data.get('site'):
  146. params[f"site__{self.fields['site'].to_field_name}"] = data.get('site')
  147. if data.get('vlan_group'):
  148. params[f"group__{self.fields['vlan_group'].to_field_name}"] = data.get('vlan_group')
  149. if params:
  150. self.fields['vlan'].queryset = self.fields['vlan'].queryset.filter(**params)
  151. class IPRangeCSVForm(NetBoxModelCSVForm):
  152. vrf = CSVModelChoiceField(
  153. queryset=VRF.objects.all(),
  154. to_field_name='name',
  155. required=False,
  156. help_text='Assigned VRF'
  157. )
  158. tenant = CSVModelChoiceField(
  159. queryset=Tenant.objects.all(),
  160. required=False,
  161. to_field_name='name',
  162. help_text='Assigned tenant'
  163. )
  164. status = CSVChoiceField(
  165. choices=IPRangeStatusChoices,
  166. help_text='Operational status'
  167. )
  168. role = CSVModelChoiceField(
  169. queryset=Role.objects.all(),
  170. required=False,
  171. to_field_name='name',
  172. help_text='Functional role'
  173. )
  174. class Meta:
  175. model = IPRange
  176. fields = (
  177. 'start_address', 'end_address', 'vrf', 'tenant', 'status', 'role', 'description', 'comments', 'tags',
  178. )
  179. class IPAddressCSVForm(NetBoxModelCSVForm):
  180. vrf = CSVModelChoiceField(
  181. queryset=VRF.objects.all(),
  182. to_field_name='name',
  183. required=False,
  184. help_text='Assigned VRF'
  185. )
  186. tenant = CSVModelChoiceField(
  187. queryset=Tenant.objects.all(),
  188. to_field_name='name',
  189. required=False,
  190. help_text='Assigned tenant'
  191. )
  192. status = CSVChoiceField(
  193. choices=IPAddressStatusChoices,
  194. help_text='Operational status'
  195. )
  196. role = CSVChoiceField(
  197. choices=IPAddressRoleChoices,
  198. required=False,
  199. help_text='Functional role'
  200. )
  201. device = CSVModelChoiceField(
  202. queryset=Device.objects.all(),
  203. required=False,
  204. to_field_name='name',
  205. help_text='Parent device of assigned interface (if any)'
  206. )
  207. virtual_machine = CSVModelChoiceField(
  208. queryset=VirtualMachine.objects.all(),
  209. required=False,
  210. to_field_name='name',
  211. help_text='Parent VM of assigned interface (if any)'
  212. )
  213. interface = CSVModelChoiceField(
  214. queryset=Interface.objects.none(), # Can also refer to VMInterface
  215. required=False,
  216. to_field_name='name',
  217. help_text='Assigned interface'
  218. )
  219. is_primary = forms.BooleanField(
  220. help_text='Make this the primary IP for the assigned device',
  221. required=False
  222. )
  223. class Meta:
  224. model = IPAddress
  225. fields = [
  226. 'address', 'vrf', 'tenant', 'status', 'role', 'device', 'virtual_machine', 'interface', 'is_primary',
  227. 'dns_name', 'description', 'comments', 'tags',
  228. ]
  229. def __init__(self, data=None, *args, **kwargs):
  230. super().__init__(data, *args, **kwargs)
  231. if data:
  232. # Limit interface queryset by assigned device
  233. if data.get('device'):
  234. self.fields['interface'].queryset = Interface.objects.filter(
  235. **{f"device__{self.fields['device'].to_field_name}": data['device']}
  236. )
  237. # Limit interface queryset by assigned device
  238. elif data.get('virtual_machine'):
  239. self.fields['interface'].queryset = VMInterface.objects.filter(
  240. **{f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data['virtual_machine']}
  241. )
  242. def clean(self):
  243. super().clean()
  244. device = self.cleaned_data.get('device')
  245. virtual_machine = self.cleaned_data.get('virtual_machine')
  246. interface = self.cleaned_data.get('interface')
  247. is_primary = self.cleaned_data.get('is_primary')
  248. # Validate is_primary
  249. if is_primary and not device and not virtual_machine:
  250. raise forms.ValidationError({
  251. "is_primary": "No device or virtual machine specified; cannot set as primary IP"
  252. })
  253. if is_primary and not interface:
  254. raise forms.ValidationError({
  255. "is_primary": "No interface specified; cannot set as primary IP"
  256. })
  257. def save(self, *args, **kwargs):
  258. # Set interface assignment
  259. if self.cleaned_data.get('interface'):
  260. self.instance.assigned_object = self.cleaned_data['interface']
  261. ipaddress = super().save(*args, **kwargs)
  262. # Set as primary for device/VM
  263. if self.cleaned_data.get('is_primary'):
  264. parent = self.cleaned_data['device'] or self.cleaned_data['virtual_machine']
  265. if self.instance.address.version == 4:
  266. parent.primary_ip4 = ipaddress
  267. elif self.instance.address.version == 6:
  268. parent.primary_ip6 = ipaddress
  269. parent.save()
  270. return ipaddress
  271. class FHRPGroupCSVForm(NetBoxModelCSVForm):
  272. protocol = CSVChoiceField(
  273. choices=FHRPGroupProtocolChoices
  274. )
  275. auth_type = CSVChoiceField(
  276. choices=FHRPGroupAuthTypeChoices,
  277. required=False
  278. )
  279. class Meta:
  280. model = FHRPGroup
  281. fields = ('protocol', 'group_id', 'auth_type', 'auth_key', 'name', 'description', 'comments', 'tags')
  282. class VLANGroupCSVForm(NetBoxModelCSVForm):
  283. slug = SlugField()
  284. scope_type = CSVContentTypeField(
  285. queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES),
  286. required=False,
  287. label='Scope type (app & model)'
  288. )
  289. min_vid = forms.IntegerField(
  290. min_value=VLAN_VID_MIN,
  291. max_value=VLAN_VID_MAX,
  292. required=False,
  293. label=f'Minimum child VLAN VID (default: {VLAN_VID_MIN})'
  294. )
  295. max_vid = forms.IntegerField(
  296. min_value=VLAN_VID_MIN,
  297. max_value=VLAN_VID_MAX,
  298. required=False,
  299. label=f'Maximum child VLAN VID (default: {VLAN_VID_MIN})'
  300. )
  301. class Meta:
  302. model = VLANGroup
  303. fields = ('name', 'slug', 'scope_type', 'scope_id', 'min_vid', 'max_vid', 'description', 'tags')
  304. labels = {
  305. 'scope_id': 'Scope ID',
  306. }
  307. class VLANCSVForm(NetBoxModelCSVForm):
  308. site = CSVModelChoiceField(
  309. queryset=Site.objects.all(),
  310. required=False,
  311. to_field_name='name',
  312. help_text='Assigned site'
  313. )
  314. group = CSVModelChoiceField(
  315. queryset=VLANGroup.objects.all(),
  316. required=False,
  317. to_field_name='name',
  318. help_text='Assigned VLAN group'
  319. )
  320. tenant = CSVModelChoiceField(
  321. queryset=Tenant.objects.all(),
  322. to_field_name='name',
  323. required=False,
  324. help_text='Assigned tenant'
  325. )
  326. status = CSVChoiceField(
  327. choices=VLANStatusChoices,
  328. help_text='Operational status'
  329. )
  330. role = CSVModelChoiceField(
  331. queryset=Role.objects.all(),
  332. required=False,
  333. to_field_name='name',
  334. help_text='Functional role'
  335. )
  336. class Meta:
  337. model = VLAN
  338. fields = ('site', 'group', 'vid', 'name', 'tenant', 'status', 'role', 'description', 'comments', 'tags')
  339. help_texts = {
  340. 'vid': 'Numeric VLAN ID (1-4094)',
  341. 'name': 'VLAN name',
  342. }
  343. class ServiceTemplateCSVForm(NetBoxModelCSVForm):
  344. protocol = CSVChoiceField(
  345. choices=ServiceProtocolChoices,
  346. help_text='IP protocol'
  347. )
  348. class Meta:
  349. model = ServiceTemplate
  350. fields = ('name', 'protocol', 'ports', 'description', 'comments', 'tags')
  351. class ServiceCSVForm(NetBoxModelCSVForm):
  352. device = CSVModelChoiceField(
  353. queryset=Device.objects.all(),
  354. required=False,
  355. to_field_name='name',
  356. help_text='Required if not assigned to a VM'
  357. )
  358. virtual_machine = CSVModelChoiceField(
  359. queryset=VirtualMachine.objects.all(),
  360. required=False,
  361. to_field_name='name',
  362. help_text='Required if not assigned to a device'
  363. )
  364. protocol = CSVChoiceField(
  365. choices=ServiceProtocolChoices,
  366. help_text='IP protocol'
  367. )
  368. class Meta:
  369. model = Service
  370. fields = ('device', 'virtual_machine', 'name', 'protocol', 'ports', 'description', 'comments', 'tags')
  371. class L2VPNCSVForm(NetBoxModelCSVForm):
  372. tenant = CSVModelChoiceField(
  373. queryset=Tenant.objects.all(),
  374. required=False,
  375. to_field_name='name',
  376. )
  377. type = CSVChoiceField(
  378. choices=L2VPNTypeChoices,
  379. help_text='L2VPN type'
  380. )
  381. class Meta:
  382. model = L2VPN
  383. fields = ('identifier', 'name', 'slug', 'type', 'description', 'comments', 'tags')
  384. class L2VPNTerminationCSVForm(NetBoxModelCSVForm):
  385. l2vpn = CSVModelChoiceField(
  386. queryset=L2VPN.objects.all(),
  387. required=True,
  388. to_field_name='name',
  389. label='L2VPN',
  390. )
  391. device = CSVModelChoiceField(
  392. queryset=Device.objects.all(),
  393. required=False,
  394. to_field_name='name',
  395. help_text='Parent device (for interface)'
  396. )
  397. virtual_machine = CSVModelChoiceField(
  398. queryset=VirtualMachine.objects.all(),
  399. required=False,
  400. to_field_name='name',
  401. help_text='Parent virtual machine (for interface)'
  402. )
  403. interface = CSVModelChoiceField(
  404. queryset=Interface.objects.none(), # Can also refer to VMInterface
  405. required=False,
  406. to_field_name='name',
  407. help_text='Assigned interface (device or VM)'
  408. )
  409. vlan = CSVModelChoiceField(
  410. queryset=VLAN.objects.all(),
  411. required=False,
  412. to_field_name='name',
  413. help_text='Assigned VLAN'
  414. )
  415. class Meta:
  416. model = L2VPNTermination
  417. fields = ('l2vpn', 'device', 'virtual_machine', 'interface', 'vlan', 'tags')
  418. def __init__(self, data=None, *args, **kwargs):
  419. super().__init__(data, *args, **kwargs)
  420. if data:
  421. # Limit interface queryset by device or VM
  422. if data.get('device'):
  423. self.fields['interface'].queryset = Interface.objects.filter(
  424. **{f"device__{self.fields['device'].to_field_name}": data['device']}
  425. )
  426. elif data.get('virtual_machine'):
  427. self.fields['interface'].queryset = VMInterface.objects.filter(
  428. **{f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data['virtual_machine']}
  429. )
  430. def clean(self):
  431. super().clean()
  432. if self.cleaned_data.get('device') and self.cleaned_data.get('virtual_machine'):
  433. raise ValidationError('Cannot import device and VM interface terminations simultaneously.')
  434. if not (self.cleaned_data.get('interface') or self.cleaned_data.get('vlan')):
  435. raise ValidationError('Each termination must specify either an interface or a VLAN.')
  436. if self.cleaned_data.get('interface') and self.cleaned_data.get('vlan'):
  437. raise ValidationError('Cannot assign both an interface and a VLAN.')
  438. self.instance.assigned_object = self.cleaned_data.get('interface') or self.cleaned_data.get('vlan')