Merge pull request #4417 from netbox-community/develop

Release v2.7.11
This commit is contained in:
Jeremy Stretch 2020-03-27 12:47:28 -04:00 committed by GitHub
commit 3d3d1bc623
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
68 changed files with 1591 additions and 1014 deletions

View File

@ -299,6 +299,24 @@ When determining the primary IP address for a device, IPv6 is preferred over IPv
--- ---
## RELEASE_CHECK_TIMEOUT
Default: 86,400 (24 hours)
The number of seconds to retain the latest version that is fetched from the GitHub API before automatically invalidating it and fetching it from the API again. This must be set to at least one hour (3600 seconds).
---
## RELEASE_CHECK_URL
Default: None
The releases of this repository are checked to detect new releases, which are shown on the home page of the web interface. You can change this to your own fork of the NetBox repository, or set it to `None` to disable the check. The URL provided **must** be compatible with the GitHub API.
Use `'https://api.github.com/repos/netbox-community/netbox/releases'` to check for release in the official NetBox repository.
---
## REPORTS_ROOT ## REPORTS_ROOT
Default: $BASE_DIR/netbox/reports/ Default: $BASE_DIR/netbox/reports/

View File

@ -46,9 +46,9 @@ DATABASE = {
[Redis](https://redis.io/) is an in-memory data store similar to memcached. While Redis has been an optional component of [Redis](https://redis.io/) is an in-memory data store similar to memcached. While Redis has been an optional component of
NetBox since the introduction of webhooks in version 2.4, it is required starting in 2.6 to support NetBox's caching NetBox since the introduction of webhooks in version 2.4, it is required starting in 2.6 to support NetBox's caching
functionality (as well as other planned features). In 2.7, the connection settings were broken down into two sections for functionality (as well as other planned features). In 2.7, the connection settings were broken down into two sections for
webhooks and caching, allowing the user to connect to different Redis instances/databases per feature. task queuing and caching, allowing the user to connect to different Redis instances/databases per feature.
Redis is configured using a configuration setting similar to `DATABASE` and these settings are the same for both of the `webhooks` and `caching` subsections: Redis is configured using a configuration setting similar to `DATABASE` and these settings are the same for both of the `tasks` and `caching` subsections:
* `HOST` - Name or IP address of the Redis server (use `localhost` if running locally) * `HOST` - Name or IP address of the Redis server (use `localhost` if running locally)
* `PORT` - TCP port of the Redis service; leave blank for default port (6379) * `PORT` - TCP port of the Redis service; leave blank for default port (6379)
@ -61,7 +61,7 @@ Example:
```python ```python
REDIS = { REDIS = {
'webhooks': { 'tasks': {
'HOST': 'redis.example.com', 'HOST': 'redis.example.com',
'PORT': 1234, 'PORT': 1234,
'PASSWORD': 'foobar', 'PASSWORD': 'foobar',
@ -84,9 +84,9 @@ REDIS = {
If you are upgrading from a version prior to v2.7, please note that the Redis connection configuration settings have If you are upgrading from a version prior to v2.7, please note that the Redis connection configuration settings have
changed. Manual modification to bring the `REDIS` section inline with the above specification is necessary changed. Manual modification to bring the `REDIS` section inline with the above specification is necessary
!!! note !!! warning
It is highly recommended to keep the webhook and cache databases separate. Using the same database number on the It is highly recommended to keep the task and cache databases separate. Using the same database number on the
same Redis instance for both may result in webhook processing data being lost during cache flushing events. same Redis instance for both may result in queued background tasks being lost during cache flushing events.
### Using Redis Sentinel ### Using Redis Sentinel
@ -102,7 +102,7 @@ Example:
```python ```python
REDIS = { REDIS = {
'webhooks': { 'tasks': {
'SENTINELS': [('mysentinel.redis.example.com', 6379)], 'SENTINELS': [('mysentinel.redis.example.com', 6379)],
'SENTINEL_SERVICE': 'netbox', 'SENTINEL_SERVICE': 'netbox',
'PASSWORD': '', 'PASSWORD': '',
@ -126,7 +126,7 @@ REDIS = {
!!! note !!! note
It is possible to have only one or the other Redis configurations to use Sentinel functionality. It is possible It is possible to have only one or the other Redis configurations to use Sentinel functionality. It is possible
for example to have the webhook use sentinel via `HOST`/`PORT` and for caching to use Sentinel via for example to have the tasks database use sentinel via `HOST`/`PORT` and for caching to use Sentinel via
`SENTINELS`/`SENTINEL_SERVICE`. `SENTINELS`/`SENTINEL_SERVICE`.

12
docs/extra.css Normal file
View File

@ -0,0 +1,12 @@
/* Custom table styling */
table {
margin-bottom: 24px;
width: 100%;
}
th {
background-color: #f0f0f0;
padding: 6px;
}
td {
padding: 6px;
}

View File

@ -172,7 +172,7 @@ Redis is a in-memory key-value store required as part of the NetBox installation
```python ```python
REDIS = { REDIS = {
'webhooks': { 'tasks': {
'HOST': 'redis.example.com', 'HOST': 'redis.example.com',
'PORT': 1234, 'PORT': 1234,
'PASSWORD': 'foobar', 'PASSWORD': 'foobar',

View File

@ -1,5 +1,31 @@
# NetBox v2.7 Release Notes # NetBox v2.7 Release Notes
## v2.7.11 (2020-03-27)
### Enhancements
* [#738](https://github.com/netbox-community/netbox/issues/738) - Add ability to automatically check for new releases (must be enabled by setting `RELEASE_CHECK_URL`)
* [#4255](https://github.com/netbox-community/netbox/issues/4255) - Custom script object variables now utilize dynamic form widgets
* [#4309](https://github.com/netbox-community/netbox/issues/4309) - Add descriptive tooltip to custom fields on object views
* [#4369](https://github.com/netbox-community/netbox/issues/4369) - Add a dedicated view for rack reservations
* [#4380](https://github.com/netbox-community/netbox/issues/4380) - Enable webhooks for rack reservations
* [#4381](https://github.com/netbox-community/netbox/issues/4381) - Enable export templates for rack reservations
* [#4382](https://github.com/netbox-community/netbox/issues/4382) - Enable custom links for rack reservations
* [#4386](https://github.com/netbox-community/netbox/issues/4386) - Update admin links for Django RQ to reflect multiple queues
* [#4389](https://github.com/netbox-community/netbox/issues/4389) - Add a bulk edit view for device bays
* [#4404](https://github.com/netbox-community/netbox/issues/4404) - Add cable trace button for circuit terminations
### Bug Fixes
* [#2769](https://github.com/netbox-community/netbox/issues/2769) - Improve `prefix_length` validation on available-prefixes API
* [#3193](https://github.com/netbox-community/netbox/issues/3193) - Fix cable tracing across multiple rear ports
* [#4340](https://github.com/netbox-community/netbox/issues/4340) - Enforce unique constraints for device and virtual machine names in the API
* [#4343](https://github.com/netbox-community/netbox/issues/4343) - Fix Markdown support for tables
* [#4365](https://github.com/netbox-community/netbox/issues/4365) - Fix exception raised on IP address bulk add view
* [#4415](https://github.com/netbox-community/netbox/issues/4415) - Fix duplicate name validation on device model
---
## v2.7.10 (2020-03-10) ## v2.7.10 (2020-03-10)
**Note:** If your deployment requires any non-core Python packages (such as `napalm`, `django-storages`, or `django-auth-ldap`), list them in a file named `local_requirements.txt` in the NetBox root directory (alongside `requirements.txt`). This will ensure they are detected and re-installed by the upgrade script when the Python virtual environment is rebuilt. **Note:** If your deployment requires any non-core Python packages (such as `napalm`, `django-storages`, or `django-auth-ldap`), list them in a file named `local_requirements.txt` in the NetBox root directory (alongside `requirements.txt`). This will ensure they are detected and re-installed by the upgrade script when the Python virtual environment is rebuilt.

View File

@ -7,11 +7,12 @@ python:
theme: theme:
name: readthedocs name: readthedocs
navigation_depth: 3 navigation_depth: 3
extra_css:
- extra.css
markdown_extensions: markdown_extensions:
- admonition: - admonition:
- markdown_include.include: - markdown_include.include:
headingOffset: 1 headingOffset: 1
nav: nav:
- Introduction: 'index.md' - Introduction: 'index.md'
- Installation: - Installation:

View File

@ -113,7 +113,6 @@ class ProviderFilterForm(BootstrapMixin, CustomFieldFilterForm):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/regions/",
value_field="slug", value_field="slug",
filter_for={ filter_for={
'site': 'region' 'site': 'region'
@ -125,7 +124,6 @@ class ProviderFilterForm(BootstrapMixin, CustomFieldFilterForm):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/sites/",
value_field="slug", value_field="slug",
) )
) )
@ -167,16 +165,10 @@ class CircuitTypeCSVForm(forms.ModelForm):
class CircuitForm(BootstrapMixin, TenancyForm, CustomFieldModelForm): class CircuitForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
provider = DynamicModelChoiceField( provider = DynamicModelChoiceField(
queryset=Provider.objects.all(), queryset=Provider.objects.all()
widget=APISelect(
api_url="/api/circuits/providers/"
)
) )
type = DynamicModelChoiceField( type = DynamicModelChoiceField(
queryset=CircuitType.objects.all(), queryset=CircuitType.objects.all()
widget=APISelect(
api_url="/api/circuits/circuit-types/"
)
) )
comments = CommentField() comments = CommentField()
tags = TagField( tags = TagField(
@ -245,17 +237,11 @@ class CircuitBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEdit
) )
type = DynamicModelChoiceField( type = DynamicModelChoiceField(
queryset=CircuitType.objects.all(), queryset=CircuitType.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/circuits/circuit-types/"
)
) )
provider = DynamicModelChoiceField( provider = DynamicModelChoiceField(
queryset=Provider.objects.all(), queryset=Provider.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/circuits/providers/"
)
) )
status = forms.ChoiceField( status = forms.ChoiceField(
choices=add_blank_choice(CircuitStatusChoices), choices=add_blank_choice(CircuitStatusChoices),
@ -265,10 +251,7 @@ class CircuitBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEdit
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenants/"
)
) )
commit_rate = forms.IntegerField( commit_rate = forms.IntegerField(
required=False, required=False,
@ -303,7 +286,6 @@ class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/circuits/circuit-types/",
value_field="slug", value_field="slug",
) )
) )
@ -312,7 +294,6 @@ class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/circuits/providers/",
value_field="slug", value_field="slug",
) )
) )
@ -326,7 +307,6 @@ class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/regions/",
value_field="slug", value_field="slug",
filter_for={ filter_for={
'site': 'region' 'site': 'region'
@ -338,7 +318,6 @@ class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/sites/",
value_field="slug", value_field="slug",
) )
) )
@ -355,6 +334,9 @@ class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
# #
class CircuitTerminationForm(BootstrapMixin, forms.ModelForm): class CircuitTerminationForm(BootstrapMixin, forms.ModelForm):
site = DynamicModelChoiceField(
queryset=Site.objects.all()
)
class Meta: class Meta:
model = CircuitTermination model = CircuitTermination
@ -368,7 +350,4 @@ class CircuitTerminationForm(BootstrapMixin, forms.ModelForm):
} }
widgets = { widgets = {
'term_side': forms.HiddenInput(), 'term_side': forms.HiddenInput(),
'site': APISelect(
api_url="/api/dcim/sites/"
)
} }

View File

@ -7,6 +7,7 @@ from dcim.constants import CONNECTION_STATUS_CHOICES
from dcim.fields import ASNField from dcim.fields import ASNField
from dcim.models import CableTermination from dcim.models import CableTermination
from extras.models import CustomFieldModel, ObjectChange, TaggedItem from extras.models import CustomFieldModel, ObjectChange, TaggedItem
from extras.utils import extras_features
from utilities.models import ChangeLoggedModel from utilities.models import ChangeLoggedModel
from utilities.utils import serialize_object from utilities.utils import serialize_object
from .choices import * from .choices import *
@ -21,6 +22,7 @@ __all__ = (
) )
@extras_features('custom_fields', 'custom_links', 'graphs', 'export_templates', 'webhooks')
class Provider(ChangeLoggedModel, CustomFieldModel): class Provider(ChangeLoggedModel, CustomFieldModel):
""" """
Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model
@ -131,6 +133,7 @@ class CircuitType(ChangeLoggedModel):
) )
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Circuit(ChangeLoggedModel, CustomFieldModel): class Circuit(ChangeLoggedModel, CustomFieldModel):
""" """
A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple

View File

@ -77,7 +77,7 @@ class CableTraceMixin(object):
# Initialize the path array # Initialize the path array
path = [] path = []
for near_end, cable, far_end in obj.trace(follow_circuits=True): for near_end, cable, far_end in obj.trace():
# Serialize each object # Serialize each object
serializer_a = get_serializer_for_model(near_end, prefix='Nested') serializer_a = get_serializer_for_model(near_end, prefix='Nested')

File diff suppressed because it is too large Load Diff

View File

@ -21,6 +21,7 @@ from dcim.constants import *
from dcim.fields import ASNField from dcim.fields import ASNField
from dcim.elevations import RackElevationSVG from dcim.elevations import RackElevationSVG
from extras.models import ConfigContextModel, CustomFieldModel, ObjectChange, TaggedItem from extras.models import ConfigContextModel, CustomFieldModel, ObjectChange, TaggedItem
from extras.utils import extras_features
from utilities.fields import ColorField, NaturalOrderingField from utilities.fields import ColorField, NaturalOrderingField
from utilities.models import ChangeLoggedModel from utilities.models import ChangeLoggedModel
from utilities.utils import serialize_object, to_meters from utilities.utils import serialize_object, to_meters
@ -75,6 +76,7 @@ __all__ = (
# Regions # Regions
# #
@extras_features('export_templates', 'webhooks')
class Region(MPTTModel, ChangeLoggedModel): class Region(MPTTModel, ChangeLoggedModel):
""" """
Sites can be grouped within geographic Regions. Sites can be grouped within geographic Regions.
@ -133,6 +135,7 @@ class Region(MPTTModel, ChangeLoggedModel):
# Sites # Sites
# #
@extras_features('custom_fields', 'custom_links', 'graphs', 'export_templates', 'webhooks')
class Site(ChangeLoggedModel, CustomFieldModel): class Site(ChangeLoggedModel, CustomFieldModel):
""" """
A Site represents a geographic location within a network; typically a building or campus. The optional facility A Site represents a geographic location within a network; typically a building or campus. The optional facility
@ -283,6 +286,7 @@ class Site(ChangeLoggedModel, CustomFieldModel):
# Racks # Racks
# #
@extras_features('export_templates')
class RackGroup(ChangeLoggedModel): class RackGroup(ChangeLoggedModel):
""" """
Racks can be grouped as subsets within a Site. The scope of a group will depend on how Sites are defined. For Racks can be grouped as subsets within a Site. The scope of a group will depend on how Sites are defined. For
@ -359,6 +363,7 @@ class RackRole(ChangeLoggedModel):
) )
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Rack(ChangeLoggedModel, CustomFieldModel): class Rack(ChangeLoggedModel, CustomFieldModel):
""" """
Devices are housed within Racks. Each rack has a defined height measured in rack units, and a front and rear face. Devices are housed within Racks. Each rack has a defined height measured in rack units, and a front and rear face.
@ -734,6 +739,7 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
return 0 return 0
@extras_features('custom_links', 'export_templates', 'webhooks')
class RackReservation(ChangeLoggedModel): class RackReservation(ChangeLoggedModel):
""" """
One or more reserved units within a Rack. One or more reserved units within a Rack.
@ -769,6 +775,9 @@ class RackReservation(ChangeLoggedModel):
def __str__(self): def __str__(self):
return "Reservation for rack {}".format(self.rack) return "Reservation for rack {}".format(self.rack)
def get_absolute_url(self):
return reverse('dcim:rackreservation', args=[self.pk])
def clean(self): def clean(self):
if self.units: if self.units:
@ -820,6 +829,7 @@ class RackReservation(ChangeLoggedModel):
# Device Types # Device Types
# #
@extras_features('export_templates', 'webhooks')
class Manufacturer(ChangeLoggedModel): class Manufacturer(ChangeLoggedModel):
""" """
A Manufacturer represents a company which produces hardware devices; for example, Juniper or Dell. A Manufacturer represents a company which produces hardware devices; for example, Juniper or Dell.
@ -850,6 +860,7 @@ class Manufacturer(ChangeLoggedModel):
) )
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class DeviceType(ChangeLoggedModel, CustomFieldModel): class DeviceType(ChangeLoggedModel, CustomFieldModel):
""" """
A DeviceType represents a particular make (Manufacturer) and model of device. It specifies rack height and depth, as A DeviceType represents a particular make (Manufacturer) and model of device. It specifies rack height and depth, as
@ -1193,6 +1204,7 @@ class Platform(ChangeLoggedModel):
) )
@extras_features('custom_fields', 'custom_links', 'graphs', 'export_templates', 'webhooks')
class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel): class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
""" """
A Device represents a piece of physical hardware mounted within a Rack. Each Device is assigned a DeviceType, A Device represents a piece of physical hardware mounted within a Rack. Each Device is assigned a DeviceType,
@ -1378,7 +1390,7 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
# because Django does not consider two NULL fields to be equal, and thus will not trigger a violation # because Django does not consider two NULL fields to be equal, and thus will not trigger a violation
# of the uniqueness constraint without manual intervention. # of the uniqueness constraint without manual intervention.
if self.name and self.tenant is None: if self.name and self.tenant is None:
if Device.objects.exclude(pk=self.pk).filter(name=self.name, tenant__isnull=True): if Device.objects.exclude(pk=self.pk).filter(name=self.name, site=self.site, tenant__isnull=True):
raise ValidationError({ raise ValidationError({
'name': 'A device with this name already exists.' 'name': 'A device with this name already exists.'
}) })
@ -1628,6 +1640,7 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
# Virtual chassis # Virtual chassis
# #
@extras_features('export_templates', 'webhooks')
class VirtualChassis(ChangeLoggedModel): class VirtualChassis(ChangeLoggedModel):
""" """
A collection of Devices which operate with a shared control plane (e.g. a switch stack). A collection of Devices which operate with a shared control plane (e.g. a switch stack).
@ -1694,6 +1707,7 @@ class VirtualChassis(ChangeLoggedModel):
# Power # Power
# #
@extras_features('custom_links', 'export_templates', 'webhooks')
class PowerPanel(ChangeLoggedModel): class PowerPanel(ChangeLoggedModel):
""" """
A distribution point for electrical power; e.g. a data center RPP. A distribution point for electrical power; e.g. a data center RPP.
@ -1740,6 +1754,7 @@ class PowerPanel(ChangeLoggedModel):
)) ))
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class PowerFeed(ChangeLoggedModel, CableTermination, CustomFieldModel): class PowerFeed(ChangeLoggedModel, CableTermination, CustomFieldModel):
""" """
An electrical circuit delivered from a PowerPanel. An electrical circuit delivered from a PowerPanel.
@ -1901,6 +1916,7 @@ class PowerFeed(ChangeLoggedModel, CableTermination, CustomFieldModel):
# Cables # Cables
# #
@extras_features('custom_links', 'export_templates', 'webhooks')
class Cable(ChangeLoggedModel): class Cable(ChangeLoggedModel):
""" """
A physical connection between two endpoints. A physical connection between two endpoints.
@ -2052,15 +2068,15 @@ class Cable(ChangeLoggedModel):
self.termination_a_type, self.termination_b_type self.termination_a_type, self.termination_b_type
)) ))
# A component with multiple positions must be connected to a component with an equal number of positions # A RearPort with multiple positions must be connected to a component with an equal number of positions
term_a_positions = getattr(self.termination_a, 'positions', 1) if isinstance(self.termination_a, RearPort) and isinstance(self.termination_b, RearPort):
term_b_positions = getattr(self.termination_b, 'positions', 1) if self.termination_a.positions != self.termination_b.positions:
if term_a_positions != term_b_positions: raise ValidationError(
raise ValidationError( "{} has {} positions and {} has {}. Both terminations must have the same number of positions.".format(
"{} has {} positions and {} has {}. Both terminations must have the same number of positions.".format( self.termination_a, self.termination_a.positions,
self.termination_a, term_a_positions, self.termination_b, term_b_positions self.termination_b, self.termination_b.positions
)
) )
)
# A termination point cannot be connected to itself # A termination point cannot be connected to itself
if self.termination_a == self.termination_b: if self.termination_a == self.termination_b:

