bulk_import.py 16 KB

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