model_forms.py 26 KB

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