View File

@ -1,3 +1,5 @@
import logging
from django.contrib.contenttypes.fields import GenericRelation from django.contrib.contenttypes.fields import GenericRelation
from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator from django.core.validators import MaxValueValidator, MinValueValidator
@ -8,9 +10,9 @@ from taggit.managers import TaggableManager
from dcim.choices import * from dcim.choices import *
from dcim.constants import * from dcim.constants import *
from dcim.exceptions import LoopDetected
from dcim.fields import MACAddressField from dcim.fields import MACAddressField
from extras.models import ObjectChange, TaggedItem from extras.models import ObjectChange, TaggedItem
from extras.utils import extras_features
from utilities.fields import NaturalOrderingField from utilities.fields import NaturalOrderingField
from utilities.ordering import naturalize_interface from utilities.ordering import naturalize_interface
from utilities.utils import serialize_object from utilities.utils import serialize_object
@ -87,7 +89,7 @@ class CableTermination(models.Model):
class Meta: class Meta:
abstract = True abstract = True
def trace(self, position=1, follow_circuits=False, cable_history=None): def trace(self):
""" """
Return a list representing a complete cable path, with each individual segment represented as a three-tuple: Return a list representing a complete cable path, with each individual segment represented as a three-tuple:
[ [
@ -96,65 +98,85 @@ class CableTermination(models.Model):
(termination E, cable, termination F) (termination E, cable, termination F)
] ]
""" """
def get_peer_port(termination, position=1, follow_circuits=False): endpoint = self
path = []
position_stack = []
def get_peer_port(termination):
from circuits.models import CircuitTermination from circuits.models import CircuitTermination
# Map a front port to its corresponding rear port # Map a front port to its corresponding rear port
if isinstance(termination, FrontPort): if isinstance(termination, FrontPort):
return termination.rear_port, termination.rear_port_position position_stack.append(termination.rear_port_position)
# Retrieve the corresponding RearPort from database to ensure we have an up-to-date instance
peer_port = RearPort.objects.get(pk=termination.rear_port.pk)
return peer_port
# Map a rear port/position to its corresponding front port # Map a rear port/position to its corresponding front port
elif isinstance(termination, RearPort): elif isinstance(termination, RearPort):
# Can't map to a FrontPort without a position
if not position_stack:
# TODO: This behavior is broken. We need a mechanism by which to return all FrontPorts mapped
# to a given RearPort so that we can update end-to-end paths when a cable is created/deleted.
# For now, we're maintaining the current behavior of tracing only to the first FrontPort.
position_stack.append(1)
position = position_stack.pop()
# Validate the position
if position not in range(1, termination.positions + 1): if position not in range(1, termination.positions + 1):
raise Exception("Invalid position for {} ({} positions): {})".format( raise Exception("Invalid position for {} ({} positions): {})".format(
termination, termination.positions, position termination, termination.positions, position
)) ))
try: try:
peer_port = FrontPort.objects.get( peer_port = FrontPort.objects.get(
rear_port=termination, rear_port=termination,
rear_port_position=position, rear_port_position=position,
) )
return peer_port, 1 return peer_port
except ObjectDoesNotExist: except ObjectDoesNotExist:
return None, None return None
# Follow a circuit to its other termination # Follow a circuit to its other termination
elif isinstance(termination, CircuitTermination) and follow_circuits: elif isinstance(termination, CircuitTermination):
peer_termination = termination.get_peer_termination() peer_termination = termination.get_peer_termination()
if peer_termination is None: if peer_termination is None:
return None, None return None
return peer_termination, position return peer_termination
# Termination is not a pass-through port # Termination is not a pass-through port
else: else:
return None, None return None
if not self.cable: logger = logging.getLogger('netbox.dcim.cable.trace')
return [(self, None, None)] logger.debug("Tracing cable from {} {}".format(self.parent, self))
# Record cable history to detect loops while endpoint is not None:
if cable_history is None:
cable_history = []
elif self.cable in cable_history:
raise LoopDetected()
cable_history.append(self.cable)
far_end = self.cable.termination_b if self.cable.termination_a == self else self.cable.termination_a # No cable connected; nothing to trace
path = [(self, self.cable, far_end)] if not endpoint.cable:
path.append((endpoint, None, None))
logger.debug("No cable connected")
return path
peer_port, position = get_peer_port(far_end, position, follow_circuits) # Check for loops
if peer_port is None: if endpoint.cable in [segment[1] for segment in path]:
return path logger.debug("Loop detected!")
return path
try: # Record the current segment in the path
next_segment = peer_port.trace(position, follow_circuits, cable_history) far_end = endpoint.get_cable_peer()
except LoopDetected: path.append((endpoint, endpoint.cable, far_end))
return path logger.debug("{}[{}] --- Cable {} ---> {}[{}]".format(
endpoint.parent, endpoint, endpoint.cable.pk, far_end.parent, far_end
))
if next_segment is None: # Get the peer port of the far end termination
return path + [(peer_port, None, None)] endpoint = get_peer_port(far_end)
if endpoint is None:
return path + next_segment return path
def get_cable_peer(self): def get_cable_peer(self):
if self.cable is None: if self.cable is None:
@ -169,6 +191,7 @@ class CableTermination(models.Model):
# Console ports # Console ports
# #
@extras_features('export_templates', 'webhooks')
class ConsolePort(CableTermination, ComponentModel): class ConsolePort(CableTermination, ComponentModel):
""" """
A physical console port within a Device. ConsolePorts connect to ConsoleServerPorts. A physical console port within a Device. ConsolePorts connect to ConsoleServerPorts.
@ -229,6 +252,7 @@ class ConsolePort(CableTermination, ComponentModel):
# Console server ports # Console server ports
# #
@extras_features('webhooks')
class ConsoleServerPort(CableTermination, ComponentModel): class ConsoleServerPort(CableTermination, ComponentModel):
""" """
A physical port within a Device (typically a designated console server) which provides access to ConsolePorts. A physical port within a Device (typically a designated console server) which provides access to ConsolePorts.
@ -282,6 +306,7 @@ class ConsoleServerPort(CableTermination, ComponentModel):
# Power ports # Power ports
# #
@extras_features('export_templates', 'webhooks')
class PowerPort(CableTermination, ComponentModel): class PowerPort(CableTermination, ComponentModel):
""" """
A physical power supply (intake) port within a Device. PowerPorts connect to PowerOutlets. A physical power supply (intake) port within a Device. PowerPorts connect to PowerOutlets.
@ -443,6 +468,7 @@ class PowerPort(CableTermination, ComponentModel):
# Power outlets # Power outlets
# #
@extras_features('webhooks')
class PowerOutlet(CableTermination, ComponentModel): class PowerOutlet(CableTermination, ComponentModel):
""" """
A physical power outlet (output) within a Device which provides power to a PowerPort. A physical power outlet (output) within a Device which provides power to a PowerPort.
@ -519,6 +545,7 @@ class PowerOutlet(CableTermination, ComponentModel):
# Interfaces # Interfaces
# #
@extras_features('graphs', 'export_templates', 'webhooks')
class Interface(CableTermination, ComponentModel): class Interface(CableTermination, ComponentModel):
""" """
A network interface within a Device or VirtualMachine. A physical Interface can connect to exactly one other A network interface within a Device or VirtualMachine. A physical Interface can connect to exactly one other
@ -792,6 +819,7 @@ class Interface(CableTermination, ComponentModel):
# Pass-through ports # Pass-through ports
# #
@extras_features('webhooks')
class FrontPort(CableTermination, ComponentModel): class FrontPort(CableTermination, ComponentModel):
""" """
A pass-through port on the front of a Device. A pass-through port on the front of a Device.
@ -864,6 +892,7 @@ class FrontPort(CableTermination, ComponentModel):
) )
@extras_features('webhooks')
class RearPort(CableTermination, ComponentModel): class RearPort(CableTermination, ComponentModel):
""" """
A pass-through port on the rear of a Device. A pass-through port on the rear of a Device.
@ -915,6 +944,7 @@ class RearPort(CableTermination, ComponentModel):
# Device bays # Device bays
# #
@extras_features('webhooks')
class DeviceBay(ComponentModel): class DeviceBay(ComponentModel):
""" """
An empty space within a Device which can house a child device An empty space within a Device which can house a child device
@ -989,6 +1019,7 @@ class DeviceBay(ComponentModel):
# Inventory items # Inventory items
# #
@extras_features('export_templates', 'webhooks')
class InventoryItem(ComponentModel): class InventoryItem(ComponentModel):
""" """
An InventoryItem represents a serialized piece of hardware within a Device, such as a line card or power supply. An InventoryItem represents a serialized piece of hardware within a Device, such as a line card or power supply.

View File

@ -1,3 +1,5 @@
import logging
from django.db.models.signals import post_save, pre_delete from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver from django.dispatch import receiver
@ -34,18 +36,22 @@ def update_connected_endpoints(instance, **kwargs):
""" """
When a Cable is saved, check for and update its two connected endpoints When a Cable is saved, check for and update its two connected endpoints
""" """
logger = logging.getLogger('netbox.dcim.cable')
# Cache the Cable on its two termination points # Cache the Cable on its two termination points
if instance.termination_a.cable != instance: if instance.termination_a.cable != instance:
logger.debug("Updating termination A for cable {}".format(instance))
instance.termination_a.cable = instance instance.termination_a.cable = instance
instance.termination_a.save() instance.termination_a.save()
if instance.termination_b.cable != instance: if instance.termination_b.cable != instance:
logger.debug("Updating termination B for cable {}".format(instance))
instance.termination_b.cable = instance instance.termination_b.cable = instance
instance.termination_b.save() instance.termination_b.save()
# Check if this Cable has formed a complete path. If so, update both endpoints. # Check if this Cable has formed a complete path. If so, update both endpoints.
endpoint_a, endpoint_b, path_status = instance.get_path_endpoints() endpoint_a, endpoint_b, path_status = instance.get_path_endpoints()
if getattr(endpoint_a, 'is_path_endpoint', False) and getattr(endpoint_b, 'is_path_endpoint', False): if getattr(endpoint_a, 'is_path_endpoint', False) and getattr(endpoint_b, 'is_path_endpoint', False):
logger.debug("Updating path endpoints: {} <---> {}".format(endpoint_a, endpoint_b))
endpoint_a.connected_endpoint = endpoint_b endpoint_a.connected_endpoint = endpoint_b
endpoint_a.connection_status = path_status endpoint_a.connection_status = path_status
endpoint_a.save() endpoint_a.save()
@ -59,18 +65,23 @@ def nullify_connected_endpoints(instance, **kwargs):
""" """
When a Cable is deleted, check for and update its two connected endpoints When a Cable is deleted, check for and update its two connected endpoints
""" """
logger = logging.getLogger('netbox.dcim.cable')
endpoint_a, endpoint_b, _ = instance.get_path_endpoints() endpoint_a, endpoint_b, _ = instance.get_path_endpoints()
# Disassociate the Cable from its termination points # Disassociate the Cable from its termination points
if instance.termination_a is not None: if instance.termination_a is not None:
logger.debug("Nullifying termination A for cable {}".format(instance))
instance.termination_a.cable = None instance.termination_a.cable = None
instance.termination_a.save() instance.termination_a.save()
if instance.termination_b is not None: if instance.termination_b is not None:
logger.debug("Nullifying termination B for cable {}".format(instance))
instance.termination_b.cable = None instance.termination_b.cable = None
instance.termination_b.save() instance.termination_b.save()
# If this Cable was part of a complete path, tear it down # If this Cable was part of a complete path, tear it down
if hasattr(endpoint_a, 'connected_endpoint') and hasattr(endpoint_b, 'connected_endpoint'): if hasattr(endpoint_a, 'connected_endpoint') and hasattr(endpoint_b, 'connected_endpoint'):
logger.debug("Tearing down path ({} <---> {})".format(endpoint_a, endpoint_b))
endpoint_a.connected_endpoint = None endpoint_a.connected_endpoint = None
endpoint_a.connection_status = None endpoint_a.connection_status = None
endpoint_a.save() endpoint_a.save()

View File

@ -338,21 +338,38 @@ class RackDetailTable(RackTable):
class RackReservationTable(BaseTable): class RackReservationTable(BaseTable):
pk = ToggleColumn() pk = ToggleColumn()
reservation = tables.LinkColumn(
viewname='dcim:rackreservation',
args=[Accessor('pk')],
accessor='pk'
)
site = tables.LinkColumn( site = tables.LinkColumn(
viewname='dcim:site', viewname='dcim:site',
accessor=Accessor('rack.site'), accessor=Accessor('rack.site'),
args=[Accessor('rack.site.slug')], args=[Accessor('rack.site.slug')],
) )
tenant = tables.TemplateColumn(template_code=COL_TENANT) tenant = tables.TemplateColumn(
rack = tables.LinkColumn('dcim:rack', args=[Accessor('rack.pk')]) template_code=COL_TENANT
unit_list = tables.Column(orderable=False, verbose_name='Units') )
rack = tables.LinkColumn(
viewname='dcim:rack',
args=[Accessor('rack.pk')]
)
unit_list = tables.Column(
orderable=False,
verbose_name='Units'
)
actions = tables.TemplateColumn( actions = tables.TemplateColumn(
template_code=RACKRESERVATION_ACTIONS, attrs={'td': {'class': 'text-right noprint'}}, verbose_name='' template_code=RACKRESERVATION_ACTIONS,
attrs={'td': {'class': 'text-right noprint'}},
verbose_name=''
) )
class Meta(BaseTable.Meta): class Meta(BaseTable.Meta):
model = RackReservation model = RackReservation
fields = ('pk', 'site', 'rack', 'unit_list', 'user', 'created', 'tenant', 'description', 'actions') fields = (
'pk', 'reservation', 'site', 'rack', 'unit_list', 'user', 'created', 'tenant', 'description', 'actions',
)
# #
@ -840,7 +857,7 @@ class DeviceBayTable(BaseTable):
class Meta(BaseTable.Meta): class Meta(BaseTable.Meta):
model = DeviceBay model = DeviceBay
fields = ('name',) fields = ('name', 'description')
class DeviceBayDetailTable(DeviceComponentDetailTable): class DeviceBayDetailTable(DeviceComponentDetailTable):
@ -848,8 +865,8 @@ class DeviceBayDetailTable(DeviceComponentDetailTable):
installed_device = tables.LinkColumn() installed_device = tables.LinkColumn()
class Meta(DeviceBayTable.Meta): class Meta(DeviceBayTable.Meta):
fields = ('pk', 'name', 'device', 'installed_device') fields = ('pk', 'name', 'device', 'installed_device', 'description')
sequence = ('pk', 'name', 'device', 'installed_device') sequence = ('pk', 'name', 'device', 'installed_device', 'description')
exclude = ('cable',) exclude = ('cable',)

View File

@ -2089,6 +2089,20 @@ class DeviceTest(APITestCase):
self.assertFalse('config_context' in response.data['results'][0]) self.assertFalse('config_context' in response.data['results'][0])
def test_unique_name_per_site_constraint(self):
data = {
'device_type': self.devicetype1.pk,
'device_role': self.devicerole1.pk,
'name': 'Test Device 1',
'site': self.site1.pk,
}
url = reverse('dcim-api:device-list')
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
class ConsolePortTest(APITestCase): class ConsolePortTest(APITestCase):

View File

