model_forms.py 26 KB

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