model_forms.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. from django import forms
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.core.exceptions import ObjectDoesNotExist, ValidationError
  4. from django.utils.translation import gettext_lazy as _
  5. from dcim.models import Device, Interface, Site
  6. from dcim.forms.mixins import ScopedForm
  7. from ipam.choices import *
  8. from ipam.constants import *
  9. from ipam.formfields import IPNetworkFormField
  10. from ipam.models import *
  11. from netbox.forms import NetBoxModelForm
  12. from tenancy.forms import TenancyForm
  13. from utilities.exceptions import PermissionsViolation
  14. from utilities.forms import add_blank_choice
  15. from utilities.forms.fields import (
  16. CommentField, ContentTypeChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, NumericArrayField,
  17. NumericRangeArrayField, SlugField
  18. )
  19. from utilities.forms.rendering import FieldSet, InlineFields, ObjectAttribute, TabbedGroups
  20. from utilities.forms.utils import get_field_value
  21. from utilities.forms.widgets import DatePicker, HTMXSelect
  22. from utilities.templatetags.builtins.filters import bettertitle
  23. from virtualization.models import VMInterface
  24. __all__ = (
  25. 'AggregateForm',
  26. 'ASNForm',
  27. 'ASNRangeForm',
  28. 'FHRPGroupForm',
  29. 'FHRPGroupAssignmentForm',
  30. 'IPAddressAssignForm',
  31. 'IPAddressBulkAddForm',
  32. 'IPAddressForm',
  33. 'IPRangeForm',
  34. 'PrefixForm',
  35. 'RIRForm',
  36. 'RoleForm',
  37. 'RouteTargetForm',
  38. 'ServiceForm',
  39. 'ServiceCreateForm',
  40. 'ServiceTemplateForm',
  41. 'VLANForm',
  42. 'VLANGroupForm',
  43. 'VLANTranslationPolicyForm',
  44. 'VLANTranslationRuleForm',
  45. 'VRFForm',
  46. )
  47. class VRFForm(TenancyForm, NetBoxModelForm):
  48. import_targets = DynamicModelMultipleChoiceField(
  49. label=_('Import targets'),
  50. queryset=RouteTarget.objects.all(),
  51. required=False
  52. )
  53. export_targets = DynamicModelMultipleChoiceField(
  54. label=_('Export targets'),
  55. queryset=RouteTarget.objects.all(),
  56. required=False
  57. )
  58. comments = CommentField()
  59. fieldsets = (
  60. FieldSet('name', 'rd', 'enforce_unique', 'description', 'tags', name=_('VRF')),
  61. FieldSet('import_targets', 'export_targets', name=_('Route Targets')),
  62. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  63. )
  64. class Meta:
  65. model = VRF
  66. fields = [
  67. 'name', 'rd', 'enforce_unique', 'import_targets', 'export_targets', 'tenant_group', 'tenant', 'description',
  68. 'comments', 'tags',
  69. ]
  70. labels = {
  71. 'rd': "RD",
  72. }
  73. class RouteTargetForm(TenancyForm, NetBoxModelForm):
  74. fieldsets = (
  75. FieldSet('name', 'description', 'tags', name=_('Route Target')),
  76. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  77. )
  78. comments = CommentField()
  79. class Meta:
  80. model = RouteTarget
  81. fields = [
  82. 'name', 'tenant_group', 'tenant', 'description', 'comments', 'tags',
  83. ]
  84. class RIRForm(NetBoxModelForm):
  85. slug = SlugField()
  86. fieldsets = (
  87. FieldSet('name', 'slug', 'is_private', 'description', 'tags', name=_('RIR')),
  88. )
  89. class Meta:
  90. model = RIR
  91. fields = [
  92. 'name', 'slug', 'is_private', 'description', 'tags',
  93. ]
  94. class AggregateForm(TenancyForm, NetBoxModelForm):
  95. rir = DynamicModelChoiceField(
  96. queryset=RIR.objects.all(),
  97. label=_('RIR'),
  98. quick_add=True
  99. )
  100. comments = CommentField()
  101. fieldsets = (
  102. FieldSet('prefix', 'rir', 'date_added', 'description', 'tags', name=_('Aggregate')),
  103. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  104. )
  105. class Meta:
  106. model = Aggregate
  107. fields = [
  108. 'prefix', 'rir', 'date_added', 'tenant_group', 'tenant', 'description', 'comments', 'tags',
  109. ]
  110. widgets = {
  111. 'date_added': DatePicker(),
  112. }
  113. class ASNRangeForm(TenancyForm, NetBoxModelForm):
  114. rir = DynamicModelChoiceField(
  115. queryset=RIR.objects.all(),
  116. label=_('RIR'),
  117. quick_add=True
  118. )
  119. slug = SlugField()
  120. fieldsets = (
  121. FieldSet('name', 'slug', 'rir', 'start', 'end', 'description', 'tags', name=_('ASN Range')),
  122. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  123. )
  124. class Meta:
  125. model = ASNRange
  126. fields = [
  127. 'name', 'slug', 'rir', 'start', 'end', 'tenant_group', 'tenant', 'description', 'tags'
  128. ]
  129. class ASNForm(TenancyForm, NetBoxModelForm):
  130. rir = DynamicModelChoiceField(
  131. queryset=RIR.objects.all(),
  132. label=_('RIR'),
  133. quick_add=True
  134. )
  135. sites = DynamicModelMultipleChoiceField(
  136. queryset=Site.objects.all(),
  137. label=_('Sites'),
  138. required=False
  139. )
  140. comments = CommentField()
  141. fieldsets = (
  142. FieldSet('asn', 'rir', 'sites', 'description', 'tags', name=_('ASN')),
  143. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  144. )
  145. class Meta:
  146. model = ASN
  147. fields = [
  148. 'asn', 'rir', 'sites', 'tenant_group', 'tenant', 'description', 'comments', 'tags'
  149. ]
  150. widgets = {
  151. 'date_added': DatePicker(),
  152. }
  153. def __init__(self, data=None, instance=None, *args, **kwargs):
  154. super().__init__(data=data, instance=instance, *args, **kwargs)
  155. if self.instance and self.instance.pk is not None:
  156. self.fields['sites'].initial = self.instance.sites.all().values_list('id', flat=True)
  157. def save(self, *args, **kwargs):
  158. instance = super().save(*args, **kwargs)
  159. instance.sites.set(self.cleaned_data['sites'])
  160. return instance
  161. class RoleForm(NetBoxModelForm):
  162. slug = SlugField()
  163. fieldsets = (
  164. FieldSet('name', 'slug', 'weight', 'description', 'tags', name=_('Role')),
  165. )
  166. class Meta:
  167. model = Role
  168. fields = [
  169. 'name', 'slug', 'weight', 'description', 'tags',
  170. ]
  171. class PrefixForm(TenancyForm, ScopedForm, NetBoxModelForm):
  172. vrf = DynamicModelChoiceField(
  173. queryset=VRF.objects.all(),
  174. required=False,
  175. label=_('VRF')
  176. )
  177. vlan = DynamicModelChoiceField(
  178. queryset=VLAN.objects.all(),
  179. required=False,
  180. selector=True,
  181. query_params={
  182. 'available_at_site': '$scope',
  183. },
  184. label=_('VLAN'),
  185. )
  186. role = DynamicModelChoiceField(
  187. label=_('Role'),
  188. queryset=Role.objects.all(),
  189. required=False,
  190. quick_add=True
  191. )
  192. comments = CommentField()
  193. fieldsets = (
  194. FieldSet(
  195. 'prefix', 'status', 'vrf', 'role', 'is_pool', 'mark_utilized', 'description', 'tags', name=_('Prefix')
  196. ),
  197. FieldSet('scope_type', 'scope', name=_('Scope')),
  198. FieldSet('vlan', name=_('VLAN Assignment')),
  199. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  200. )
  201. class Meta:
  202. model = Prefix
  203. fields = [
  204. 'prefix', 'vrf', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized', 'scope_type', 'tenant_group',
  205. 'tenant', 'description', 'comments', 'tags',
  206. ]
  207. def __init__(self, *args, **kwargs):
  208. super().__init__(*args, **kwargs)
  209. # #18605: only filter VLAN select list if scope field is a Site
  210. if scope_field := self.fields.get('scope', None):
  211. if scope_field.queryset.model is not Site:
  212. self.fields['vlan'].widget.attrs.pop('data-dynamic-params', None)
  213. class IPRangeForm(TenancyForm, NetBoxModelForm):
  214. vrf = DynamicModelChoiceField(
  215. queryset=VRF.objects.all(),
  216. required=False,
  217. label=_('VRF')
  218. )
  219. role = DynamicModelChoiceField(
  220. label=_('Role'),
  221. queryset=Role.objects.all(),
  222. required=False,
  223. quick_add=True
  224. )
  225. comments = CommentField()
  226. fieldsets = (
  227. FieldSet(
  228. 'vrf', 'start_address', 'end_address', 'role', 'status', 'mark_populated', 'mark_utilized', 'description',
  229. 'tags', name=_('IP Range')
  230. ),
  231. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  232. )
  233. class Meta:
  234. model = IPRange
  235. fields = [
  236. 'vrf', 'start_address', 'end_address', 'status', 'role', 'tenant_group', 'tenant', 'mark_populated',
  237. 'mark_utilized', 'description', 'comments', 'tags',
  238. ]
  239. class IPAddressForm(TenancyForm, NetBoxModelForm):
  240. interface = DynamicModelChoiceField(
  241. queryset=Interface.objects.all(),
  242. required=False,
  243. context={
  244. 'parent': 'device',
  245. },
  246. selector=True,
  247. label=_('Interface'),
  248. )
  249. vminterface = DynamicModelChoiceField(
  250. queryset=VMInterface.objects.all(),
  251. required=False,
  252. context={
  253. 'parent': 'virtual_machine',
  254. },
  255. selector=True,
  256. label=_('Interface'),
  257. )
  258. fhrpgroup = DynamicModelChoiceField(
  259. queryset=FHRPGroup.objects.all(),
  260. required=False,
  261. selector=True,
  262. label=_('FHRP Group')
  263. )
  264. vrf = DynamicModelChoiceField(
  265. queryset=VRF.objects.all(),
  266. required=False,
  267. label=_('VRF')
  268. )
  269. nat_inside = DynamicModelChoiceField(
  270. queryset=IPAddress.objects.all(),
  271. required=False,
  272. selector=True,
  273. label=_('IP Address'),
  274. )
  275. primary_for_parent = forms.BooleanField(
  276. required=False,
  277. label=_('Make this the primary IP for the device/VM')
  278. )
  279. oob_for_parent = forms.BooleanField(
  280. required=False,
  281. label=_('Make this the out-of-band IP for the device')
  282. )
  283. comments = CommentField()
  284. fieldsets = (
  285. FieldSet('address', 'status', 'role', 'vrf', 'dns_name', 'description', 'tags', name=_('IP Address')),
  286. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  287. FieldSet(
  288. TabbedGroups(
  289. FieldSet('interface', name=_('Device')),
  290. FieldSet('vminterface', name=_('Virtual Machine')),
  291. FieldSet('fhrpgroup', name=_('FHRP Group')),
  292. ),
  293. 'primary_for_parent', 'oob_for_parent', name=_('Assignment')
  294. ),
  295. FieldSet('nat_inside', name=_('NAT IP (Inside)')),
  296. )
  297. class Meta:
  298. model = IPAddress
  299. fields = [
  300. 'address', 'vrf', 'status', 'role', 'dns_name', 'primary_for_parent', 'oob_for_parent', 'nat_inside',
  301. 'tenant_group', 'tenant', 'description', 'comments', 'tags',
  302. ]
  303. def __init__(self, *args, **kwargs):
  304. # Initialize helper selectors
  305. instance = kwargs.get('instance')
  306. initial = kwargs.get('initial', {}).copy()
  307. if instance:
  308. if type(instance.assigned_object) is Interface:
  309. initial['interface'] = instance.assigned_object
  310. elif type(instance.assigned_object) is VMInterface:
  311. initial['vminterface'] = instance.assigned_object
  312. elif type(instance.assigned_object) is FHRPGroup:
  313. initial['fhrpgroup'] = instance.assigned_object
  314. kwargs['initial'] = initial
  315. super().__init__(*args, **kwargs)
  316. # Initialize parent object & fields if IP address is already assigned
  317. if self.instance.pk and self.instance.assigned_object:
  318. parent = getattr(self.instance.assigned_object, 'parent_object', None)
  319. if parent and (
  320. self.instance.address.version == 4 and parent.primary_ip4_id == self.instance.pk or
  321. self.instance.address.version == 6 and parent.primary_ip6_id == self.instance.pk
  322. ):
  323. self.initial['primary_for_parent'] = True
  324. if parent and getattr(parent, 'oob_ip_id', None) == self.instance.pk:
  325. self.initial['oob_for_parent'] = True
  326. if type(instance.assigned_object) is Interface:
  327. self.fields['interface'].widget.add_query_params({
  328. 'device_id': instance.assigned_object.device.pk,
  329. })
  330. elif type(instance.assigned_object) is VMInterface:
  331. self.fields['vminterface'].widget.add_query_params({
  332. 'virtual_machine_id': instance.assigned_object.virtual_machine.pk,
  333. })
  334. # Disable object assignment fields if the IP address is designated as primary
  335. if self.initial.get('primary_for_parent'):
  336. self.fields['interface'].disabled = True
  337. self.fields['vminterface'].disabled = True
  338. self.fields['fhrpgroup'].disabled = True
  339. def clean(self):
  340. super().clean()
  341. # Handle object assignment
  342. selected_objects = [
  343. field for field in ('interface', 'vminterface', 'fhrpgroup') if self.cleaned_data[field]
  344. ]
  345. if len(selected_objects) > 1:
  346. raise forms.ValidationError({
  347. selected_objects[1]: _("An IP address can only be assigned to a single object.")
  348. })
  349. elif selected_objects:
  350. assigned_object = self.cleaned_data[selected_objects[0]]
  351. if self.instance.pk and self.instance.assigned_object and assigned_object != self.instance.assigned_object:
  352. if self.cleaned_data['primary_for_parent']:
  353. raise ValidationError(
  354. _("Cannot reassign primary IP address for the parent device/VM")
  355. )
  356. if self.cleaned_data['oob_for_parent']:
  357. raise ValidationError(
  358. _("Cannot reassign out-of-Band IP address for the parent device")
  359. )
  360. self.instance.assigned_object = assigned_object
  361. else:
  362. self.instance.assigned_object = None
  363. # Primary IP assignment is only available if an interface has been assigned.
  364. interface = self.cleaned_data.get('interface') or self.cleaned_data.get('vminterface')
  365. if self.cleaned_data.get('primary_for_parent') and not interface:
  366. self.add_error(
  367. 'primary_for_parent', _("Only IP addresses assigned to an interface can be designated as primary IPs.")
  368. )
  369. # OOB IP assignment is only available if device interface has been assigned.
  370. interface = self.cleaned_data.get('interface')
  371. if self.cleaned_data.get('oob_for_parent') and not interface:
  372. self.add_error(
  373. 'oob_for_parent', _(
  374. "Only IP addresses assigned to a device interface can be designated as the out-of-band IP for a "
  375. "device."
  376. )
  377. )
  378. def save(self, *args, **kwargs):
  379. ipaddress = super().save(*args, **kwargs)
  380. # Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine.
  381. interface = self.instance.assigned_object
  382. if type(interface) in (Interface, VMInterface):
  383. parent = interface.parent_object
  384. parent.snapshot()
  385. if self.cleaned_data['primary_for_parent']:
  386. if ipaddress.address.version == 4:
  387. parent.primary_ip4 = ipaddress
  388. else:
  389. parent.primary_ip6 = ipaddress
  390. parent.save()
  391. elif ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress:
  392. parent.primary_ip4 = None
  393. parent.save()
  394. elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress:
  395. parent.primary_ip6 = None
  396. parent.save()
  397. # Assign/clear this IPAddress as the OOB for the associated Device
  398. if type(interface) is Interface:
  399. parent = interface.parent_object
  400. parent.snapshot()
  401. if self.cleaned_data['oob_for_parent']:
  402. parent.oob_ip = ipaddress
  403. parent.save()
  404. elif parent.oob_ip == ipaddress:
  405. parent.oob_ip = None
  406. parent.save()
  407. return ipaddress
  408. class IPAddressBulkAddForm(TenancyForm, NetBoxModelForm):
  409. vrf = DynamicModelChoiceField(
  410. queryset=VRF.objects.all(),
  411. required=False,
  412. label=_('VRF')
  413. )
  414. class Meta:
  415. model = IPAddress
  416. fields = [
  417. 'address', 'vrf', 'status', 'role', 'dns_name', 'description', 'tenant_group', 'tenant', 'tags',
  418. ]
  419. class IPAddressAssignForm(forms.Form):
  420. vrf_id = DynamicModelChoiceField(
  421. queryset=VRF.objects.all(),
  422. required=False,
  423. label=_('VRF')
  424. )
  425. q = forms.CharField(
  426. required=False,
  427. label=_('Search'),
  428. )
  429. class FHRPGroupForm(NetBoxModelForm):
  430. # Optionally create a new IPAddress along with the FHRPGroup
  431. ip_vrf = DynamicModelChoiceField(
  432. queryset=VRF.objects.all(),
  433. required=False,
  434. label=_('VRF')
  435. )
  436. ip_address = IPNetworkFormField(
  437. required=False,
  438. label=_('Address')
  439. )
  440. ip_status = forms.ChoiceField(
  441. choices=add_blank_choice(IPAddressStatusChoices),
  442. required=False,
  443. label=_('Status')
  444. )
  445. comments = CommentField()
  446. fieldsets = (
  447. FieldSet('protocol', 'group_id', 'name', 'description', 'tags', name=_('FHRP Group')),
  448. FieldSet('auth_type', 'auth_key', name=_('Authentication')),
  449. FieldSet('ip_vrf', 'ip_address', 'ip_status', name=_('Virtual IP Address'))
  450. )
  451. class Meta:
  452. model = FHRPGroup
  453. fields = (
  454. 'protocol', 'group_id', 'auth_type', 'auth_key', 'name', 'ip_vrf', 'ip_address', 'ip_status', 'description',
  455. 'comments', 'tags',
  456. )
  457. def save(self, *args, **kwargs):
  458. instance = super().save(*args, **kwargs)
  459. user = getattr(instance, '_user', None) # Set under FHRPGroupEditView.alter_object()
  460. # Check if we need to create a new IPAddress for the group
  461. if self.cleaned_data.get('ip_address'):
  462. ipaddress = IPAddress(
  463. vrf=self.cleaned_data['ip_vrf'],
  464. address=self.cleaned_data['ip_address'],
  465. status=self.cleaned_data['ip_status'],
  466. role=FHRP_PROTOCOL_ROLE_MAPPINGS.get(self.cleaned_data['protocol'], IPAddressRoleChoices.ROLE_VIP),
  467. assigned_object=instance
  468. )
  469. ipaddress.save()
  470. # Check that the new IPAddress conforms with any assigned object-level permissions
  471. if not IPAddress.objects.restrict(user, 'add').filter(pk=ipaddress.pk).first():
  472. raise PermissionsViolation()
  473. return instance
  474. def clean(self):
  475. super().clean()
  476. ip_vrf = self.cleaned_data.get('ip_vrf')
  477. ip_address = self.cleaned_data.get('ip_address')
  478. ip_status = self.cleaned_data.get('ip_status')
  479. if ip_address:
  480. ip_form = IPAddressForm({
  481. 'address': ip_address,
  482. 'vrf': ip_vrf,
  483. 'status': ip_status,
  484. })
  485. if not ip_form.is_valid():
  486. self.errors.update({
  487. f'ip_{field}': error for field, error in ip_form.errors.items()
  488. })
  489. class FHRPGroupAssignmentForm(forms.ModelForm):
  490. group = DynamicModelChoiceField(
  491. label=_('Group'),
  492. queryset=FHRPGroup.objects.all()
  493. )
  494. fieldsets = (
  495. FieldSet(ObjectAttribute('interface'), 'group', 'priority'),
  496. )
  497. class Meta:
  498. model = FHRPGroupAssignment
  499. fields = ('group', 'priority')
  500. def __init__(self, *args, **kwargs):
  501. super().__init__(*args, **kwargs)
  502. ipaddresses = self.instance.interface.ip_addresses.all()
  503. for ipaddress in ipaddresses:
  504. self.fields['group'].widget.add_query_param('related_ip', ipaddress.pk)
  505. def clean_group(self):
  506. group = self.cleaned_data['group']
  507. conflicting_assignments = FHRPGroupAssignment.objects.filter(
  508. interface_type=self.instance.interface_type,
  509. interface_id=self.instance.interface_id,
  510. group=group
  511. )
  512. if self.instance.id:
  513. conflicting_assignments = conflicting_assignments.exclude(id=self.instance.id)
  514. if conflicting_assignments.exists():
  515. raise forms.ValidationError(
  516. _('Assignment already exists')
  517. )
  518. return group
  519. class VLANGroupForm(TenancyForm, NetBoxModelForm):
  520. slug = SlugField()
  521. vid_ranges = NumericRangeArrayField(
  522. label=_('VLAN IDs')
  523. )
  524. scope_type = ContentTypeChoiceField(
  525. queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES),
  526. widget=HTMXSelect(),
  527. required=False,
  528. label=_('Scope type')
  529. )
  530. scope = DynamicModelChoiceField(
  531. label=_('Scope'),
  532. queryset=Site.objects.none(), # Initial queryset
  533. required=False,
  534. disabled=True,
  535. selector=True
  536. )
  537. fieldsets = (
  538. FieldSet('name', 'slug', 'description', 'tags', name=_('VLAN Group')),
  539. FieldSet('vid_ranges', name=_('Child VLANs')),
  540. FieldSet('scope_type', 'scope', name=_('Scope')),
  541. FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
  542. )
  543. class Meta:
  544. model = VLANGroup
  545. fields = [
  546. 'name', 'slug', 'description', 'vid_ranges', 'scope_type', 'tenant_group', 'tenant', 'tags',
  547. ]
  548. def __init__(self, *args, **kwargs):
  549. instance = kwargs.get('instance')
  550. initial = kwargs.get('initial', {})
  551. if instance is not None and instance.scope:
  552. initial['scope'] = instance.scope
  553. kwargs['initial'] = initial
  554. super().__init__(*args, **kwargs)
  555. if scope_type_id := get_field_value(self, 'scope_type'):
  556. try:
  557. scope_type = ContentType.objects.get(pk=scope_type_id)
  558. model = scope_type.model_class()
  559. self.fields['scope'].queryset = model.objects.all()
  560. self.fields['scope'].widget.attrs['selector'] = model._meta.label_lower
  561. self.fields['scope'].disabled = False
  562. self.fields['scope'].label = _(bettertitle(model._meta.verbose_name))
  563. except ObjectDoesNotExist:
  564. pass
  565. if self.instance and scope_type_id != self.instance.scope_type_id:
  566. self.initial['scope'] = None
  567. def clean(self):
  568. super().clean()
  569. # Assign the selected scope (if any)
  570. self.instance.scope = self.cleaned_data.get('scope')
  571. class VLANForm(TenancyForm, NetBoxModelForm):
  572. group = DynamicModelChoiceField(
  573. queryset=VLANGroup.objects.all(),
  574. required=False,
  575. selector=True,
  576. label=_('VLAN Group')
  577. )
  578. site = DynamicModelChoiceField(
  579. label=_('Site'),
  580. queryset=Site.objects.all(),
  581. required=False,
  582. null_option='None',
  583. selector=True
  584. )
  585. role = DynamicModelChoiceField(
  586. label=_('Role'),
  587. queryset=Role.objects.all(),
  588. required=False,
  589. quick_add=True
  590. )
  591. qinq_svlan = DynamicModelChoiceField(
  592. label=_('Q-in-Q SVLAN'),
  593. queryset=VLAN.objects.all(),
  594. required=False,
  595. query_params={
  596. 'qinq_role': VLANQinQRoleChoices.ROLE_SERVICE,
  597. }
  598. )
  599. comments = CommentField()
  600. class Meta:
  601. model = VLAN
  602. fields = [
  603. 'site', 'group', 'vid', 'name', 'status', 'role', 'tenant_group', 'tenant', 'qinq_role', 'qinq_svlan',
  604. 'description', 'comments', 'tags',
  605. ]
  606. class VLANTranslationPolicyForm(NetBoxModelForm):
  607. fieldsets = (
  608. FieldSet('name', 'description', 'tags', name=_('VLAN Translation Policy')),
  609. )
  610. class Meta:
  611. model = VLANTranslationPolicy
  612. fields = [
  613. 'name', 'description', 'tags',
  614. ]
  615. class VLANTranslationRuleForm(NetBoxModelForm):
  616. policy = DynamicModelChoiceField(
  617. label=_('Policy'),
  618. queryset=VLANTranslationPolicy.objects.all(),
  619. selector=True
  620. )
  621. fieldsets = (
  622. FieldSet('policy', 'local_vid', 'remote_vid', 'description', 'tags', name=_('VLAN Translation Rule')),
  623. )
  624. class Meta:
  625. model = VLANTranslationRule
  626. fields = [
  627. 'policy', 'local_vid', 'remote_vid', 'description', 'tags',
  628. ]
  629. class ServiceTemplateForm(NetBoxModelForm):
  630. ports = NumericArrayField(
  631. label=_('Ports'),
  632. base_field=forms.IntegerField(
  633. min_value=SERVICE_PORT_MIN,
  634. max_value=SERVICE_PORT_MAX
  635. ),
  636. help_text=_("Comma-separated list of one or more port numbers. A range may be specified using a hyphen.")
  637. )
  638. comments = CommentField()
  639. fieldsets = (
  640. FieldSet('name', 'protocol', 'ports', 'description', 'tags', name=_('Service Template')),
  641. )
  642. class Meta:
  643. model = ServiceTemplate
  644. fields = ('name', 'protocol', 'ports', 'description', 'comments', 'tags')
  645. class ServiceForm(NetBoxModelForm):
  646. parent_object_type = ContentTypeChoiceField(
  647. queryset=ContentType.objects.filter(SERVICE_ASSIGNMENT_MODELS),
  648. widget=HTMXSelect(),
  649. required=True,
  650. label=_('Parent type')
  651. )
  652. parent = DynamicModelChoiceField(
  653. label=_('Parent'),
  654. queryset=Device.objects.none(), # Initial queryset
  655. required=True,
  656. disabled=True,
  657. selector=True
  658. )
  659. ports = NumericArrayField(
  660. label=_('Ports'),
  661. base_field=forms.IntegerField(
  662. min_value=SERVICE_PORT_MIN,
  663. max_value=SERVICE_PORT_MAX
  664. ),
  665. help_text=_("Comma-separated list of one or more port numbers. A range may be specified using a hyphen.")
  666. )
  667. ipaddresses = DynamicModelMultipleChoiceField(
  668. queryset=IPAddress.objects.all(),
  669. required=False,
  670. label=_('IP Addresses'),
  671. query_params={
  672. 'device_id': '$device',
  673. 'virtual_machine_id': '$virtual_machine',
  674. }
  675. )
  676. comments = CommentField()
  677. fieldsets = (
  678. FieldSet(
  679. 'parent_object_type', 'parent', 'name',
  680. InlineFields('protocol', 'ports', label=_('Port(s)')),
  681. 'ipaddresses', 'description', 'tags', name=_('Service')
  682. ),
  683. )
  684. class Meta:
  685. model = Service
  686. fields = [
  687. 'name', 'protocol', 'ports', 'ipaddresses', 'description', 'comments', 'tags',
  688. 'parent_object_type',
  689. ]
  690. def __init__(self, *args, **kwargs):
  691. initial = kwargs.get('initial', {}).copy()
  692. if (instance := kwargs.get('instance', None)) and instance.parent:
  693. initial['parent'] = instance.parent
  694. kwargs['initial'] = initial
  695. super().__init__(*args, **kwargs)
  696. if (parent_object_type_id := get_field_value(self, 'parent_object_type')):
  697. try:
  698. parent_type = ContentType.objects.get(pk=parent_object_type_id)
  699. model = parent_type.model_class()
  700. self.fields['parent'].queryset = model.objects.all()
  701. self.fields['parent'].widget.attrs['selector'] = model._meta.label_lower
  702. self.fields['parent'].disabled = False
  703. self.fields['parent'].label = _(bettertitle(model._meta.verbose_name))
  704. except ObjectDoesNotExist:
  705. pass
  706. if self.instance and self.instance.pk and parent_object_type_id != self.instance.parent_object_type_id:
  707. self.initial['parent'] = None
  708. def clean(self):
  709. super().clean()
  710. self.instance.parent = self.cleaned_data.get('parent')
  711. class ServiceCreateForm(ServiceForm):
  712. service_template = DynamicModelChoiceField(
  713. label=_('Service template'),
  714. queryset=ServiceTemplate.objects.all(),
  715. required=False
  716. )
  717. fieldsets = (
  718. FieldSet(
  719. 'parent_object_type', 'parent',
  720. TabbedGroups(
  721. FieldSet('service_template', name=_('From Template')),
  722. FieldSet('name', 'protocol', 'ports', name=_('Custom')),
  723. ),
  724. 'ipaddresses', 'description', 'tags', name=_('Service')
  725. ),
  726. )
  727. class Meta(ServiceForm.Meta):
  728. fields = [
  729. 'service_template', 'name', 'protocol', 'ports', 'ipaddresses', 'description',
  730. 'comments', 'tags', 'parent_object_type',
  731. ]
  732. def __init__(self, *args, **kwargs):
  733. super().__init__(*args, **kwargs)
  734. # Fields which may be populated from a ServiceTemplate are not required
  735. for field in ('name', 'protocol', 'ports'):
  736. self.fields[field].required = False
  737. self.fields[field].widget.is_required = False
  738. def clean(self):
  739. super().clean()
  740. if self.cleaned_data['service_template']:
  741. # Create a new Service from the specified template
  742. service_template = self.cleaned_data['service_template']
  743. self.cleaned_data['name'] = service_template.name
  744. self.cleaned_data['protocol'] = service_template.protocol
  745. self.cleaned_data['ports'] = service_template.ports
  746. if not self.cleaned_data['description']:
  747. self.cleaned_data['description'] = service_template.description
  748. elif not all(self.cleaned_data[f] for f in ('name', 'protocol', 'ports')):
  749. raise forms.ValidationError(_("Must specify name, protocol, and port(s) if not using a service template."))