@ -1,6 +1,7 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.test import TestCase from django.test import TestCase
from circuits.models import *
from dcim.choices import * from dcim.choices import *
from dcim.models import * from dcim.models import *
from tenancy.models import Tenant from tenancy.models import Tenant
@ -459,95 +460,346 @@ class CableTestCase(TestCase):
class CablePathTestCase(TestCase): class CablePathTestCase(TestCase):
def setUp(self): @classmethod
def setUpTestData(cls):
site = Site.objects.create(name='Test Site 1', slug='test-site-1') site = Site.objects.create(name='Site 1', slug='site-1')
manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1') manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
devicetype = DeviceType.objects.create( devicetype = DeviceType.objects.create(
manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1' manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
) )
devicerole = DeviceRole.objects.create( devicerole = DeviceRole.objects.create(
name='Test Device Role 1', slug='test-device-role-1', color='ff0000' name='Device Role 1', slug='device-role-1', color='ff0000'
)
self.device1 = Device.objects.create(
device_type=devicetype, device_role=devicerole, name='Test Device 1', site=site
)
self.device2 = Device.objects.create(
device_type=devicetype, device_role=devicerole, name='Test Device 2', site=site
)
self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
self.panel1 = Device.objects.create(
device_type=devicetype, device_role=devicerole, name='Test Panel 1', site=site
)
self.panel2 = Device.objects.create(
device_type=devicetype, device_role=devicerole, name='Test Panel 2', site=site
)
self.rear_port1 = RearPort.objects.create(
device=self.panel1, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C
)
self.front_port1 = FrontPort.objects.create(
device=self.panel1, name='Front Port 1', type=PortTypeChoices.TYPE_8P8C, rear_port=self.rear_port1
)
self.rear_port2 = RearPort.objects.create(
device=self.panel2, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C
)
self.front_port2 = FrontPort.objects.create(
device=self.panel2, name='Front Port 2', type=PortTypeChoices.TYPE_8P8C, rear_port=self.rear_port2
) )
provider = Provider.objects.create(name='Provider 1', slug='provider-1')
circuittype = CircuitType.objects.create(name='Circuit Type 1', slug='circuit-type-1')
circuit = Circuit.objects.create(provider=provider, type=circuittype, cid='1')
CircuitTermination.objects.bulk_create((
CircuitTermination(circuit=circuit, site=site, term_side='A', port_speed=1000),
CircuitTermination(circuit=circuit, site=site, term_side='Z', port_speed=1000),
))
def test_path_completion(self): # Create four network devices with four interfaces each
devices = (
Device(device_type=devicetype, device_role=devicerole, name='Device 1', site=site),
Device(device_type=devicetype, device_role=devicerole, name='Device 2', site=site),
Device(device_type=devicetype, device_role=devicerole, name='Device 3', site=site),
Device(device_type=devicetype, device_role=devicerole, name='Device 4', site=site),
)
Device.objects.bulk_create(devices)
for device in devices:
Interface.objects.bulk_create((
Interface(device=device, name='Interface 1', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
Interface(device=device, name='Interface 2', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
Interface(device=device, name='Interface 3', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
Interface(device=device, name='Interface 4', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
))
# First segment # Create four patch panels, each with one rear port and four front ports
cable1 = Cable(termination_a=self.interface1, termination_b=self.front_port1) patch_panels = (
Device(device_type=devicetype, device_role=devicerole, name='Panel 1', site=site),
Device(device_type=devicetype, device_role=devicerole, name='Panel 2', site=site),
Device(device_type=devicetype, device_role=devicerole, name='Panel 3', site=site),
Device(device_type=devicetype, device_role=devicerole, name='Panel 4', site=site),
)
Device.objects.bulk_create(patch_panels)
for patch_panel in patch_panels:
rearport = RearPort.objects.create(device=patch_panel, name='Rear Port 1', positions=4, type=PortTypeChoices.TYPE_8P8C)
FrontPort.objects.bulk_create((
FrontPort(device=patch_panel, name='Front Port 1', rear_port=rearport, rear_port_position=1, type=PortTypeChoices.TYPE_8P8C),
FrontPort(device=patch_panel, name='Front Port 2', rear_port=rearport, rear_port_position=2, type=PortTypeChoices.TYPE_8P8C),
FrontPort(device=patch_panel, name='Front Port 3', rear_port=rearport, rear_port_position=3, type=PortTypeChoices.TYPE_8P8C),
FrontPort(device=patch_panel, name='Front Port 4', rear_port=rearport, rear_port_position=4, type=PortTypeChoices.TYPE_8P8C),
))
def test_direct_connection(self):
"""
[Device 1] ----- [Device 2]
Iface1 Iface1
"""
# Create cable
cable = Cable(
termination_a=Interface.objects.get(device__name='Device 1', name='Interface 1'),
termination_b=Interface.objects.get(device__name='Device 2', name='Interface 1')
)
cable.save()
# Retrieve endpoints
endpoint_a = Interface.objects.get(device__name='Device 1', name='Interface 1')
endpoint_b = Interface.objects.get(device__name='Device 2', name='Interface 1')
# Validate connections
self.assertEqual(endpoint_a.connected_endpoint, endpoint_b)
self.assertEqual(endpoint_b.connected_endpoint, endpoint_a)
self.assertTrue(endpoint_a.connection_status)
self.assertTrue(endpoint_b.connection_status)
# Delete cable
cable.delete()
# Refresh endpoints
endpoint_a.refresh_from_db()
endpoint_b.refresh_from_db()
# Check that connections have been nullified
self.assertIsNone(endpoint_a.connected_endpoint)
self.assertIsNone(endpoint_b.connected_endpoint)
self.assertIsNone(endpoint_a.connection_status)
self.assertIsNone(endpoint_b.connection_status)
def test_connection_via_patch(self):
"""
1 2 3
[Device 1] ----- [Panel 1] ----- [Panel 2] ----- [Device 2]
Iface1 FP1 RP1 RP1 FP1 Iface1
"""
# Create cables
cable1 = Cable(
termination_a=Interface.objects.get(device__name='Device 1', name='Interface 1'),
termination_b=FrontPort.objects.get(device__name='Panel 1', name='Front Port 1')
)
cable1.save() cable1.save()
interface1 = Interface.objects.get(pk=self.interface1.pk) cable2 = Cable(
self.assertIsNone(interface1.connected_endpoint) termination_a=RearPort.objects.get(device__name='Panel 1', name='Rear Port 1'),
self.assertIsNone(interface1.connection_status) termination_b=RearPort.objects.get(device__name='Panel 2', name='Rear Port 1')
)
# Second segment
cable2 = Cable(termination_a=self.rear_port1, termination_b=self.rear_port2)
cable2.save() cable2.save()
interface1 = Interface.objects.get(pk=self.interface1.pk)
self.assertIsNone(interface1.connected_endpoint)
self.assertIsNone(interface1.connection_status)
# Third segment
cable3 = Cable( cable3 = Cable(
termination_a=self.front_port2, termination_a=FrontPort.objects.get(device__name='Panel 2', name='Front Port 1'),
termination_b=self.interface2, termination_b=Interface.objects.get(device__name='Device 2', name='Interface 1')
status=CableStatusChoices.STATUS_PLANNED
) )
cable3.save() cable3.save()
interface1 = Interface.objects.get(pk=self.interface1.pk)
self.assertEqual(interface1.connected_endpoint, self.interface2)
self.assertFalse(interface1.connection_status)
# Switch third segment from planned to connected # Retrieve endpoints
cable3.status = CableStatusChoices.STATUS_CONNECTED endpoint_a = Interface.objects.get(device__name='Device 1', name='Interface 1')
cable3.save() endpoint_b = Interface.objects.get(device__name='Device 2', name='Interface 1')
interface1 = Interface.objects.get(pk=self.interface1.pk)
self.assertEqual(interface1.connected_endpoint, self.interface2)
self.assertTrue(interface1.connection_status)
def test_path_teardown(self): # Validate connections
self.assertEqual(endpoint_a.connected_endpoint, endpoint_b)
self.assertEqual(endpoint_b.connected_endpoint, endpoint_a)
self.assertTrue(endpoint_a.connection_status)
self.assertTrue(endpoint_b.connection_status)
# Build the path # Delete cable 2
cable1 = Cable(termination_a=self.interface1, termination_b=self.front_port1)
cable1.save()
cable2 = Cable(termination_a=self.rear_port1, termination_b=self.rear_port2)
cable2.save()
cable3 = Cable(termination_a=self.front_port2, termination_b=self.interface2)
cable3.save()
interface1 = Interface.objects.get(pk=self.interface1.pk)
self.assertEqual(interface1.connected_endpoint, self.interface2)
self.assertTrue(interface1.connection_status)
# Remove a cable
cable2.delete() cable2.delete()
interface1 = Interface.objects.get(pk=self.interface1.pk)
self.assertIsNone(interface1.connected_endpoint) # Refresh endpoints
self.assertIsNone(interface1.connection_status) endpoint_a.refresh_from_db()
interface2 = Interface.objects.get(pk=self.interface2.pk) endpoint_b.refresh_from_db()
self.assertIsNone(interface2.connected_endpoint)
self.assertIsNone(interface2.connection_status) # Check that connections have been nullified
self.assertIsNone(endpoint_a.connected_endpoint)
self.assertIsNone(endpoint_b.connected_endpoint)
self.assertIsNone(endpoint_a.connection_status)
self.assertIsNone(endpoint_b.connection_status)
def test_connection_via_multiple_patches(self):
"""
1 2 3 4 5
[Device 1] ----- [Panel 1] ----- [Panel 2] ----- [Panel 3] ----- [Panel 4] ----- [Device 2]
Iface1 FP1 RP1 RP1 FP1 FP1 RP1 RP1 FP1 Iface1
"""
# Create cables
cable1 = Cable(
termination_a=Interface.objects.get(device__name='Device 1', name='Interface 1'),
termination_b=FrontPort.objects.get(device__name='Panel 1', name='Front Port 1')
)
cable1.save()
cable2 = Cable(
termination_a=RearPort.objects.get(device__name='Panel 1', name='Rear Port 1'),
termination_b=RearPort.objects.get(device__name='Panel 2', name='Rear Port 1')
)
cable2.save()
cable3 = Cable(
termination_a=FrontPort.objects.get(device__name='Panel 2', name='Front Port 1'),
termination_b=FrontPort.objects.get(device__name='Panel 3', name='Front Port 1')
)
cable3.save()
cable4 = Cable(
termination_a=RearPort.objects.get(device__name='Panel 3', name='Rear Port 1'),
termination_b=RearPort.objects.get(device__name='Panel 4', name='Rear Port 1')
)
cable4.save()
cable5 = Cable(
termination_a=FrontPort.objects.get(device__name='Panel 4', name='Front Port 1'),
termination_b=Interface.objects.get(device__name='Device 2', name='Interface 1')
)
cable5.save()
# Retrieve endpoints
endpoint_a = Interface.objects.get(device__name='Device 1', name='Interface 1')
endpoint_b = Interface.objects.get(device__name='Device 2', name='Interface 1')
# Validate connections
self.assertEqual(endpoint_a.connected_endpoint, endpoint_b)
self.assertEqual(endpoint_b.connected_endpoint, endpoint_a)
self.assertTrue(endpoint_a.connection_status)
self.assertTrue(endpoint_b.connection_status)
# Delete cable 3
cable3.delete()
# Refresh endpoints
endpoint_a.refresh_from_db()
endpoint_b.refresh_from_db()
# Check that connections have been nullified
self.assertIsNone(endpoint_a.connected_endpoint)
self.assertIsNone(endpoint_b.connected_endpoint)
self.assertIsNone(endpoint_a.connection_status)
self.assertIsNone(endpoint_b.connection_status)
def test_connection_via_stacked_rear_ports(self):
"""
1 2 3 4 5
[Device 1] ----- [Panel 1] ----- [Panel 2] ----- [Panel 3] ----- [Panel 4] ----- [Device 2]
Iface1 FP1 RP1 FP1 RP1 RP1 FP1 RP1 FP1 Iface1
"""
# Create cables
cable1 = Cable(
termination_a=Interface.objects.get(device__name='Device 1', name='Interface 1'),
termination_b=FrontPort.objects.get(device__name='Panel 1', name='Front Port 1')
)
cable1.save()
cable2 = Cable(
termination_a=RearPort.objects.get(device__name='Panel 1', name='Rear Port 1'),
termination_b=FrontPort.objects.get(device__name='Panel 2', name='Front Port 1')
)
cable2.save()
cable3 = Cable(
termination_a=RearPort.objects.get(device__name='Panel 2', name='Rear Port 1'),
termination_b=RearPort.objects.get(device__name='Panel 3', name='Rear Port 1')
)
cable3.save()
cable4 = Cable(
termination_a=FrontPort.objects.get(device__name='Panel 3', name='Front Port 1'),
termination_b=RearPort.objects.get(device__name='Panel 4', name='Rear Port 1')
)
cable4.save()
cable5 = Cable(
termination_a=FrontPort.objects.get(device__name='Panel 4', name='Front Port 1'),
termination_b=Interface.objects.get(device__name='Device 2', name='Interface 1')
)
cable5.save()
# Retrieve endpoints
endpoint_a = Interface.objects.get(device__name='Device 1', name='Interface 1')
endpoint_b = Interface.objects.get(device__name='Device 2', name='Interface 1')
# Validate connections
self.assertEqual(endpoint_a.connected_endpoint, endpoint_b)
self.assertEqual(endpoint_b.connected_endpoint, endpoint_a)
self.assertTrue(endpoint_a.connection_status)
self.assertTrue(endpoint_b.connection_status)
# Delete cable 3
cable3.delete()
# Refresh endpoints
endpoint_a.refresh_from_db()
endpoint_b.refresh_from_db()
# Check that connections have been nullified
self.assertIsNone(endpoint_a.connected_endpoint)
self.assertIsNone(endpoint_b.connected_endpoint)
self.assertIsNone(endpoint_a.connection_status)
self.assertIsNone(endpoint_b.connection_status)
def test_connection_via_circuit(self):
"""
1 2
[Device 1] ----- [Circuit] ----- [Device 2]
Iface1 A Z Iface1
"""
# Create cables
cable1 = Cable(
termination_a=Interface.objects.get(device__name='Device 1', name='Interface 1'),
termination_b=CircuitTermination.objects.get(term_side='A')
)
cable1.save()
cable2 = Cable(
termination_a=CircuitTermination.objects.get(term_side='Z'),
termination_b=Interface.objects.get(device__name='Device 2', name='Interface 1')
)
cable2.save()
# Retrieve endpoints
endpoint_a = Interface.objects.get(device__name='Device 1', name='Interface 1')
endpoint_b = Interface.objects.get(device__name='Device 2', name='Interface 1')
# Validate connections
self.assertEqual(endpoint_a.connected_endpoint, endpoint_b)
self.assertEqual(endpoint_b.connected_endpoint, endpoint_a)
self.assertTrue(endpoint_a.connection_status)
self.assertTrue(endpoint_b.connection_status)
# Delete circuit
circuit = Circuit.objects.first().delete()
# Refresh endpoints
endpoint_a.refresh_from_db()
endpoint_b.refresh_from_db()
# Check that connections have been nullified
self.assertIsNone(endpoint_a.connected_endpoint)
self.assertIsNone(endpoint_b.connected_endpoint)
self.assertIsNone(endpoint_a.connection_status)
self.assertIsNone(endpoint_b.connection_status)
def test_connection_via_patched_circuit(self):
"""
1 2 3 4
[Device 1] ----- [Panel 1] ----- [Circuit] ----- [Panel 2] ----- [Device 2]
Iface1 FP1 RP1 A Z RP1 FP1 Iface1
"""
# Create cables
cable1 = Cable(
termination_a=Interface.objects.get(device__name='Device 1', name='Interface 1'),
termination_b=FrontPort.objects.get(device__name='Panel 1', name='Front Port 1')
)
cable1.save()
cable2 = Cable(
termination_a=RearPort.objects.get(device__name='Panel 1', name='Rear Port 1'),
termination_b=CircuitTermination.objects.get(term_side='A')
)
cable2.save()
cable3 = Cable(
termination_a=CircuitTermination.objects.get(term_side='Z'),
termination_b=RearPort.objects.get(device__name='Panel 2', name='Rear Port 1')
)
cable3.save()
cable4 = Cable(
termination_a=FrontPort.objects.get(device__name='Panel 2', name='Front Port 1'),
termination_b=Interface.objects.get(device__name='Device 2', name='Interface 1')
)
cable4.save()
# Retrieve endpoints
endpoint_a = Interface.objects.get(device__name='Device 1', name='Interface 1')
endpoint_b = Interface.objects.get(device__name='Device 2', name='Interface 1')
# Validate connections
self.assertEqual(endpoint_a.connected_endpoint, endpoint_b)
self.assertEqual(endpoint_b.connected_endpoint, endpoint_a)
self.assertTrue(endpoint_a.connection_status)
self.assertTrue(endpoint_b.connection_status)
# Delete circuit
circuit = Circuit.objects.first().delete()
# Refresh endpoints
endpoint_a.refresh_from_db()
endpoint_b.refresh_from_db()
# Check that connections have been nullified
self.assertIsNone(endpoint_a.connected_endpoint)
self.assertIsNone(endpoint_b.connected_endpoint)
self.assertIsNone(endpoint_a.connection_status)
self.assertIsNone(endpoint_b.connection_status)

View File

@ -172,10 +172,6 @@ class RackRoleTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
class RackReservationTestCase(ViewTestCases.PrimaryObjectViewTestCase): class RackReservationTestCase(ViewTestCases.PrimaryObjectViewTestCase):
model = RackReservation model = RackReservation
# Disable inapplicable tests
test_get_object = None
test_create_object = None
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
@ -1333,37 +1329,37 @@ class RearPortTestCase(ViewTestCases.DeviceComponentViewTestCase):
class DeviceBayTestCase(ViewTestCases.DeviceComponentViewTestCase): class DeviceBayTestCase(ViewTestCases.DeviceComponentViewTestCase):
model = DeviceBay model = DeviceBay
# Disable inapplicable views
test_bulk_edit_objects = None
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
device1 = create_test_device('Device 1') device = create_test_device('Device 1')
device2 = create_test_device('Device 2')
# Update the DeviceType subdevice role to allow adding DeviceBays # Update the DeviceType subdevice role to allow adding DeviceBays
DeviceType.objects.update(subdevice_role=SubdeviceRoleChoices.ROLE_PARENT) DeviceType.objects.update(subdevice_role=SubdeviceRoleChoices.ROLE_PARENT)
DeviceBay.objects.bulk_create([ DeviceBay.objects.bulk_create([
DeviceBay(device=device1, name='Device Bay 1'), DeviceBay(device=device, name='Device Bay 1'),
DeviceBay(device=device1, name='Device Bay 2'), DeviceBay(device=device, name='Device Bay 2'),
DeviceBay(device=device1, name='Device Bay 3'), DeviceBay(device=device, name='Device Bay 3'),
]) ])
cls.form_data = { cls.form_data = {
'device': device2.pk, 'device': device.pk,
'name': 'Device Bay X', 'name': 'Device Bay X',
'description': 'A device bay', 'description': 'A device bay',
'tags': 'Alpha,Bravo,Charlie', 'tags': 'Alpha,Bravo,Charlie',
} }
cls.bulk_create_data = { cls.bulk_create_data = {
'device': device2.pk, 'device': device.pk,
'name_pattern': 'Device Bay [4-6]', 'name_pattern': 'Device Bay [4-6]',
'description': 'A device bay', 'description': 'A device bay',
'tags': 'Alpha,Bravo,Charlie', 'tags': 'Alpha,Bravo,Charlie',
} }
cls.bulk_edit_data = {
'description': 'New description',
}
cls.csv_data = ( cls.csv_data = (
"device,name", "device,name",
"Device 1,Device Bay 4", "Device 1,Device Bay 4",

View File

@ -2,7 +2,6 @@ from django.urls import path
from extras.views import ObjectChangeLogView, ImageAttachmentEditView from extras.views import ObjectChangeLogView, ImageAttachmentEditView
from ipam.views import ServiceCreateView from ipam.views import ServiceCreateView
from secrets.views import secret_add
from . import views from . import views
from .models import ( from .models import (
Cable, ConsolePort, ConsoleServerPort, Device, DeviceRole, DeviceType, FrontPort, Interface, Manufacturer, Platform, Cable, ConsolePort, ConsoleServerPort, Device, DeviceRole, DeviceType, FrontPort, Interface, Manufacturer, Platform,
@ -51,9 +50,11 @@ urlpatterns = [
# Rack reservations # Rack reservations
path('rack-reservations/', views.RackReservationListView.as_view(), name='rackreservation_list'), path('rack-reservations/', views.RackReservationListView.as_view(), name='rackreservation_list'),
path('rack-reservations/add/', views.RackReservationCreateView.as_view(), name='rackreservation_add'),
path('rack-reservations/import/', views.RackReservationImportView.as_view(), name='rackreservation_import'), path('rack-reservations/import/', views.RackReservationImportView.as_view(), name='rackreservation_import'),
path('rack-reservations/edit/', views.RackReservationBulkEditView.as_view(), name='rackreservation_bulk_edit'), path('rack-reservations/edit/', views.RackReservationBulkEditView.as_view(), name='rackreservation_bulk_edit'),
path('rack-reservations/delete/', views.RackReservationBulkDeleteView.as_view(), name='rackreservation_bulk_delete'), path('rack-reservations/delete/', views.RackReservationBulkDeleteView.as_view(), name='rackreservation_bulk_delete'),
path('rack-reservations/<int:pk>/', views.RackReservationView.as_view(), name='rackreservation'),
path('rack-reservations/<int:pk>/edit/', views.RackReservationEditView.as_view(), name='rackreservation_edit'), path('rack-reservations/<int:pk>/edit/', views.RackReservationEditView.as_view(), name='rackreservation_edit'),
path('rack-reservations/<int:pk>/delete/', views.RackReservationDeleteView.as_view(), name='rackreservation_delete'), path('rack-reservations/<int:pk>/delete/', views.RackReservationDeleteView.as_view(), name='rackreservation_delete'),
path('rack-reservations/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='rackreservation_changelog', kwargs={'model': RackReservation}), path('rack-reservations/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='rackreservation_changelog', kwargs={'model': RackReservation}),
@ -69,7 +70,6 @@ urlpatterns = [
path('racks/<int:pk>/edit/', views.RackEditView.as_view(), name='rack_edit'), path('racks/<int:pk>/edit/', views.RackEditView.as_view(), name='rack_edit'),
path('racks/<int:pk>/delete/', views.RackDeleteView.as_view(), name='rack_delete'), path('racks/<int:pk>/delete/', views.RackDeleteView.as_view(), name='rack_delete'),
path('racks/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='rack_changelog', kwargs={'model': Rack}), path('racks/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='rack_changelog', kwargs={'model': Rack}),
path('racks/<int:rack>/reservations/add/', views.RackReservationCreateView.as_view(), name='rack_add_reservation'),
path('racks/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='rack_add_image', kwargs={'model': Rack}), path('racks/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='rack_add_image', kwargs={'model': Rack}),
# Manufacturers # Manufacturers
@ -179,7 +179,6 @@ urlpatterns = [
path('devices/<int:pk>/status/', views.DeviceStatusView.as_view(), name='device_status'), path('devices/<int:pk>/status/', views.DeviceStatusView.as_view(), name='device_status'),
path('devices/<int:pk>/lldp-neighbors/', views.DeviceLLDPNeighborsView.as_view(), name='device_lldp_neighbors'), path('devices/<int:pk>/lldp-neighbors/', views.DeviceLLDPNeighborsView.as_view(), name='device_lldp_neighbors'),
path('devices/<int:pk>/config/', views.DeviceConfigView.as_view(), name='device_config'), path('devices/<int:pk>/config/', views.DeviceConfigView.as_view(), name='device_config'),
path('devices/<int:pk>/add-secret/', secret_add, name='device_addsecret'),
path('devices/<int:device>/services/assign/', ServiceCreateView.as_view(), name='device_service_assign'), path('devices/<int:device>/services/assign/', ServiceCreateView.as_view(), name='device_service_assign'),
path('devices/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='device_add_image', kwargs={'model': Device}), path('devices/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='device_add_image', kwargs={'model': Device}),
@ -285,7 +284,7 @@ urlpatterns = [
path('device-bays/', views.DeviceBayListView.as_view(), name='devicebay_list'), path('device-bays/', views.DeviceBayListView.as_view(), name='devicebay_list'),
path('device-bays/add/', views.DeviceBayCreateView.as_view(), name='devicebay_add'), path('device-bays/add/', views.DeviceBayCreateView.as_view(), name='devicebay_add'),
path('device-bays/import/', views.DeviceBayBulkImportView.as_view(), name='devicebay_import'), path('device-bays/import/', views.DeviceBayBulkImportView.as_view(), name='devicebay_import'),
# TODO: Bulk edit view for DeviceBays path('device-bays/edit/', views.DeviceBayBulkEditView.as_view(), name='devicebay_bulk_edit'),
path('device-bays/rename/', views.DeviceBayBulkRenameView.as_view(), name='devicebay_bulk_rename'), path('device-bays/rename/', views.DeviceBayBulkRenameView.as_view(), name='devicebay_bulk_rename'),
path('device-bays/delete/', views.DeviceBayBulkDeleteView.as_view(), name='devicebay_bulk_delete'), path('device-bays/delete/', views.DeviceBayBulkDeleteView.as_view(), name='devicebay_bulk_delete'),
path('device-bays/<int:pk>/edit/', views.DeviceBayEditView.as_view(), name='devicebay_edit'), path('device-bays/<int:pk>/edit/', views.DeviceBayEditView.as_view(), name='devicebay_edit'),

View File

@ -473,20 +473,32 @@ class RackReservationListView(PermissionRequiredMixin, ObjectListView):
action_buttons = ('export',) action_buttons = ('export',)
class RackReservationView(PermissionRequiredMixin, View):
permission_required = 'dcim.view_rackreservation'
def get(self, request, pk):
rackreservation = get_object_or_404(RackReservation.objects.prefetch_related('rack'), pk=pk)
return render(request, 'dcim/rackreservation.html', {
'rackreservation': rackreservation,
})
class RackReservationCreateView(PermissionRequiredMixin, ObjectEditView): class RackReservationCreateView(PermissionRequiredMixin, ObjectEditView):
permission_required = 'dcim.add_rackreservation' permission_required = 'dcim.add_rackreservation'
model = RackReservation model = RackReservation
model_form = forms.RackReservationForm model_form = forms.RackReservationForm
template_name = 'dcim/rackreservation_edit.html'
default_return_url = 'dcim:rackreservation_list'
def alter_obj(self, obj, request, args, kwargs): def alter_obj(self, obj, request, args, kwargs):
if not obj.pk: if not obj.pk:
obj.rack = get_object_or_404(Rack, pk=kwargs['rack']) if 'rack' in request.GET:
obj.rack = get_object_or_404(Rack, pk=request.GET.get('rack'))
obj.user = request.user obj.user = request.user
return obj return obj
def get_return_url(self, request, obj):
return obj.rack.get_absolute_url()
class RackReservationEditView(RackReservationCreateView): class RackReservationEditView(RackReservationCreateView):
permission_required = 'dcim.change_rackreservation' permission_required = 'dcim.change_rackreservation'
@ -495,9 +507,7 @@ class RackReservationEditView(RackReservationCreateView):
class RackReservationDeleteView(PermissionRequiredMixin, ObjectDeleteView): class RackReservationDeleteView(PermissionRequiredMixin, ObjectDeleteView):
permission_required = 'dcim.delete_rackreservation' permission_required = 'dcim.delete_rackreservation'
model = RackReservation model = RackReservation
default_return_url = 'dcim:rackreservation_list'
def get_return_url(self, request, obj):
return obj.rack.get_absolute_url()
class RackReservationImportView(PermissionRequiredMixin, BulkImportView): class RackReservationImportView(PermissionRequiredMixin, BulkImportView):
@ -1883,6 +1893,14 @@ class DeviceBayBulkImportView(PermissionRequiredMixin, BulkImportView):
default_return_url = 'dcim:devicebay_list' default_return_url = 'dcim:devicebay_list'
class DeviceBayBulkEditView(PermissionRequiredMixin, BulkEditView):
permission_required = 'dcim.change_devicebay'
queryset = DeviceBay.objects.all()
filterset = filters.DeviceBayFilterSet
table = tables.DeviceBayTable
form = forms.DeviceBayBulkEditForm
class DeviceBayBulkRenameView(PermissionRequiredMixin, BulkRenameView): class DeviceBayBulkRenameView(PermissionRequiredMixin, BulkRenameView):
permission_required = 'dcim.change_devicebay' permission_required = 'dcim.change_devicebay'
queryset = DeviceBay.objects.all() queryset = DeviceBay.objects.all()
@ -2009,7 +2027,7 @@ class CableTraceView(PermissionRequiredMixin, View):
def get(self, request, model, pk): def get(self, request, model, pk):
obj = get_object_or_404(model, pk=pk) obj = get_object_or_404(model, pk=pk)
trace = obj.trace(follow_circuits=True) trace = obj.trace()
total_length = sum([entry[1]._abs_length for entry in trace if entry[1] and entry[1]._abs_length]) total_length = sum([entry[1]._abs_length for entry in trace if entry[1] and entry[1]._abs_length])
return render(request, 'dcim/cable_trace.html', { return render(request, 'dcim/cable_trace.html', {

View File

@ -13,6 +13,7 @@ from extras.constants import *
from extras.models import ( from extras.models import (
ConfigContext, ExportTemplate, Graph, ImageAttachment, ObjectChange, ReportResult, Tag, ConfigContext, ExportTemplate, Graph, ImageAttachment, ObjectChange, ReportResult, Tag,
) )
from extras.utils import FeatureQuery
from tenancy.api.nested_serializers import NestedTenantSerializer, NestedTenantGroupSerializer from tenancy.api.nested_serializers import NestedTenantSerializer, NestedTenantGroupSerializer
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from users.api.nested_serializers import NestedUserSerializer from users.api.nested_serializers import NestedUserSerializer
@ -31,7 +32,7 @@ from .nested_serializers import *
class GraphSerializer(ValidatedModelSerializer): class GraphSerializer(ValidatedModelSerializer):
type = ContentTypeField( type = ContentTypeField(
queryset=ContentType.objects.filter(GRAPH_MODELS), queryset=ContentType.objects.filter(FeatureQuery('graphs').get_query()),
) )
class Meta: class Meta:
@ -67,7 +68,7 @@ class RenderedGraphSerializer(serializers.ModelSerializer):
class ExportTemplateSerializer(ValidatedModelSerializer): class ExportTemplateSerializer(ValidatedModelSerializer):
content_type = ContentTypeField( content_type = ContentTypeField(
queryset=ContentType.objects.filter(EXPORTTEMPLATE_MODELS), queryset=ContentType.objects.filter(FeatureQuery('export_templates').get_query()),
) )
template_language = ChoiceField( template_language = ChoiceField(
choices=TemplateLanguageChoices, choices=TemplateLanguageChoices,

View File

@ -1,129 +1,3 @@
from django.db.models import Q
# Models which support custom fields
CUSTOMFIELD_MODELS = Q(
Q(app_label='circuits', model__in=[
'circuit',
'provider',
]) |
Q(app_label='dcim', model__in=[
'device',
'devicetype',
'powerfeed',
'rack',
'site',
]) |
Q(app_label='ipam', model__in=[
'aggregate',
'ipaddress',
'prefix',
'service',
'vlan',
'vrf',
]) |
Q(app_label='secrets', model__in=[
'secret',
]) |
Q(app_label='tenancy', model__in=[
'tenant',
]) |
Q(app_label='virtualization', model__in=[
'cluster',
'virtualmachine',
])
)
# Custom links
CUSTOMLINK_MODELS = Q(
Q(app_label='circuits', model__in=[
'circuit',
'provider',
]) |
Q(app_label='dcim', model__in=[
'cable',
'device',
'devicetype',
'powerpanel',
'powerfeed',
'rack',
'site',
]) |
Q(app_label='ipam', model__in=[
'aggregate',
'ipaddress',
'prefix',
'service',
'vlan',
'vrf',
]) |
Q(app_label='secrets', model__in=[
'secret',
]) |
Q(app_label='tenancy', model__in=[
'tenant',
]) |
Q(app_label='virtualization', model__in=[
'cluster',
'virtualmachine',
])
)
# Models which can have Graphs associated with them
GRAPH_MODELS = Q(
Q(app_label='circuits', model__in=[
'provider',
]) |
Q(app_label='dcim', model__in=[
'device',
'interface',
'site',
])
)
# Models which support export templates
EXPORTTEMPLATE_MODELS = Q(
Q(app_label='circuits', model__in=[
'circuit',
'provider',
]) |
Q(app_label='dcim', model__in=[
'cable',
'consoleport',
'device',
'devicetype',
'interface',
'inventoryitem',
'manufacturer',
'powerpanel',
'powerport',
'powerfeed',
'rack',
'rackgroup',
'region',
'site',
'virtualchassis',
]) |
Q(app_label='ipam', model__in=[
'aggregate',
'ipaddress',
'prefix',
'service',
'vlan',
'vrf',
]) |
Q(app_label='secrets', model__in=[
'secret',
]) |
Q(app_label='tenancy', model__in=[
'tenant',
]) |
Q(app_label='virtualization', model__in=[
'cluster',
'virtualmachine',
])
)
# Report logging levels # Report logging levels
LOG_DEFAULT = 0 LOG_DEFAULT = 0
LOG_SUCCESS = 10 LOG_SUCCESS = 10
@ -138,51 +12,14 @@ LOG_LEVEL_CODES = {
LOG_FAILURE: 'failure', LOG_FAILURE: 'failure',
} }
# Webhook content types
HTTP_CONTENT_TYPE_JSON = 'application/json' HTTP_CONTENT_TYPE_JSON = 'application/json'
# Models which support registered webhooks # Registerable extras features
WEBHOOK_MODELS = Q( EXTRAS_FEATURES = [
Q(app_label='circuits', model__in=[ 'custom_fields',
'circuit', 'custom_links',
'provider', 'graphs',
]) | 'export_templates',
Q(app_label='dcim', model__in=[ 'webhooks'
'cable', ]
'consoleport',
'consoleserverport',
'device',
'devicebay',
'devicetype',
'frontport',
'interface',
'inventoryitem',
'manufacturer',
'poweroutlet',
'powerpanel',
'powerport',
'powerfeed',
'rack',
'rearport',
'region',
'site',
'virtualchassis',
]) |
Q(app_label='ipam', model__in=[
'aggregate',
'ipaddress',
'prefix',
'service',
'vlan',
'vrf',
]) |
Q(app_label='secrets', model__in=[
'secret',
]) |
Q(app_label='tenancy', model__in=[
'tenant',
]) |
Q(app_label='virtualization', model__in=[
'cluster',
'virtualmachine',
])
)

View File

@ -198,60 +198,36 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm):
) )
sites = DynamicModelMultipleChoiceField( sites = DynamicModelMultipleChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False
widget=APISelectMultiple(
api_url="/api/dcim/sites/"
)
) )
roles = DynamicModelMultipleChoiceField( roles = DynamicModelMultipleChoiceField(
queryset=DeviceRole.objects.all(), queryset=DeviceRole.objects.all(),
required=False, required=False
widget=APISelectMultiple(
api_url="/api/dcim/device-roles/"
)
) )
platforms = DynamicModelMultipleChoiceField( platforms = DynamicModelMultipleChoiceField(
queryset=Platform.objects.all(), queryset=Platform.objects.all(),
required=False, required=False
widget=APISelectMultiple(
api_url="/api/dcim/platforms/"
)
) )
cluster_groups = DynamicModelMultipleChoiceField( cluster_groups = DynamicModelMultipleChoiceField(
queryset=ClusterGroup.objects.all(), queryset=ClusterGroup.objects.all(),
required=False, required=False
widget=APISelectMultiple(
api_url="/api/virtualization/cluster-groups/"
)
) )
clusters = DynamicModelMultipleChoiceField( clusters = DynamicModelMultipleChoiceField(
queryset=Cluster.objects.all(), queryset=Cluster.objects.all(),
required=False, required=False
widget=APISelectMultiple(
api_url="/api/virtualization/clusters/"
)
) )
tenant_groups = DynamicModelMultipleChoiceField( tenant_groups = DynamicModelMultipleChoiceField(
queryset=TenantGroup.objects.all(), queryset=TenantGroup.objects.all(),
required=False, required=False
widget=APISelectMultiple(
api_url="/api/tenancy/tenant-groups/"
)
) )
tenants = DynamicModelMultipleChoiceField( tenants = DynamicModelMultipleChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelectMultiple(
api_url="/api/tenancy/tenants/"
)
) )
tags = DynamicModelMultipleChoiceField( tags = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(), queryset=Tag.objects.all(),
to_field_name='slug', to_field_name='slug',
required=False, required=False
widget=APISelectMultiple(
api_url="/api/extras/tags/"
)
) )
data = JSONField( data = JSONField(
label='' label=''
@ -299,7 +275,6 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/regions/",
value_field="slug", value_field="slug",
) )
) )
@ -308,7 +283,6 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/sites/",
value_field="slug", value_field="slug",
) )
) )
@ -317,7 +291,6 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/device-roles/",
value_field="slug", value_field="slug",
) )
) )
@ -326,7 +299,6 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/platforms/",
value_field="slug", value_field="slug",
) )
) )
@ -335,24 +307,19 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/virtualization/cluster-groups/",
value_field="slug", value_field="slug",
) )
) )
cluster_id = DynamicModelMultipleChoiceField( cluster_id = DynamicModelMultipleChoiceField(
queryset=Cluster.objects.all(), queryset=Cluster.objects.all(),
required=False, required=False,
label='Cluster', label='Cluster'
widget=APISelectMultiple(
api_url="/api/virtualization/clusters/",
)
) )
tenant_group = DynamicModelMultipleChoiceField( tenant_group = DynamicModelMultipleChoiceField(
queryset=TenantGroup.objects.all(), queryset=TenantGroup.objects.all(),
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/tenancy/tenant-groups/",
value_field="slug", value_field="slug",
) )
) )
@ -361,7 +328,6 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/tenancy/tenants/",
value_field="slug", value_field="slug",
) )
) )
@ -370,7 +336,6 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/extras/tags/",
value_field="slug", value_field="slug",
) )
) )

