forms.py 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. import re
  2. from django import forms
  3. from django.db.models import Count, Q
  4. from extras.forms import CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
  5. from ipam.models import IPAddress
  6. from tenancy.forms import bulkedit_tenant_choices
  7. from tenancy.models import Tenant
  8. from utilities.forms import (
  9. APISelect, add_blank_choice, BootstrapMixin, BulkImportForm, CommentField, CSVDataField, ExpandableNameField,
  10. FilterChoiceField, FlexibleModelChoiceField, Livesearch, SelectWithDisabled, SmallTextarea, SlugField,
  11. )
  12. from .models import (
  13. DeviceBay, DeviceBayTemplate, CONNECTION_STATUS_CHOICES, CONNECTION_STATUS_PLANNED, CONNECTION_STATUS_CONNECTED,
  14. ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceRole, DeviceType,
  15. Interface, IFACE_FF_VIRTUAL, InterfaceConnection, InterfaceTemplate, Manufacturer, Module, Platform, PowerOutlet,
  16. PowerOutletTemplate, PowerPort, PowerPortTemplate, RACK_TYPE_CHOICES, RACK_WIDTH_CHOICES, Rack, RackGroup, RackRole,
  17. Site, STATUS_CHOICES, SUBDEVICE_ROLE_CHILD
  18. )
  19. FORM_STATUS_CHOICES = [
  20. ['', '---------'],
  21. ]
  22. FORM_STATUS_CHOICES += STATUS_CHOICES
  23. DEVICE_BY_PK_RE = '{\d+\}'
  24. def get_device_by_name_or_pk(name):
  25. """
  26. Attempt to retrieve a device by either its name or primary key ('{pk}').
  27. """
  28. if re.match(DEVICE_BY_PK_RE, name):
  29. pk = name.strip('{}')
  30. device = Device.objects.get(pk=pk)
  31. else:
  32. device = Device.objects.get(name=name)
  33. return device
  34. def bulkedit_platform_choices():
  35. choices = [
  36. (None, '---------'),
  37. (0, 'None'),
  38. ]
  39. choices += [(p.pk, p.name) for p in Platform.objects.all()]
  40. return choices
  41. def bulkedit_rackgroup_choices():
  42. """
  43. Include an option to remove the currently assigned group from a rack.
  44. """
  45. choices = [
  46. (None, '---------'),
  47. (0, 'None'),
  48. ]
  49. choices += [(r.pk, r) for r in RackGroup.objects.all()]
  50. return choices
  51. def bulkedit_rackrole_choices():
  52. """
  53. Include an option to remove the currently assigned role from a rack.
  54. """
  55. choices = [
  56. (None, '---------'),
  57. (0, 'None'),
  58. ]
  59. choices += [(r.pk, r.name) for r in RackRole.objects.all()]
  60. return choices
  61. #
  62. # Sites
  63. #
  64. class SiteForm(BootstrapMixin, CustomFieldForm):
  65. slug = SlugField()
  66. comments = CommentField()
  67. class Meta:
  68. model = Site
  69. fields = ['name', 'slug', 'tenant', 'facility', 'asn', 'physical_address', 'shipping_address', 'comments']
  70. widgets = {
  71. 'physical_address': SmallTextarea(attrs={'rows': 3}),
  72. 'shipping_address': SmallTextarea(attrs={'rows': 3}),
  73. }
  74. help_texts = {
  75. 'name': "Full name of the site",
  76. 'facility': "Data center provider and facility (e.g. Equinix NY7)",
  77. 'asn': "BGP autonomous system number",
  78. 'physical_address': "Physical location of the building (e.g. for GPS)",
  79. 'shipping_address': "If different from the physical address"
  80. }
  81. class SiteFromCSVForm(forms.ModelForm):
  82. tenant = forms.ModelChoiceField(Tenant.objects.all(), to_field_name='name', required=False,
  83. error_messages={'invalid_choice': 'Tenant not found.'})
  84. class Meta:
  85. model = Site
  86. fields = ['name', 'slug', 'tenant', 'facility', 'asn']
  87. class SiteImportForm(BulkImportForm, BootstrapMixin):
  88. csv = CSVDataField(csv_form=SiteFromCSVForm)
  89. class SiteBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
  90. pk = forms.ModelMultipleChoiceField(queryset=Site.objects.all(), widget=forms.MultipleHiddenInput)
  91. tenant = forms.TypedChoiceField(choices=bulkedit_tenant_choices, coerce=int, required=False, label='Tenant')
  92. class SiteFilterForm(BootstrapMixin, CustomFieldFilterForm):
  93. model = Site
  94. tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('sites')), to_field_name='slug',
  95. null_option=(0, 'None'))
  96. #
  97. # Rack groups
  98. #
  99. class RackGroupForm(forms.ModelForm, BootstrapMixin):
  100. slug = SlugField()
  101. class Meta:
  102. model = RackGroup
  103. fields = ['site', 'name', 'slug']
  104. class RackGroupFilterForm(forms.Form, BootstrapMixin):
  105. site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('rack_groups')), to_field_name='slug')
  106. #
  107. # Rack roles
  108. #
  109. class RackRoleForm(forms.ModelForm, BootstrapMixin):
  110. slug = SlugField()
  111. class Meta:
  112. model = RackRole
  113. fields = ['name', 'slug', 'color']
  114. #
  115. # Racks
  116. #
  117. class RackForm(BootstrapMixin, CustomFieldForm):
  118. group = forms.ModelChoiceField(queryset=RackGroup.objects.all(), required=False, label='Group', widget=APISelect(
  119. api_url='/api/dcim/rack-groups/?site_id={{site}}',
  120. ))
  121. comments = CommentField()
  122. class Meta:
  123. model = Rack
  124. fields = ['site', 'group', 'name', 'facility_id', 'tenant', 'role', 'type', 'width', 'u_height', 'comments']
  125. help_texts = {
  126. 'site': "The site at which the rack exists",
  127. 'name': "Organizational rack name",
  128. 'facility_id': "The unique rack ID assigned by the facility",
  129. 'u_height': "Height in rack units",
  130. }
  131. widgets = {
  132. 'site': forms.Select(attrs={'filter-for': 'group'}),
  133. }
  134. def __init__(self, *args, **kwargs):
  135. super(RackForm, self).__init__(*args, **kwargs)
  136. # Limit rack group choices
  137. if self.is_bound and self.data.get('site'):
  138. self.fields['group'].queryset = RackGroup.objects.filter(site__pk=self.data['site'])
  139. elif self.initial.get('site'):
  140. self.fields['group'].queryset = RackGroup.objects.filter(site=self.initial['site'])
  141. else:
  142. self.fields['group'].choices = []
  143. class RackFromCSVForm(forms.ModelForm):
  144. site = forms.ModelChoiceField(queryset=Site.objects.all(), to_field_name='name',
  145. error_messages={'invalid_choice': 'Site not found.'})
  146. group_name = forms.CharField(required=False)
  147. tenant = forms.ModelChoiceField(Tenant.objects.all(), to_field_name='name', required=False,
  148. error_messages={'invalid_choice': 'Tenant not found.'})
  149. role = forms.ModelChoiceField(RackRole.objects.all(), to_field_name='name', required=False,
  150. error_messages={'invalid_choice': 'Role not found.'})
  151. type = forms.CharField(required=False)
  152. class Meta:
  153. model = Rack
  154. fields = ['site', 'group_name', 'name', 'facility_id', 'tenant', 'role', 'type', 'width', 'u_height']
  155. def clean(self):
  156. site = self.cleaned_data.get('site')
  157. group = self.cleaned_data.get('group_name')
  158. # Validate rack group
  159. if site and group:
  160. try:
  161. self.instance.group = RackGroup.objects.get(site=site, name=group)
  162. except RackGroup.DoesNotExist:
  163. self.add_error('group_name', "Invalid rack group ({})".format(group))
  164. def clean_type(self):
  165. rack_type = self.cleaned_data['type']
  166. if not rack_type:
  167. return None
  168. try:
  169. choices = {v.lower(): k for k, v in RACK_TYPE_CHOICES}
  170. return choices[rack_type.lower()]
  171. except KeyError:
  172. raise forms.ValidationError('Invalid rack type ({}). Valid choices are: {}.'.format(
  173. rack_type,
  174. ', '.join({v: k for k, v in RACK_TYPE_CHOICES}),
  175. ))
  176. class RackImportForm(BulkImportForm, BootstrapMixin):
  177. csv = CSVDataField(csv_form=RackFromCSVForm)
  178. class RackBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
  179. pk = forms.ModelMultipleChoiceField(queryset=Rack.objects.all(), widget=forms.MultipleHiddenInput)
  180. site = forms.ModelChoiceField(queryset=Site.objects.all(), required=False, label='Site')
  181. group = forms.TypedChoiceField(choices=bulkedit_rackgroup_choices, coerce=int, required=False, label='Group')
  182. tenant = forms.TypedChoiceField(choices=bulkedit_tenant_choices, coerce=int, required=False, label='Tenant')
  183. role = forms.TypedChoiceField(choices=bulkedit_rackrole_choices, coerce=int, required=False, label='Role')
  184. type = forms.ChoiceField(choices=add_blank_choice(RACK_TYPE_CHOICES), required=False, label='Type')
  185. width = forms.ChoiceField(choices=add_blank_choice(RACK_WIDTH_CHOICES), required=False, label='Width')
  186. u_height = forms.IntegerField(required=False, label='Height (U)')
  187. comments = CommentField()
  188. class RackFilterForm(BootstrapMixin, CustomFieldFilterForm):
  189. model = Rack
  190. site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('racks')), to_field_name='slug')
  191. group_id = FilterChoiceField(queryset=RackGroup.objects.select_related('site')
  192. .annotate(filter_count=Count('racks')), label='Rack group', null_option=(0, 'None'))
  193. tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('racks')), to_field_name='slug',
  194. null_option=(0, 'None'))
  195. role = FilterChoiceField(queryset=RackRole.objects.annotate(filter_count=Count('racks')), to_field_name='slug',
  196. null_option=(0, 'None'))
  197. #
  198. # Manufacturers
  199. #
  200. class ManufacturerForm(forms.ModelForm, BootstrapMixin):
  201. slug = SlugField()
  202. class Meta:
  203. model = Manufacturer
  204. fields = ['name', 'slug']
  205. #
  206. # Device types
  207. #
  208. class DeviceTypeForm(forms.ModelForm, BootstrapMixin):
  209. slug = SlugField(slug_source='model')
  210. class Meta:
  211. model = DeviceType
  212. fields = ['manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'is_console_server',
  213. 'is_pdu', 'is_network_device', 'subdevice_role']
  214. class DeviceTypeBulkEditForm(forms.Form, BootstrapMixin):
  215. pk = forms.ModelMultipleChoiceField(queryset=DeviceType.objects.all(), widget=forms.MultipleHiddenInput)
  216. manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all(), required=False)
  217. u_height = forms.IntegerField(min_value=1, required=False)
  218. class DeviceTypeFilterForm(forms.Form, BootstrapMixin):
  219. manufacturer = FilterChoiceField(queryset=Manufacturer.objects.annotate(filter_count=Count('device_types')),
  220. to_field_name='slug')
  221. #
  222. # Device component templates
  223. #
  224. class ConsolePortTemplateForm(forms.ModelForm, BootstrapMixin):
  225. name_pattern = ExpandableNameField(label='Name')
  226. class Meta:
  227. model = ConsolePortTemplate
  228. fields = ['name_pattern']
  229. class ConsoleServerPortTemplateForm(forms.ModelForm, BootstrapMixin):
  230. name_pattern = ExpandableNameField(label='Name')
  231. class Meta:
  232. model = ConsoleServerPortTemplate
  233. fields = ['name_pattern']
  234. class PowerPortTemplateForm(forms.ModelForm, BootstrapMixin):
  235. name_pattern = ExpandableNameField(label='Name')
  236. class Meta:
  237. model = PowerPortTemplate
  238. fields = ['name_pattern']
  239. class PowerOutletTemplateForm(forms.ModelForm, BootstrapMixin):
  240. name_pattern = ExpandableNameField(label='Name')
  241. class Meta:
  242. model = PowerOutletTemplate
  243. fields = ['name_pattern']
  244. class InterfaceTemplateForm(forms.ModelForm, BootstrapMixin):
  245. name_pattern = ExpandableNameField(label='Name')
  246. class Meta:
  247. model = InterfaceTemplate
  248. fields = ['name_pattern', 'form_factor', 'mgmt_only']
  249. class DeviceBayTemplateForm(forms.ModelForm, BootstrapMixin):
  250. name_pattern = ExpandableNameField(label='Name')
  251. class Meta:
  252. model = DeviceBayTemplate
  253. fields = ['name_pattern']
  254. #
  255. # Device roles
  256. #
  257. class DeviceRoleForm(forms.ModelForm, BootstrapMixin):
  258. slug = SlugField()
  259. class Meta:
  260. model = DeviceRole
  261. fields = ['name', 'slug', 'color']
  262. #
  263. # Platforms
  264. #
  265. class PlatformForm(forms.ModelForm, BootstrapMixin):
  266. slug = SlugField()
  267. class Meta:
  268. model = Platform
  269. fields = ['name', 'slug']
  270. #
  271. # Devices
  272. #
  273. class DeviceForm(BootstrapMixin, CustomFieldForm):
  274. site = forms.ModelChoiceField(queryset=Site.objects.all(), widget=forms.Select(attrs={'filter-for': 'rack'}))
  275. rack = forms.ModelChoiceField(queryset=Rack.objects.all(), widget=APISelect(
  276. api_url='/api/dcim/racks/?site_id={{site}}',
  277. display_field='display_name',
  278. attrs={'filter-for': 'position'}
  279. ))
  280. position = forms.TypedChoiceField(required=False, empty_value=None,
  281. help_text="For multi-U devices, this is the lowest occupied rack unit.",
  282. widget=APISelect(api_url='/api/dcim/racks/{{rack}}/rack-units/?face={{face}}',
  283. disabled_indicator='device'))
  284. manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all(),
  285. widget=forms.Select(attrs={'filter-for': 'device_type'}))
  286. device_type = forms.ModelChoiceField(queryset=DeviceType.objects.all(), label='Device type', widget=APISelect(
  287. api_url='/api/dcim/device-types/?manufacturer_id={{manufacturer}}',
  288. display_field='model'
  289. ))
  290. comments = CommentField()
  291. class Meta:
  292. model = Device
  293. fields = ['name', 'device_role', 'tenant', 'device_type', 'serial', 'asset_tag', 'site', 'rack', 'position',
  294. 'face', 'status', 'platform', 'primary_ip4', 'primary_ip6', 'comments']
  295. help_texts = {
  296. 'device_role': "The function this device serves",
  297. 'serial': "Chassis serial number",
  298. }
  299. widgets = {
  300. 'face': forms.Select(attrs={'filter-for': 'position'}),
  301. 'manufacturer': forms.Select(attrs={'filter-for': 'device_type'}),
  302. }
  303. def __init__(self, *args, **kwargs):
  304. super(DeviceForm, self).__init__(*args, **kwargs)
  305. if self.instance.pk:
  306. # Initialize helper selections
  307. self.initial['site'] = self.instance.rack.site
  308. self.initial['manufacturer'] = self.instance.device_type.manufacturer
  309. # Compile list of choices for primary IPv4 and IPv6 addresses
  310. for family in [4, 6]:
  311. ip_choices = []
  312. interface_ips = IPAddress.objects.filter(family=family, interface__device=self.instance)
  313. ip_choices += [(ip.id, u'{} ({})'.format(ip.address, ip.interface)) for ip in interface_ips]
  314. nat_ips = IPAddress.objects.filter(family=family, nat_inside__interface__device=self.instance)\
  315. .select_related('nat_inside__interface')
  316. ip_choices += [(ip.id, u'{} ({} NAT)'.format(ip.address, ip.nat_inside.interface)) for ip in nat_ips]
  317. self.fields['primary_ip{}'.format(family)].choices = [(None, '---------')] + ip_choices
  318. else:
  319. # An object that doesn't exist yet can't have any IPs assigned to it
  320. self.fields['primary_ip4'].choices = []
  321. self.fields['primary_ip4'].widget.attrs['readonly'] = True
  322. self.fields['primary_ip6'].choices = []
  323. self.fields['primary_ip6'].widget.attrs['readonly'] = True
  324. # Limit rack choices
  325. if self.is_bound and self.data.get('site'):
  326. self.fields['rack'].queryset = Rack.objects.filter(site__pk=self.data['site'])
  327. elif self.initial.get('site'):
  328. self.fields['rack'].queryset = Rack.objects.filter(site=self.initial['site'])
  329. else:
  330. self.fields['rack'].choices = []
  331. # Rack position
  332. pk = self.instance.pk if self.instance.pk else None
  333. try:
  334. if self.is_bound and self.data.get('rack') and str(self.data.get('face')):
  335. position_choices = Rack.objects.get(pk=self.data['rack'])\
  336. .get_rack_units(face=self.data.get('face'), exclude=pk)
  337. elif self.initial.get('rack') and str(self.initial.get('face')):
  338. position_choices = Rack.objects.get(pk=self.initial['rack'])\
  339. .get_rack_units(face=self.initial.get('face'), exclude=pk)
  340. else:
  341. position_choices = []
  342. except Rack.DoesNotExist:
  343. position_choices = []
  344. self.fields['position'].choices = [('', '---------')] + [
  345. (p['id'], {
  346. 'label': p['name'],
  347. 'disabled': bool(p['device'] and p['id'] != self.initial.get('position')),
  348. }) for p in position_choices
  349. ]
  350. # Limit device_type choices
  351. if self.is_bound:
  352. self.fields['device_type'].queryset = DeviceType.objects.filter(manufacturer__pk=self.data['manufacturer'])\
  353. .select_related('manufacturer')
  354. elif self.initial.get('manufacturer'):
  355. self.fields['device_type'].queryset = DeviceType.objects.filter(manufacturer=self.initial['manufacturer'])\
  356. .select_related('manufacturer')
  357. else:
  358. self.fields['device_type'].choices = []
  359. # Disable rack assignment if this is a child device installed in a parent device
  360. if pk and self.instance.device_type.is_child_device and hasattr(self.instance, 'parent_bay'):
  361. self.fields['site'].disabled = True
  362. self.fields['rack'].disabled = True
  363. self.initial['site'] = self.instance.parent_bay.device.rack.site_id
  364. self.initial['rack'] = self.instance.parent_bay.device.rack_id
  365. class BaseDeviceFromCSVForm(forms.ModelForm):
  366. device_role = forms.ModelChoiceField(queryset=DeviceRole.objects.all(), to_field_name='name',
  367. error_messages={'invalid_choice': 'Invalid device role.'})
  368. tenant = forms.ModelChoiceField(Tenant.objects.all(), to_field_name='name', required=False,
  369. error_messages={'invalid_choice': 'Tenant not found.'})
  370. manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all(), to_field_name='name',
  371. error_messages={'invalid_choice': 'Invalid manufacturer.'})
  372. model_name = forms.CharField()
  373. platform = forms.ModelChoiceField(queryset=Platform.objects.all(), required=False, to_field_name='name',
  374. error_messages={'invalid_choice': 'Invalid platform.'})
  375. class Meta:
  376. fields = []
  377. model = Device
  378. def clean(self):
  379. manufacturer = self.cleaned_data.get('manufacturer')
  380. model_name = self.cleaned_data.get('model_name')
  381. # Validate device type
  382. if manufacturer and model_name:
  383. try:
  384. self.instance.device_type = DeviceType.objects.get(manufacturer=manufacturer, model=model_name)
  385. except DeviceType.DoesNotExist:
  386. self.add_error('model_name', "Invalid device type ({} {})".format(manufacturer, model_name))
  387. class DeviceFromCSVForm(BaseDeviceFromCSVForm):
  388. site = forms.ModelChoiceField(queryset=Site.objects.all(), to_field_name='name', error_messages={
  389. 'invalid_choice': 'Invalid site name.',
  390. })
  391. rack_name = forms.CharField()
  392. face = forms.CharField(required=False)
  393. class Meta(BaseDeviceFromCSVForm.Meta):
  394. fields = ['name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag',
  395. 'site', 'rack_name', 'position', 'face']
  396. def clean(self):
  397. super(DeviceFromCSVForm, self).clean()
  398. site = self.cleaned_data.get('site')
  399. rack_name = self.cleaned_data.get('rack_name')
  400. # Validate rack
  401. if site and rack_name:
  402. try:
  403. self.instance.rack = Rack.objects.get(site=site, name=rack_name)
  404. except Rack.DoesNotExist:
  405. self.add_error('rack_name', "Invalid rack ({})".format(rack_name))
  406. def clean_face(self):
  407. face = self.cleaned_data['face']
  408. if not face:
  409. return None
  410. try:
  411. return {
  412. 'front': 0,
  413. 'rear': 1,
  414. }[face.lower()]
  415. except KeyError:
  416. raise forms.ValidationError('Invalid rack face ({}); must be "front" or "rear".'.format(face))
  417. class ChildDeviceFromCSVForm(BaseDeviceFromCSVForm):
  418. parent = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name', required=False,
  419. error_messages={'invalid_choice': 'Parent device not found.'})
  420. device_bay_name = forms.CharField(required=False)
  421. class Meta(BaseDeviceFromCSVForm.Meta):
  422. fields = ['name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag',
  423. 'parent', 'device_bay_name']
  424. def clean(self):
  425. super(ChildDeviceFromCSVForm, self).clean()
  426. parent = self.cleaned_data.get('parent')
  427. device_bay_name = self.cleaned_data.get('device_bay_name')
  428. # Validate device bay
  429. if parent and device_bay_name:
  430. try:
  431. device_bay = DeviceBay.objects.get(device=parent, name=device_bay_name)
  432. if device_bay.installed_device:
  433. self.add_error('device_bay_name',
  434. "Device bay ({} {}) is already occupied".format(parent, device_bay_name))
  435. else:
  436. self.instance.parent_bay = device_bay
  437. except DeviceBay.DoesNotExist:
  438. self.add_error('device_bay_name', "Parent device/bay ({} {}) not found".format(parent, device_bay_name))
  439. class DeviceImportForm(BulkImportForm, BootstrapMixin):
  440. csv = CSVDataField(csv_form=DeviceFromCSVForm)
  441. class ChildDeviceImportForm(BulkImportForm, BootstrapMixin):
  442. csv = CSVDataField(csv_form=ChildDeviceFromCSVForm)
  443. class DeviceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
  444. pk = forms.ModelMultipleChoiceField(queryset=Device.objects.all(), widget=forms.MultipleHiddenInput)
  445. device_type = forms.ModelChoiceField(queryset=DeviceType.objects.all(), required=False, label='Type')
  446. device_role = forms.ModelChoiceField(queryset=DeviceRole.objects.all(), required=False, label='Role')
  447. tenant = forms.TypedChoiceField(choices=bulkedit_tenant_choices, coerce=int, required=False, label='Tenant')
  448. platform = forms.TypedChoiceField(choices=bulkedit_platform_choices, coerce=int, required=False,
  449. label='Platform')
  450. status = forms.ChoiceField(choices=FORM_STATUS_CHOICES, required=False, initial='', label='Status')
  451. serial = forms.CharField(max_length=50, required=False, label='Serial Number')
  452. class DeviceFilterForm(BootstrapMixin, CustomFieldFilterForm):
  453. model = Device
  454. site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('racks__devices')), to_field_name='slug')
  455. rack_group_id = FilterChoiceField(queryset=RackGroup.objects.annotate(filter_count=Count('racks__devices')),
  456. label='Rack Group')
  457. role = FilterChoiceField(queryset=DeviceRole.objects.annotate(filter_count=Count('devices')), to_field_name='slug')
  458. tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('devices')), to_field_name='slug',
  459. null_option=(0, 'None'))
  460. device_type_id = FilterChoiceField(queryset=DeviceType.objects.select_related('manufacturer')
  461. .annotate(filter_count=Count('instances')), label='Type')
  462. platform = FilterChoiceField(queryset=Platform.objects.annotate(filter_count=Count('devices')),
  463. to_field_name='slug', null_option=(0, 'None'))
  464. status = forms.NullBooleanField(required=False, widget=forms.Select(choices=FORM_STATUS_CHOICES))
  465. #
  466. # Console ports
  467. #
  468. class ConsolePortForm(forms.ModelForm, BootstrapMixin):
  469. class Meta:
  470. model = ConsolePort
  471. fields = ['device', 'name']
  472. widgets = {
  473. 'device': forms.HiddenInput(),
  474. }
  475. class ConsolePortCreateForm(forms.Form, BootstrapMixin):
  476. name_pattern = ExpandableNameField(label='Name')
  477. class ConsoleConnectionCSVForm(forms.Form):
  478. console_server = FlexibleModelChoiceField(queryset=Device.objects.filter(device_type__is_console_server=True),
  479. to_field_name='name',
  480. error_messages={'invalid_choice': 'Console server not found'})
  481. cs_port = forms.CharField()
  482. device = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
  483. error_messages={'invalid_choice': 'Device not found'})
  484. console_port = forms.CharField()
  485. status = forms.ChoiceField(choices=[('planned', 'Planned'), ('connected', 'Connected')])
  486. def clean(self):
  487. # Validate console server port
  488. if self.cleaned_data.get('console_server'):
  489. try:
  490. cs_port = ConsoleServerPort.objects.get(device=self.cleaned_data['console_server'],
  491. name=self.cleaned_data['cs_port'])
  492. if ConsolePort.objects.filter(cs_port=cs_port):
  493. raise forms.ValidationError("Console server port is already occupied (by {} {})"
  494. .format(cs_port.connected_console.device, cs_port.connected_console))
  495. except ConsoleServerPort.DoesNotExist:
  496. raise forms.ValidationError("Invalid console server port ({} {})"
  497. .format(self.cleaned_data['console_server'], self.cleaned_data['cs_port']))
  498. # Validate console port
  499. if self.cleaned_data.get('device'):
  500. try:
  501. console_port = ConsolePort.objects.get(device=self.cleaned_data['device'],
  502. name=self.cleaned_data['console_port'])
  503. if console_port.cs_port:
  504. raise forms.ValidationError("Console port is already connected (to {} {})"
  505. .format(console_port.cs_port.device, console_port.cs_port))
  506. except ConsolePort.DoesNotExist:
  507. raise forms.ValidationError("Invalid console port ({} {})"
  508. .format(self.cleaned_data['device'], self.cleaned_data['console_port']))
  509. class ConsoleConnectionImportForm(BulkImportForm, BootstrapMixin):
  510. csv = CSVDataField(csv_form=ConsoleConnectionCSVForm)
  511. def clean(self):
  512. records = self.cleaned_data.get('csv')
  513. if not records:
  514. return
  515. connection_list = []
  516. for i, record in enumerate(records, start=1):
  517. form = self.fields['csv'].csv_form(data=record)
  518. if form.is_valid():
  519. console_port = ConsolePort.objects.get(device=form.cleaned_data['device'],
  520. name=form.cleaned_data['console_port'])
  521. console_port.cs_port = ConsoleServerPort.objects.get(device=form.cleaned_data['console_server'],
  522. name=form.cleaned_data['cs_port'])
  523. if form.cleaned_data['status'] == 'planned':
  524. console_port.connection_status = CONNECTION_STATUS_PLANNED
  525. else:
  526. console_port.connection_status = CONNECTION_STATUS_CONNECTED
  527. connection_list.append(console_port)
  528. else:
  529. for field, errors in form.errors.items():
  530. for e in errors:
  531. self.add_error('csv', "Record {} {}: {}".format(i, field, e))
  532. self.cleaned_data['csv'] = connection_list
  533. class ConsolePortConnectionForm(forms.ModelForm, BootstrapMixin):
  534. rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
  535. widget=forms.Select(attrs={'filter-for': 'console_server'}))
  536. console_server = forms.ModelChoiceField(queryset=Device.objects.all(), label='Console Server', required=False,
  537. widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}&is_console_server=True',
  538. attrs={'filter-for': 'cs_port'}))
  539. livesearch = forms.CharField(required=False, label='Console Server', widget=Livesearch(
  540. query_key='q', query_url='dcim-api:device_list', field_to_update='console_server')
  541. )
  542. cs_port = forms.ModelChoiceField(queryset=ConsoleServerPort.objects.all(), label='Port',
  543. widget=APISelect(api_url='/api/dcim/devices/{{console_server}}/console-server-ports/',
  544. disabled_indicator='connected_console'))
  545. class Meta:
  546. model = ConsolePort
  547. fields = ['rack', 'console_server', 'livesearch', 'cs_port', 'connection_status']
  548. labels = {
  549. 'cs_port': 'Port',
  550. 'connection_status': 'Status',
  551. }
  552. def __init__(self, *args, **kwargs):
  553. super(ConsolePortConnectionForm, self).__init__(*args, **kwargs)
  554. if not self.instance.pk:
  555. raise RuntimeError("ConsolePortConnectionForm must be initialized with an existing ConsolePort instance.")
  556. self.fields['rack'].queryset = Rack.objects.filter(site=self.instance.device.rack.site)
  557. self.fields['cs_port'].required = True
  558. self.fields['connection_status'].choices = CONNECTION_STATUS_CHOICES
  559. # Initialize console server choices
  560. if self.is_bound and self.data.get('rack'):
  561. self.fields['console_server'].queryset = Device.objects.filter(rack=self.data['rack'], device_type__is_console_server=True)
  562. elif self.initial.get('rack'):
  563. self.fields['console_server'].queryset = Device.objects.filter(rack=self.initial['rack'], device_type__is_console_server=True)
  564. else:
  565. self.fields['console_server'].choices = []
  566. # Initialize CS port choices
  567. if self.is_bound:
  568. self.fields['cs_port'].queryset = ConsoleServerPort.objects.filter(device__pk=self.data['console_server'])
  569. elif self.initial.get('console_server', None):
  570. self.fields['cs_port'].queryset = ConsoleServerPort.objects.filter(device__pk=self.initial['console_server'])
  571. else:
  572. self.fields['cs_port'].choices = []
  573. #
  574. # Console server ports
  575. #
  576. class ConsoleServerPortForm(forms.ModelForm, BootstrapMixin):
  577. class Meta:
  578. model = ConsoleServerPort
  579. fields = ['device', 'name']
  580. widgets = {
  581. 'device': forms.HiddenInput(),
  582. }
  583. class ConsoleServerPortCreateForm(forms.Form, BootstrapMixin):
  584. name_pattern = ExpandableNameField(label='Name')
  585. class ConsoleServerPortConnectionForm(forms.Form, BootstrapMixin):
  586. rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
  587. widget=forms.Select(attrs={'filter-for': 'device'}))
  588. device = forms.ModelChoiceField(queryset=Device.objects.all(), label='Device', required=False,
  589. widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}',
  590. attrs={'filter-for': 'port'}))
  591. livesearch = forms.CharField(required=False, label='Device', widget=Livesearch(
  592. query_key='q', query_url='dcim-api:device_list', field_to_update='device')
  593. )
  594. port = forms.ModelChoiceField(queryset=ConsolePort.objects.all(), label='Port',
  595. widget=APISelect(api_url='/api/dcim/devices/{{device}}/console-ports/',
  596. disabled_indicator='cs_port'))
  597. connection_status = forms.BooleanField(required=False, initial=CONNECTION_STATUS_CONNECTED, label='Status',
  598. widget=forms.Select(choices=CONNECTION_STATUS_CHOICES))
  599. class Meta:
  600. fields = ['rack', 'device', 'livesearch', 'port', 'connection_status']
  601. labels = {
  602. 'connection_status': 'Status',
  603. }
  604. def __init__(self, consoleserverport, *args, **kwargs):
  605. super(ConsoleServerPortConnectionForm, self).__init__(*args, **kwargs)
  606. self.fields['rack'].queryset = Rack.objects.filter(site=consoleserverport.device.rack.site)
  607. # Initialize device choices
  608. if self.is_bound and self.data.get('rack'):
  609. self.fields['device'].queryset = Device.objects.filter(rack=self.data['rack'])
  610. elif self.initial.get('rack', None):
  611. self.fields['device'].queryset = Device.objects.filter(rack=self.initial['rack'])
  612. else:
  613. self.fields['device'].choices = []
  614. # Initialize port choices
  615. if self.is_bound:
  616. self.fields['port'].queryset = ConsolePort.objects.filter(device__pk=self.data['device'])
  617. elif self.initial.get('device', None):
  618. self.fields['port'].queryset = ConsolePort.objects.filter(device_pk=self.initial['device'])
  619. else:
  620. self.fields['port'].choices = []
  621. #
  622. # Power ports
  623. #
  624. class PowerPortForm(forms.ModelForm, BootstrapMixin):
  625. class Meta:
  626. model = PowerPort
  627. fields = ['device', 'name']
  628. widgets = {
  629. 'device': forms.HiddenInput(),
  630. }
  631. class PowerPortCreateForm(forms.Form, BootstrapMixin):
  632. name_pattern = ExpandableNameField(label='Name')
  633. class PowerConnectionCSVForm(forms.Form):
  634. pdu = FlexibleModelChoiceField(queryset=Device.objects.filter(device_type__is_pdu=True), to_field_name='name',
  635. error_messages={'invalid_choice': 'PDU not found.'})
  636. power_outlet = forms.CharField()
  637. device = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
  638. error_messages={'invalid_choice': 'Device not found'})
  639. power_port = forms.CharField()
  640. status = forms.ChoiceField(choices=[('planned', 'Planned'), ('connected', 'Connected')])
  641. def clean(self):
  642. # Validate power outlet
  643. if self.cleaned_data.get('pdu'):
  644. try:
  645. power_outlet = PowerOutlet.objects.get(device=self.cleaned_data['pdu'],
  646. name=self.cleaned_data['power_outlet'])
  647. if PowerPort.objects.filter(power_outlet=power_outlet):
  648. raise forms.ValidationError("Power outlet is already occupied (by {} {})"
  649. .format(power_outlet.connected_port.device,
  650. power_outlet.connected_port))
  651. except PowerOutlet.DoesNotExist:
  652. raise forms.ValidationError("Invalid PDU port ({} {})"
  653. .format(self.cleaned_data['pdu'], self.cleaned_data['power_outlet']))
  654. # Validate power port
  655. if self.cleaned_data.get('device'):
  656. try:
  657. power_port = PowerPort.objects.get(device=self.cleaned_data['device'],
  658. name=self.cleaned_data['power_port'])
  659. if power_port.power_outlet:
  660. raise forms.ValidationError("Power port is already connected (to {} {})"
  661. .format(power_port.power_outlet.device, power_port.power_outlet))
  662. except PowerPort.DoesNotExist:
  663. raise forms.ValidationError("Invalid power port ({} {})"
  664. .format(self.cleaned_data['device'], self.cleaned_data['power_port']))
  665. class PowerConnectionImportForm(BulkImportForm, BootstrapMixin):
  666. csv = CSVDataField(csv_form=PowerConnectionCSVForm)
  667. def clean(self):
  668. records = self.cleaned_data.get('csv')
  669. if not records:
  670. return
  671. connection_list = []
  672. for i, record in enumerate(records, start=1):
  673. form = self.fields['csv'].csv_form(data=record)
  674. if form.is_valid():
  675. power_port = PowerPort.objects.get(device=form.cleaned_data['device'],
  676. name=form.cleaned_data['power_port'])
  677. power_port.power_outlet = PowerOutlet.objects.get(device=form.cleaned_data['pdu'],
  678. name=form.cleaned_data['power_outlet'])
  679. if form.cleaned_data['status'] == 'planned':
  680. power_port.connection_status = CONNECTION_STATUS_PLANNED
  681. else:
  682. power_port.connection_status = CONNECTION_STATUS_CONNECTED
  683. connection_list.append(power_port)
  684. else:
  685. for field, errors in form.errors.items():
  686. for e in errors:
  687. self.add_error('csv', "Record {} {}: {}".format(i, field, e))
  688. self.cleaned_data['csv'] = connection_list
  689. class PowerPortConnectionForm(forms.ModelForm, BootstrapMixin):
  690. rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
  691. widget=forms.Select(attrs={'filter-for': 'pdu'}))
  692. pdu = forms.ModelChoiceField(queryset=Device.objects.all(), label='PDU', required=False,
  693. widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}&is_pdu=True',
  694. attrs={'filter-for': 'power_outlet'}))
  695. livesearch = forms.CharField(required=False, label='PDU', widget=Livesearch(
  696. query_key='q', query_url='dcim-api:device_list', field_to_update='pdu')
  697. )
  698. power_outlet = forms.ModelChoiceField(queryset=PowerOutlet.objects.all(), label='Outlet',
  699. widget=APISelect(api_url='/api/dcim/devices/{{pdu}}/power-outlets/',
  700. disabled_indicator='connected_port'))
  701. class Meta:
  702. model = PowerPort
  703. fields = ['rack', 'pdu', 'livesearch', 'power_outlet', 'connection_status']
  704. labels = {
  705. 'power_outlet': 'Outlet',
  706. 'connection_status': 'Status',
  707. }
  708. def __init__(self, *args, **kwargs):
  709. super(PowerPortConnectionForm, self).__init__(*args, **kwargs)
  710. if not self.instance.pk:
  711. raise RuntimeError("PowerPortConnectionForm must be initialized with an existing PowerPort instance.")
  712. self.fields['rack'].queryset = Rack.objects.filter(site=self.instance.device.rack.site)
  713. self.fields['power_outlet'].required = True
  714. self.fields['connection_status'].choices = CONNECTION_STATUS_CHOICES
  715. # Initialize PDU choices
  716. if self.is_bound and self.data.get('rack'):
  717. self.fields['pdu'].queryset = Device.objects.filter(rack=self.data['rack'], device_type__is_pdu=True)
  718. elif self.initial.get('rack', None):
  719. self.fields['pdu'].queryset = Device.objects.filter(rack=self.initial['rack'], device_type__is_pdu=True)
  720. else:
  721. self.fields['pdu'].choices = []
  722. # Initialize power outlet choices
  723. if self.is_bound:
  724. self.fields['power_outlet'].queryset = PowerOutlet.objects.filter(device__pk=self.data['pdu'])
  725. elif self.initial.get('pdu', None):
  726. self.fields['power_outlet'].queryset = PowerOutlet.objects.filter(device__pk=self.initial['pdu'])
  727. else:
  728. self.fields['power_outlet'].choices = []
  729. #
  730. # Power outlets
  731. #
  732. class PowerOutletForm(forms.ModelForm, BootstrapMixin):
  733. class Meta:
  734. model = PowerOutlet
  735. fields = ['device', 'name']
  736. widgets = {
  737. 'device': forms.HiddenInput(),
  738. }
  739. class PowerOutletCreateForm(forms.Form, BootstrapMixin):
  740. name_pattern = ExpandableNameField(label='Name')
  741. class PowerOutletConnectionForm(forms.Form, BootstrapMixin):
  742. rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
  743. widget=forms.Select(attrs={'filter-for': 'device'}))
  744. device = forms.ModelChoiceField(queryset=Device.objects.all(), label='Device', required=False,
  745. widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}',
  746. attrs={'filter-for': 'port'}))
  747. livesearch = forms.CharField(required=False, label='Device', widget=Livesearch(
  748. query_key='q', query_url='dcim-api:device_list', field_to_update='device')
  749. )
  750. port = forms.ModelChoiceField(queryset=PowerPort.objects.all(), label='Port',
  751. widget=APISelect(api_url='/api/dcim/devices/{{device}}/power-ports/',
  752. disabled_indicator='power_outlet'))
  753. connection_status = forms.BooleanField(required=False, initial=CONNECTION_STATUS_CONNECTED, label='Status',
  754. widget=forms.Select(choices=CONNECTION_STATUS_CHOICES))
  755. class Meta:
  756. fields = ['rack', 'device', 'livesearch', 'port', 'connection_status']
  757. labels = {
  758. 'connection_status': 'Status',
  759. }
  760. def __init__(self, poweroutlet, *args, **kwargs):
  761. super(PowerOutletConnectionForm, self).__init__(*args, **kwargs)
  762. self.fields['rack'].queryset = Rack.objects.filter(site=poweroutlet.device.rack.site)
  763. # Initialize device choices
  764. if self.is_bound and self.data.get('rack'):
  765. self.fields['device'].queryset = Device.objects.filter(rack=self.data['rack'])
  766. elif self.initial.get('rack', None):
  767. self.fields['device'].queryset = Device.objects.filter(rack=self.initial['rack'])
  768. else:
  769. self.fields['device'].choices = []
  770. # Initialize port choices
  771. if self.is_bound:
  772. self.fields['port'].queryset = PowerPort.objects.filter(device__pk=self.data['device'])
  773. elif self.initial.get('device', None):
  774. self.fields['port'].queryset = PowerPort.objects.filter(device_pk=self.initial['device'])
  775. else:
  776. self.fields['port'].choices = []
  777. #
  778. # Interfaces
  779. #
  780. class InterfaceForm(forms.ModelForm, BootstrapMixin):
  781. class Meta:
  782. model = Interface
  783. fields = ['device', 'name', 'form_factor', 'mac_address', 'mgmt_only', 'description']
  784. widgets = {
  785. 'device': forms.HiddenInput(),
  786. }
  787. class InterfaceCreateForm(forms.ModelForm, BootstrapMixin):
  788. name_pattern = ExpandableNameField(label='Name')
  789. class Meta:
  790. model = Interface
  791. fields = ['name_pattern', 'form_factor', 'mac_address', 'mgmt_only', 'description']
  792. class InterfaceBulkCreateForm(InterfaceCreateForm, BootstrapMixin):
  793. pk = forms.ModelMultipleChoiceField(queryset=Device.objects.all(), widget=forms.MultipleHiddenInput)
  794. #
  795. # Interface connections
  796. #
  797. class InterfaceConnectionForm(forms.ModelForm, BootstrapMixin):
  798. interface_a = forms.ChoiceField(choices=[], widget=SelectWithDisabled, label='Interface')
  799. rack_b = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
  800. widget=forms.Select(attrs={'filter-for': 'device_b'}))
  801. device_b = forms.ModelChoiceField(queryset=Device.objects.all(), label='Device', required=False,
  802. widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack_b}}',
  803. attrs={'filter-for': 'interface_b'}))
  804. livesearch = forms.CharField(required=False, label='Device', widget=Livesearch(
  805. query_key='q', query_url='dcim-api:device_list', field_to_update='device_b')
  806. )
  807. interface_b = forms.ModelChoiceField(queryset=Interface.objects.all(), label='Interface',
  808. widget=APISelect(api_url='/api/dcim/devices/{{device_b}}/interfaces/?type=physical',
  809. disabled_indicator='is_connected'))
  810. class Meta:
  811. model = InterfaceConnection
  812. fields = ['interface_a', 'rack_b', 'device_b', 'interface_b', 'livesearch', 'connection_status']
  813. def __init__(self, device_a, *args, **kwargs):
  814. super(InterfaceConnectionForm, self).__init__(*args, **kwargs)
  815. self.fields['rack_b'].queryset = Rack.objects.filter(site=device_a.rack.site)
  816. # Initialize interface A choices
  817. device_a_interfaces = Interface.objects.filter(device=device_a).exclude(form_factor=IFACE_FF_VIRTUAL) \
  818. .select_related('circuit', 'connected_as_a', 'connected_as_b')
  819. self.fields['interface_a'].choices = [
  820. (iface.id, {'label': iface.name, 'disabled': iface.is_connected}) for iface in device_a_interfaces
  821. ]
  822. # Initialize device_b choices if rack_b is set
  823. if self.is_bound and self.data.get('rack_b'):
  824. self.fields['device_b'].queryset = Device.objects.filter(rack__pk=self.data['rack_b'])
  825. elif self.initial.get('rack_b'):
  826. self.fields['device_b'].queryset = Device.objects.filter(rack=self.initial['rack_b'])
  827. else:
  828. self.fields['device_b'].choices = []
  829. # Initialize interface_b choices if device_b is set
  830. if self.is_bound:
  831. device_b_interfaces = Interface.objects.filter(device=self.data['device_b']) \
  832. .exclude(form_factor=IFACE_FF_VIRTUAL).select_related('circuit', 'connected_as_a', 'connected_as_b')
  833. elif self.initial.get('device_b'):
  834. device_b_interfaces = Interface.objects.filter(device=self.initial['device_b']) \
  835. .exclude(form_factor=IFACE_FF_VIRTUAL).select_related('circuit', 'connected_as_a', 'connected_as_b')
  836. else:
  837. device_b_interfaces = []
  838. self.fields['interface_b'].choices = [
  839. (iface.id, {'label': iface.name, 'disabled': iface.is_connected}) for iface in device_b_interfaces
  840. ]
  841. class InterfaceConnectionCSVForm(forms.Form):
  842. device_a = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
  843. error_messages={'invalid_choice': 'Device A not found.'})
  844. interface_a = forms.CharField()
  845. device_b = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
  846. error_messages={'invalid_choice': 'Device B not found.'})
  847. interface_b = forms.CharField()
  848. status = forms.ChoiceField(choices=[('planned', 'Planned'), ('connected', 'Connected')])
  849. def clean(self):
  850. # Validate interface A
  851. if self.cleaned_data.get('device_a'):
  852. try:
  853. interface_a = Interface.objects.get(device=self.cleaned_data['device_a'],
  854. name=self.cleaned_data['interface_a'])
  855. except Interface.DoesNotExist:
  856. raise forms.ValidationError("Invalid interface ({} {})"
  857. .format(self.cleaned_data['device_a'], self.cleaned_data['interface_a']))
  858. try:
  859. InterfaceConnection.objects.get(Q(interface_a=interface_a) | Q(interface_b=interface_a))
  860. raise forms.ValidationError("{} {} is already connected"
  861. .format(self.cleaned_data['device_a'], self.cleaned_data['interface_a']))
  862. except InterfaceConnection.DoesNotExist:
  863. pass
  864. # Validate interface B
  865. if self.cleaned_data.get('device_b'):
  866. try:
  867. interface_b = Interface.objects.get(device=self.cleaned_data['device_b'],
  868. name=self.cleaned_data['interface_b'])
  869. except Interface.DoesNotExist:
  870. raise forms.ValidationError("Invalid interface ({} {})"
  871. .format(self.cleaned_data['device_b'], self.cleaned_data['interface_b']))
  872. try:
  873. InterfaceConnection.objects.get(Q(interface_a=interface_b) | Q(interface_b=interface_b))
  874. raise forms.ValidationError("{} {} is already connected"
  875. .format(self.cleaned_data['device_b'], self.cleaned_data['interface_b']))
  876. except InterfaceConnection.DoesNotExist:
  877. pass
  878. class InterfaceConnectionImportForm(BulkImportForm, BootstrapMixin):
  879. csv = CSVDataField(csv_form=InterfaceConnectionCSVForm)
  880. def clean(self):
  881. records = self.cleaned_data.get('csv')
  882. if not records:
  883. return
  884. connection_list = []
  885. occupied_interfaces = []
  886. for i, record in enumerate(records, start=1):
  887. form = self.fields['csv'].csv_form(data=record)
  888. if form.is_valid():
  889. interface_a = Interface.objects.get(device=form.cleaned_data['device_a'],
  890. name=form.cleaned_data['interface_a'])
  891. if interface_a in occupied_interfaces:
  892. raise forms.ValidationError("{} {} found in multiple connections"
  893. .format(interface_a.device.name, interface_a.name))
  894. interface_b = Interface.objects.get(device=form.cleaned_data['device_b'],
  895. name=form.cleaned_data['interface_b'])
  896. if interface_b in occupied_interfaces:
  897. raise forms.ValidationError("{} {} found in multiple connections"
  898. .format(interface_b.device.name, interface_b.name))
  899. connection = InterfaceConnection(interface_a=interface_a, interface_b=interface_b)
  900. if form.cleaned_data['status'] == 'planned':
  901. connection.connection_status = CONNECTION_STATUS_PLANNED
  902. else:
  903. connection.connection_status = CONNECTION_STATUS_CONNECTED
  904. connection_list.append(connection)
  905. occupied_interfaces.append(interface_a)
  906. occupied_interfaces.append(interface_b)
  907. else:
  908. for field, errors in form.errors.items():
  909. for e in errors:
  910. self.add_error('csv', "Record {} {}: {}".format(i, field, e))
  911. self.cleaned_data['csv'] = connection_list
  912. class InterfaceConnectionDeletionForm(forms.Form, BootstrapMixin):
  913. confirm = forms.BooleanField(required=True)
  914. # Used for HTTP redirect upon successful deletion
  915. device = forms.ModelChoiceField(queryset=Device.objects.all(), widget=forms.HiddenInput(), required=False)
  916. #
  917. # Device bays
  918. #
  919. class DeviceBayForm(forms.ModelForm, BootstrapMixin):
  920. class Meta:
  921. model = DeviceBay
  922. fields = ['device', 'name']
  923. widgets = {
  924. 'device': forms.HiddenInput(),
  925. }
  926. class DeviceBayCreateForm(forms.Form, BootstrapMixin):
  927. name_pattern = ExpandableNameField(label='Name')
  928. class PopulateDeviceBayForm(forms.Form, BootstrapMixin):
  929. installed_device = forms.ModelChoiceField(queryset=Device.objects.all(), label='Child Device',
  930. help_text="Child devices must first be created within the rack occupied "
  931. "by the parent device. Then they can be assigned to a bay.")
  932. def __init__(self, device_bay, *args, **kwargs):
  933. super(PopulateDeviceBayForm, self).__init__(*args, **kwargs)
  934. children_queryset = Device.objects.filter(rack=device_bay.device.rack,
  935. parent_bay__isnull=True,
  936. device_type__u_height=0,
  937. device_type__subdevice_role=SUBDEVICE_ROLE_CHILD)\
  938. .exclude(pk=device_bay.device.pk)
  939. self.fields['installed_device'].queryset = children_queryset
  940. #
  941. # Connections
  942. #
  943. class ConsoleConnectionFilterForm(forms.Form, BootstrapMixin):
  944. site = forms.ModelChoiceField(required=False, queryset=Site.objects.all(), to_field_name='slug')
  945. class PowerConnectionFilterForm(forms.Form, BootstrapMixin):
  946. site = forms.ModelChoiceField(required=False, queryset=Site.objects.all(), to_field_name='slug')
  947. class InterfaceConnectionFilterForm(forms.Form, BootstrapMixin):
  948. site = forms.ModelChoiceField(required=False, queryset=Site.objects.all(), to_field_name='slug')
  949. #
  950. # IP addresses
  951. #
  952. class IPAddressForm(forms.ModelForm, BootstrapMixin):
  953. set_as_primary = forms.BooleanField(label='Set as primary IP for device', required=False)
  954. class Meta:
  955. model = IPAddress
  956. fields = ['address', 'vrf', 'interface', 'set_as_primary']
  957. help_texts = {
  958. 'address': 'IPv4 or IPv6 address (with mask)'
  959. }
  960. def __init__(self, device, *args, **kwargs):
  961. super(IPAddressForm, self).__init__(*args, **kwargs)
  962. self.fields['vrf'].empty_label = 'Global'
  963. self.fields['interface'].queryset = device.interfaces.all()
  964. self.fields['interface'].required = True
  965. # If this device does not have any IP addresses assigned, default to setting the first IP as its primary
  966. if not IPAddress.objects.filter(interface__device=device).count():
  967. self.fields['set_as_primary'].initial = True
  968. #
  969. # Interfaces
  970. #
  971. class ModuleForm(forms.ModelForm, BootstrapMixin):
  972. class Meta:
  973. model = Module
  974. fields = ['name', 'manufacturer', 'part_id', 'serial']