model_forms.py 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.utils.translation import gettext as _
  5. from timezone_field import TimeZoneFormField
  6. from dcim.choices import *
  7. from dcim.constants import *
  8. from dcim.models import *
  9. from ipam.models import ASN, IPAddress, VLAN, VLANGroup, VRF
  10. from netbox.forms import NetBoxModelForm
  11. from tenancy.forms import TenancyForm
  12. from utilities.forms import (
  13. APISelect, add_blank_choice, BootstrapMixin, ClearableFileInput, CommentField, ContentTypeChoiceField,
  14. DynamicModelChoiceField, DynamicModelMultipleChoiceField, JSONField, NumericArrayField, SelectWithPK, SmallTextarea,
  15. SlugField, StaticSelect, SelectSpeedWidget,
  16. )
  17. from virtualization.models import Cluster, ClusterGroup
  18. from wireless.models import WirelessLAN, WirelessLANGroup
  19. from .common import InterfaceCommonForm
  20. __all__ = (
  21. 'CableForm',
  22. 'ConsolePortForm',
  23. 'ConsolePortTemplateForm',
  24. 'ConsoleServerPortForm',
  25. 'ConsoleServerPortTemplateForm',
  26. 'DeviceBayForm',
  27. 'DeviceBayTemplateForm',
  28. 'DeviceForm',
  29. 'DeviceRoleForm',
  30. 'DeviceTypeForm',
  31. 'DeviceVCMembershipForm',
  32. 'FrontPortForm',
  33. 'FrontPortTemplateForm',
  34. 'InterfaceForm',
  35. 'InterfaceTemplateForm',
  36. 'InventoryItemForm',
  37. 'InventoryItemRoleForm',
  38. 'InventoryItemTemplateForm',
  39. 'LocationForm',
  40. 'ManufacturerForm',
  41. 'ModuleForm',
  42. 'ModuleBayForm',
  43. 'ModuleBayTemplateForm',
  44. 'ModuleTypeForm',
  45. 'PlatformForm',
  46. 'PopulateDeviceBayForm',
  47. 'PowerFeedForm',
  48. 'PowerOutletForm',
  49. 'PowerOutletTemplateForm',
  50. 'PowerPanelForm',
  51. 'PowerPortForm',
  52. 'PowerPortTemplateForm',
  53. 'RackForm',
  54. 'RackReservationForm',
  55. 'RackRoleForm',
  56. 'RearPortForm',
  57. 'RearPortTemplateForm',
  58. 'RegionForm',
  59. 'SiteForm',
  60. 'SiteGroupForm',
  61. 'VCMemberSelectForm',
  62. 'VirtualChassisForm',
  63. 'VirtualDeviceContextForm'
  64. )
  65. INTERFACE_MODE_HELP_TEXT = """
  66. Access: One untagged VLAN<br />
  67. Tagged: One untagged VLAN and/or one or more tagged VLANs<br />
  68. Tagged (All): Implies all VLANs are available (w/optional untagged VLAN)
  69. """
  70. class RegionForm(NetBoxModelForm):
  71. parent = DynamicModelChoiceField(
  72. queryset=Region.objects.all(),
  73. required=False
  74. )
  75. slug = SlugField()
  76. fieldsets = (
  77. ('Region', (
  78. 'parent', 'name', 'slug', 'description', 'tags',
  79. )),
  80. )
  81. class Meta:
  82. model = Region
  83. fields = (
  84. 'parent', 'name', 'slug', 'description', 'tags',
  85. )
  86. class SiteGroupForm(NetBoxModelForm):
  87. parent = DynamicModelChoiceField(
  88. queryset=SiteGroup.objects.all(),
  89. required=False
  90. )
  91. slug = SlugField()
  92. fieldsets = (
  93. ('Site Group', (
  94. 'parent', 'name', 'slug', 'description', 'tags',
  95. )),
  96. )
  97. class Meta:
  98. model = SiteGroup
  99. fields = (
  100. 'parent', 'name', 'slug', 'description', 'tags',
  101. )
  102. class SiteForm(TenancyForm, NetBoxModelForm):
  103. region = DynamicModelChoiceField(
  104. queryset=Region.objects.all(),
  105. required=False
  106. )
  107. group = DynamicModelChoiceField(
  108. queryset=SiteGroup.objects.all(),
  109. required=False
  110. )
  111. asns = DynamicModelMultipleChoiceField(
  112. queryset=ASN.objects.all(),
  113. label=_('ASNs'),
  114. required=False
  115. )
  116. slug = SlugField()
  117. time_zone = TimeZoneFormField(
  118. choices=add_blank_choice(TimeZoneFormField().choices),
  119. required=False,
  120. widget=StaticSelect()
  121. )
  122. comments = CommentField()
  123. fieldsets = (
  124. ('Site', (
  125. 'name', 'slug', 'status', 'region', 'group', 'facility', 'asns', 'time_zone', 'description', 'tags',
  126. )),
  127. ('Tenancy', ('tenant_group', 'tenant')),
  128. ('Contact Info', ('physical_address', 'shipping_address', 'latitude', 'longitude')),
  129. )
  130. class Meta:
  131. model = Site
  132. fields = (
  133. 'name', 'slug', 'status', 'region', 'group', 'tenant_group', 'tenant', 'facility', 'asns', 'time_zone',
  134. 'description', 'physical_address', 'shipping_address', 'latitude', 'longitude', 'comments', 'tags',
  135. )
  136. widgets = {
  137. 'physical_address': SmallTextarea(
  138. attrs={
  139. 'rows': 3,
  140. }
  141. ),
  142. 'shipping_address': SmallTextarea(
  143. attrs={
  144. 'rows': 3,
  145. }
  146. ),
  147. 'status': StaticSelect(),
  148. 'time_zone': StaticSelect(),
  149. }
  150. help_texts = {
  151. 'name': _("Full name of the site"),
  152. 'facility': _("Data center provider and facility (e.g. Equinix NY7)"),
  153. 'time_zone': _("Local time zone"),
  154. 'description': _("Short description (will appear in sites list)"),
  155. 'physical_address': _("Physical location of the building (e.g. for GPS)"),
  156. 'shipping_address': _("If different from the physical address"),
  157. 'latitude': _("Latitude in decimal format (xx.yyyyyy)"),
  158. 'longitude': _("Longitude in decimal format (xx.yyyyyy)")
  159. }
  160. class LocationForm(TenancyForm, NetBoxModelForm):
  161. region = DynamicModelChoiceField(
  162. queryset=Region.objects.all(),
  163. required=False,
  164. initial_params={
  165. 'sites': '$site'
  166. }
  167. )
  168. site_group = DynamicModelChoiceField(
  169. queryset=SiteGroup.objects.all(),
  170. required=False,
  171. initial_params={
  172. 'sites': '$site'
  173. }
  174. )
  175. site = DynamicModelChoiceField(
  176. queryset=Site.objects.all(),
  177. query_params={
  178. 'region_id': '$region',
  179. 'group_id': '$site_group',
  180. }
  181. )
  182. parent = DynamicModelChoiceField(
  183. queryset=Location.objects.all(),
  184. required=False,
  185. query_params={
  186. 'site_id': '$site'
  187. }
  188. )
  189. slug = SlugField()
  190. fieldsets = (
  191. ('Location', (
  192. 'region', 'site_group', 'site', 'parent', 'name', 'slug', 'status', 'description', 'tags',
  193. )),
  194. ('Tenancy', ('tenant_group', 'tenant')),
  195. )
  196. class Meta:
  197. model = Location
  198. fields = (
  199. 'region', 'site_group', 'site', 'parent', 'name', 'slug', 'status', 'description', 'tenant_group', 'tenant',
  200. 'tags',
  201. )
  202. widgets = {
  203. 'status': StaticSelect(),
  204. }
  205. class RackRoleForm(NetBoxModelForm):
  206. slug = SlugField()
  207. fieldsets = (
  208. ('Rack Role', (
  209. 'name', 'slug', 'color', 'description', 'tags',
  210. )),
  211. )
  212. class Meta:
  213. model = RackRole
  214. fields = [
  215. 'name', 'slug', 'color', 'description', 'tags',
  216. ]
  217. class RackForm(TenancyForm, NetBoxModelForm):
  218. region = DynamicModelChoiceField(
  219. queryset=Region.objects.all(),
  220. required=False,
  221. initial_params={
  222. 'sites': '$site'
  223. }
  224. )
  225. site_group = DynamicModelChoiceField(
  226. queryset=SiteGroup.objects.all(),
  227. required=False,
  228. initial_params={
  229. 'sites': '$site'
  230. }
  231. )
  232. site = DynamicModelChoiceField(
  233. queryset=Site.objects.all(),
  234. query_params={
  235. 'region_id': '$region',
  236. 'group_id': '$site_group',
  237. }
  238. )
  239. location = DynamicModelChoiceField(
  240. queryset=Location.objects.all(),
  241. required=False,
  242. query_params={
  243. 'site_id': '$site'
  244. }
  245. )
  246. role = DynamicModelChoiceField(
  247. queryset=RackRole.objects.all(),
  248. required=False
  249. )
  250. comments = CommentField()
  251. class Meta:
  252. model = Rack
  253. fields = [
  254. 'region', 'site_group', 'site', 'location', 'name', 'facility_id', 'tenant_group', 'tenant', 'status',
  255. 'role', 'serial', 'asset_tag', 'type', 'width', 'u_height', 'desc_units', 'outer_width', 'outer_depth',
  256. 'outer_unit', 'mounting_depth', 'weight', 'max_weight', 'weight_unit', 'description', 'comments', 'tags',
  257. ]
  258. help_texts = {
  259. 'site': _("The site at which the rack exists"),
  260. 'name': _("Organizational rack name"),
  261. 'facility_id': _("The unique rack ID assigned by the facility"),
  262. 'u_height': _("Height in rack units"),
  263. }
  264. widgets = {
  265. 'status': StaticSelect(),
  266. 'type': StaticSelect(),
  267. 'width': StaticSelect(),
  268. 'outer_unit': StaticSelect(),
  269. 'weight_unit': StaticSelect(),
  270. }
  271. class RackReservationForm(TenancyForm, NetBoxModelForm):
  272. region = DynamicModelChoiceField(
  273. queryset=Region.objects.all(),
  274. required=False,
  275. initial_params={
  276. 'sites': '$site'
  277. }
  278. )
  279. site_group = DynamicModelChoiceField(
  280. queryset=SiteGroup.objects.all(),
  281. required=False,
  282. initial_params={
  283. 'sites': '$site'
  284. }
  285. )
  286. site = DynamicModelChoiceField(
  287. queryset=Site.objects.all(),
  288. required=False,
  289. query_params={
  290. 'region_id': '$region',
  291. 'group_id': '$site_group',
  292. }
  293. )
  294. location = DynamicModelChoiceField(
  295. queryset=Location.objects.all(),
  296. required=False,
  297. query_params={
  298. 'site_id': '$site'
  299. }
  300. )
  301. rack = DynamicModelChoiceField(
  302. queryset=Rack.objects.all(),
  303. query_params={
  304. 'site_id': '$site',
  305. 'location_id': '$location',
  306. }
  307. )
  308. units = NumericArrayField(
  309. base_field=forms.IntegerField(),
  310. help_text=_("Comma-separated list of numeric unit IDs. A range may be specified using a hyphen.")
  311. )
  312. user = forms.ModelChoiceField(
  313. queryset=User.objects.order_by(
  314. 'username'
  315. ),
  316. widget=StaticSelect()
  317. )
  318. comments = CommentField()
  319. fieldsets = (
  320. ('Reservation', ('region', 'site_group', 'site', 'location', 'rack', 'units', 'user', 'description', 'tags')),
  321. ('Tenancy', ('tenant_group', 'tenant')),
  322. )
  323. class Meta:
  324. model = RackReservation
  325. fields = [
  326. 'region', 'site_group', 'site', 'location', 'rack', 'units', 'user', 'tenant_group', 'tenant',
  327. 'description', 'comments', 'tags',
  328. ]
  329. class ManufacturerForm(NetBoxModelForm):
  330. slug = SlugField()
  331. fieldsets = (
  332. ('Manufacturer', (
  333. 'name', 'slug', 'description', 'tags',
  334. )),
  335. )
  336. class Meta:
  337. model = Manufacturer
  338. fields = [
  339. 'name', 'slug', 'description', 'tags',
  340. ]
  341. class DeviceTypeForm(NetBoxModelForm):
  342. manufacturer = DynamicModelChoiceField(
  343. queryset=Manufacturer.objects.all()
  344. )
  345. slug = SlugField(
  346. slug_source='model'
  347. )
  348. comments = CommentField()
  349. fieldsets = (
  350. ('Device Type', ('manufacturer', 'model', 'slug', 'description', 'tags')),
  351. ('Chassis', (
  352. 'u_height', 'is_full_depth', 'part_number', 'subdevice_role', 'airflow', 'weight', 'weight_unit',
  353. )),
  354. ('Images', ('front_image', 'rear_image')),
  355. )
  356. class Meta:
  357. model = DeviceType
  358. fields = [
  359. 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow',
  360. 'weight', 'weight_unit', 'front_image', 'rear_image', 'description', 'comments', 'tags',
  361. ]
  362. widgets = {
  363. 'airflow': StaticSelect(),
  364. 'subdevice_role': StaticSelect(),
  365. 'front_image': ClearableFileInput(attrs={
  366. 'accept': DEVICETYPE_IMAGE_FORMATS
  367. }),
  368. 'rear_image': ClearableFileInput(attrs={
  369. 'accept': DEVICETYPE_IMAGE_FORMATS
  370. }),
  371. 'weight_unit': StaticSelect(),
  372. }
  373. class ModuleTypeForm(NetBoxModelForm):
  374. manufacturer = DynamicModelChoiceField(
  375. queryset=Manufacturer.objects.all()
  376. )
  377. comments = CommentField()
  378. fieldsets = (
  379. ('Module Type', ('manufacturer', 'model', 'part_number', 'description', 'tags')),
  380. ('Weight', ('weight', 'weight_unit'))
  381. )
  382. class Meta:
  383. model = ModuleType
  384. fields = [
  385. 'manufacturer', 'model', 'part_number', 'weight', 'weight_unit', 'description', 'comments', 'tags',
  386. ]
  387. widgets = {
  388. 'weight_unit': StaticSelect(),
  389. }
  390. class DeviceRoleForm(NetBoxModelForm):
  391. slug = SlugField()
  392. fieldsets = (
  393. ('Device Role', (
  394. 'name', 'slug', 'color', 'vm_role', 'description', 'tags',
  395. )),
  396. )
  397. class Meta:
  398. model = DeviceRole
  399. fields = [
  400. 'name', 'slug', 'color', 'vm_role', 'description', 'tags',
  401. ]
  402. class PlatformForm(NetBoxModelForm):
  403. manufacturer = DynamicModelChoiceField(
  404. queryset=Manufacturer.objects.all(),
  405. required=False
  406. )
  407. slug = SlugField(
  408. max_length=64
  409. )
  410. fieldsets = (
  411. ('Platform', (
  412. 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags',
  413. )),
  414. )
  415. class Meta:
  416. model = Platform
  417. fields = [
  418. 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags',
  419. ]
  420. widgets = {
  421. 'napalm_args': SmallTextarea(),
  422. }
  423. class DeviceForm(TenancyForm, NetBoxModelForm):
  424. region = DynamicModelChoiceField(
  425. queryset=Region.objects.all(),
  426. required=False,
  427. initial_params={
  428. 'sites': '$site'
  429. }
  430. )
  431. site_group = DynamicModelChoiceField(
  432. queryset=SiteGroup.objects.all(),
  433. required=False,
  434. initial_params={
  435. 'sites': '$site'
  436. }
  437. )
  438. site = DynamicModelChoiceField(
  439. queryset=Site.objects.all(),
  440. query_params={
  441. 'region_id': '$region',
  442. 'group_id': '$site_group',
  443. }
  444. )
  445. location = DynamicModelChoiceField(
  446. queryset=Location.objects.all(),
  447. required=False,
  448. query_params={
  449. 'site_id': '$site'
  450. },
  451. initial_params={
  452. 'racks': '$rack'
  453. }
  454. )
  455. rack = DynamicModelChoiceField(
  456. queryset=Rack.objects.all(),
  457. required=False,
  458. query_params={
  459. 'site_id': '$site',
  460. 'location_id': '$location',
  461. }
  462. )
  463. position = forms.DecimalField(
  464. required=False,
  465. help_text=_("The lowest-numbered unit occupied by the device"),
  466. widget=APISelect(
  467. api_url='/api/dcim/racks/{{rack}}/elevation/',
  468. attrs={
  469. 'disabled-indicator': 'device',
  470. 'data-dynamic-params': '[{"fieldName":"face","queryParam":"face"}]'
  471. }
  472. )
  473. )
  474. manufacturer = DynamicModelChoiceField(
  475. queryset=Manufacturer.objects.all(),
  476. required=False,
  477. initial_params={
  478. 'device_types': '$device_type'
  479. }
  480. )
  481. device_type = DynamicModelChoiceField(
  482. queryset=DeviceType.objects.all(),
  483. query_params={
  484. 'manufacturer_id': '$manufacturer'
  485. }
  486. )
  487. device_role = DynamicModelChoiceField(
  488. queryset=DeviceRole.objects.all()
  489. )
  490. platform = DynamicModelChoiceField(
  491. queryset=Platform.objects.all(),
  492. required=False,
  493. query_params={
  494. 'manufacturer_id': ['$manufacturer', 'null']
  495. }
  496. )
  497. cluster_group = DynamicModelChoiceField(
  498. queryset=ClusterGroup.objects.all(),
  499. required=False,
  500. null_option='None',
  501. initial_params={
  502. 'clusters': '$cluster'
  503. }
  504. )
  505. cluster = DynamicModelChoiceField(
  506. queryset=Cluster.objects.all(),
  507. required=False,
  508. query_params={
  509. 'group_id': '$cluster_group'
  510. }
  511. )
  512. comments = CommentField()
  513. local_context_data = JSONField(
  514. required=False,
  515. label=''
  516. )
  517. virtual_chassis = DynamicModelChoiceField(
  518. queryset=VirtualChassis.objects.all(),
  519. required=False
  520. )
  521. vc_position = forms.IntegerField(
  522. required=False,
  523. label=_('Position'),
  524. help_text=_("The position in the virtual chassis this device is identified by")
  525. )
  526. vc_priority = forms.IntegerField(
  527. required=False,
  528. label=_('Priority'),
  529. help_text=_("The priority of the device in the virtual chassis")
  530. )
  531. class Meta:
  532. model = Device
  533. fields = [
  534. 'name', 'device_role', 'device_type', 'serial', 'asset_tag', 'region', 'site_group', 'site', 'rack',
  535. 'location', 'position', 'face', 'status', 'airflow', 'platform', 'primary_ip4', 'primary_ip6',
  536. 'cluster_group', 'cluster', 'tenant_group', 'tenant', 'virtual_chassis', 'vc_position', 'vc_priority',
  537. 'description', 'comments', 'tags', 'local_context_data'
  538. ]
  539. help_texts = {
  540. 'device_role': _("The function this device serves"),
  541. 'serial': _("Chassis serial number"),
  542. 'local_context_data': _("Local config context data overwrites all source contexts in the final rendered "
  543. "config context"),
  544. }
  545. widgets = {
  546. 'face': StaticSelect(),
  547. 'status': StaticSelect(),
  548. 'airflow': StaticSelect(),
  549. 'primary_ip4': StaticSelect(),
  550. 'primary_ip6': StaticSelect(),
  551. }
  552. def __init__(self, *args, **kwargs):
  553. super().__init__(*args, **kwargs)
  554. if self.instance.pk:
  555. # Compile list of choices for primary IPv4 and IPv6 addresses
  556. for family in [4, 6]:
  557. ip_choices = [(None, '---------')]
  558. # Gather PKs of all interfaces belonging to this Device or a peer VirtualChassis member
  559. interface_ids = self.instance.vc_interfaces(if_master=False).values_list('pk', flat=True)
  560. # Collect interface IPs
  561. interface_ips = IPAddress.objects.filter(
  562. address__family=family,
  563. assigned_object_type=ContentType.objects.get_for_model(Interface),
  564. assigned_object_id__in=interface_ids
  565. ).prefetch_related('assigned_object')
  566. if interface_ips:
  567. ip_list = [(ip.id, f'{ip.address} ({ip.assigned_object})') for ip in interface_ips]
  568. ip_choices.append(('Interface IPs', ip_list))
  569. # Collect NAT IPs
  570. nat_ips = IPAddress.objects.prefetch_related('nat_inside').filter(
  571. address__family=family,
  572. nat_inside__assigned_object_type=ContentType.objects.get_for_model(Interface),
  573. nat_inside__assigned_object_id__in=interface_ids
  574. ).prefetch_related('assigned_object')
  575. if nat_ips:
  576. ip_list = [(ip.id, f'{ip.address} (NAT)') for ip in nat_ips]
  577. ip_choices.append(('NAT IPs', ip_list))
  578. self.fields['primary_ip{}'.format(family)].choices = ip_choices
  579. # If editing an existing device, exclude it from the list of occupied rack units. This ensures that a device
  580. # can be flipped from one face to another.
  581. self.fields['position'].widget.add_query_param('exclude', self.instance.pk)
  582. # Disable rack assignment if this is a child device installed in a parent device
  583. if self.instance.device_type.is_child_device and hasattr(self.instance, 'parent_bay'):
  584. self.fields['site'].disabled = True
  585. self.fields['rack'].disabled = True
  586. self.initial['site'] = self.instance.parent_bay.device.site_id
  587. self.initial['rack'] = self.instance.parent_bay.device.rack_id
  588. else:
  589. # An object that doesn't exist yet can't have any IPs assigned to it
  590. self.fields['primary_ip4'].choices = []
  591. self.fields['primary_ip4'].widget.attrs['readonly'] = True
  592. self.fields['primary_ip6'].choices = []
  593. self.fields['primary_ip6'].widget.attrs['readonly'] = True
  594. # Rack position
  595. position = self.data.get('position') or self.initial.get('position')
  596. if position:
  597. self.fields['position'].widget.choices = [(position, f'U{position}')]
  598. class ModuleForm(NetBoxModelForm):
  599. device = DynamicModelChoiceField(
  600. queryset=Device.objects.all(),
  601. initial_params={
  602. 'modulebays': '$module_bay'
  603. }
  604. )
  605. module_bay = DynamicModelChoiceField(
  606. queryset=ModuleBay.objects.all(),
  607. query_params={
  608. 'device_id': '$device'
  609. }
  610. )
  611. manufacturer = DynamicModelChoiceField(
  612. queryset=Manufacturer.objects.all(),
  613. required=False,
  614. initial_params={
  615. 'module_types': '$module_type'
  616. }
  617. )
  618. module_type = DynamicModelChoiceField(
  619. queryset=ModuleType.objects.all(),
  620. query_params={
  621. 'manufacturer_id': '$manufacturer'
  622. }
  623. )
  624. comments = CommentField()
  625. replicate_components = forms.BooleanField(
  626. required=False,
  627. initial=True,
  628. help_text=_("Automatically populate components associated with this module type")
  629. )
  630. adopt_components = forms.BooleanField(
  631. required=False,
  632. initial=False,
  633. help_text=_("Adopt already existing components")
  634. )
  635. fieldsets = (
  636. ('Module', (
  637. 'device', 'module_bay', 'manufacturer', 'module_type', 'status', 'description', 'tags',
  638. )),
  639. ('Hardware', (
  640. 'serial', 'asset_tag', 'replicate_components', 'adopt_components',
  641. )),
  642. )
  643. class Meta:
  644. model = Module
  645. fields = [
  646. 'device', 'module_bay', 'manufacturer', 'module_type', 'status', 'serial', 'asset_tag', 'tags',
  647. 'replicate_components', 'adopt_components', 'description', 'comments',
  648. ]
  649. def __init__(self, *args, **kwargs):
  650. super().__init__(*args, **kwargs)
  651. if self.instance.pk:
  652. self.fields['device'].disabled = True
  653. self.fields['replicate_components'].initial = False
  654. self.fields['replicate_components'].disabled = True
  655. self.fields['adopt_components'].initial = False
  656. self.fields['adopt_components'].disabled = True
  657. def save(self, *args, **kwargs):
  658. # If replicate_components is False, disable automatic component replication on the instance
  659. if self.instance.pk or not self.cleaned_data['replicate_components']:
  660. self.instance._disable_replication = True
  661. if self.cleaned_data['adopt_components']:
  662. self.instance._adopt_components = True
  663. return super().save(*args, **kwargs)
  664. def clean(self):
  665. super().clean()
  666. replicate_components = self.cleaned_data.get("replicate_components")
  667. adopt_components = self.cleaned_data.get("adopt_components")
  668. device = self.cleaned_data['device']
  669. module_type = self.cleaned_data['module_type']
  670. module_bay = self.cleaned_data['module_bay']
  671. # Bail out if we are not installing a new module or if we are not replicating components
  672. if self.instance.pk or not replicate_components:
  673. return
  674. for templates, component_attribute in [
  675. ("consoleporttemplates", "consoleports"),
  676. ("consoleserverporttemplates", "consoleserverports"),
  677. ("interfacetemplates", "interfaces"),
  678. ("powerporttemplates", "powerports"),
  679. ("poweroutlettemplates", "poweroutlets"),
  680. ("rearporttemplates", "rearports"),
  681. ("frontporttemplates", "frontports")
  682. ]:
  683. # Prefetch installed components
  684. installed_components = {
  685. component.name: component for component in getattr(device, component_attribute).all()
  686. }
  687. # Get the templates for the module type.
  688. for template in getattr(module_type, templates).all():
  689. # Installing modules with placeholders require that the bay has a position value
  690. if MODULE_TOKEN in template.name and not module_bay.position:
  691. raise forms.ValidationError(
  692. "Cannot install module with placeholder values in a module bay with no position defined"
  693. )
  694. resolved_name = template.name.replace(MODULE_TOKEN, module_bay.position)
  695. existing_item = installed_components.get(resolved_name)
  696. # It is not possible to adopt components already belonging to a module
  697. if adopt_components and existing_item and existing_item.module:
  698. raise forms.ValidationError(
  699. f"Cannot adopt {template.component_model.__name__} '{resolved_name}' as it already belongs "
  700. f"to a module"
  701. )
  702. # If we are not adopting components we error if the component exists
  703. if not adopt_components and resolved_name in installed_components:
  704. raise forms.ValidationError(
  705. f"{template.component_model.__name__} - {resolved_name} already exists"
  706. )
  707. class CableForm(TenancyForm, NetBoxModelForm):
  708. comments = CommentField()
  709. class Meta:
  710. model = Cable
  711. fields = [
  712. 'type', 'status', 'tenant_group', 'tenant', 'label', 'color', 'length', 'length_unit', 'description',
  713. 'comments', 'tags',
  714. ]
  715. widgets = {
  716. 'status': StaticSelect,
  717. 'type': StaticSelect,
  718. 'length_unit': StaticSelect,
  719. }
  720. error_messages = {
  721. 'length': {
  722. 'max_value': 'Maximum length is 32767 (any unit)'
  723. }
  724. }
  725. class PowerPanelForm(NetBoxModelForm):
  726. region = DynamicModelChoiceField(
  727. queryset=Region.objects.all(),
  728. required=False,
  729. initial_params={
  730. 'sites': '$site'
  731. }
  732. )
  733. site_group = DynamicModelChoiceField(
  734. queryset=SiteGroup.objects.all(),
  735. required=False,
  736. initial_params={
  737. 'sites': '$site'
  738. }
  739. )
  740. site = DynamicModelChoiceField(
  741. queryset=Site.objects.all(),
  742. query_params={
  743. 'region_id': '$region',
  744. 'group_id': '$site_group',
  745. }
  746. )
  747. location = DynamicModelChoiceField(
  748. queryset=Location.objects.all(),
  749. required=False,
  750. query_params={
  751. 'site_id': '$site'
  752. }
  753. )
  754. comments = CommentField()
  755. fieldsets = (
  756. ('Power Panel', ('region', 'site_group', 'site', 'location', 'name', 'description', 'tags')),
  757. )
  758. class Meta:
  759. model = PowerPanel
  760. fields = [
  761. 'region', 'site_group', 'site', 'location', 'name', 'description', 'comments', 'tags',
  762. ]
  763. class PowerFeedForm(NetBoxModelForm):
  764. region = DynamicModelChoiceField(
  765. queryset=Region.objects.all(),
  766. required=False,
  767. initial_params={
  768. 'sites__powerpanel': '$power_panel'
  769. }
  770. )
  771. site_group = DynamicModelChoiceField(
  772. queryset=SiteGroup.objects.all(),
  773. required=False,
  774. initial_params={
  775. 'sites': '$site'
  776. }
  777. )
  778. site = DynamicModelChoiceField(
  779. queryset=Site.objects.all(),
  780. required=False,
  781. initial_params={
  782. 'powerpanel': '$power_panel'
  783. },
  784. query_params={
  785. 'region_id': '$region',
  786. 'group_id': '$site_group',
  787. }
  788. )
  789. power_panel = DynamicModelChoiceField(
  790. queryset=PowerPanel.objects.all(),
  791. query_params={
  792. 'site_id': '$site'
  793. }
  794. )
  795. location = DynamicModelChoiceField(
  796. queryset=Location.objects.all(),
  797. required=False,
  798. query_params={
  799. 'site_id': '$site'
  800. },
  801. initial_params={
  802. 'racks': '$rack'
  803. }
  804. )
  805. rack = DynamicModelChoiceField(
  806. queryset=Rack.objects.all(),
  807. required=False,
  808. query_params={
  809. 'location_id': '$location',
  810. 'site_id': '$site'
  811. }
  812. )
  813. comments = CommentField()
  814. fieldsets = (
  815. ('Power Panel', ('region', 'site', 'power_panel')),
  816. ('Power Feed', ('location', 'rack', 'name', 'status', 'type', 'description', 'mark_connected', 'tags')),
  817. ('Characteristics', ('supply', 'voltage', 'amperage', 'phase', 'max_utilization')),
  818. )
  819. class Meta:
  820. model = PowerFeed
  821. fields = [
  822. 'region', 'site_group', 'site', 'power_panel', 'location', 'rack', 'name', 'status', 'type',
  823. 'mark_connected', 'supply', 'phase', 'voltage', 'amperage', 'max_utilization', 'description', 'comments',
  824. 'tags',
  825. ]
  826. widgets = {
  827. 'status': StaticSelect(),
  828. 'type': StaticSelect(),
  829. 'supply': StaticSelect(),
  830. 'phase': StaticSelect(),
  831. }
  832. #
  833. # Virtual chassis
  834. #
  835. class VirtualChassisForm(NetBoxModelForm):
  836. master = forms.ModelChoiceField(
  837. queryset=Device.objects.all(),
  838. required=False,
  839. )
  840. comments = CommentField()
  841. class Meta:
  842. model = VirtualChassis
  843. fields = [
  844. 'name', 'domain', 'master', 'description', 'comments', 'tags',
  845. ]
  846. widgets = {
  847. 'master': SelectWithPK(),
  848. }
  849. def __init__(self, *args, **kwargs):
  850. super().__init__(*args, **kwargs)
  851. self.fields['master'].queryset = Device.objects.filter(virtual_chassis=self.instance)
  852. class DeviceVCMembershipForm(forms.ModelForm):
  853. class Meta:
  854. model = Device
  855. fields = [
  856. 'vc_position', 'vc_priority',
  857. ]
  858. labels = {
  859. 'vc_position': 'Position',
  860. 'vc_priority': 'Priority',
  861. }
  862. def __init__(self, validate_vc_position=False, *args, **kwargs):
  863. super().__init__(*args, **kwargs)
  864. # Require VC position (only required when the Device is a VirtualChassis member)
  865. self.fields['vc_position'].required = True
  866. # Add bootstrap classes to form elements.
  867. self.fields['vc_position'].widget.attrs = {'class': 'form-control'}
  868. self.fields['vc_priority'].widget.attrs = {'class': 'form-control'}
  869. # Validation of vc_position is optional. This is only required when adding a new member to an existing
  870. # VirtualChassis. Otherwise, vc_position validation is handled by BaseVCMemberFormSet.
  871. self.validate_vc_position = validate_vc_position
  872. def clean_vc_position(self):
  873. vc_position = self.cleaned_data['vc_position']
  874. if self.validate_vc_position:
  875. conflicting_members = Device.objects.filter(
  876. virtual_chassis=self.instance.virtual_chassis,
  877. vc_position=vc_position
  878. )
  879. if conflicting_members.exists():
  880. raise forms.ValidationError(
  881. 'A virtual chassis member already exists in position {}.'.format(vc_position)
  882. )
  883. return vc_position
  884. class VCMemberSelectForm(BootstrapMixin, forms.Form):
  885. region = DynamicModelChoiceField(
  886. queryset=Region.objects.all(),
  887. required=False,
  888. initial_params={
  889. 'sites': '$site'
  890. }
  891. )
  892. site_group = DynamicModelChoiceField(
  893. queryset=SiteGroup.objects.all(),
  894. required=False,
  895. initial_params={
  896. 'sites': '$site'
  897. }
  898. )
  899. site = DynamicModelChoiceField(
  900. queryset=Site.objects.all(),
  901. required=False,
  902. query_params={
  903. 'region_id': '$region',
  904. 'group_id': '$site_group',
  905. }
  906. )
  907. rack = DynamicModelChoiceField(
  908. queryset=Rack.objects.all(),
  909. required=False,
  910. null_option='None',
  911. query_params={
  912. 'site_id': '$site'
  913. }
  914. )
  915. device = DynamicModelChoiceField(
  916. queryset=Device.objects.all(),
  917. query_params={
  918. 'site_id': '$site',
  919. 'rack_id': '$rack',
  920. 'virtual_chassis_id': 'null',
  921. }
  922. )
  923. def clean_device(self):
  924. device = self.cleaned_data['device']
  925. if device.virtual_chassis is not None:
  926. raise forms.ValidationError(
  927. f"Device {device} is already assigned to a virtual chassis."
  928. )
  929. return device
  930. #
  931. # Device component templates
  932. #
  933. class ComponentTemplateForm(BootstrapMixin, forms.ModelForm):
  934. device_type = DynamicModelChoiceField(
  935. queryset=DeviceType.objects.all()
  936. )
  937. def __init__(self, *args, **kwargs):
  938. super().__init__(*args, **kwargs)
  939. # Disable reassignment of DeviceType when editing an existing instance
  940. if self.instance.pk:
  941. self.fields['device_type'].disabled = True
  942. class ModularComponentTemplateForm(ComponentTemplateForm):
  943. device_type = DynamicModelChoiceField(
  944. queryset=DeviceType.objects.all().all(),
  945. required=False
  946. )
  947. module_type = DynamicModelChoiceField(
  948. queryset=ModuleType.objects.all(),
  949. required=False
  950. )
  951. def __init__(self, *args, **kwargs):
  952. super().__init__(*args, **kwargs)
  953. # Disable reassignment of ModuleType when editing an existing instance
  954. if self.instance.pk:
  955. self.fields['module_type'].disabled = True
  956. class ConsolePortTemplateForm(ModularComponentTemplateForm):
  957. fieldsets = (
  958. (None, ('device_type', 'module_type', 'name', 'label', 'type', 'description')),
  959. )
  960. class Meta:
  961. model = ConsolePortTemplate
  962. fields = [
  963. 'device_type', 'module_type', 'name', 'label', 'type', 'description',
  964. ]
  965. widgets = {
  966. 'type': StaticSelect,
  967. }
  968. class ConsoleServerPortTemplateForm(ModularComponentTemplateForm):
  969. fieldsets = (
  970. (None, ('device_type', 'module_type', 'name', 'label', 'type', 'description')),
  971. )
  972. class Meta:
  973. model = ConsoleServerPortTemplate
  974. fields = [
  975. 'device_type', 'module_type', 'name', 'label', 'type', 'description',
  976. ]
  977. widgets = {
  978. 'type': StaticSelect,
  979. }
  980. class PowerPortTemplateForm(ModularComponentTemplateForm):
  981. fieldsets = (
  982. (None, (
  983. 'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
  984. )),
  985. )
  986. class Meta:
  987. model = PowerPortTemplate
  988. fields = [
  989. 'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
  990. ]
  991. widgets = {
  992. 'type': StaticSelect(),
  993. }
  994. class PowerOutletTemplateForm(ModularComponentTemplateForm):
  995. power_port = DynamicModelChoiceField(
  996. queryset=PowerPortTemplate.objects.all(),
  997. required=False,
  998. query_params={
  999. 'devicetype_id': '$device_type',
  1000. }
  1001. )
  1002. fieldsets = (
  1003. (None, ('device_type', 'module_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description')),
  1004. )
  1005. class Meta:
  1006. model = PowerOutletTemplate
  1007. fields = [
  1008. 'device_type', 'module_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description',
  1009. ]
  1010. widgets = {
  1011. 'type': StaticSelect(),
  1012. 'feed_leg': StaticSelect(),
  1013. }
  1014. class InterfaceTemplateForm(ModularComponentTemplateForm):
  1015. fieldsets = (
  1016. (None, ('device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'description')),
  1017. ('PoE', ('poe_mode', 'poe_type'))
  1018. )
  1019. class Meta:
  1020. model = InterfaceTemplate
  1021. fields = [
  1022. 'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'description', 'poe_mode', 'poe_type',
  1023. ]
  1024. widgets = {
  1025. 'type': StaticSelect(),
  1026. 'poe_mode': StaticSelect(),
  1027. 'poe_type': StaticSelect(),
  1028. }
  1029. class FrontPortTemplateForm(ModularComponentTemplateForm):
  1030. rear_port = DynamicModelChoiceField(
  1031. queryset=RearPortTemplate.objects.all(),
  1032. required=False,
  1033. query_params={
  1034. 'devicetype_id': '$device_type',
  1035. 'moduletype_id': '$module_type',
  1036. }
  1037. )
  1038. fieldsets = (
  1039. (None, (
  1040. 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position',
  1041. 'description',
  1042. )),
  1043. )
  1044. class Meta:
  1045. model = FrontPortTemplate
  1046. fields = [
  1047. 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position',
  1048. 'description',
  1049. ]
  1050. widgets = {
  1051. 'type': StaticSelect(),
  1052. }
  1053. class RearPortTemplateForm(ModularComponentTemplateForm):
  1054. fieldsets = (
  1055. (None, ('device_type', 'module_type', 'name', 'label', 'type', 'color', 'positions', 'description')),
  1056. )
  1057. class Meta:
  1058. model = RearPortTemplate
  1059. fields = [
  1060. 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'positions', 'description',
  1061. ]
  1062. widgets = {
  1063. 'type': StaticSelect(),
  1064. }
  1065. class ModuleBayTemplateForm(ComponentTemplateForm):
  1066. fieldsets = (
  1067. (None, ('device_type', 'name', 'label', 'position', 'description')),
  1068. )
  1069. class Meta:
  1070. model = ModuleBayTemplate
  1071. fields = [
  1072. 'device_type', 'name', 'label', 'position', 'description',
  1073. ]
  1074. class DeviceBayTemplateForm(ComponentTemplateForm):
  1075. fieldsets = (
  1076. (None, ('device_type', 'name', 'label', 'description')),
  1077. )
  1078. class Meta:
  1079. model = DeviceBayTemplate
  1080. fields = [
  1081. 'device_type', 'name', 'label', 'description',
  1082. ]
  1083. class InventoryItemTemplateForm(ComponentTemplateForm):
  1084. parent = DynamicModelChoiceField(
  1085. queryset=InventoryItemTemplate.objects.all(),
  1086. required=False,
  1087. query_params={
  1088. 'devicetype_id': '$device_type'
  1089. }
  1090. )
  1091. role = DynamicModelChoiceField(
  1092. queryset=InventoryItemRole.objects.all(),
  1093. required=False
  1094. )
  1095. manufacturer = DynamicModelChoiceField(
  1096. queryset=Manufacturer.objects.all(),
  1097. required=False
  1098. )
  1099. component_type = ContentTypeChoiceField(
  1100. queryset=ContentType.objects.all(),
  1101. limit_choices_to=MODULAR_COMPONENT_TEMPLATE_MODELS,
  1102. required=False,
  1103. widget=forms.HiddenInput
  1104. )
  1105. component_id = forms.IntegerField(
  1106. required=False,
  1107. widget=forms.HiddenInput
  1108. )
  1109. fieldsets = (
  1110. (None, (
  1111. 'device_type', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'description',
  1112. 'component_type', 'component_id',
  1113. )),
  1114. )
  1115. class Meta:
  1116. model = InventoryItemTemplate
  1117. fields = [
  1118. 'device_type', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'description',
  1119. 'component_type', 'component_id',
  1120. ]
  1121. #
  1122. # Device components
  1123. #
  1124. class DeviceComponentForm(NetBoxModelForm):
  1125. device = DynamicModelChoiceField(
  1126. queryset=Device.objects.all()
  1127. )
  1128. def __init__(self, *args, **kwargs):
  1129. super().__init__(*args, **kwargs)
  1130. # Disable reassignment of Device when editing an existing instance
  1131. if self.instance.pk:
  1132. self.fields['device'].disabled = True
  1133. class ModularDeviceComponentForm(DeviceComponentForm):
  1134. module = DynamicModelChoiceField(
  1135. queryset=Module.objects.all(),
  1136. required=False,
  1137. query_params={
  1138. 'device_id': '$device',
  1139. }
  1140. )
  1141. class ConsolePortForm(ModularDeviceComponentForm):
  1142. fieldsets = (
  1143. (None, (
  1144. 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
  1145. )),
  1146. )
  1147. class Meta:
  1148. model = ConsolePort
  1149. fields = [
  1150. 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
  1151. ]
  1152. widgets = {
  1153. 'type': StaticSelect(),
  1154. 'speed': StaticSelect(),
  1155. }
  1156. class ConsoleServerPortForm(ModularDeviceComponentForm):
  1157. fieldsets = (
  1158. (None, (
  1159. 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
  1160. )),
  1161. )
  1162. class Meta:
  1163. model = ConsoleServerPort
  1164. fields = [
  1165. 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
  1166. ]
  1167. widgets = {
  1168. 'type': StaticSelect(),
  1169. 'speed': StaticSelect(),
  1170. }
  1171. class PowerPortForm(ModularDeviceComponentForm):
  1172. fieldsets = (
  1173. (None, (
  1174. 'device', 'module', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected',
  1175. 'description', 'tags',
  1176. )),
  1177. )
  1178. class Meta:
  1179. model = PowerPort
  1180. fields = [
  1181. 'device', 'module', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected',
  1182. 'description', 'tags',
  1183. ]
  1184. widgets = {
  1185. 'type': StaticSelect(),
  1186. }
  1187. class PowerOutletForm(ModularDeviceComponentForm):
  1188. power_port = DynamicModelChoiceField(
  1189. queryset=PowerPort.objects.all(),
  1190. required=False,
  1191. query_params={
  1192. 'device_id': '$device',
  1193. }
  1194. )
  1195. fieldsets = (
  1196. (None, (
  1197. 'device', 'module', 'name', 'label', 'type', 'power_port', 'feed_leg', 'mark_connected', 'description',
  1198. 'tags',
  1199. )),
  1200. )
  1201. class Meta:
  1202. model = PowerOutlet
  1203. fields = [
  1204. 'device', 'module', 'name', 'label', 'type', 'power_port', 'feed_leg', 'mark_connected', 'description',
  1205. 'tags',
  1206. ]
  1207. widgets = {
  1208. 'type': StaticSelect(),
  1209. 'feed_leg': StaticSelect(),
  1210. }
  1211. class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm):
  1212. vdcs = DynamicModelMultipleChoiceField(
  1213. queryset=VirtualDeviceContext.objects.all(),
  1214. required=False,
  1215. label='Virtual Device Contexts',
  1216. query_params={
  1217. 'device_id': '$device',
  1218. }
  1219. )
  1220. parent = DynamicModelChoiceField(
  1221. queryset=Interface.objects.all(),
  1222. required=False,
  1223. label=_('Parent interface'),
  1224. query_params={
  1225. 'device_id': '$device',
  1226. }
  1227. )
  1228. bridge = DynamicModelChoiceField(
  1229. queryset=Interface.objects.all(),
  1230. required=False,
  1231. label=_('Bridged interface'),
  1232. query_params={
  1233. 'device_id': '$device',
  1234. }
  1235. )
  1236. lag = DynamicModelChoiceField(
  1237. queryset=Interface.objects.all(),
  1238. required=False,
  1239. label=_('LAG interface'),
  1240. query_params={
  1241. 'device_id': '$device',
  1242. 'type': 'lag',
  1243. }
  1244. )
  1245. wireless_lan_group = DynamicModelChoiceField(
  1246. queryset=WirelessLANGroup.objects.all(),
  1247. required=False,
  1248. label=_('Wireless LAN group')
  1249. )
  1250. wireless_lans = DynamicModelMultipleChoiceField(
  1251. queryset=WirelessLAN.objects.all(),
  1252. required=False,
  1253. label=_('Wireless LANs'),
  1254. query_params={
  1255. 'group_id': '$wireless_lan_group',
  1256. }
  1257. )
  1258. vlan_group = DynamicModelChoiceField(
  1259. queryset=VLANGroup.objects.all(),
  1260. required=False,
  1261. label=_('VLAN group')
  1262. )
  1263. untagged_vlan = DynamicModelChoiceField(
  1264. queryset=VLAN.objects.all(),
  1265. required=False,
  1266. label=_('Untagged VLAN'),
  1267. query_params={
  1268. 'group_id': '$vlan_group',
  1269. 'available_on_device': '$device',
  1270. }
  1271. )
  1272. tagged_vlans = DynamicModelMultipleChoiceField(
  1273. queryset=VLAN.objects.all(),
  1274. required=False,
  1275. label=_('Tagged VLANs'),
  1276. query_params={
  1277. 'group_id': '$vlan_group',
  1278. 'available_on_device': '$device',
  1279. }
  1280. )
  1281. vrf = DynamicModelChoiceField(
  1282. queryset=VRF.objects.all(),
  1283. required=False,
  1284. label=_('VRF')
  1285. )
  1286. wwn = forms.CharField(
  1287. empty_value=None,
  1288. required=False,
  1289. label=_('WWN')
  1290. )
  1291. fieldsets = (
  1292. ('Interface', ('device', 'module', 'name', 'label', 'type', 'speed', 'duplex', 'description', 'tags')),
  1293. ('Addressing', ('vrf', 'mac_address', 'wwn')),
  1294. ('Operation', ('vdcs', 'mtu', 'tx_power', 'enabled', 'mgmt_only', 'mark_connected')),
  1295. ('Related Interfaces', ('parent', 'bridge', 'lag')),
  1296. ('PoE', ('poe_mode', 'poe_type')),
  1297. ('802.1Q Switching', ('mode', 'vlan_group', 'untagged_vlan', 'tagged_vlans')),
  1298. ('Wireless', (
  1299. 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'wireless_lan_group', 'wireless_lans',
  1300. )),
  1301. )
  1302. class Meta:
  1303. model = Interface
  1304. fields = [
  1305. 'device', 'module', 'vdcs', 'name', 'label', 'type', 'speed', 'duplex', 'enabled', 'parent', 'bridge', 'lag',
  1306. 'mac_address', 'wwn', 'mtu', 'mgmt_only', 'mark_connected', 'description', 'poe_mode', 'poe_type', 'mode',
  1307. 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'wireless_lans',
  1308. 'untagged_vlan', 'tagged_vlans', 'vrf', 'tags',
  1309. ]
  1310. widgets = {
  1311. 'type': StaticSelect(),
  1312. 'speed': SelectSpeedWidget(),
  1313. 'poe_mode': StaticSelect(),
  1314. 'poe_type': StaticSelect(),
  1315. 'duplex': StaticSelect(),
  1316. 'mode': StaticSelect(),
  1317. 'rf_role': StaticSelect(),
  1318. 'rf_channel': StaticSelect(),
  1319. }
  1320. labels = {
  1321. 'mode': '802.1Q Mode',
  1322. }
  1323. help_texts = {
  1324. 'mode': INTERFACE_MODE_HELP_TEXT,
  1325. 'rf_channel_frequency': _("Populated by selected channel (if set)"),
  1326. 'rf_channel_width': _("Populated by selected channel (if set)"),
  1327. }
  1328. class FrontPortForm(ModularDeviceComponentForm):
  1329. rear_port = DynamicModelChoiceField(
  1330. queryset=RearPort.objects.all(),
  1331. query_params={
  1332. 'device_id': '$device',
  1333. }
  1334. )
  1335. fieldsets = (
  1336. (None, (
  1337. 'device', 'module', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'mark_connected',
  1338. 'description', 'tags',
  1339. )),
  1340. )
  1341. class Meta:
  1342. model = FrontPort
  1343. fields = [
  1344. 'device', 'module', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'mark_connected',
  1345. 'description', 'tags',
  1346. ]
  1347. widgets = {
  1348. 'type': StaticSelect(),
  1349. }
  1350. class RearPortForm(ModularDeviceComponentForm):
  1351. fieldsets = (
  1352. (None, (
  1353. 'device', 'module', 'name', 'label', 'type', 'color', 'positions', 'mark_connected', 'description', 'tags',
  1354. )),
  1355. )
  1356. class Meta:
  1357. model = RearPort
  1358. fields = [
  1359. 'device', 'module', 'name', 'label', 'type', 'color', 'positions', 'mark_connected', 'description', 'tags',
  1360. ]
  1361. widgets = {
  1362. 'type': StaticSelect(),
  1363. }
  1364. class ModuleBayForm(DeviceComponentForm):
  1365. fieldsets = (
  1366. (None, ('device', 'name', 'label', 'position', 'description', 'tags',)),
  1367. )
  1368. class Meta:
  1369. model = ModuleBay
  1370. fields = [
  1371. 'device', 'name', 'label', 'position', 'description', 'tags',
  1372. ]
  1373. class DeviceBayForm(DeviceComponentForm):
  1374. fieldsets = (
  1375. (None, ('device', 'name', 'label', 'description', 'tags',)),
  1376. )
  1377. class Meta:
  1378. model = DeviceBay
  1379. fields = [
  1380. 'device', 'name', 'label', 'description', 'tags',
  1381. ]
  1382. class PopulateDeviceBayForm(BootstrapMixin, forms.Form):
  1383. installed_device = forms.ModelChoiceField(
  1384. queryset=Device.objects.all(),
  1385. label=_('Child Device'),
  1386. help_text=_("Child devices must first be created and assigned to the site/rack of the parent device."),
  1387. widget=StaticSelect(),
  1388. )
  1389. def __init__(self, device_bay, *args, **kwargs):
  1390. super().__init__(*args, **kwargs)
  1391. self.fields['installed_device'].queryset = Device.objects.filter(
  1392. site=device_bay.device.site,
  1393. rack=device_bay.device.rack,
  1394. parent_bay__isnull=True,
  1395. device_type__u_height=0,
  1396. device_type__subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
  1397. ).exclude(pk=device_bay.device.pk)
  1398. class InventoryItemForm(DeviceComponentForm):
  1399. parent = DynamicModelChoiceField(
  1400. queryset=InventoryItem.objects.all(),
  1401. required=False,
  1402. query_params={
  1403. 'device_id': '$device'
  1404. }
  1405. )
  1406. role = DynamicModelChoiceField(
  1407. queryset=InventoryItemRole.objects.all(),
  1408. required=False
  1409. )
  1410. manufacturer = DynamicModelChoiceField(
  1411. queryset=Manufacturer.objects.all(),
  1412. required=False
  1413. )
  1414. component_type = ContentTypeChoiceField(
  1415. queryset=ContentType.objects.all(),
  1416. limit_choices_to=MODULAR_COMPONENT_MODELS,
  1417. required=False,
  1418. widget=forms.HiddenInput
  1419. )
  1420. component_id = forms.IntegerField(
  1421. required=False,
  1422. widget=forms.HiddenInput
  1423. )
  1424. fieldsets = (
  1425. ('Inventory Item', ('device', 'parent', 'name', 'label', 'role', 'description', 'tags')),
  1426. ('Hardware', ('manufacturer', 'part_id', 'serial', 'asset_tag')),
  1427. )
  1428. class Meta:
  1429. model = InventoryItem
  1430. fields = [
  1431. 'device', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag',
  1432. 'description', 'component_type', 'component_id', 'tags',
  1433. ]
  1434. #
  1435. # Device component roles
  1436. #
  1437. class InventoryItemRoleForm(NetBoxModelForm):
  1438. slug = SlugField()
  1439. fieldsets = (
  1440. ('Inventory Item Role', (
  1441. 'name', 'slug', 'color', 'description', 'tags',
  1442. )),
  1443. )
  1444. class Meta:
  1445. model = InventoryItemRole
  1446. fields = [
  1447. 'name', 'slug', 'color', 'description', 'tags',
  1448. ]
  1449. class VirtualDeviceContextForm(TenancyForm, NetBoxModelForm):
  1450. region = DynamicModelChoiceField(
  1451. queryset=Region.objects.all(),
  1452. required=False,
  1453. initial_params={
  1454. 'sites': '$site'
  1455. }
  1456. )
  1457. site_group = DynamicModelChoiceField(
  1458. queryset=SiteGroup.objects.all(),
  1459. required=False,
  1460. initial_params={
  1461. 'sites': '$site'
  1462. }
  1463. )
  1464. site = DynamicModelChoiceField(
  1465. queryset=Site.objects.all(),
  1466. required=False,
  1467. query_params={
  1468. 'region_id': '$region',
  1469. 'group_id': '$site_group',
  1470. }
  1471. )
  1472. location = DynamicModelChoiceField(
  1473. queryset=Location.objects.all(),
  1474. required=False,
  1475. query_params={
  1476. 'site_id': '$site'
  1477. },
  1478. initial_params={
  1479. 'racks': '$rack'
  1480. }
  1481. )
  1482. rack = DynamicModelChoiceField(
  1483. queryset=Rack.objects.all(),
  1484. required=False,
  1485. query_params={
  1486. 'site_id': '$site',
  1487. 'location_id': '$location',
  1488. }
  1489. )
  1490. device = DynamicModelChoiceField(
  1491. queryset=Device.objects.all(),
  1492. query_params={
  1493. 'site_id': '$site',
  1494. 'location_id': '$location',
  1495. 'rack_id': '$rack',
  1496. }
  1497. )
  1498. primary_ip4 = DynamicModelChoiceField(
  1499. queryset=IPAddress.objects.all(),
  1500. label='Primary IPv4',
  1501. required=False,
  1502. query_params={
  1503. 'device_id': '$device',
  1504. 'family': '4',
  1505. }
  1506. )
  1507. primary_ip6 = DynamicModelChoiceField(
  1508. queryset=IPAddress.objects.all(),
  1509. label='Primary IPv6',
  1510. required=False,
  1511. query_params={
  1512. 'device_id': '$device',
  1513. 'family': '6',
  1514. }
  1515. )
  1516. fieldsets = (
  1517. ('Assigned Device', ('region', 'site_group', 'site', 'location', 'rack', 'device')),
  1518. ('Virtual Device Context', ('name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tags')),
  1519. ('Tenancy', ('tenant_group', 'tenant'))
  1520. )
  1521. class Meta:
  1522. model = VirtualDeviceContext
  1523. fields = [
  1524. 'region', 'site_group', 'site', 'location', 'rack', 'device', 'name', 'status', 'identifier',
  1525. 'primary_ip4', 'primary_ip6', 'tenant_group', 'tenant', 'comments', 'tags'
  1526. ]
  1527. widgets = {
  1528. 'status': StaticSelect(),
  1529. 'primary_ip4': StaticSelect(),
  1530. 'primary_ip6': StaticSelect(),
  1531. }