View File

@ -0,0 +1,16 @@
from django.conf import settings
from django_rq.management.commands.rqworker import Command as _Command
class Command(_Command):
"""
Subclass django_rq's built-in rqworker to listen on all configured queues if none are specified (instead
of only the 'default' queue).
"""
def handle(self, *args, **options):
# If no queues have been specified on the command line, listen on all configured queues.
if len(args) < 1:
args = settings.RQ_QUEUES
super().handle(*args, **options)

View File

@ -0,0 +1,40 @@
# Generated by Django 2.2.11 on 2020-03-14 06:50
from django.db import migrations, models
import django.db.models.deletion
import extras.utils
class Migration(migrations.Migration):
dependencies = [
('extras', '0038_webhook_template_support'),
]
operations = [
migrations.AlterField(
model_name='customfield',
name='obj_type',
field=models.ManyToManyField(limit_choices_to=extras.utils.FeatureQuery('custom_fields'), related_name='custom_fields', to='contenttypes.ContentType'),
),
migrations.AlterField(
model_name='customlink',
name='content_type',
field=models.ForeignKey(limit_choices_to=extras.utils.FeatureQuery('custom_links'), on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
),
migrations.AlterField(
model_name='exporttemplate',
name='content_type',
field=models.ForeignKey(limit_choices_to=extras.utils.FeatureQuery('export_templates'), on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
),
migrations.AlterField(
model_name='graph',
name='type',
field=models.ForeignKey(limit_choices_to=extras.utils.FeatureQuery('graphs'), on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
),
migrations.AlterField(
model_name='webhook',
name='obj_type',
field=models.ManyToManyField(limit_choices_to=extras.utils.FeatureQuery('webhooks'), related_name='webhooks', to='contenttypes.ContentType'),
),
]

View File

@ -22,6 +22,7 @@ from utilities.utils import deepmerge, render_jinja2
from .choices import * from .choices import *
from .constants import * from .constants import *
from .querysets import ConfigContextQuerySet from .querysets import ConfigContextQuerySet
from .utils import FeatureQuery
__all__ = ( __all__ = (
@ -58,7 +59,7 @@ class Webhook(models.Model):
to=ContentType, to=ContentType,
related_name='webhooks', related_name='webhooks',
verbose_name='Object types', verbose_name='Object types',
limit_choices_to=WEBHOOK_MODELS, limit_choices_to=FeatureQuery('webhooks'),
help_text="The object(s) to which this Webhook applies." help_text="The object(s) to which this Webhook applies."
) )
name = models.CharField( name = models.CharField(
@ -223,7 +224,7 @@ class CustomField(models.Model):
to=ContentType, to=ContentType,
related_name='custom_fields', related_name='custom_fields',
verbose_name='Object(s)', verbose_name='Object(s)',
limit_choices_to=CUSTOMFIELD_MODELS, limit_choices_to=FeatureQuery('custom_fields'),
help_text='The object(s) to which this field applies.' help_text='The object(s) to which this field applies.'
) )
type = models.CharField( type = models.CharField(
@ -470,7 +471,7 @@ class CustomLink(models.Model):
content_type = models.ForeignKey( content_type = models.ForeignKey(
to=ContentType, to=ContentType,
on_delete=models.CASCADE, on_delete=models.CASCADE,
limit_choices_to=CUSTOMLINK_MODELS limit_choices_to=FeatureQuery('custom_links')
) )
name = models.CharField( name = models.CharField(
max_length=100, max_length=100,
@ -518,7 +519,7 @@ class Graph(models.Model):
type = models.ForeignKey( type = models.ForeignKey(
to=ContentType, to=ContentType,
on_delete=models.CASCADE, on_delete=models.CASCADE,
limit_choices_to=GRAPH_MODELS limit_choices_to=FeatureQuery('graphs')
) )
weight = models.PositiveSmallIntegerField( weight = models.PositiveSmallIntegerField(
default=1000 default=1000
@ -581,7 +582,7 @@ class ExportTemplate(models.Model):
content_type = models.ForeignKey( content_type = models.ForeignKey(
to=ContentType, to=ContentType,
on_delete=models.CASCADE, on_delete=models.CASCADE,
limit_choices_to=EXPORTTEMPLATE_MODELS limit_choices_to=FeatureQuery('export_templates')
) )
name = models.CharField( name = models.CharField(
max_length=100 max_length=100

21
netbox/extras/registry.py Normal file
View File

@ -0,0 +1,21 @@
class Registry(dict):
"""
Central registry for registration of functionality. Once a store (key) is defined, it cannot be overwritten or
deleted (although its value may be manipulated).
"""
def __getitem__(self, key):
try:
return super().__getitem__(key)
except KeyError:
raise KeyError("Invalid store: {}".format(key))
def __setitem__(self, key, value):
if key in self:
raise KeyError("Store already set: {}".format(key))
super().__setitem__(key, value)
def __delitem__(self, key):
raise TypeError("Cannot delete stores from registry")
registry = Registry()

View File

@ -18,6 +18,7 @@ from ipam.formfields import IPAddressFormField, IPNetworkFormField
from ipam.validators import MaxPrefixLengthValidator, MinPrefixLengthValidator, prefix_validator from ipam.validators import MaxPrefixLengthValidator, MinPrefixLengthValidator, prefix_validator
from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_SUCCESS, LOG_WARNING from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_SUCCESS, LOG_WARNING
from utilities.exceptions import AbortTransaction from utilities.exceptions import AbortTransaction
from utilities.forms import DynamicModelChoiceField, DynamicModelMultipleChoiceField
from .forms import ScriptForm from .forms import ScriptForm
from .signals import purge_changelog from .signals import purge_changelog
@ -167,7 +168,7 @@ class ObjectVar(ScriptVariable):
""" """
NetBox object representation. The provided QuerySet will determine the choices available. NetBox object representation. The provided QuerySet will determine the choices available.
""" """
form_field = forms.ModelChoiceField form_field = DynamicModelChoiceField
def __init__(self, queryset, *args, **kwargs): def __init__(self, queryset, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -184,7 +185,7 @@ class MultiObjectVar(ScriptVariable):
""" """
Like ObjectVar, but can represent one or more objects. Like ObjectVar, but can represent one or more objects.
""" """
form_field = forms.ModelMultipleChoiceField form_field = DynamicModelMultipleChoiceField
def __init__(self, queryset, *args, **kwargs): def __init__(self, queryset, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)

View File

@ -8,9 +8,9 @@ from rest_framework import status
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Platform, Rack, RackGroup, RackRole, Region, Site from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Platform, Rack, RackGroup, RackRole, Region, Site
from extras.api.views import ScriptViewSet from extras.api.views import ScriptViewSet
from extras.choices import * from extras.choices import *
from extras.constants import GRAPH_MODELS
from extras.models import ConfigContext, Graph, ExportTemplate, Tag from extras.models import ConfigContext, Graph, ExportTemplate, Tag
from extras.scripts import BooleanVar, IntegerVar, Script, StringVar from extras.scripts import BooleanVar, IntegerVar, Script, StringVar
from extras.utils import FeatureQuery
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from utilities.testing import APITestCase, choices_to_dict from utilities.testing import APITestCase, choices_to_dict
@ -35,7 +35,7 @@ class AppTest(APITestCase):
self.assertEqual(choices_to_dict(response.data.get('export-template:template_language')), TemplateLanguageChoices.as_dict()) self.assertEqual(choices_to_dict(response.data.get('export-template:template_language')), TemplateLanguageChoices.as_dict())
# Graph # Graph
content_types = ContentType.objects.filter(GRAPH_MODELS) content_types = ContentType.objects.filter(FeatureQuery('graphs').get_query())
graph_type_choices = { graph_type_choices = {
"{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types "{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types
} }

View File

@ -3,8 +3,8 @@ from django.test import TestCase
from dcim.models import DeviceRole, Platform, Region, Site from dcim.models import DeviceRole, Platform, Region, Site
from extras.choices import * from extras.choices import *
from extras.constants import GRAPH_MODELS
from extras.filters import * from extras.filters import *
from extras.utils import FeatureQuery
from extras.models import ConfigContext, ExportTemplate, Graph from extras.models import ConfigContext, ExportTemplate, Graph
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from virtualization.models import Cluster, ClusterGroup, ClusterType from virtualization.models import Cluster, ClusterGroup, ClusterType
@ -18,7 +18,7 @@ class GraphTestCase(TestCase):
def setUpTestData(cls): def setUpTestData(cls):
# Get the first three available types # Get the first three available types
content_types = ContentType.objects.filter(GRAPH_MODELS)[:3] content_types = ContentType.objects.filter(FeatureQuery('graphs').get_query())[:3]
graphs = ( graphs = (
Graph(name='Graph 1', type=content_types[0], template_language=TemplateLanguageChoices.LANGUAGE_DJANGO, source='http://example.com/1'), Graph(name='Graph 1', type=content_types[0], template_language=TemplateLanguageChoices.LANGUAGE_DJANGO, source='http://example.com/1'),
@ -32,7 +32,7 @@ class GraphTestCase(TestCase):
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
def test_type(self): def test_type(self):
content_type = ContentType.objects.filter(GRAPH_MODELS).first() content_type = ContentType.objects.filter(FeatureQuery('graphs').get_query()).first()
params = {'type': content_type.pk} params = {'type': content_type.pk}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

View File

@ -0,0 +1,33 @@
from django.test import TestCase
from extras.registry import Registry
class RegistryTest(TestCase):
def test_add_store(self):
reg = Registry()
reg['foo'] = 123
self.assertEqual(reg['foo'], 123)
def test_manipulate_store(self):
reg = Registry()
reg['foo'] = [1, 2]
reg['foo'].append(3)
self.assertListEqual(reg['foo'], [1, 2, 3])
def test_overwrite_store(self):
reg = Registry()
reg['foo'] = 123
with self.assertRaises(KeyError):
reg['foo'] = 456
def test_delete_store(self):
reg = Registry()
reg['foo'] = 123
with self.assertRaises(TypeError):
del(reg['foo'])

View File

@ -1,6 +1,13 @@
import collections
from django.db.models import Q
from django.utils.deconstruct import deconstructible
from taggit.managers import _TaggableManager from taggit.managers import _TaggableManager
from utilities.querysets import DummyQuerySet from utilities.querysets import DummyQuerySet
from extras.constants import EXTRAS_FEATURES
from extras.registry import registry
def is_taggable(obj): def is_taggable(obj):
""" """
@ -13,3 +20,46 @@ def is_taggable(obj):
if isinstance(obj.tags, DummyQuerySet): if isinstance(obj.tags, DummyQuerySet):
return True return True
return False return False
@deconstructible
class FeatureQuery:
"""
Helper class that delays evaluation of the registry contents for the functionality store
until it has been populated.
"""
def __init__(self, feature):
self.feature = feature
def __call__(self):
return self.get_query()
def get_query(self):
"""
Given an extras feature, return a Q object for content type lookup
"""
query = Q()
for app_label, models in registry['model_features'][self.feature].items():
query |= Q(app_label=app_label, model__in=models)
return query
def extras_features(*features):
"""
Decorator used to register extras provided features to a model
"""
def wrapper(model_class):
# Initialize the model_features store if not already defined
if 'model_features' not in registry:
registry['model_features'] = {
f: collections.defaultdict(list) for f in EXTRAS_FEATURES
}
for feature in features:
if feature in EXTRAS_FEATURES:
app_label, model_name = model_class._meta.label_lower.split('.')
registry['model_features'][feature][app_label].append(model_name)
else:
raise ValueError('{} is not a valid extras feature!'.format(feature))
return model_class
return wrapper

View File

@ -8,6 +8,7 @@ from extras.models import Webhook
from utilities.api import get_serializer_for_model from utilities.api import get_serializer_for_model
from .choices import * from .choices import *
from .constants import * from .constants import *
from .utils import FeatureQuery
def generate_signature(request_body, secret): def generate_signature(request_body, secret):
@ -29,7 +30,7 @@ def enqueue_webhooks(instance, user, request_id, action):
""" """
obj_type = ContentType.objects.get_for_model(instance.__class__) obj_type = ContentType.objects.get_for_model(instance.__class__)
webhook_models = ContentType.objects.filter(WEBHOOK_MODELS) webhook_models = ContentType.objects.filter(FeatureQuery('webhooks').get_query())
if obj_type not in webhook_models: if obj_type not in webhook_models:
return return

View File

@ -154,6 +154,33 @@ class PrefixSerializer(TaggitSerializer, CustomFieldModelSerializer):
read_only_fields = ['family'] read_only_fields = ['family']
class PrefixLengthSerializer(serializers.Serializer):
prefix_length = serializers.IntegerField()
def to_internal_value(self, data):
requested_prefix = data.get('prefix_length')
if requested_prefix is None:
raise serializers.ValidationError({
'prefix_length': 'this field can not be missing'
})
if not isinstance(requested_prefix, int):
raise serializers.ValidationError({
'prefix_length': 'this field must be int type'
})
prefix = self.context.get('prefix')
if prefix.family == 4 and requested_prefix > 32:
raise serializers.ValidationError({
'prefix_length': 'Invalid prefix length ({}) for IPv4'.format((requested_prefix))
})
elif prefix.family == 6 and requested_prefix > 128:
raise serializers.ValidationError({
'prefix_length': 'Invalid prefix length ({}) for IPv6'.format((requested_prefix))
})
return data
class AvailablePrefixSerializer(serializers.Serializer): class AvailablePrefixSerializer(serializers.Serializer):
""" """
Representation of a prefix which does not exist in the database. Representation of a prefix which does not exist in the database.

View File

@ -105,45 +105,25 @@ class PrefixViewSet(CustomFieldModelViewSet):
if not request.user.has_perm('ipam.add_prefix'): if not request.user.has_perm('ipam.add_prefix'):
raise PermissionDenied() raise PermissionDenied()
# Normalize to a list of objects # Validate Requested Prefixes' length
requested_prefixes = request.data if isinstance(request.data, list) else [request.data] serializer = serializers.PrefixLengthSerializer(
data=request.data if isinstance(request.data, list) else [request.data],
many=True,
context={
'request': request,
'prefix': prefix,
}
)
if not serializer.is_valid():
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST
)
requested_prefixes = serializer.validated_data
# Allocate prefixes to the requested objects based on availability within the parent # Allocate prefixes to the requested objects based on availability within the parent
for i, requested_prefix in enumerate(requested_prefixes): for i, requested_prefix in enumerate(requested_prefixes):
# Validate requested prefix size
prefix_length = requested_prefix.get('prefix_length')
if prefix_length is None:
return Response(
{
"detail": "Item {}: prefix_length field missing".format(i)
},
status=status.HTTP_400_BAD_REQUEST
)
try:
prefix_length = int(prefix_length)
except ValueError:
return Response(
{
"detail": "Item {}: Invalid prefix length ({})".format(i, prefix_length),
},
status=status.HTTP_400_BAD_REQUEST
)
if prefix.family == 4 and prefix_length > 32:
return Response(
{
"detail": "Item {}: Invalid prefix length ({}) for IPv4".format(i, prefix_length),
},
status=status.HTTP_400_BAD_REQUEST
)
elif prefix.family == 6 and prefix_length > 128:
return Response(
{
"detail": "Item {}: Invalid prefix length ({}) for IPv6".format(i, prefix_length),
},
status=status.HTTP_400_BAD_REQUEST
)
# Find the first available prefix equal to or larger than the requested size # Find the first available prefix equal to or larger than the requested size
for available_prefix in available_prefixes.iter_cidrs(): for available_prefix in available_prefixes.iter_cidrs():
if requested_prefix['prefix_length'] >= available_prefix.prefixlen: if requested_prefix['prefix_length'] >= available_prefix.prefixlen:

View File

@ -78,10 +78,7 @@ class VRFBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenants/"
)
) )
enforce_unique = forms.NullBooleanField( enforce_unique = forms.NullBooleanField(
required=False, required=False,
@ -150,10 +147,7 @@ class RIRFilterForm(BootstrapMixin, forms.Form):
class AggregateForm(BootstrapMixin, CustomFieldModelForm): class AggregateForm(BootstrapMixin, CustomFieldModelForm):
rir = DynamicModelChoiceField( rir = DynamicModelChoiceField(
queryset=RIR.objects.all(), queryset=RIR.objects.all()
widget=APISelect(
api_url="/api/ipam/rirs/"
)
) )
tags = TagField( tags = TagField(
required=False required=False
@ -196,10 +190,7 @@ class AggregateBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEd
rir = DynamicModelChoiceField( rir = DynamicModelChoiceField(
queryset=RIR.objects.all(), queryset=RIR.objects.all(),
required=False, required=False,
label='RIR', label='RIR'
widget=APISelect(
api_url="/api/ipam/rirs/"
)
) )
date_added = forms.DateField( date_added = forms.DateField(
required=False required=False
@ -236,7 +227,6 @@ class AggregateFilterForm(BootstrapMixin, CustomFieldFilterForm):
required=False, required=False,
label='RIR', label='RIR',
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/rirs/",
value_field="slug", value_field="slug",
) )
) )
@ -276,16 +266,12 @@ class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
vrf = DynamicModelChoiceField( vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(), queryset=VRF.objects.all(),
required=False, required=False,
label='VRF', label='VRF'
widget=APISelect(
api_url="/api/ipam/vrfs/",
)
) )
site = DynamicModelChoiceField( site = DynamicModelChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/dcim/sites/",
filter_for={ filter_for={
'vlan_group': 'site_id', 'vlan_group': 'site_id',
'vlan': 'site_id', 'vlan': 'site_id',
@ -300,7 +286,6 @@ class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
required=False, required=False,
label='VLAN group', label='VLAN group',
widget=APISelect( widget=APISelect(
api_url='/api/ipam/vlan-groups/',
filter_for={ filter_for={
'vlan': 'group_id' 'vlan': 'group_id'
}, },
@ -314,16 +299,12 @@ class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
required=False, required=False,
label='VLAN', label='VLAN',
widget=APISelect( widget=APISelect(
api_url='/api/ipam/vlans/',
display_field='display_name' display_field='display_name'
) )
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=Role.objects.all(), queryset=Role.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/ipam/roles/"
)
) )
tags = TagField(required=False) tags = TagField(required=False)
@ -447,18 +428,12 @@ class PrefixBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditF
) )
site = DynamicModelChoiceField( site = DynamicModelChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/dcim/sites/"
)
) )
vrf = DynamicModelChoiceField( vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(), queryset=VRF.objects.all(),
required=False, required=False,
label='VRF', label='VRF'
widget=APISelect(
api_url="/api/ipam/vrfs/"
)
) )
prefix_length = forms.IntegerField( prefix_length = forms.IntegerField(
min_value=PREFIX_LENGTH_MIN, min_value=PREFIX_LENGTH_MIN,
@ -467,10 +442,7 @@ class PrefixBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditF
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenants/"
)
) )
status = forms.ChoiceField( status = forms.ChoiceField(
choices=add_blank_choice(PrefixStatusChoices), choices=add_blank_choice(PrefixStatusChoices),
@ -479,10 +451,7 @@ class PrefixBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditF
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=Role.objects.all(), queryset=Role.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/ipam/roles/"
)
) )
is_pool = forms.NullBooleanField( is_pool = forms.NullBooleanField(
required=False, required=False,
@ -536,7 +505,6 @@ class PrefixFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm)
required=False, required=False,
label='VRF', label='VRF',
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/vrfs/",
null_option=True, null_option=True,
) )
) )
@ -550,7 +518,6 @@ class PrefixFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm)
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/regions/",
value_field="slug", value_field="slug",
filter_for={ filter_for={
'site': 'region' 'site': 'region'
@ -562,7 +529,6 @@ class PrefixFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm)
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/sites/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -572,7 +538,6 @@ class PrefixFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm)
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/roles/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -603,17 +568,13 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel
vrf = DynamicModelChoiceField( vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(), queryset=VRF.objects.all(),
required=False, required=False,
label='VRF', label='VRF'
widget=APISelect(
api_url="/api/ipam/vrfs/"
)
) )
nat_site = DynamicModelChoiceField( nat_site = DynamicModelChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False,
label='Site', label='Site',
widget=APISelect( widget=APISelect(
api_url="/api/dcim/sites/",
filter_for={ filter_for={
'nat_rack': 'site_id', 'nat_rack': 'site_id',
'nat_device': 'site_id' 'nat_device': 'site_id'
@ -625,7 +586,6 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel
required=False, required=False,
label='Rack', label='Rack',
widget=APISelect( widget=APISelect(
api_url='/api/dcim/racks/',
display_field='display_name', display_field='display_name',
filter_for={ filter_for={
'nat_device': 'rack_id' 'nat_device': 'rack_id'
@ -640,19 +600,17 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel
required=False, required=False,
label='Device', label='Device',
widget=APISelect( widget=APISelect(
api_url='/api/dcim/devices/',
display_field='display_name', display_field='display_name',
filter_for={ filter_for={
'nat_inside': 'device_id' 'nat_inside': 'device_id'
} }
) )
) )
nat_vrf = forms.ModelChoiceField( nat_vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(), queryset=VRF.objects.all(),
required=False, required=False,
label='VRF', label='VRF',
widget=APISelect( widget=APISelect(
api_url="/api/ipam/vrfs/",
filter_for={ filter_for={
'nat_inside': 'vrf_id' 'nat_inside': 'vrf_id'
} }
@ -663,7 +621,6 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel
required=False, required=False,
label='IP Address', label='IP Address',
widget=APISelect( widget=APISelect(
api_url='/api/ipam/ip-addresses/',
display_field='address' display_field='address'
) )
) )
@ -761,10 +718,7 @@ class IPAddressBulkAddForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
vrf = DynamicModelChoiceField( vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(), queryset=VRF.objects.all(),
required=False, required=False,
label='VRF', label='VRF'
widget=APISelect(
api_url="/api/ipam/vrfs/"
)
) )
class Meta: class Meta:
@ -913,10 +867,7 @@ class IPAddressBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEd
vrf = DynamicModelChoiceField( vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(), queryset=VRF.objects.all(),
required=False, required=False,
label='VRF', label='VRF'
widget=APISelect(
api_url="/api/ipam/vrfs/"
)
) )
mask_length = forms.IntegerField( mask_length = forms.IntegerField(
min_value=IPADDRESS_MASK_LENGTH_MIN, min_value=IPADDRESS_MASK_LENGTH_MIN,
@ -925,10 +876,7 @@ class IPAddressBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEd
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenants/"
)
) )
status = forms.ChoiceField( status = forms.ChoiceField(
choices=add_blank_choice(IPAddressStatusChoices), choices=add_blank_choice(IPAddressStatusChoices),
@ -960,10 +908,7 @@ class IPAddressAssignForm(BootstrapMixin, forms.Form):
queryset=VRF.objects.all(), queryset=VRF.objects.all(),
required=False, required=False,
label='VRF', label='VRF',
empty_label='Global', empty_label='Global'
widget=APISelect(
api_url="/api/ipam/vrfs/"
)
) )
q = forms.CharField( q = forms.CharField(
required=False, required=False,
@ -1007,7 +952,6 @@ class IPAddressFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterFo
required=False, required=False,
label='VRF', label='VRF',
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/vrfs/",
null_option=True, null_option=True,
) )
) )
@ -1038,10 +982,7 @@ class IPAddressFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterFo
class VLANGroupForm(BootstrapMixin, forms.ModelForm): class VLANGroupForm(BootstrapMixin, forms.ModelForm):
site = DynamicModelChoiceField( site = DynamicModelChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/dcim/sites/"
)
) )
slug = SlugField() slug = SlugField()
@ -1078,7 +1019,6 @@ class VLANGroupFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/regions/",
value_field="slug", value_field="slug",
filter_for={ filter_for={
'site': 'region', 'site': 'region',
@ -1090,7 +1030,6 @@ class VLANGroupFilterForm(BootstrapMixin, forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/sites/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -1106,7 +1045,6 @@ class VLANForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/dcim/sites/",
filter_for={ filter_for={
'group': 'site_id' 'group': 'site_id'
}, },
@ -1117,17 +1055,11 @@ class VLANForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
) )
group = DynamicModelChoiceField( group = DynamicModelChoiceField(
queryset=VLANGroup.objects.all(), queryset=VLANGroup.objects.all(),
required=False, required=False
widget=APISelect(
api_url='/api/ipam/vlan-groups/',
)
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=Role.objects.all(), queryset=Role.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/ipam/roles/"
)
) )
tags = TagField(required=False) tags = TagField(required=False)
@ -1222,24 +1154,15 @@ class VLANBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditFor
) )
site = DynamicModelChoiceField( site = DynamicModelChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/dcim/sites/"
)
) )
group = DynamicModelChoiceField( group = DynamicModelChoiceField(
queryset=VLANGroup.objects.all(), queryset=VLANGroup.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/ipam/vlan-groups/"
)
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenants/"
)
) )
status = forms.ChoiceField( status = forms.ChoiceField(
choices=add_blank_choice(VLANStatusChoices), choices=add_blank_choice(VLANStatusChoices),
@ -1248,10 +1171,7 @@ class VLANBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditFor
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=Role.objects.all(), queryset=Role.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/ipam/roles/"
)
) )
description = forms.CharField( description = forms.CharField(
max_length=100, max_length=100,
@ -1276,7 +1196,6 @@ class VLANFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/regions/",
value_field="slug", value_field="slug",
filter_for={ filter_for={
'site': 'region', 'site': 'region',
@ -1289,7 +1208,6 @@ class VLANFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/sites/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -1299,7 +1217,6 @@ class VLANFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
required=False, required=False,
label='VLAN group', label='VLAN group',
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/vlan-groups/",
null_option=True, null_option=True,
) )
) )
@ -1313,7 +1230,6 @@ class VLANFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/roles/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )

