models.py 31 KB

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