model_forms.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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', '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', 'vdcs', 'name', 'label', 'type', 'speed', 'duplex', 'description', 'tags')),
  1293. ('Addressing', ('vrf', 'mac_address', 'wwn')),
  1294. ('Operation', ('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. }