View File

@ -10,6 +10,7 @@ from taggit.managers import TaggableManager
from dcim.models import Device, Interface from dcim.models import Device, Interface
from extras.models import CustomFieldModel, ObjectChange, TaggedItem from extras.models import CustomFieldModel, ObjectChange, TaggedItem
from extras.utils import extras_features
from utilities.models import ChangeLoggedModel from utilities.models import ChangeLoggedModel
from utilities.utils import serialize_object from utilities.utils import serialize_object
from virtualization.models import VirtualMachine from virtualization.models import VirtualMachine
@ -34,6 +35,7 @@ __all__ = (
) )
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class VRF(ChangeLoggedModel, CustomFieldModel): class VRF(ChangeLoggedModel, CustomFieldModel):
""" """
A virtual routing and forwarding (VRF) table represents a discrete layer three forwarding domain (e.g. a routing A virtual routing and forwarding (VRF) table represents a discrete layer three forwarding domain (e.g. a routing
@ -145,6 +147,7 @@ class RIR(ChangeLoggedModel):
) )
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Aggregate(ChangeLoggedModel, CustomFieldModel): class Aggregate(ChangeLoggedModel, CustomFieldModel):
""" """
An aggregate exists at the root level of the IP address space hierarchy in NetBox. Aggregates are used to organize An aggregate exists at the root level of the IP address space hierarchy in NetBox. Aggregates are used to organize
@ -285,6 +288,7 @@ class Role(ChangeLoggedModel):
) )
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Prefix(ChangeLoggedModel, CustomFieldModel): class Prefix(ChangeLoggedModel, CustomFieldModel):
""" """
A Prefix represents an IPv4 or IPv6 network, including mask length. Prefixes can optionally be assigned to Sites and A Prefix represents an IPv4 or IPv6 network, including mask length. Prefixes can optionally be assigned to Sites and
@ -551,6 +555,7 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
return int(float(child_count) / prefix_size * 100) return int(float(child_count) / prefix_size * 100)
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class IPAddress(ChangeLoggedModel, CustomFieldModel): class IPAddress(ChangeLoggedModel, CustomFieldModel):
""" """
An IPAddress represents an individual IPv4 or IPv6 address and its mask. The mask length should match what is An IPAddress represents an individual IPv4 or IPv6 address and its mask. The mask length should match what is
@ -854,6 +859,7 @@ class VLANGroup(ChangeLoggedModel):
return None return None
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class VLAN(ChangeLoggedModel, CustomFieldModel): class VLAN(ChangeLoggedModel, CustomFieldModel):
""" """
A VLAN is a distinct layer two forwarding domain identified by a 12-bit integer (1-4094). Each VLAN must be assigned A VLAN is a distinct layer two forwarding domain identified by a 12-bit integer (1-4094). Each VLAN must be assigned
@ -978,6 +984,7 @@ class VLAN(ChangeLoggedModel, CustomFieldModel):
).distinct() ).distinct()
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Service(ChangeLoggedModel, CustomFieldModel): class Service(ChangeLoggedModel, CustomFieldModel):
""" """
A Service represents a layer-four service (e.g. HTTP or SSH) running on a Device or VirtualMachine. A Service may A Service represents a layer-four service (e.g. HTTP or SSH) running on a Device or VirtualMachine. A Service may

