models.py 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. from collections import OrderedDict
  2. from datetime import date
  3. import graphviz
  4. from django.contrib.auth.models import User
  5. from django.contrib.contenttypes.fields import GenericForeignKey
  6. from django.contrib.contenttypes.models import ContentType
  7. from django.contrib.postgres.fields import JSONField
  8. from django.core.validators import ValidationError
  9. from django.db import models
  10. from django.db.models import F, Q
  11. from django.http import HttpResponse
  12. from django.template import Template, Context
  13. from django.urls import reverse
  14. from jinja2 import Environment
  15. from taggit.models import TagBase, GenericTaggedItemBase
  16. from dcim.constants import CONNECTION_STATUS_CONNECTED
  17. from utilities.fields import ColorField
  18. from utilities.utils import deepmerge, foreground_color, model_names_to_filter_dict
  19. from .constants import *
  20. from .querysets import ConfigContextQuerySet
  21. #
  22. # Webhooks
  23. #
  24. def get_webhook_models():
  25. return model_names_to_filter_dict(WEBHOOK_MODELS)
  26. class Webhook(models.Model):
  27. """
  28. A Webhook defines a request that will be sent to a remote application when an object is created, updated, and/or
  29. delete in NetBox. The request will contain a representation of the object, which the remote application can act on.
  30. Each Webhook can be limited to firing only on certain actions or certain object types.
  31. """
  32. obj_type = models.ManyToManyField(
  33. to=ContentType,
  34. related_name='webhooks',
  35. verbose_name='Object types',
  36. limit_choices_to=get_webhook_models,
  37. help_text="The object(s) to which this Webhook applies."
  38. )
  39. name = models.CharField(
  40. max_length=150,
  41. unique=True
  42. )
  43. type_create = models.BooleanField(
  44. default=False,
  45. help_text="Call this webhook when a matching object is created."
  46. )
  47. type_update = models.BooleanField(
  48. default=False,
  49. help_text="Call this webhook when a matching object is updated."
  50. )
  51. type_delete = models.BooleanField(
  52. default=False,
  53. help_text="Call this webhook when a matching object is deleted."
  54. )
  55. payload_url = models.CharField(
  56. max_length=500,
  57. verbose_name='URL',
  58. help_text="A POST will be sent to this URL when the webhook is called."
  59. )
  60. http_content_type = models.PositiveSmallIntegerField(
  61. choices=WEBHOOK_CT_CHOICES,
  62. default=WEBHOOK_CT_JSON,
  63. verbose_name='HTTP content type'
  64. )
  65. additional_headers = JSONField(
  66. null=True,
  67. blank=True,
  68. help_text="User supplied headers which should be added to the request in addition to the HTTP content type. "
  69. "Headers are supplied as key/value pairs in a JSON object."
  70. )
  71. secret = models.CharField(
  72. max_length=255,
  73. blank=True,
  74. help_text="When provided, the request will include a 'X-Hook-Signature' "
  75. "header containing a HMAC hex digest of the payload body using "
  76. "the secret as the key. The secret is not transmitted in "
  77. "the request."
  78. )
  79. enabled = models.BooleanField(
  80. default=True
  81. )
  82. ssl_verification = models.BooleanField(
  83. default=True,
  84. verbose_name='SSL verification',
  85. help_text="Enable SSL certificate verification. Disable with caution!"
  86. )
  87. ca_file_path = models.CharField(
  88. max_length=4096,
  89. null=True,
  90. blank=True,
  91. verbose_name='CA File Path',
  92. help_text='The specific CA certificate file to use for SSL verification. '
  93. 'Leave blank to use the system defaults.'
  94. )
  95. class Meta:
  96. unique_together = ('payload_url', 'type_create', 'type_update', 'type_delete',)
  97. def __str__(self):
  98. return self.name
  99. def clean(self):
  100. """
  101. Validate model
  102. """
  103. if not self.type_create and not self.type_delete and not self.type_update:
  104. raise ValidationError(
  105. "You must select at least one type: create, update, and/or delete."
  106. )
  107. if not self.ssl_verification and self.ca_file_path:
  108. raise ValidationError({
  109. 'ca_file_path': 'Do not specify a CA certificate file if SSL verification is dissabled.'
  110. })
  111. # Verify that JSON data is provided as an object
  112. if self.additional_headers and type(self.additional_headers) is not dict:
  113. raise ValidationError({
  114. 'additional_headers': 'Header JSON data must be in object form. Example: {"X-API-KEY": "abc123"}'
  115. })
  116. #
  117. # Custom fields
  118. #
  119. class CustomFieldModel(models.Model):
  120. _cf = None
  121. class Meta:
  122. abstract = True
  123. def cache_custom_fields(self):
  124. """
  125. Cache all custom field values for this instance
  126. """
  127. self._cf = {
  128. field.name: value for field, value in self.get_custom_fields().items()
  129. }
  130. @property
  131. def cf(self):
  132. """
  133. Name-based CustomFieldValue accessor for use in templates
  134. """
  135. if self._cf is None:
  136. self.cache_custom_fields()
  137. return self._cf
  138. def get_custom_fields(self):
  139. """
  140. Return a dictionary of custom fields for a single object in the form {<field>: value}.
  141. """
  142. # Find all custom fields applicable to this type of object
  143. content_type = ContentType.objects.get_for_model(self)
  144. fields = CustomField.objects.filter(obj_type=content_type)
  145. # If the object exists, populate its custom fields with values
  146. if hasattr(self, 'pk'):
  147. values = self.custom_field_values.all()
  148. values_dict = {cfv.field_id: cfv.value for cfv in values}
  149. return OrderedDict([(field, values_dict.get(field.pk)) for field in fields])
  150. else:
  151. return OrderedDict([(field, None) for field in fields])
  152. def get_custom_field_models():
  153. return model_names_to_filter_dict(CUSTOMFIELD_MODELS)
  154. class CustomField(models.Model):
  155. obj_type = models.ManyToManyField(
  156. to=ContentType,
  157. related_name='custom_fields',
  158. verbose_name='Object(s)',
  159. limit_choices_to=get_custom_field_models,
  160. help_text='The object(s) to which this field applies.'
  161. )
  162. type = models.PositiveSmallIntegerField(
  163. choices=CUSTOMFIELD_TYPE_CHOICES,
  164. default=CF_TYPE_TEXT
  165. )
  166. name = models.CharField(
  167. max_length=50,
  168. unique=True
  169. )
  170. label = models.CharField(
  171. max_length=50,
  172. blank=True,
  173. help_text='Name of the field as displayed to users (if not provided, '
  174. 'the field\'s name will be used)'
  175. )
  176. description = models.CharField(
  177. max_length=100,
  178. blank=True
  179. )
  180. required = models.BooleanField(
  181. default=False,
  182. help_text='If true, this field is required when creating new objects '
  183. 'or editing an existing object.'
  184. )
  185. filter_logic = models.PositiveSmallIntegerField(
  186. choices=CF_FILTER_CHOICES,
  187. default=CF_FILTER_LOOSE,
  188. help_text='Loose matches any instance of a given string; exact '
  189. 'matches the entire field.'
  190. )
  191. default = models.CharField(
  192. max_length=100,
  193. blank=True,
  194. help_text='Default value for the field. Use "true" or "false" for booleans.'
  195. )
  196. weight = models.PositiveSmallIntegerField(
  197. default=100,
  198. help_text='Fields with higher weights appear lower in a form.'
  199. )
  200. class Meta:
  201. ordering = ['weight', 'name']
  202. def __str__(self):
  203. return self.label or self.name.replace('_', ' ').capitalize()
  204. def serialize_value(self, value):
  205. """
  206. Serialize the given value to a string suitable for storage as a CustomFieldValue
  207. """
  208. if value is None:
  209. return ''
  210. if self.type == CF_TYPE_BOOLEAN:
  211. return str(int(bool(value)))
  212. if self.type == CF_TYPE_DATE:
  213. # Could be date/datetime object or string
  214. try:
  215. return value.strftime('%Y-%m-%d')
  216. except AttributeError:
  217. return value
  218. if self.type == CF_TYPE_SELECT:
  219. # Could be ModelChoiceField or TypedChoiceField
  220. return str(value.id) if hasattr(value, 'id') else str(value)
  221. return value
  222. def deserialize_value(self, serialized_value):
  223. """
  224. Convert a string into the object it represents depending on the type of field
  225. """
  226. if serialized_value == '':
  227. return None
  228. if self.type == CF_TYPE_INTEGER:
  229. return int(serialized_value)
  230. if self.type == CF_TYPE_BOOLEAN:
  231. return bool(int(serialized_value))
  232. if self.type == CF_TYPE_DATE:
  233. # Read date as YYYY-MM-DD
  234. return date(*[int(n) for n in serialized_value.split('-')])
  235. if self.type == CF_TYPE_SELECT:
  236. return self.choices.get(pk=int(serialized_value))
  237. return serialized_value
  238. class CustomFieldValue(models.Model):
  239. field = models.ForeignKey(
  240. to='extras.CustomField',
  241. on_delete=models.CASCADE,
  242. related_name='values'
  243. )
  244. obj_type = models.ForeignKey(
  245. to=ContentType,
  246. on_delete=models.PROTECT,
  247. related_name='+'
  248. )
  249. obj_id = models.PositiveIntegerField()
  250. obj = GenericForeignKey(
  251. ct_field='obj_type',
  252. fk_field='obj_id'
  253. )
  254. serialized_value = models.CharField(
  255. max_length=255
  256. )
  257. class Meta:
  258. ordering = ['obj_type', 'obj_id']
  259. unique_together = ['field', 'obj_type', 'obj_id']
  260. def __str__(self):
  261. return '{} {}'.format(self.obj, self.field)
  262. @property
  263. def value(self):
  264. return self.field.deserialize_value(self.serialized_value)
  265. @value.setter
  266. def value(self, value):
  267. self.serialized_value = self.field.serialize_value(value)
  268. def save(self, *args, **kwargs):
  269. # Delete this object if it no longer has a value to store
  270. if self.pk and self.value is None:
  271. self.delete()
  272. else:
  273. super().save(*args, **kwargs)
  274. class CustomFieldChoice(models.Model):
  275. field = models.ForeignKey(
  276. to='extras.CustomField',
  277. on_delete=models.CASCADE,
  278. related_name='choices',
  279. limit_choices_to={'type': CF_TYPE_SELECT}
  280. )
  281. value = models.CharField(
  282. max_length=100
  283. )
  284. weight = models.PositiveSmallIntegerField(
  285. default=100,
  286. help_text='Higher weights appear lower in the list'
  287. )
  288. class Meta:
  289. ordering = ['field', 'weight', 'value']
  290. unique_together = ['field', 'value']
  291. def __str__(self):
  292. return self.value
  293. def clean(self):
  294. if self.field.type != CF_TYPE_SELECT:
  295. raise ValidationError("Custom field choices can only be assigned to selection fields.")
  296. def delete(self, using=None, keep_parents=False):
  297. # When deleting a CustomFieldChoice, delete all CustomFieldValues which point to it
  298. pk = self.pk
  299. super().delete(using, keep_parents)
  300. CustomFieldValue.objects.filter(field__type=CF_TYPE_SELECT, serialized_value=str(pk)).delete()
  301. #
  302. # Custom links
  303. #
  304. def get_custom_link_models():
  305. return model_names_to_filter_dict(CUSTOMLINK_MODELS)
  306. class CustomLink(models.Model):
  307. """
  308. A custom link to an external representation of a NetBox object. The link text and URL fields accept Jinja2 template
  309. code to be rendered with an object as context.
  310. """
  311. content_type = models.ForeignKey(
  312. to=ContentType,
  313. on_delete=models.CASCADE,
  314. limit_choices_to=get_custom_link_models
  315. )
  316. name = models.CharField(
  317. max_length=100,
  318. unique=True
  319. )
  320. text = models.CharField(
  321. max_length=500,
  322. help_text="Jinja2 template code for link text"
  323. )
  324. url = models.CharField(
  325. max_length=500,
  326. verbose_name='URL',
  327. help_text="Jinja2 template code for link URL"
  328. )
  329. weight = models.PositiveSmallIntegerField(
  330. default=100
  331. )
  332. group_name = models.CharField(
  333. max_length=50,
  334. blank=True,
  335. help_text="Links with the same group will appear as a dropdown menu"
  336. )
  337. button_class = models.CharField(
  338. max_length=30,
  339. choices=BUTTON_CLASS_CHOICES,
  340. default=BUTTON_CLASS_DEFAULT,
  341. help_text="The class of the first link in a group will be used for the dropdown button"
  342. )
  343. new_window = models.BooleanField(
  344. help_text="Force link to open in a new window"
  345. )
  346. class Meta:
  347. ordering = ['group_name', 'weight', 'name']
  348. def __str__(self):
  349. return self.name
  350. #
  351. # Graphs
  352. #
  353. class Graph(models.Model):
  354. type = models.PositiveSmallIntegerField(
  355. choices=GRAPH_TYPE_CHOICES
  356. )
  357. weight = models.PositiveSmallIntegerField(
  358. default=1000
  359. )
  360. name = models.CharField(
  361. max_length=100,
  362. verbose_name='Name'
  363. )
  364. source = models.CharField(
  365. max_length=500,
  366. verbose_name='Source URL'
  367. )
  368. link = models.URLField(
  369. blank=True,
  370. verbose_name='Link URL'
  371. )
  372. class Meta:
  373. ordering = ['type', 'weight', 'name']
  374. def __str__(self):
  375. return self.name
  376. def embed_url(self, obj):
  377. template = Template(self.source)
  378. return template.render(Context({'obj': obj}))
  379. def embed_link(self, obj):
  380. if self.link is None:
  381. return ''
  382. template = Template(self.link)
  383. return template.render(Context({'obj': obj}))
  384. #
  385. # Export templates
  386. #
  387. def get_export_template_models():
  388. return model_names_to_filter_dict(EXPORTTEMPLATE_MODELS)
  389. class ExportTemplate(models.Model):
  390. content_type = models.ForeignKey(
  391. to=ContentType,
  392. on_delete=models.CASCADE,
  393. limit_choices_to=get_export_template_models
  394. )
  395. name = models.CharField(
  396. max_length=100
  397. )
  398. description = models.CharField(
  399. max_length=200,
  400. blank=True
  401. )
  402. template_language = models.PositiveSmallIntegerField(
  403. choices=TEMPLATE_LANGUAGE_CHOICES,
  404. default=TEMPLATE_LANGUAGE_JINJA2
  405. )
  406. template_code = models.TextField(
  407. help_text='The list of objects being exported is passed as a context variable named <code>queryset</code>.'
  408. )
  409. mime_type = models.CharField(
  410. max_length=50,
  411. blank=True,
  412. verbose_name='MIME type',
  413. help_text='Defaults to <code>text/plain</code>'
  414. )
  415. file_extension = models.CharField(
  416. max_length=15,
  417. blank=True,
  418. help_text='Extension to append to the rendered filename'
  419. )
  420. class Meta:
  421. ordering = ['content_type', 'name']
  422. unique_together = [
  423. ['content_type', 'name']
  424. ]
  425. def __str__(self):
  426. return '{}: {}'.format(self.content_type, self.name)
  427. def render(self, queryset):
  428. """
  429. Render the contents of the template.
  430. """
  431. context = {
  432. 'queryset': queryset
  433. }
  434. if self.template_language == TEMPLATE_LANGUAGE_DJANGO:
  435. template = Template(self.template_code)
  436. output = template.render(Context(context))
  437. elif self.template_language == TEMPLATE_LANGUAGE_JINJA2:
  438. template = Environment().from_string(source=self.template_code)
  439. output = template.render(**context)
  440. else:
  441. return None
  442. # Replace CRLF-style line terminators
  443. output = output.replace('\r\n', '\n')
  444. return output
  445. def render_to_response(self, queryset):
  446. """
  447. Render the template to an HTTP response, delivered as a named file attachment
  448. """
  449. output = self.render(queryset)
  450. mime_type = 'text/plain' if not self.mime_type else self.mime_type
  451. # Build the response
  452. response = HttpResponse(output, content_type=mime_type)
  453. filename = 'netbox_{}{}'.format(
  454. queryset.model._meta.verbose_name_plural,
  455. '.{}'.format(self.file_extension) if self.file_extension else ''
  456. )
  457. response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
  458. return response
  459. #
  460. # Topology maps
  461. #
  462. class TopologyMap(models.Model):
  463. name = models.CharField(
  464. max_length=50,
  465. unique=True
  466. )
  467. slug = models.SlugField(
  468. unique=True
  469. )
  470. type = models.PositiveSmallIntegerField(
  471. choices=TOPOLOGYMAP_TYPE_CHOICES,
  472. default=TOPOLOGYMAP_TYPE_NETWORK
  473. )
  474. site = models.ForeignKey(
  475. to='dcim.Site',
  476. on_delete=models.CASCADE,
  477. related_name='topology_maps',
  478. blank=True,
  479. null=True
  480. )
  481. device_patterns = models.TextField(
  482. help_text='Identify devices to include in the diagram using regular '
  483. 'expressions, one per line. Each line will result in a new '
  484. 'tier of the drawing. Separate multiple regexes within a '
  485. 'line using semicolons. Devices will be rendered in the '
  486. 'order they are defined.'
  487. )
  488. description = models.CharField(
  489. max_length=100,
  490. blank=True
  491. )
  492. class Meta:
  493. ordering = ['name']
  494. def __str__(self):
  495. return self.name
  496. @property
  497. def device_sets(self):
  498. if not self.device_patterns:
  499. return None
  500. return [line.strip() for line in self.device_patterns.split('\n')]
  501. def render(self, img_format='png'):
  502. from dcim.models import Device
  503. # Construct the graph
  504. if self.type == TOPOLOGYMAP_TYPE_NETWORK:
  505. G = graphviz.Graph
  506. else:
  507. G = graphviz.Digraph
  508. self.graph = G()
  509. self.graph.graph_attr['ranksep'] = '1'
  510. seen = set()
  511. for i, device_set in enumerate(self.device_sets):
  512. subgraph = G(name='sg{}'.format(i))
  513. subgraph.graph_attr['rank'] = 'same'
  514. subgraph.graph_attr['directed'] = 'true'
  515. # Add a pseudonode for each device_set to enforce hierarchical layout
  516. subgraph.node('set{}'.format(i), label='', shape='none', width='0')
  517. if i:
  518. self.graph.edge('set{}'.format(i - 1), 'set{}'.format(i), style='invis')
  519. # Add each device to the graph
  520. devices = []
  521. for query in device_set.strip(';').split(';'): # Split regexes on semicolons
  522. devices += Device.objects.filter(name__regex=query).prefetch_related('device_role')
  523. # Remove duplicate devices
  524. devices = [d for d in devices if d.id not in seen]
  525. seen.update([d.id for d in devices])
  526. for d in devices:
  527. bg_color = '#{}'.format(d.device_role.color)
  528. fg_color = '#{}'.format(foreground_color(d.device_role.color))
  529. subgraph.node(d.name, style='filled', fillcolor=bg_color, fontcolor=fg_color, fontname='sans')
  530. # Add an invisible connection to each successive device in a set to enforce horizontal order
  531. for j in range(0, len(devices) - 1):
  532. subgraph.edge(devices[j].name, devices[j + 1].name, style='invis')
  533. self.graph.subgraph(subgraph)
  534. # Compile list of all devices
  535. device_superset = Q()
  536. for device_set in self.device_sets:
  537. for query in device_set.split(';'): # Split regexes on semicolons
  538. device_superset = device_superset | Q(name__regex=query)
  539. devices = Device.objects.filter(*(device_superset,))
  540. # Draw edges depending on graph type
  541. if self.type == TOPOLOGYMAP_TYPE_NETWORK:
  542. self.add_network_connections(devices)
  543. elif self.type == TOPOLOGYMAP_TYPE_CONSOLE:
  544. self.add_console_connections(devices)
  545. elif self.type == TOPOLOGYMAP_TYPE_POWER:
  546. self.add_power_connections(devices)
  547. return self.graph.pipe(format=img_format)
  548. def add_network_connections(self, devices):
  549. from circuits.models import CircuitTermination
  550. from dcim.models import Interface
  551. # Add all interface connections to the graph
  552. connected_interfaces = Interface.objects.prefetch_related(
  553. '_connected_interface__device'
  554. ).filter(
  555. Q(device__in=devices) | Q(_connected_interface__device__in=devices),
  556. _connected_interface__isnull=False,
  557. pk__lt=F('_connected_interface')
  558. )
  559. for interface in connected_interfaces:
  560. style = 'solid' if interface.connection_status == CONNECTION_STATUS_CONNECTED else 'dashed'
  561. self.graph.edge(interface.device.name, interface.connected_endpoint.device.name, style=style)
  562. # Add all circuits to the graph
  563. for termination in CircuitTermination.objects.filter(term_side='A', connected_endpoint__device__in=devices):
  564. peer_termination = termination.get_peer_termination()
  565. if (peer_termination is not None and peer_termination.interface is not None and
  566. peer_termination.interface.device in devices):
  567. self.graph.edge(termination.interface.device.name, peer_termination.interface.device.name, color='blue')
  568. def add_console_connections(self, devices):
  569. from dcim.models import ConsolePort
  570. # Add all console connections to the graph
  571. for cp in ConsolePort.objects.filter(device__in=devices, connected_endpoint__device__in=devices):
  572. style = 'solid' if cp.connection_status == CONNECTION_STATUS_CONNECTED else 'dashed'
  573. self.graph.edge(cp.connected_endpoint.device.name, cp.device.name, style=style)
  574. def add_power_connections(self, devices):
  575. from dcim.models import PowerPort
  576. # Add all power connections to the graph
  577. for pp in PowerPort.objects.filter(device__in=devices, _connected_poweroutlet__device__in=devices):
  578. style = 'solid' if pp.connection_status == CONNECTION_STATUS_CONNECTED else 'dashed'
  579. self.graph.edge(pp.connected_endpoint.device.name, pp.device.name, style=style)
  580. #
  581. # Image attachments
  582. #
  583. def image_upload(instance, filename):
  584. path = 'image-attachments/'
  585. # Rename the file to the provided name, if any. Attempt to preserve the file extension.
  586. extension = filename.rsplit('.')[-1].lower()
  587. if instance.name and extension in ['bmp', 'gif', 'jpeg', 'jpg', 'png']:
  588. filename = '.'.join([instance.name, extension])
  589. elif instance.name:
  590. filename = instance.name
  591. return '{}{}_{}_{}'.format(path, instance.content_type.name, instance.object_id, filename)
  592. class ImageAttachment(models.Model):
  593. """
  594. An uploaded image which is associated with an object.
  595. """
  596. content_type = models.ForeignKey(
  597. to=ContentType,
  598. on_delete=models.CASCADE
  599. )
  600. object_id = models.PositiveIntegerField()
  601. parent = GenericForeignKey(
  602. ct_field='content_type',
  603. fk_field='object_id'
  604. )
  605. image = models.ImageField(
  606. upload_to=image_upload,
  607. height_field='image_height',
  608. width_field='image_width'
  609. )
  610. image_height = models.PositiveSmallIntegerField()
  611. image_width = models.PositiveSmallIntegerField()
  612. name = models.CharField(
  613. max_length=50,
  614. blank=True
  615. )
  616. created = models.DateTimeField(
  617. auto_now_add=True
  618. )
  619. class Meta:
  620. ordering = ['name']
  621. def __str__(self):
  622. if self.name:
  623. return self.name
  624. filename = self.image.name.rsplit('/', 1)[-1]
  625. return filename.split('_', 2)[2]
  626. def delete(self, *args, **kwargs):
  627. _name = self.image.name
  628. super().delete(*args, **kwargs)
  629. # Delete file from disk
  630. self.image.delete(save=False)
  631. # Deleting the file erases its name. We restore the image's filename here in case we still need to reference it
  632. # before the request finishes. (For example, to display a message indicating the ImageAttachment was deleted.)
  633. self.image.name = _name
  634. @property
  635. def size(self):
  636. """
  637. Wrapper around `image.size` to suppress an OSError in case the file is inaccessible. When S3 storage is used
  638. ClientError is suppressed instead.
  639. """
  640. from django.conf import settings
  641. if settings.MEDIA_STORAGE and settings.MEDIA_STORAGE['BACKEND'] == 'S3':
  642. # For S3 we need to handle a different exception
  643. from botocore.exceptions import ClientError
  644. try:
  645. return self.image.size
  646. except ClientError:
  647. return None
  648. try:
  649. return self.image.size
  650. except OSError:
  651. return None
  652. #
  653. # Config contexts
  654. #
  655. class ConfigContext(models.Model):
  656. """
  657. A ConfigContext represents a set of arbitrary data available to any Device or VirtualMachine matching its assigned
  658. qualifiers (region, site, etc.). For example, the data stored in a ConfigContext assigned to site A and tenant B
  659. will be available to a Device in site A assigned to tenant B. Data is stored in JSON format.
  660. """
  661. name = models.CharField(
  662. max_length=100,
  663. unique=True
  664. )
  665. weight = models.PositiveSmallIntegerField(
  666. default=1000
  667. )
  668. description = models.CharField(
  669. max_length=100,
  670. blank=True
  671. )
  672. is_active = models.BooleanField(
  673. default=True,
  674. )
  675. regions = models.ManyToManyField(
  676. to='dcim.Region',
  677. related_name='+',
  678. blank=True
  679. )
  680. sites = models.ManyToManyField(
  681. to='dcim.Site',
  682. related_name='+',
  683. blank=True
  684. )
  685. roles = models.ManyToManyField(
  686. to='dcim.DeviceRole',
  687. related_name='+',
  688. blank=True
  689. )
  690. platforms = models.ManyToManyField(
  691. to='dcim.Platform',
  692. related_name='+',
  693. blank=True
  694. )
  695. tenant_groups = models.ManyToManyField(
  696. to='tenancy.TenantGroup',
  697. related_name='+',
  698. blank=True
  699. )
  700. tenants = models.ManyToManyField(
  701. to='tenancy.Tenant',
  702. related_name='+',
  703. blank=True
  704. )
  705. data = JSONField()
  706. objects = ConfigContextQuerySet.as_manager()
  707. class Meta:
  708. ordering = ['weight', 'name']
  709. def __str__(self):
  710. return self.name
  711. def get_absolute_url(self):
  712. return reverse('extras:configcontext', kwargs={'pk': self.pk})
  713. def clean(self):
  714. # Verify that JSON data is provided as an object
  715. if type(self.data) is not dict:
  716. raise ValidationError(
  717. {'data': 'JSON data must be in object form. Example: {"foo": 123}'}
  718. )
  719. class ConfigContextModel(models.Model):
  720. """
  721. A model which includes local configuration context data. This local data will override any inherited data from
  722. ConfigContexts.
  723. """
  724. local_context_data = JSONField(
  725. blank=True,
  726. null=True,
  727. )
  728. class Meta:
  729. abstract = True
  730. def get_config_context(self):
  731. """
  732. Return the rendered configuration context for a device or VM.
  733. """
  734. # Compile all config data, overwriting lower-weight values with higher-weight values where a collision occurs
  735. data = OrderedDict()
  736. for context in ConfigContext.objects.get_for_object(self):
  737. data = deepmerge(data, context.data)
  738. # If the object has local config context data defined, merge it last
  739. if self.local_context_data:
  740. data = deepmerge(data, self.local_context_data)
  741. return data
  742. def clean(self):
  743. super().clean()
  744. # Verify that JSON data is provided as an object
  745. if self.local_context_data and type(self.local_context_data) is not dict:
  746. raise ValidationError(
  747. {'local_context_data': 'JSON data must be in object form. Example: {"foo": 123}'}
  748. )
  749. #
  750. # Custom scripts
  751. #
  752. class Script(models.Model):
  753. """
  754. Dummy model used to generate permissions for custom scripts. Does not exist in the database.
  755. """
  756. class Meta:
  757. managed = False
  758. permissions = (
  759. ('run_script', 'Can run script'),
  760. )
  761. #
  762. # Report results
  763. #
  764. class ReportResult(models.Model):
  765. """
  766. This model stores the results from running a user-defined report.
  767. """
  768. report = models.CharField(
  769. max_length=255,
  770. unique=True
  771. )
  772. created = models.DateTimeField(
  773. auto_now_add=True
  774. )
  775. user = models.ForeignKey(
  776. to=User,
  777. on_delete=models.SET_NULL,
  778. related_name='+',
  779. blank=True,
  780. null=True
  781. )
  782. failed = models.BooleanField()
  783. data = JSONField()
  784. class Meta:
  785. ordering = ['report']
  786. #
  787. # Change logging
  788. #
  789. class ObjectChange(models.Model):
  790. """
  791. Record a change to an object and the user account associated with that change. A change record may optionally
  792. indicate an object related to the one being changed. For example, a change to an interface may also indicate the
  793. parent device. This will ensure changes made to component models appear in the parent model's changelog.
  794. """
  795. time = models.DateTimeField(
  796. auto_now_add=True,
  797. editable=False,
  798. db_index=True
  799. )
  800. user = models.ForeignKey(
  801. to=User,
  802. on_delete=models.SET_NULL,
  803. related_name='changes',
  804. blank=True,
  805. null=True
  806. )
  807. user_name = models.CharField(
  808. max_length=150,
  809. editable=False
  810. )
  811. request_id = models.UUIDField(
  812. editable=False
  813. )
  814. action = models.PositiveSmallIntegerField(
  815. choices=OBJECTCHANGE_ACTION_CHOICES
  816. )
  817. changed_object_type = models.ForeignKey(
  818. to=ContentType,
  819. on_delete=models.PROTECT,
  820. related_name='+'
  821. )
  822. changed_object_id = models.PositiveIntegerField()
  823. changed_object = GenericForeignKey(
  824. ct_field='changed_object_type',
  825. fk_field='changed_object_id'
  826. )
  827. related_object_type = models.ForeignKey(
  828. to=ContentType,
  829. on_delete=models.PROTECT,
  830. related_name='+',
  831. blank=True,
  832. null=True
  833. )
  834. related_object_id = models.PositiveIntegerField(
  835. blank=True,
  836. null=True
  837. )
  838. related_object = GenericForeignKey(
  839. ct_field='related_object_type',
  840. fk_field='related_object_id'
  841. )
  842. object_repr = models.CharField(
  843. max_length=200,
  844. editable=False
  845. )
  846. object_data = JSONField(
  847. editable=False
  848. )
  849. csv_headers = [
  850. 'time', 'user', 'user_name', 'request_id', 'action', 'changed_object_type', 'changed_object_id',
  851. 'related_object_type', 'related_object_id', 'object_repr', 'object_data',
  852. ]
  853. class Meta:
  854. ordering = ['-time']
  855. def __str__(self):
  856. return '{} {} {} by {}'.format(
  857. self.changed_object_type,
  858. self.object_repr,
  859. self.get_action_display().lower(),
  860. self.user_name
  861. )
  862. def save(self, *args, **kwargs):
  863. # Record the user's name and the object's representation as static strings
  864. if not self.user_name:
  865. self.user_name = self.user.username
  866. if not self.object_repr:
  867. self.object_repr = str(self.changed_object)
  868. return super().save(*args, **kwargs)
  869. def get_absolute_url(self):
  870. return reverse('extras:objectchange', args=[self.pk])
  871. def to_csv(self):
  872. return (
  873. self.time,
  874. self.user,
  875. self.user_name,
  876. self.request_id,
  877. self.get_action_display(),
  878. self.changed_object_type,
  879. self.changed_object_id,
  880. self.related_object_type,
  881. self.related_object_id,
  882. self.object_repr,
  883. self.object_data,
  884. )
  885. #
  886. # Tags
  887. #
  888. # TODO: figure out a way around this circular import for ObjectChange
  889. from utilities.models import ChangeLoggedModel # noqa: E402
  890. class Tag(TagBase, ChangeLoggedModel):
  891. color = ColorField(
  892. default='9e9e9e'
  893. )
  894. comments = models.TextField(
  895. blank=True,
  896. default=''
  897. )
  898. def get_absolute_url(self):
  899. return reverse('extras:tag', args=[self.slug])
  900. class TaggedItem(GenericTaggedItemBase):
  901. tag = models.ForeignKey(
  902. to=Tag,
  903. related_name="%(app_label)s_%(class)s_items",
  904. on_delete=models.CASCADE
  905. )
  906. class Meta:
  907. index_together = (
  908. ("content_type", "object_id")
  909. )