model_forms.py 27 KB

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