View File

@ -611,10 +611,15 @@ class PrefixTest(APITestCase):
self.assertEqual(response.data['description'], data['description']) self.assertEqual(response.data['description'], data['description'])
# Try to create one more prefix # Try to create one more prefix
response = self.client.post(url, {'prefix_length': 30}, **self.header) response = self.client.post(url, {'prefix_length': 30}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT) self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
self.assertIn('detail', response.data) self.assertIn('detail', response.data)
# Try to create invalid prefix type
response = self.client.post(url, {'prefix_length': '30'}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
self.assertIn('prefix_length', response.data[0])
def test_create_multiple_available_prefixes(self): def test_create_multiple_available_prefixes(self):
prefix = Prefix.objects.create(prefix=IPNetwork('192.0.2.0/28'), is_pool=True) prefix = Prefix.objects.create(prefix=IPNetwork('192.0.2.0/28'), is_pool=True)

View File

@ -11,7 +11,6 @@ class NetBoxAdminSite(AdminSite):
site_header = 'NetBox Administration' site_header = 'NetBox Administration'
site_title = 'NetBox' site_title = 'NetBox'
site_url = '/{}'.format(settings.BASE_PATH) site_url = '/{}'.format(settings.BASE_PATH)
index_template = 'django_rq/index.html'
admin_site = NetBoxAdminSite(name='admin') admin_site = NetBoxAdminSite(name='admin')

View File

@ -21,11 +21,11 @@ DATABASE = {
'CONN_MAX_AGE': 300, # Max database connection age 'CONN_MAX_AGE': 300, # Max database connection age
} }
# Redis database settings. The Redis database is used for caching and background processing such as webhooks # Redis database settings. Redis is used for caching and for queuing background tasks such as webhook events. A separate
# Seperate sections for webhooks and caching allow for connecting to seperate Redis instances/datbases if desired. # configuration exists for each. Full connection details are required in both sections, and it is strongly recommended
# Full connection details are required in both sections, even if they are the same. # to use two separate database IDs.
REDIS = { REDIS = {
'webhooks': { 'tasks': {
'HOST': 'localhost', 'HOST': 'localhost',
'PORT': 6379, 'PORT': 6379,
# Comment out `HOST` and `PORT` lines and uncomment the following if using Redis Sentinel # Comment out `HOST` and `PORT` lines and uncomment the following if using Redis Sentinel
@ -179,6 +179,14 @@ PAGINATE_COUNT = 50
# prefer IPv4 instead. # prefer IPv4 instead.
PREFER_IPV4 = False PREFER_IPV4 = False
# This determines how often the GitHub API is called to check the latest release of NetBox. Must be at least 1 hour.
RELEASE_CHECK_TIMEOUT = 24 * 3600
# This repository is used to check whether there is a new release of NetBox available. Set to None to disable the
# version check or use the URL below to check for release in the official NetBox repository.
RELEASE_CHECK_URL = None
# RELEASE_CHECK_URL = 'https://api.github.com/repos/netbox-community/netbox/releases'
# The file path where custom reports will be stored. A trailing slash is not needed. Note that the default value of # The file path where custom reports will be stored. A trailing slash is not needed. Note that the default value of
# this setting is derived from the installed location. # this setting is derived from the installed location.
# REPORTS_ROOT = '/opt/netbox/netbox/reports' # REPORTS_ROOT = '/opt/netbox/netbox/reports'

33
netbox/netbox/releases.py Normal file
View File

@ -0,0 +1,33 @@
import logging
from cacheops import CacheMiss, cache
from django.conf import settings
from django_rq import get_queue
from utilities.background_tasks import get_releases
logger = logging.getLogger('netbox.releases')
def get_latest_release(pre_releases=False):
if settings.RELEASE_CHECK_URL:
logger.debug("Checking for most recent release")
try:
latest_release = cache.get('latest_release')
if latest_release:
logger.debug("Found cached release: {}".format(latest_release))
return latest_release
except CacheMiss:
# Check for an existing job. This can happen if the RQ worker process is not running.
queue = get_queue('check_releases')
if queue.jobs:
logger.warning("Job to check for new releases is already queued; skipping")
else:
# Get the releases in the background worker, it will fill the cache
logger.info("Initiating background task to retrieve updated releases list")
get_releases.delay(pre_releases=pre_releases)
else:
logger.debug("Skipping release check; RELEASE_CHECK_URL not defined")
return 'unknown', None

View File

@ -1,18 +1,21 @@
import logging import logging
import os import os
import platform import platform
import re
import socket import socket
import warnings import warnings
from urllib.parse import urlsplit
from django.contrib.messages import constants as messages from django.contrib.messages import constants as messages
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import URLValidator
# #
# Environment setup # Environment setup
# #
VERSION = '2.7.10' VERSION = '2.7.11'
# Hostname # Hostname
HOSTNAME = platform.node() HOSTNAME = platform.node()
@ -94,6 +97,8 @@ NAPALM_TIMEOUT = getattr(configuration, 'NAPALM_TIMEOUT', 30)
NAPALM_USERNAME = getattr(configuration, 'NAPALM_USERNAME', '') NAPALM_USERNAME = getattr(configuration, 'NAPALM_USERNAME', '')
PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50) PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False) PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
RELEASE_CHECK_URL = getattr(configuration, 'RELEASE_CHECK_URL', None)
RELEASE_CHECK_TIMEOUT = getattr(configuration, 'RELEASE_CHECK_TIMEOUT', 24 * 3600)
REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/') REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/') SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/')
SESSION_FILE_PATH = getattr(configuration, 'SESSION_FILE_PATH', None) SESSION_FILE_PATH = getattr(configuration, 'SESSION_FILE_PATH', None)
@ -103,6 +108,20 @@ SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s')
TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a') TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a')
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC') TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
# Validate update repo URL and timeout
if RELEASE_CHECK_URL:
try:
URLValidator(RELEASE_CHECK_URL)
except ValidationError:
raise ImproperlyConfigured(
"RELEASE_CHECK_URL must be a valid API URL. Example: "
"https://api.github.com/repos/netbox-community/netbox"
)
# Enforce a minimum cache timeout for update checks
if RELEASE_CHECK_TIMEOUT < 3600:
raise ImproperlyConfigured("RELEASE_CHECK_TIMEOUT has to be at least 3600 seconds (1 hour)")
# #
# Database # Database
@ -159,31 +178,40 @@ if STORAGE_CONFIG and STORAGE_BACKEND is None:
# Redis # Redis
# #
if 'webhooks' not in REDIS: # Background task queuing
raise ImproperlyConfigured( if 'tasks' in REDIS:
"REDIS section in configuration.py is missing webhooks subsection." TASKS_REDIS = REDIS['tasks']
elif 'webhooks' in REDIS:
# TODO: Remove support for 'webhooks' name in v2.9
warnings.warn(
"The 'webhooks' REDIS configuration section has been renamed to 'tasks'. Please update your configuration as "
"support for the old name will be removed in a future release."
) )
if 'caching' not in REDIS: TASKS_REDIS = REDIS['webhooks']
else:
raise ImproperlyConfigured(
"REDIS section in configuration.py is missing the 'tasks' subsection."
)
TASKS_REDIS_HOST = TASKS_REDIS.get('HOST', 'localhost')
TASKS_REDIS_PORT = TASKS_REDIS.get('PORT', 6379)
TASKS_REDIS_SENTINELS = TASKS_REDIS.get('SENTINELS', [])
TASKS_REDIS_USING_SENTINEL = all([
isinstance(TASKS_REDIS_SENTINELS, (list, tuple)),
len(TASKS_REDIS_SENTINELS) > 0
])
TASKS_REDIS_SENTINEL_SERVICE = TASKS_REDIS.get('SENTINEL_SERVICE', 'default')
TASKS_REDIS_PASSWORD = TASKS_REDIS.get('PASSWORD', '')
TASKS_REDIS_DATABASE = TASKS_REDIS.get('DATABASE', 0)
TASKS_REDIS_DEFAULT_TIMEOUT = TASKS_REDIS.get('DEFAULT_TIMEOUT', 300)
TASKS_REDIS_SSL = TASKS_REDIS.get('SSL', False)
# Caching
if 'caching' in REDIS:
CACHING_REDIS = REDIS['caching']
else:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"REDIS section in configuration.py is missing caching subsection." "REDIS section in configuration.py is missing caching subsection."
) )
WEBHOOKS_REDIS = REDIS.get('webhooks', {})
WEBHOOKS_REDIS_HOST = WEBHOOKS_REDIS.get('HOST', 'localhost')
WEBHOOKS_REDIS_PORT = WEBHOOKS_REDIS.get('PORT', 6379)
WEBHOOKS_REDIS_SENTINELS = WEBHOOKS_REDIS.get('SENTINELS', [])
WEBHOOKS_REDIS_USING_SENTINEL = all([
isinstance(WEBHOOKS_REDIS_SENTINELS, (list, tuple)),
len(WEBHOOKS_REDIS_SENTINELS) > 0
])
WEBHOOKS_REDIS_SENTINEL_SERVICE = WEBHOOKS_REDIS.get('SENTINEL_SERVICE', 'default')
WEBHOOKS_REDIS_PASSWORD = WEBHOOKS_REDIS.get('PASSWORD', '')
WEBHOOKS_REDIS_DATABASE = WEBHOOKS_REDIS.get('DATABASE', 0)
WEBHOOKS_REDIS_DEFAULT_TIMEOUT = WEBHOOKS_REDIS.get('DEFAULT_TIMEOUT', 300)
WEBHOOKS_REDIS_SSL = WEBHOOKS_REDIS.get('SSL', False)
CACHING_REDIS = REDIS.get('caching', {})
CACHING_REDIS_HOST = CACHING_REDIS.get('HOST', 'localhost') CACHING_REDIS_HOST = CACHING_REDIS.get('HOST', 'localhost')
CACHING_REDIS_PORT = CACHING_REDIS.get('PORT', 6379) CACHING_REDIS_PORT = CACHING_REDIS.get('PORT', 6379)
CACHING_REDIS_SENTINELS = CACHING_REDIS.get('SENTINELS', []) CACHING_REDIS_SENTINELS = CACHING_REDIS.get('SENTINELS', [])
@ -238,7 +266,6 @@ INSTALLED_APPS = [
'corsheaders', 'corsheaders',
'debug_toolbar', 'debug_toolbar',
'django_filters', 'django_filters',
'django_rq',
'django_tables2', 'django_tables2',
'django_prometheus', 'django_prometheus',
'mptt', 'mptt',
@ -255,6 +282,7 @@ INSTALLED_APPS = [
'users', 'users',
'utilities', 'utilities',
'virtualization', 'virtualization',
'django_rq', # Must come after extras to allow overriding management commands
'drf_yasg', 'drf_yasg',
] ]
@ -548,26 +576,31 @@ SWAGGER_SETTINGS = {
# Django RQ (Webhooks backend) # Django RQ (Webhooks backend)
# #
RQ_QUEUES = { if TASKS_REDIS_USING_SENTINEL:
'default': { RQ_PARAMS = {
'HOST': WEBHOOKS_REDIS_HOST, 'SENTINELS': TASKS_REDIS_SENTINELS,
'PORT': WEBHOOKS_REDIS_PORT, 'MASTER_NAME': TASKS_REDIS_SENTINEL_SERVICE,
'DB': WEBHOOKS_REDIS_DATABASE, 'DB': TASKS_REDIS_DATABASE,
'PASSWORD': WEBHOOKS_REDIS_PASSWORD, 'PASSWORD': TASKS_REDIS_PASSWORD,
'DEFAULT_TIMEOUT': WEBHOOKS_REDIS_DEFAULT_TIMEOUT,
'SSL': WEBHOOKS_REDIS_SSL,
} if not WEBHOOKS_REDIS_USING_SENTINEL else {
'SENTINELS': WEBHOOKS_REDIS_SENTINELS,
'MASTER_NAME': WEBHOOKS_REDIS_SENTINEL_SERVICE,
'DB': WEBHOOKS_REDIS_DATABASE,
'PASSWORD': WEBHOOKS_REDIS_PASSWORD,
'SOCKET_TIMEOUT': None, 'SOCKET_TIMEOUT': None,
'CONNECTION_KWARGS': { 'CONNECTION_KWARGS': {
'socket_connect_timeout': WEBHOOKS_REDIS_DEFAULT_TIMEOUT 'socket_connect_timeout': TASKS_REDIS_DEFAULT_TIMEOUT
}, },
} }
} else:
RQ_PARAMS = {
'HOST': TASKS_REDIS_HOST,
'PORT': TASKS_REDIS_PORT,
'DB': TASKS_REDIS_DATABASE,
'PASSWORD': TASKS_REDIS_PASSWORD,
'DEFAULT_TIMEOUT': TASKS_REDIS_DEFAULT_TIMEOUT,
'SSL': TASKS_REDIS_SSL,
}
RQ_QUEUES = {
'default': RQ_PARAMS, # Webhooks
'check_releases': RQ_PARAMS,
}
# #
# Django debug toolbar # Django debug toolbar

View File

@ -0,0 +1,166 @@
from io import BytesIO
from logging import ERROR
from unittest.mock import Mock, patch
import requests
from cacheops import CacheMiss, RedisCache
from django.test import SimpleTestCase, override_settings
from packaging.version import Version
from requests import Response
from utilities.background_tasks import get_releases
def successful_github_response(url, *_args, **_kwargs):
r = Response()
r.url = url
r.status_code = 200
r.reason = 'OK'
r.headers = {
'Content-Type': 'application/json; charset=utf-8',
}
r.raw = BytesIO(b'''[
{
"html_url": "https://github.com/netbox-community/netbox/releases/tag/v2.7.8",
"tag_name": "v2.7.8",
"prerelease": false
},
{
"html_url": "https://github.com/netbox-community/netbox/releases/tag/v2.6-beta1",
"tag_name": "v2.6-beta1",
"prerelease": true
},
{
"html_url": "https://github.com/netbox-community/netbox/releases/tag/v2.5.9",
"tag_name": "v2.5.9",
"prerelease": false
}
]
''')
return r
def unsuccessful_github_response(url, *_args, **_kwargs):
r = Response()
r.url = url
r.status_code = 404
r.reason = 'Not Found'
r.headers = {
'Content-Type': 'application/json; charset=utf-8',
}
r.raw = BytesIO(b'''{
"message": "Not Found",
"documentation_url": "https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository"
}
''')
return r
@override_settings(RELEASE_CHECK_URL='https://localhost/unittest/releases', RELEASE_CHECK_TIMEOUT=160876)
class GetReleasesTestCase(SimpleTestCase):
@patch.object(requests, 'get')
@patch.object(RedisCache, 'set')
@patch.object(RedisCache, 'get')
def test_pre_releases(self, dummy_cache_get: Mock, dummy_cache_set: Mock, dummy_request_get: Mock):
dummy_cache_get.side_effect = CacheMiss()
dummy_request_get.side_effect = successful_github_response
releases = get_releases(pre_releases=True)
# Check result
self.assertListEqual(releases, [
(Version('2.7.8'), 'https://github.com/netbox-community/netbox/releases/tag/v2.7.8'),
(Version('2.6b1'), 'https://github.com/netbox-community/netbox/releases/tag/v2.6-beta1'),
(Version('2.5.9'), 'https://github.com/netbox-community/netbox/releases/tag/v2.5.9')
])
# Check if correct request is made
dummy_request_get.assert_called_once_with(
'https://localhost/unittest/releases',
headers={'Accept': 'application/vnd.github.v3+json'}
)
# Check if result is put in cache
dummy_cache_set.assert_called_once_with(
'latest_release',
max(releases),
160876
)
@patch.object(requests, 'get')
@patch.object(RedisCache, 'set')
@patch.object(RedisCache, 'get')
def test_no_pre_releases(self, dummy_cache_get: Mock, dummy_cache_set: Mock, dummy_request_get: Mock):
dummy_cache_get.side_effect = CacheMiss()
dummy_request_get.side_effect = successful_github_response
releases = get_releases(pre_releases=False)
# Check result
self.assertListEqual(releases, [
(Version('2.7.8'), 'https://github.com/netbox-community/netbox/releases/tag/v2.7.8'),
(Version('2.5.9'), 'https://github.com/netbox-community/netbox/releases/tag/v2.5.9')
])
# Check if correct request is made
dummy_request_get.assert_called_once_with(
'https://localhost/unittest/releases',
headers={'Accept': 'application/vnd.github.v3+json'}
)
# Check if result is put in cache
dummy_cache_set.assert_called_once_with(
'latest_release',
max(releases),
160876
)
@patch.object(requests, 'get')
@patch.object(RedisCache, 'set')
@patch.object(RedisCache, 'get')
def test_failed_request(self, dummy_cache_get: Mock, dummy_cache_set: Mock, dummy_request_get: Mock):
dummy_cache_get.side_effect = CacheMiss()
dummy_request_get.side_effect = unsuccessful_github_response
with self.assertLogs(level=ERROR) as cm:
releases = get_releases()
# Check log entry
self.assertEqual(len(cm.output), 1)
log_output = cm.output[0]
last_log_line = log_output.split('\n')[-1]
self.assertRegex(last_log_line, '404 .* Not Found')
# Check result
self.assertListEqual(releases, [])
# Check if correct request is made
dummy_request_get.assert_called_once_with(
'https://localhost/unittest/releases',
headers={'Accept': 'application/vnd.github.v3+json'}
)
# Check if failure is put in cache
dummy_cache_set.assert_called_once_with(
'latest_release_no_retry',
'https://localhost/unittest/releases',
900
)
@patch.object(requests, 'get')
@patch.object(RedisCache, 'set')
@patch.object(RedisCache, 'get')
def test_blocked_retry(self, dummy_cache_get: Mock, dummy_cache_set: Mock, dummy_request_get: Mock):
dummy_cache_get.return_value = 'https://localhost/unittest/releases'
dummy_request_get.side_effect = successful_github_response
releases = get_releases()
# Check result
self.assertListEqual(releases, [])
# Check if request is NOT made
dummy_request_get.assert_not_called()
# Check if cache is not updated
dummy_cache_set.assert_not_called()

View File

@ -1,6 +1,7 @@
from django.conf import settings from django.conf import settings
from django.conf.urls import include from django.conf.urls import include
from django.urls import path, re_path from django.urls import path, re_path, reverse
from django.views.generic.base import RedirectView
from django.views.static import serve from django.views.static import serve
from drf_yasg import openapi from drf_yasg import openapi
from drf_yasg.views import get_schema_view from drf_yasg.views import get_schema_view
@ -9,6 +10,18 @@ from netbox.views import APIRootView, HomeView, StaticMediaFailureView, SearchVi
from users.views import LoginView, LogoutView from users.views import LoginView, LogoutView
from .admin import admin_site from .admin import admin_site
# TODO: Remove in v2.9
class RQRedirectView(RedirectView):
"""
Temporary 301 redirect from the old URL to the new one.
"""
permanent = True
def get_redirect_url(self, *args, **kwargs):
return reverse('rq_home')
openapi_info = openapi.Info( openapi_info = openapi.Info(
title="NetBox API", title="NetBox API",
default_version='v2', default_version='v2',
@ -61,7 +74,9 @@ _patterns = [
# Admin # Admin
path('admin/', admin_site.urls), path('admin/', admin_site.urls),
path('admin/webhook-backend-status/', include('django_rq.urls')), path('admin/background-tasks/', include('django_rq.urls')),
# TODO: Remove in v2.9
path('admin/webhook-backend-status/', RQRedirectView.as_view()),
# Errors # Errors
path('media-failure/', StaticMediaFailureView.as_view(), name='media_failure'), path('media-failure/', StaticMediaFailureView.as_view(), name='media_failure'),

View File

@ -1,8 +1,10 @@
from collections import OrderedDict from collections import OrderedDict
from django.conf import settings
from django.db.models import Count, F from django.db.models import Count, F
from django.shortcuts import render from django.shortcuts import render
from django.views.generic import View from django.views.generic import View
from packaging import version
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from rest_framework.views import APIView from rest_framework.views import APIView
@ -25,6 +27,7 @@ from extras.models import ObjectChange, ReportResult
from ipam.filters import AggregateFilterSet, IPAddressFilterSet, PrefixFilterSet, VLANFilterSet, VRFFilterSet from ipam.filters import AggregateFilterSet, IPAddressFilterSet, PrefixFilterSet, VLANFilterSet, VRFFilterSet
from ipam.models import Aggregate, IPAddress, Prefix, VLAN, VRF from ipam.models import Aggregate, IPAddress, Prefix, VLAN, VRF
from ipam.tables import AggregateTable, IPAddressTable, PrefixTable, VLANTable, VRFTable from ipam.tables import AggregateTable, IPAddressTable, PrefixTable, VLANTable, VRFTable
from netbox.releases import get_latest_release
from secrets.filters import SecretFilterSet from secrets.filters import SecretFilterSet
from secrets.models import Secret from secrets.models import Secret
from secrets.tables import SecretTable from secrets.tables import SecretTable
@ -240,11 +243,24 @@ class HomeView(View):
} }
# Check whether a new release is available. (Only for staff/superusers.)
new_release = None
if request.user.is_staff or request.user.is_superuser:
latest_release, release_url = get_latest_release()
if isinstance(latest_release, version.Version):
current_version = version.parse(settings.VERSION)
if latest_release > current_version:
new_release = {
'version': str(latest_release),
'url': release_url,
}
return render(request, self.template_name, { return render(request, self.template_name, {
'search_form': SearchForm(), 'search_form': SearchForm(),
'stats': stats, 'stats': stats,
'report_results': ReportResult.objects.order_by('-created')[:10], 'report_results': ReportResult.objects.order_by('-created')[:10],
'changelog': ObjectChange.objects.prefetch_related('user', 'changed_object_type')[:15] 'changelog': ObjectChange.objects.prefetch_related('user', 'changed_object_type')[:15],
'new_release': new_release,
}) })

View File

@ -71,6 +71,9 @@ class SecretRoleCSVForm(forms.ModelForm):
# #
class SecretForm(BootstrapMixin, CustomFieldModelForm): class SecretForm(BootstrapMixin, CustomFieldModelForm):
device = DynamicModelChoiceField(
queryset=Device.objects.all()
)
plaintext = forms.CharField( plaintext = forms.CharField(
max_length=SECRET_PLAINTEXT_MAX_LENGTH, max_length=SECRET_PLAINTEXT_MAX_LENGTH,
required=False, required=False,
@ -88,10 +91,7 @@ class SecretForm(BootstrapMixin, CustomFieldModelForm):
widget=forms.PasswordInput() widget=forms.PasswordInput()
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=SecretRole.objects.all(), queryset=SecretRole.objects.all()
widget=APISelect(
api_url="/api/secrets/secret-roles/"
)
) )
tags = TagField( tags = TagField(
required=False required=False
@ -100,7 +100,7 @@ class SecretForm(BootstrapMixin, CustomFieldModelForm):
class Meta: class Meta:
model = Secret model = Secret
fields = [ fields = [
'role', 'name', 'plaintext', 'plaintext2', 'tags', 'device', 'role', 'name', 'plaintext', 'plaintext2', 'tags',
] ]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -160,10 +160,7 @@ class SecretBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditF
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=SecretRole.objects.all(), queryset=SecretRole.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/secrets/secret-roles/"
)
) )
name = forms.CharField( name = forms.CharField(
max_length=100, max_length=100,
@ -187,7 +184,6 @@ class SecretFilterForm(BootstrapMixin, CustomFieldFilterForm):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/secrets/secret-roles/",
value_field="slug", value_field="slug",
) )
) )

View File

@ -16,6 +16,7 @@ from taggit.managers import TaggableManager
from dcim.models import Device from dcim.models import Device
from extras.models import CustomFieldModel, TaggedItem from extras.models import CustomFieldModel, TaggedItem
from extras.utils import extras_features
from utilities.models import ChangeLoggedModel from utilities.models import ChangeLoggedModel
from .exceptions import InvalidKey from .exceptions import InvalidKey
from .hashers import SecretValidationHasher from .hashers import SecretValidationHasher
@ -295,6 +296,7 @@ class SecretRole(ChangeLoggedModel):
return user in self.users.all() or user.groups.filter(pk__in=self.groups.all()).exists() return user in self.users.all() or user.groups.filter(pk__in=self.groups.all()).exists()
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Secret(ChangeLoggedModel, CustomFieldModel): class Secret(ChangeLoggedModel, CustomFieldModel):
""" """
A Secret stores an AES256-encrypted copy of sensitive data, such as passwords or secret keys. An irreversible A Secret stores an AES256-encrypted copy of sensitive data, such as passwords or secret keys. An irreversible

View File

@ -17,6 +17,7 @@ urlpatterns = [
# Secrets # Secrets
path('secrets/', views.SecretListView.as_view(), name='secret_list'), path('secrets/', views.SecretListView.as_view(), name='secret_list'),
path('secrets/add/', views.secret_add, name='secret_add'),
path('secrets/import/', views.SecretBulkImportView.as_view(), name='secret_import'), path('secrets/import/', views.SecretBulkImportView.as_view(), name='secret_import'),
path('secrets/edit/', views.SecretBulkEditView.as_view(), name='secret_bulk_edit'), path('secrets/edit/', views.SecretBulkEditView.as_view(), name='secret_bulk_edit'),
path('secrets/delete/', views.SecretBulkDeleteView.as_view(), name='secret_bulk_delete'), path('secrets/delete/', views.SecretBulkDeleteView.as_view(), name='secret_bulk_delete'),

View File

@ -8,9 +8,8 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse from django.urls import reverse
from django.views.generic import View from django.views.generic import View
from dcim.models import Device
from utilities.views import ( from utilities.views import (
BulkDeleteView, BulkEditView, BulkImportView, ObjectDeleteView, ObjectEditView, ObjectListView, BulkDeleteView, BulkEditView, BulkImportView, GetReturnURLMixin, ObjectDeleteView, ObjectEditView, ObjectListView,
) )
from . import filters, forms, tables from . import filters, forms, tables
from .decorators import userkey_required from .decorators import userkey_required
@ -89,12 +88,9 @@ class SecretView(PermissionRequiredMixin, View):
@permission_required('secrets.add_secret') @permission_required('secrets.add_secret')
@userkey_required() @userkey_required()
def secret_add(request, pk): def secret_add(request):
# Retrieve device secret = Secret()
device = get_object_or_404(Device, pk=pk)
secret = Secret(device=device)
session_key = get_session_key(request) session_key = get_session_key(request)
if request.method == 'POST': if request.method == 'POST':
@ -123,17 +119,20 @@ def secret_add(request, pk):
messages.success(request, "Added new secret: {}.".format(secret)) messages.success(request, "Added new secret: {}.".format(secret))
if '_addanother' in request.POST: if '_addanother' in request.POST:
return redirect('dcim:device_addsecret', pk=device.pk) return redirect('secrets:secret_add')
else: else:
return redirect('secrets:secret', pk=secret.pk) return redirect('secrets:secret', pk=secret.pk)
else: else:
form = forms.SecretForm(instance=secret) initial_data = {
'device': request.GET.get('device'),
}
form = forms.SecretForm(initial=initial_data)
return render(request, 'secrets/secret_edit.html', { return render(request, 'secrets/secret_edit.html', {
'secret': secret, 'secret': secret,
'form': form, 'form': form,
'return_url': device.get_absolute_url(), 'return_url': GetReturnURLMixin().get_return_url(request, secret)
}) })

View File

@ -0,0 +1,19 @@
{% extends "admin/index.html" %}
{% block content_title %}{% endblock %}
{% block sidebar %}
{{ block.super }}
<div class="module">
<table style="width: 100%">
<caption>Utilities</caption>
<tbody>
<tr>
<th>
<a href="{% url 'rq_home' %}">Background Tasks</a>
</th>
</tr>
</tbody>
</table>
</div>
{% endblock %}

View File

@ -48,6 +48,9 @@
</div> </div>
{% endif %} {% endif %}
<a href="{{ termination.cable.get_absolute_url }}">{{ termination.cable }}</a> <a href="{{ termination.cable.get_absolute_url }}">{{ termination.cable }}</a>
<a href="{% url 'circuits:circuittermination_trace' pk=termination.pk %}" class="btn btn-primary btn-xs" title="Trace">
<i class="fa fa-share-alt" aria-hidden="true"></i>
</a>
{% if termination.connected_endpoint %} {% if termination.connected_endpoint %}
to <a href="{% url 'dcim:device' pk=termination.connected_endpoint.device.pk %}">{{ termination.connected_endpoint.device }}</a> to <a href="{% url 'dcim:device' pk=termination.connected_endpoint.device.pk %}">{{ termination.connected_endpoint.device }}</a>
<i class="fa fa-angle-right"></i> {{ termination.connected_endpoint }} <i class="fa fa-angle-right"></i> {{ termination.connected_endpoint }}

View File

@ -426,7 +426,7 @@
{% csrf_token %} {% csrf_token %}
</form> </form>
<div class="panel-footer text-right noprint"> <div class="panel-footer text-right noprint">
<a href="{% url 'dcim:device_addsecret' pk=device.pk %}" class="btn btn-xs btn-primary"> <a href="{% url 'secrets:secret_add' %}?device={{ device.pk }}&return_url={{ device.get_absolute_url }}" class="btn btn-xs btn-primary">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
Add secret Add secret
</a> </a>

View File

@ -271,7 +271,9 @@
</tr> </tr>
{% for resv in reservations %} {% for resv in reservations %}
<tr> <tr>
<td>{{ resv.unit_list }}</td> <td>
<a href="{{ resv.get_absolute_url }}">{{ resv.unit_list }}</a>
</td>
<td> <td>
{% if resv.tenant %} {% if resv.tenant %}
<a href="{{ resv.tenant.get_absolute_url }}">{{ resv.tenant }}</a> <a href="{{ resv.tenant.get_absolute_url }}">{{ resv.tenant }}</a>
@ -285,12 +287,12 @@
</td> </td>
<td class="text-right noprint"> <td class="text-right noprint">
{% if perms.dcim.change_rackreservation %} {% if perms.dcim.change_rackreservation %}
<a href="{% url 'dcim:rackreservation_edit' pk=resv.pk %}" class="btn btn-warning btn-xs" title="Edit reservation"> <a href="{% url 'dcim:rackreservation_edit' pk=resv.pk %}&return_url={{ rack.get_absolute_url }}" class="btn btn-warning btn-xs" title="Edit reservation">
<i class="glyphicon glyphicon-pencil" aria-hidden="true"></i> <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>
</a> </a>
{% endif %} {% endif %}
{% if perms.dcim.delete_rackreservation %} {% if perms.dcim.delete_rackreservation %}
<a href="{% url 'dcim:rackreservation_delete' pk=resv.pk %}" class="btn btn-danger btn-xs" title="Delete reservation"> <a href="{% url 'dcim:rackreservation_delete' pk=resv.pk %}&return_url={{ rack.get_absolute_url }}" class="btn btn-danger btn-xs" title="Delete reservation">
<i class="glyphicon glyphicon-trash" aria-hidden="true"></i> <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
</a> </a>
{% endif %} {% endif %}
@ -303,7 +305,7 @@
{% endif %} {% endif %}
{% if perms.dcim.add_rackreservation %} {% if perms.dcim.add_rackreservation %}
<div class="panel-footer text-right noprint"> <div class="panel-footer text-right noprint">
<a href="{% url 'dcim:rack_add_reservation' rack=rack.pk %}" class="btn btn-primary btn-xs"> <a href="{% url 'dcim:rackreservation_add' %}?rack={{ rack.pk }}&return_url={{ rack.get_absolute_url }}" class="btn btn-primary btn-xs">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
Add a reservation Add a reservation
</a> </a>

View File

@ -0,0 +1,149 @@
{% extends '_base.html' %}
{% load buttons %}
{% load custom_links %}
{% load helpers %}
{% load static %}
{% block header %}
<div class="row noprint">
<div class="col-sm-8 col-md-9">
<ol class="breadcrumb">
<li><a href="{% url 'dcim:rackreservation_list' %}">Rack Reservations</a></li>
<li><a href="{{ rackreservation.rack.get_absolute_url }}">{{ rackreservation.rack }}</a></li>
<li>Units {{ rackreservation.unit_list }}</li>
</ol>
</div>
<div class="col-sm-4 col-md-3">
<form action="{% url 'dcim:rackreservation_list' %}" method="get">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search racks" />
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">
<span class="fa fa-search" aria-hidden="true"></span>
</button>
</span>
</div>
</form>
</div>
</div>
<div class="pull-right noprint">
{% if perms.dcim.change_rackreservation %}
{% edit_button rackreservation %}
{% endif %}
{% if perms.dcim.delete_rackreservation %}
{% delete_button rackreservation %}
{% endif %}
</div>
<h1>{% block title %}{{ rackreservation }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=rackreservation %}
<div class="pull-right noprint">
{% custom_links rackreservation %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ rackreservation.get_absolute_url }}">Rack</a>
</li>
{% if perms.extras.view_objectchange %}
<li role="presentation"{% if active_tab == 'changelog' %} class="active"{% endif %}>
<a href="{% url 'dcim:rackreservation_changelog' pk=rackreservation.pk %}">Change Log</a>
</li>
{% endif %}
</ul>
{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<strong>Rack</strong>
</div>
<table class="table table-hover panel-body attr-table">
{% with rack=rackreservation.rack %}
<tr>
<td>Site</td>
<td>
{% if rack.site.region %}
<a href="{{ rack.site.region.get_absolute_url }}">{{ rack.site.region }}</a>
<i class="fa fa-angle-right"></i>
{% endif %}
<a href="{% url 'dcim:site' slug=rack.site.slug %}">{{ rack.site }}</a>
</td>
</tr>
<tr>
<td>Group</td>
<td>
{% if rack.group %}
<a href="{% url 'dcim:rack_list' %}?site={{ rack.site.slug }}&group={{ rack.group.slug }}">{{ rack.group }}</a>
{% else %}
<span class="text-muted">None</span>
{% endif %}
</td>
</tr>
<tr>
<td>Rack</td>
<td>
<a href="{{ rack.get_absolute_url }}">{{ rack }}</a>
</td>
</tr>
{% endwith %}
</table>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<strong>Reservation Details</strong>
</div>
<table class="table table-hover panel-body attr-table">
<tr>
<td>Units</td>
<td>{{ rackreservation.unit_list }}</td>
</tr>
<tr>
<td>Tenant</td>
<td>
{% if rackreservation.tenant %}
{% if rackreservation.tenant.group %}
<a href="{{ rackreservation.tenant.group.get_absolute_url }}">{{ rackreservation.tenant.group }}</a>
<i class="fa fa-angle-right"></i>
{% endif %}
<a href="{{ rackreservation.tenant.get_absolute_url }}">{{ rackreservation.tenant }}</a>
{% else %}
<span class="text-muted">None</span>
{% endif %}
</td>
</tr>
<tr>
<td>User</td>
<td>{{ rackreservation.user }}</td>
</tr>
<tr>
<td>Description</td>
<td>{{ rackreservation.description }}</td>
</tr>
</table>
</div>
</div>
<div class="col-md-6">
{% with rack=rackreservation.rack %}
<div class="row" style="margin-bottom: 20px">
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="rack_header">
<h4>Front</h4>
</div>
{% include 'dcim/inc/rack_elevation.html' with face='front' %}
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="rack_header">
<h4>Rear</h4>
</div>
{% include 'dcim/inc/rack_elevation.html' with face='rear' %}
</div>
</div>
{% endwith %}
</div>
</div>
{% endblock %}
{% block javascript %}
<script src="{% static 'js/rack_elevations.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}

View File

@ -0,0 +1,21 @@
{% extends 'utilities/obj_edit.html' %}
{% load form_helpers %}
{% block form %}
<div class="panel panel-default">
<div class="panel-heading"><strong>{{ obj_type|capfirst }}</strong></div>
<div class="panel-body">
<div class="form-group">
<label class="col-md-3 control-label">Rack</label>
<div class="col-md-9">
<p class="form-control-static">{{ obj.rack }}</p>
</div>
</div>
{% render_field form.units %}
{% render_field form.user %}
{% render_field form.tenant_group %}
{% render_field form.tenant %}
{% render_field form.description %}
</div>
</div>
{% endblock %}

View File

@ -1,6 +1,19 @@
{% extends '_base.html' %} {% extends '_base.html' %}
{% load helpers %} {% load helpers %}
{% block header %}
{{ block.super }}
{% if new_release %}
{# new_release is set only if the current user is a superuser or staff member #}
<div class="alert alert-info text-center" role="alert">
<i class="fa fa-info-circle"></i>
A new release is available: <a href="{{ new_release.url }}">NetBox v{{ new_release.version }}</a> |
<a href="https://netbox.readthedocs.io/en/stable/installation/upgrading/">Upgrade instructions</a>
</div>
{% endif %}
{% endblock %}
{% block content %} {% block content %}
{% include 'search_form.html' %} {% include 'search_form.html' %}
<div class="row"> <div class="row">

View File

@ -7,7 +7,7 @@
<table class="table table-hover panel-body attr-table"> <table class="table table-hover panel-body attr-table">
{% for field, value in custom_fields.items %} {% for field, value in custom_fields.items %}
<tr> <tr>
<td>{{ field }}</td> <td><span title="{{ field.description }}">{{ field }}</span></td>
<td> <td>
{% if field.type == 'boolean' and value == True %} {% if field.type == 'boolean' and value == True %}
<i class="glyphicon glyphicon-ok text-success" title="True"></i> <i class="glyphicon glyphicon-ok text-success" title="True"></i>

View File

@ -462,6 +462,7 @@
<li{% if not perms.secrets.view_secret %} class="disabled"{% endif %}> <li{% if not perms.secrets.view_secret %} class="disabled"{% endif %}>
{% if perms.secrets.add_secret %} {% if perms.secrets.add_secret %}
<div class="buttons pull-right"> <div class="buttons pull-right">
<a href="{% url 'secrets:secret_add' %}" class="btn btn-xs btn-success" title="Add"><i class="fa fa-plus"></i></a>
<a href="{% url 'secrets:secret_import' %}" class="btn btn-xs btn-info" title="Import"><i class="fa fa-download"></i></a> <a href="{% url 'secrets:secret_import' %}" class="btn btn-xs btn-info" title="Import"><i class="fa fa-download"></i></a>
</div> </div>
{% endif %} {% endif %}

View File

@ -21,12 +21,7 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"><strong>Secret Attributes</strong></div> <div class="panel-heading"><strong>Secret Attributes</strong></div>
<div class="panel-body"> <div class="panel-body">
<div class="form-group"> {% render_field form.device %}
<label class="col-md-3 control-label required">Device</label>
<div class="col-md-9">
<p class="form-control-static">{{ secret.device }}</p>
</div>
</div>
{% render_field form.role %} {% render_field form.role %}
{% render_field form.name %} {% render_field form.name %}
{% render_field form.userkeys %} {% render_field form.userkeys %}

View File

@ -51,7 +51,7 @@
</div> </div>
</div> </div>
</form> </form>
{% if settings.DOCS_ROOT %} {% if obj and settings.DOCS_ROOT %}
{% include 'inc/modal.html' with name='docs' content=obj|get_docs %} {% include 'inc/modal.html' with name='docs' content=obj|get_docs %}
{% endif %} {% endif %}
{% endblock %} {% endblock %}

View File

@ -44,10 +44,7 @@ class TenantForm(BootstrapMixin, CustomFieldModelForm):
slug = SlugField() slug = SlugField()
group = DynamicModelChoiceField( group = DynamicModelChoiceField(
queryset=TenantGroup.objects.all(), queryset=TenantGroup.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenant-groups/"
)
) )
comments = CommentField() comments = CommentField()
tags = TagField( tags = TagField(
@ -89,10 +86,7 @@ class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditF
) )
group = DynamicModelChoiceField( group = DynamicModelChoiceField(
queryset=TenantGroup.objects.all(), queryset=TenantGroup.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenant-groups/"
)
) )
class Meta: class Meta:
@ -112,7 +106,6 @@ class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/tenancy/tenant-groups/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -129,7 +122,6 @@ class TenancyForm(forms.Form):
queryset=TenantGroup.objects.all(), queryset=TenantGroup.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/tenancy/tenant-groups/",
filter_for={ filter_for={
'tenant': 'group_id', 'tenant': 'group_id',
}, },
@ -140,10 +132,7 @@ class TenancyForm(forms.Form):
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url='/api/tenancy/tenants/'
)
) )
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -164,7 +153,6 @@ class TenancyFilterForm(forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/tenancy/tenant-groups/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
filter_for={ filter_for={
@ -177,7 +165,6 @@ class TenancyFilterForm(forms.Form):
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/tenancy/tenants/",
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )

View File

@ -4,6 +4,7 @@ from django.urls import reverse
from taggit.managers import TaggableManager from taggit.managers import TaggableManager
from extras.models import CustomFieldModel, TaggedItem from extras.models import CustomFieldModel, TaggedItem
from extras.utils import extras_features
from utilities.models import ChangeLoggedModel from utilities.models import ChangeLoggedModel
@ -43,6 +44,7 @@ class TenantGroup(ChangeLoggedModel):
) )
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Tenant(ChangeLoggedModel, CustomFieldModel): class Tenant(ChangeLoggedModel, CustomFieldModel):
""" """
A Tenant represents an organization served by the NetBox owner. This is typically a customer or an internal A Tenant represents an organization served by the NetBox owner. This is typically a customer or an internal

View File

@ -234,6 +234,7 @@ class ValidatedModelSerializer(ModelSerializer):
for k, v in attrs.items(): for k, v in attrs.items():
setattr(instance, k, v) setattr(instance, k, v)
instance.clean() instance.clean()
instance.validate_unique()
return data return data

View File

@ -0,0 +1,52 @@
import logging
import requests
from cacheops.simple import cache, CacheMiss
from django.conf import settings
from django_rq import job
from packaging import version
# Get an instance of a logger
logger = logging.getLogger('netbox.releases')
@job('check_releases')
def get_releases(pre_releases=False):
url = settings.RELEASE_CHECK_URL
headers = {
'Accept': 'application/vnd.github.v3+json',
}
releases = []
# Check whether this URL has failed recently and shouldn't be retried yet
try:
if url == cache.get('latest_release_no_retry'):
logger.info("Skipping release check; URL failed recently: {}".format(url))
return []
except CacheMiss:
pass
try:
logger.debug("Fetching new releases from {}".format(url))
response = requests.get(url, headers=headers)
response.raise_for_status()
total_releases = len(response.json())
for release in response.json():
if 'tag_name' not in release:
continue
if not pre_releases and (release.get('devrelease') or release.get('prerelease')):
continue
releases.append((version.parse(release['tag_name']), release.get('html_url')))
logger.debug("Found {} releases; {} usable".format(total_releases, len(releases)))
except requests.exceptions.RequestException:
# The request failed. Set a flag in the cache to disable future checks to this URL for 15 minutes.
logger.exception("Error while fetching {}. Disabling checks for 15 minutes.".format(url))
cache.set('latest_release_no_retry', url, 900)
return []
# Cache the most recent release
cache.set('latest_release', max(releases), settings.RELEASE_CHECK_TIMEOUT)
return releases

View File

@ -10,6 +10,7 @@ from django.conf import settings
from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
from django.db.models import Count from django.db.models import Count
from django.forms import BoundField from django.forms import BoundField
from django.urls import reverse
from .choices import unpack_grouped_choices from .choices import unpack_grouped_choices
from .constants import * from .constants import *
@ -252,7 +253,7 @@ class APISelect(SelectWithDisabled):
""" """
A select widget populated via an API call A select widget populated via an API call
:param api_url: API URL :param api_url: API endpoint URL. Required if not set automatically by the parent field.
:param display_field: (Optional) Field to display for child in selection list. Defaults to `name`. :param display_field: (Optional) Field to display for child in selection list. Defaults to `name`.
:param value_field: (Optional) Field to use for the option value in selection list. Defaults to `id`. :param value_field: (Optional) Field to use for the option value in selection list. Defaults to `id`.
:param disabled_indicator: (Optional) Mark option as disabled if this field equates true. :param disabled_indicator: (Optional) Mark option as disabled if this field equates true.
@ -269,7 +270,7 @@ class APISelect(SelectWithDisabled):
""" """
def __init__( def __init__(
self, self,
api_url, api_url=None,
display_field=None, display_field=None,
value_field=None, value_field=None,
disabled_indicator=None, disabled_indicator=None,
@ -285,7 +286,8 @@ class APISelect(SelectWithDisabled):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.attrs['class'] = 'netbox-select2-api' self.attrs['class'] = 'netbox-select2-api'
self.attrs['data-url'] = '/{}{}'.format(settings.BASE_PATH, api_url.lstrip('/')) # Inject BASE_PATH if api_url:
self.attrs['data-url'] = '/{}{}'.format(settings.BASE_PATH, api_url.lstrip('/')) # Inject BASE_PATH
if full: if full:
self.attrs['data-full'] = full self.attrs['data-full'] = full
if display_field: if display_field:
@ -566,6 +568,10 @@ class TagFilterField(forms.MultipleChoiceField):
class DynamicModelChoiceMixin: class DynamicModelChoiceMixin:
filter = django_filters.ModelChoiceFilter filter = django_filters.ModelChoiceFilter
widget = APISelect
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_bound_field(self, form, field_name): def get_bound_field(self, form, field_name):
bound_field = BoundField(form, self, field_name) bound_field = BoundField(form, self, field_name)
@ -579,6 +585,14 @@ class DynamicModelChoiceMixin:
else: else:
self.queryset = self.queryset.none() self.queryset = self.queryset.none()
# Set the data URL on the APISelect widget (if not already set)
widget = bound_field.field.widget
if not widget.attrs.get('data-url'):
app_label = self.queryset.model._meta.app_label
model_name = self.queryset.model._meta.model_name
data_url = reverse('{}-api:{}-list'.format(app_label, model_name))
widget.attrs['data-url'] = data_url
return bound_field return bound_field
@ -595,6 +609,7 @@ class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultip
A multiple-choice version of DynamicModelChoiceField. A multiple-choice version of DynamicModelChoiceField.
""" """
filter = django_filters.ModelMultipleChoiceFilter filter = django_filters.ModelMultipleChoiceFilter
widget = APISelectMultiple
class LaxURLField(forms.URLField): class LaxURLField(forms.URLField):

View File

@ -40,7 +40,7 @@ def render_markdown(value):
value = strip_tags(value) value = strip_tags(value)
# Render Markdown # Render Markdown
html = markdown(value, extensions=['fenced_code']) html = markdown(value, extensions=['fenced_code', 'tables'])
return mark_safe(html) return mark_safe(html)
@ -196,7 +196,7 @@ def get_docs(model):
return "Unable to load documentation, error reading file: {}".format(path) return "Unable to load documentation, error reading file: {}".format(path)
# Render Markdown with the admonition extension # Render Markdown with the admonition extension
content = markdown(content, extensions=['admonition', 'fenced_code']) content = markdown(content, extensions=['admonition', 'fenced_code', 'tables'])
return mark_safe(content) return mark_safe(content)

View File

@ -9,7 +9,7 @@ from dcim.models import Device, DeviceRole, Interface, Platform, Rack, Region, S
from extras.forms import ( from extras.forms import (
AddRemoveTagsForm, CustomFieldBulkEditForm, CustomFieldModelCSVForm, CustomFieldModelForm, CustomFieldFilterForm, AddRemoveTagsForm, CustomFieldBulkEditForm, CustomFieldModelCSVForm, CustomFieldModelForm, CustomFieldFilterForm,
) )
from ipam.models import IPAddress, VLANGroup, VLAN from ipam.models import IPAddress, VLAN
from tenancy.forms import TenancyFilterForm, TenancyForm from tenancy.forms import TenancyFilterForm, TenancyForm
from tenancy.models import Tenant from tenancy.models import Tenant
from utilities.forms import ( from utilities.forms import (
@ -77,24 +77,15 @@ class ClusterGroupCSVForm(forms.ModelForm):
class ClusterForm(BootstrapMixin, TenancyForm, CustomFieldModelForm): class ClusterForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
type = DynamicModelChoiceField( type = DynamicModelChoiceField(
queryset=ClusterType.objects.all(), queryset=ClusterType.objects.all()
widget=APISelect(
api_url="/api/virtualization/cluster-types/"
)
) )
group = DynamicModelChoiceField( group = DynamicModelChoiceField(
queryset=ClusterGroup.objects.all(), queryset=ClusterGroup.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/virtualization/cluster-groups/"
)
) )
site = DynamicModelChoiceField( site = DynamicModelChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/dcim/sites/"
)
) )
comments = CommentField() comments = CommentField()
tags = TagField( tags = TagField(
@ -157,31 +148,19 @@ class ClusterBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEdit
) )
type = DynamicModelChoiceField( type = DynamicModelChoiceField(
queryset=ClusterType.objects.all(), queryset=ClusterType.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/virtualization/cluster-types/"
)
) )
group = DynamicModelChoiceField( group = DynamicModelChoiceField(
queryset=ClusterGroup.objects.all(), queryset=ClusterGroup.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/virtualization/cluster-groups/"
)
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/tenancy/tenants/"
)
) )
site = DynamicModelChoiceField( site = DynamicModelChoiceField(
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False
widget=APISelect(
api_url="/api/dcim/sites/"
)
) )
comments = CommentField( comments = CommentField(
widget=SmallTextarea, widget=SmallTextarea,
@ -205,7 +184,6 @@ class ClusterFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/virtualization/cluster-types/",
value_field='slug', value_field='slug',
) )
) )
@ -214,7 +192,6 @@ class ClusterFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/regions/",
value_field="slug", value_field="slug",
filter_for={ filter_for={
'site': 'region' 'site': 'region'
@ -226,7 +203,6 @@ class ClusterFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/dcim/sites/",
value_field='slug', value_field='slug',
null_option=True, null_option=True,
) )
@ -236,7 +212,6 @@ class ClusterFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/virtualization/cluster-groups/",
value_field='slug', value_field='slug',
null_option=True, null_option=True,
) )
@ -249,7 +224,6 @@ class ClusterAddDevicesForm(BootstrapMixin, forms.Form):
queryset=Region.objects.all(), queryset=Region.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/dcim/regions/",
filter_for={ filter_for={
"site": "region_id", "site": "region_id",
}, },
@ -262,7 +236,6 @@ class ClusterAddDevicesForm(BootstrapMixin, forms.Form):
queryset=Site.objects.all(), queryset=Site.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url='/api/dcim/sites/',
filter_for={ filter_for={
"rack": "site_id", "rack": "site_id",
"devices": "site_id", "devices": "site_id",
@ -273,7 +246,6 @@ class ClusterAddDevicesForm(BootstrapMixin, forms.Form):
queryset=Rack.objects.all(), queryset=Rack.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url='/api/dcim/racks/',
filter_for={ filter_for={
"devices": "rack_id" "devices": "rack_id"
}, },
@ -285,7 +257,6 @@ class ClusterAddDevicesForm(BootstrapMixin, forms.Form):
devices = DynamicModelMultipleChoiceField( devices = DynamicModelMultipleChoiceField(
queryset=Device.objects.filter(cluster__isnull=True), queryset=Device.objects.filter(cluster__isnull=True),
widget=APISelectMultiple( widget=APISelectMultiple(
api_url='/api/dcim/devices/',
display_field='display_name', display_field='display_name',
disabled_indicator='cluster' disabled_indicator='cluster'
) )
@ -334,7 +305,6 @@ class VirtualMachineForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
queryset=ClusterGroup.objects.all(), queryset=ClusterGroup.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url='/api/virtualization/cluster-groups/',
filter_for={ filter_for={
"cluster": "group_id", "cluster": "group_id",
}, },
@ -344,16 +314,12 @@ class VirtualMachineForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
) )
) )
cluster = DynamicModelChoiceField( cluster = DynamicModelChoiceField(
queryset=Cluster.objects.all(), queryset=Cluster.objects.all()
widget=APISelect(
api_url='/api/virtualization/clusters/'
)
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=DeviceRole.objects.all(), queryset=DeviceRole.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/dcim/device-roles/",
additional_query_params={ additional_query_params={
"vm_role": "True" "vm_role": "True"
} }
@ -361,10 +327,7 @@ class VirtualMachineForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
) )
platform = DynamicModelChoiceField( platform = DynamicModelChoiceField(
queryset=Platform.objects.all(), queryset=Platform.objects.all(),
required=False, required=False
widget=APISelect(
api_url='/api/dcim/platforms/'
)
) )
tags = TagField( tags = TagField(
required=False required=False
@ -499,10 +462,7 @@ class VirtualMachineBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldB
) )
cluster = DynamicModelChoiceField( cluster = DynamicModelChoiceField(
queryset=Cluster.objects.all(), queryset=Cluster.objects.all(),
required=False, required=False
widget=APISelect(
api_url='/api/virtualization/clusters/'
)
) )
role = DynamicModelChoiceField( role = DynamicModelChoiceField(
queryset=DeviceRole.objects.filter( queryset=DeviceRole.objects.filter(
@ -510,7 +470,6 @@ class VirtualMachineBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldB
), ),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/dcim/device-roles/",
additional_query_params={ additional_query_params={
"vm_role": "True" "vm_role": "True"
} }
@ -518,17 +477,11 @@ class VirtualMachineBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldB
) )
tenant = DynamicModelChoiceField( tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False
widget=APISelect(
api_url='/api/tenancy/tenants/'
)
) )
platform = DynamicModelChoiceField( platform = DynamicModelChoiceField(
queryset=Platform.objects.all(), queryset=Platform.objects.all(),
required=False, required=False
widget=APISelect(
api_url='/api/dcim/platforms/'
)
) )
vcpus = forms.IntegerField( vcpus = forms.IntegerField(
required=False, required=False,
@ -568,7 +521,6 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url='/api/virtualization/cluster-groups/',
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -578,7 +530,6 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url='/api/virtualization/cluster-types/',
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -586,17 +537,13 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
cluster_id = DynamicModelMultipleChoiceField( cluster_id = DynamicModelMultipleChoiceField(
queryset=Cluster.objects.all(), queryset=Cluster.objects.all(),
required=False, required=False,
label='Cluster', label='Cluster'
widget=APISelectMultiple(
api_url='/api/virtualization/clusters/',
)
) )
region = DynamicModelMultipleChoiceField( region = DynamicModelMultipleChoiceField(
queryset=Region.objects.all(), queryset=Region.objects.all(),
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url='/api/dcim/regions/',
value_field="slug", value_field="slug",
filter_for={ filter_for={
'site': 'region' 'site': 'region'
@ -608,7 +555,6 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url='/api/dcim/sites/',
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -618,7 +564,6 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url='/api/dcim/device-roles/',
value_field="slug", value_field="slug",
null_option=True, null_option=True,
additional_query_params={ additional_query_params={
@ -636,7 +581,6 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
to_field_name='slug', to_field_name='slug',
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url='/api/dcim/platforms/',
value_field="slug", value_field="slug",
null_option=True, null_option=True,
) )
@ -657,7 +601,6 @@ class InterfaceForm(BootstrapMixin, forms.ModelForm):
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/ipam/vlans/",
display_field='display_name', display_field='display_name',
full=True, full=True,
additional_query_params={ additional_query_params={
@ -669,7 +612,6 @@ class InterfaceForm(BootstrapMixin, forms.ModelForm):
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/vlans/",
display_field='display_name', display_field='display_name',
full=True, full=True,
additional_query_params={ additional_query_params={
@ -766,7 +708,6 @@ class InterfaceCreateForm(BootstrapMixin, forms.Form):
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/ipam/vlans/",
display_field='display_name', display_field='display_name',
full=True, full=True,
additional_query_params={ additional_query_params={
@ -778,7 +719,6 @@ class InterfaceCreateForm(BootstrapMixin, forms.Form):
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/vlans/",
display_field='display_name', display_field='display_name',
full=True, full=True,
additional_query_params={ additional_query_params={
@ -836,7 +776,6 @@ class InterfaceBulkEditForm(BootstrapMixin, BulkEditForm):
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
required=False, required=False,
widget=APISelect( widget=APISelect(
api_url="/api/ipam/vlans/",
display_field='display_name', display_field='display_name',
full=True, full=True,
additional_query_params={ additional_query_params={
@ -848,7 +787,6 @@ class InterfaceBulkEditForm(BootstrapMixin, BulkEditForm):
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
required=False, required=False,
widget=APISelectMultiple( widget=APISelectMultiple(
api_url="/api/ipam/vlans/",
display_field='display_name', display_field='display_name',
full=True, full=True,
additional_query_params={ additional_query_params={

View File

@ -7,6 +7,7 @@ from taggit.managers import TaggableManager
from dcim.models import Device from dcim.models import Device
from extras.models import ConfigContextModel, CustomFieldModel, TaggedItem from extras.models import ConfigContextModel, CustomFieldModel, TaggedItem
from extras.utils import extras_features
from utilities.models import ChangeLoggedModel from utilities.models import ChangeLoggedModel
from .choices import * from .choices import *
@ -91,6 +92,7 @@ class ClusterGroup(ChangeLoggedModel):
# Clusters # Clusters
# #
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class Cluster(ChangeLoggedModel, CustomFieldModel): class Cluster(ChangeLoggedModel, CustomFieldModel):
""" """
A cluster of VirtualMachines. Each Cluster may optionally be associated with one or more Devices. A cluster of VirtualMachines. Each Cluster may optionally be associated with one or more Devices.
@ -177,6 +179,7 @@ class Cluster(ChangeLoggedModel, CustomFieldModel):
# Virtual machines # Virtual machines
# #
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class VirtualMachine(ChangeLoggedModel, ConfigContextModel, CustomFieldModel): class VirtualMachine(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
""" """
A virtual machine which runs inside a Cluster. A virtual machine which runs inside a Cluster.

View File

@ -501,6 +501,18 @@ class VirtualMachineTest(APITestCase):
self.assertFalse('config_context' in response.data['results'][0]) self.assertFalse('config_context' in response.data['results'][0])
def test_unique_name_per_cluster_constraint(self):
data = {
'name': 'Test Virtual Machine 1',
'cluster': self.cluster1.pk,
}
url = reverse('virtualization-api:virtualmachine-list')
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
class InterfaceTest(APITestCase): class InterfaceTest(APITestCase):