diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml index 80810f2ba..9ed740fff 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yaml +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -14,7 +14,7 @@ body: attributes: label: NetBox version description: What version of NetBox are you currently running? - placeholder: v3.4.3 + placeholder: v3.4.4 validations: required: true - type: dropdown diff --git a/.github/ISSUE_TEMPLATE/feature_request.yaml b/.github/ISSUE_TEMPLATE/feature_request.yaml index 975fc025a..8e4ab54a5 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yaml +++ b/.github/ISSUE_TEMPLATE/feature_request.yaml @@ -14,7 +14,7 @@ body: attributes: label: NetBox version description: What version of NetBox are you currently running? - placeholder: v3.4.3 + placeholder: v3.4.4 validations: required: true - type: dropdown diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index ab259af2a..3b37aae56 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -24,7 +24,7 @@ jobs: necessary. close-pr-message: > This PR has been automatically closed due to lack of activity. - days-before-stale: 60 + days-before-stale: 90 days-before-close: 30 exempt-issue-labels: 'status: accepted,status: blocked,status: needs milestone' operations-per-run: 100 diff --git a/README.md b/README.md index e14f31b56..053aa8461 100644 --- a/README.md +++ b/README.md @@ -1,71 +1,59 @@
NetBox logo + + The premiere source of truth powering network automation
+![Master branch build status](https://github.com/netbox-community/netbox/workflows/CI/badge.svg?branch=master) + NetBox is the leading solution for modeling and documenting modern networks. By combining the traditional disciplines of IP address management (IPAM) and datacenter infrastructure management (DCIM) with powerful APIs and extensions, NetBox provides the ideal "source of truth" to power network automation. -Available as open source software under the Apache 2.0 license, NetBox is -employed by thousands of organizations around the world. +Available as open source software under the Apache 2.0 license, NetBox serves +as the cornerstone for network automation in thousands of organizations. -![Master branch build status](https://github.com/netbox-community/netbox/workflows/CI/badge.svg?branch=master) - -[![Timeline graph](https://images.repography.com/29023055/netbox-community/netbox/recent-activity/31db894eee74b8a5475e3af307a81b6c_timeline.svg)](https://github.com/netbox-community/netbox/commits) -[![Issue status graph](https://images.repography.com/29023055/netbox-community/netbox/recent-activity/31db894eee74b8a5475e3af307a81b6c_issues.svg)](https://github.com/netbox-community/netbox/issues) -[![Pull request status graph](https://images.repography.com/29023055/netbox-community/netbox/recent-activity/31db894eee74b8a5475e3af307a81b6c_prs.svg)](https://github.com/netbox-community/netbox/pulls) -[![Top contributors](https://images.repography.com/29023055/netbox-community/netbox/recent-activity/31db894eee74b8a5475e3af307a81b6c_users.svg)](https://github.com/netbox-community/netbox/graphs/contributors) -
Stats via [Repography](https://repography.com) - -## About NetBox +* **Physical infrasucture:** Accurately model the physical world, from global regions down to individual racks of gear. Then connect everything - network, console, and power! +* **Modern IPAM:** All the standard IPAM functionality you expect, plus VRF import/export tracking, VLAN management, and overlay support. +* **Data circuits:** Confidently manage the delivery of crtical circuits from various service providers, modeled seamlessly alongside your own infrastructure. +* **Power tracking:** Map the distribution of power from upstream sources to individual feeds and outlets. +* **Organization:** Manage tenant and contact assignments natively. +* **Powerful search:** Easily find anything you need using a single global search function. +* **Comprehensive logging:** Leverage both automatic change logging and user-submitted journal entries to track your network's growth over time. +* **Endless customization:** Custom fields, custom links, tags, export templates, custom validation, reports, scripts, and more! +* **Flexible permissions:** An advanced permissions systems enables very flexible delegation of permissions. +* **Integrations:** Easily connect NetBox to your other tooling via its REST & GraphQL APIs. +* **Plugins:** Not finding what you need in the core application? Try one of many community plugins - or build your own! ![Screenshot of NetBox UI](docs/media/screenshots/netbox-ui.png "NetBox UI") -Myriad infrastructure components can be modeled in NetBox, including: +## Getting Started -* Hierarchical regions, site groups, sites, and locations -* Racks, devices, and device components -* Cables and wireless connections -* Power distribution -* Data circuits and providers -* Virtual machines and clusters -* IP prefixes, ranges, and addresses -* VRFs and route targets -* L2VPN and overlays -* FHRP groups (VRRP, HSRP, etc.) -* AS numbers -* VLANs and scoped VLAN groups -* Organizational tenants and contacts +* Just want to explore? Check out [our public demo](https://demo.netbox.dev/) right now! +* The [official documentation](https://docs.netbox.dev) offers a comprehensive introduction. +* Choose your deployment: [self-hosted](https://github.com/netbox-community/netbox), [Docker](https://github.com/netbox-community/netbox-docker), or [NetBox Cloud](https://netboxlabs.com/netbox-cloud/). +* Check out [our wiki](https://github.com/netbox-community/netbox/wiki/Community-Contributions) for even more projects to get the most out of NetBox! -In addition to its extensive built-in models and functionality, NetBox can be -customized and extended through the use of: +## Get Involved -* Custom fields -* Custom links -* Configuration contexts -* Custom model validation rules -* Reports -* Custom scripts -* Export templates -* Conditional webhooks -* Plugins -* Single sign-on (SSO) authentication -* NAPALM integration -* Detailed change logging +* Follow [@NetBoxOfficial](https://twitter.com/NetBoxOfficial) on Twitter! +* Join the conversation on [the discussion forum](https://github.com/netbox-community/netbox/discussions) and [Slack](https://netdev.chat/)! +* Already a power user? You can [suggest a feature](https://github.com/netbox-community/netbox/issues/new?assignees=&labels=type%3A+feature&template=feature_request.yaml) or [report a bug](https://github.com/netbox-community/netbox/issues/new?assignees=&labels=type%3A+bug&template=bug_report.yaml) on GitHub. +* Contributions from the community are encouraged and appreciated! Check out our [contributing guide](CONTRIBUTING.md) to get started. -NetBox also features a complete REST API as well as a GraphQL API for easily -integrating with other tools and systems. - -The complete documentation for NetBox can be found at [docs.netbox.dev](https://docs.netbox.dev/). -A public demo instance is available at [demo.netbox.dev](https://demo.netbox.dev). - -NetBox runs as a web application atop the [Django](https://www.djangoproject.com/) -Python framework with a [PostgreSQL](https://www.postgresql.org/) database. For a -complete list of requirements, see `requirements.txt`. The code is available -[on GitHub](https://github.com/netbox-community/netbox). +## Project Stats + +
+ Timeline graph + Issues graph + Pull requests graph + Top contributors +
Stats via Repography +
+ +## Sponsors
-

Thank you to our sponsors!

[![NetBox Labs](https://raw.githubusercontent.com/wiki/netbox-community/netbox/images/sponsors/netbox_labs.png)](https://netboxlabs.com)            @@ -76,34 +64,10 @@ complete list of requirements, see `requirements.txt`. The code is available [![Sentry](https://raw.githubusercontent.com/wiki/netbox-community/netbox/images/sponsors/sentry.png)](https://sentry.io)            [![Equinix Metal](https://raw.githubusercontent.com/wiki/netbox-community/netbox/images/sponsors/equinix.png)](https://metal.equinix.com) -            - [![Stellar Technologies](https://raw.githubusercontent.com/wiki/netbox-community/netbox/images/sponsors/stellar.png)](https://stellar.tech)
-### Discussion - -* [GitHub Discussions](https://github.com/netbox-community/netbox/discussions) - Discussion forum hosted by GitHub; ideal for Q&A and other structured discussions -* [Slack](https://netdev.chat/) - Real-time chat hosted by the NetDev Community; best for unstructured discussion or just hanging out - -### Installation - -Please see [the documentation](https://docs.netbox.dev/) for -instructions on installing NetBox. To upgrade NetBox, please download the -[latest release](https://github.com/netbox-community/netbox/releases) and -run `upgrade.sh`. - -### Providing Feedback - -The best platform for general feedback, assistance, and other discussion is our -[GitHub discussions](https://github.com/netbox-community/netbox/discussions). -To report a bug or request a specific feature, please open a GitHub issue using -the [appropriate template](https://github.com/netbox-community/netbox/issues/new/choose). - -If you are interested in contributing to the development of NetBox, please read -our [contributing guide](CONTRIBUTING.md) prior to beginning any work. - -### Screenshots +## Screenshots ![Screenshot of main page (dark mode)](docs/media/screenshots/home-dark.png "Main page (dark mode)") @@ -112,8 +76,3 @@ our [contributing guide](CONTRIBUTING.md) prior to beginning any work. ![Screenshot of prefixes hierarchy](docs/media/screenshots/prefixes-list.png "Prefixes hierarchy") ![Screenshot of cable trace](docs/media/screenshots/cable-trace.png "Cable tracing") - -### Related projects - -Please see [our wiki](https://github.com/netbox-community/netbox/wiki/Community-Contributions) -for a list of relevant community projects. diff --git a/base_requirements.txt b/base_requirements.txt index af8646493..41cfb581f 100644 --- a/base_requirements.txt +++ b/base_requirements.txt @@ -1,6 +1,6 @@ # HTML sanitizer # https://github.com/mozilla/bleach -bleach +bleach<6.0 # The Python web framework on which NetBox is built # https://github.com/django/django diff --git a/docs/administration/housekeeping.md b/docs/administration/housekeeping.md index da1a5443b..fcc3aa04e 100644 --- a/docs/administration/housekeeping.md +++ b/docs/administration/housekeeping.md @@ -5,6 +5,7 @@ NetBox includes a `housekeeping` management command that should be run nightly. * Clearing expired authentication sessions from the database * Deleting changelog records older than the configured [retention time](../configuration/miscellaneous.md#changelog_retention) * Deleting job result records older than the configured [retention time](../configuration/miscellaneous.md#jobresult_retention) +* Check for new NetBox releases (if [`RELEASE_CHECK_URL`](../configuration/miscellaneous.md#release_check_url) is set) This command can be invoked directly, or by using the shell script provided at `/opt/netbox/contrib/netbox-housekeeping.sh`. This script can be linked from your cron scheduler's daily jobs directory (e.g. `/etc/cron.daily`) or referenced directly within the cron configuration file. diff --git a/docs/configuration/miscellaneous.md b/docs/configuration/miscellaneous.md index 4eb090554..eac5d0a2f 100644 --- a/docs/configuration/miscellaneous.md +++ b/docs/configuration/miscellaneous.md @@ -69,6 +69,14 @@ By default, NetBox will permit users to create duplicate prefixes and IP address --- +## FILE_UPLOAD_MAX_MEMORY_SIZE + +Default: 2621440 (i.e. 2.5 MB). + +The maximum size (in bytes) that an upload will be before it gets streamed to the file system. Changing this setting can be useful for example to be able to upload files bigger than 2.5MB to custom scripts for processing. + +--- + ## GRAPHQL_ENABLED !!! tip "Dynamic Configuration Parameter" diff --git a/docs/customization/custom-scripts.md b/docs/customization/custom-scripts.md index 6c02d08a1..eb4a8626b 100644 --- a/docs/customization/custom-scripts.md +++ b/docs/customization/custom-scripts.md @@ -140,6 +140,19 @@ obj.full_clean() obj.save() ``` +## Error handling + +Sometimes things go wrong and a script will run into an `Exception`. If that happens and an uncaught exception is raised by the custom script, the execution is aborted and a full stack trace is reported. + +Although this is helpful for debugging, in some situations it might be required to cleanly abort the execution of a custom script (e.g. because of invalid input data) and thereby make sure no changes are performed on the database. In this case the script can throw an `AbortScript` exception, which will prevent the stack trace from being reported, but still terminating the script's execution and reporting a given error message. + +```python +from utilities.exceptions import AbortScript + +if some_error: + raise AbortScript("Some meaningful error message") +``` + ## Variable Reference ### Default Options diff --git a/docs/development/adding-models.md b/docs/development/adding-models.md index aef11d666..7de897a97 100644 --- a/docs/development/adding-models.md +++ b/docs/development/adding-models.md @@ -54,15 +54,19 @@ Each model should have a corresponding FilterSet class defined. This is used to Create a table class for the model in `tables.py` by subclassing `utilities.tables.BaseTable`. Under the table's `Meta` class, be sure to list both the fields and default columns. -## 9. Create the object template +## 9. Create a SearchIndex subclass + +If this model will be included in global search results, create a subclass of `netbox.search.SearchIndex` for it and specify the fields to be indexed. + +## 10. Create the object template Create the HTML template for the object view. (The other views each typically employ a generic template.) This template should extend `generic/object.html`. -## 10. Add the model to the navigation menu +## 11. Add the model to the navigation menu Add the relevant navigation menu items in `netbox/netbox/navigation/menu.py`. -## 11. REST API components +## 12. REST API components Create the following for each model: @@ -71,13 +75,13 @@ Create the following for each model: * API view in `api/views.py` * Endpoint route in `api/urls.py` -## 12. GraphQL API components +## 13. GraphQL API components Create a Graphene object type for the model in `graphql/types.py` by subclassing the appropriate class from `netbox.graphql.types`. Also extend the schema class defined in `graphql/schema.py` with the individual object and object list fields per the established convention. -## 13. Add tests +## 14. Add tests Add tests for the following: @@ -85,7 +89,7 @@ Add tests for the following: * API views * Filter sets -## 14. Documentation +## 15. Documentation Create a new documentation page for the model in `docs/models//.md`. Include this file under the "features" documentation where appropriate. diff --git a/docs/development/application-registry.md b/docs/development/application-registry.md index c2f894711..fe2c08d56 100644 --- a/docs/development/application-registry.md +++ b/docs/development/application-registry.md @@ -8,6 +8,14 @@ The registry can be inspected by importing `registry` from `extras.registry`. ## Stores +### `data_backends` + +A dictionary mapping data backend types to their respective classes. These are used to interact with [remote data sources](../models/core/datasource.md). + +### `denormalized_fields` + +Stores registration made using `netbox.denormalized.register()`. For each model, a list of related models and their field mappings is maintained to facilitate automatic updates. + ### `model_features` A dictionary of particular features (e.g. custom fields) mapped to the NetBox models which support them, arranged by app. For example: @@ -20,38 +28,23 @@ A dictionary of particular features (e.g. custom fields) mapped to the NetBox mo ... }, 'webhooks': { - ... + 'extras': ['configcontext', 'tag', ...], + 'dcim': ['site', 'rack', 'devicetype', ...], }, ... } ``` -### `plugin_menu_items` +Supported model features are listed in the [features matrix](./models.md#features-matrix). -Navigation menu items provided by NetBox plugins. Each plugin is registered as a key with the list of menu items it provides. An example: +### `plugins` -```python -{ - 'Plugin A': ( - , , , - ), - 'Plugin B': ( - , , , - ), -} -``` +This store maintains all registered items for plugins, such as navigation menus, template extensions, etc. -### `plugin_template_extensions` +### `search` -Plugin content that gets embedded into core NetBox templates. The store comprises NetBox models registered as dictionary keys, each pointing to a list of applicable template extension classes that exist. An example: +A dictionary mapping each model (identified by its app and label) to its search index class, if one has been registered for it. -```python -{ - 'dcim.site': [ - , , , - ], - 'dcim.rack': [ - , , - ], -} -``` +### `views` + +A hierarchical mapping of registered views for each model. Mappings are added using the `register_model_view()` decorator, and URLs paths can be generated from these using `get_model_urls()`. diff --git a/docs/development/models.md b/docs/development/models.md index af11617c8..6f3998977 100644 --- a/docs/development/models.md +++ b/docs/development/models.md @@ -2,38 +2,43 @@ ## Model Types -A NetBox model represents a discrete object type such as a device or IP address. Per [Django convention](https://docs.djangoproject.com/en/stable/topics/db/models/), each model is defined as a Python class and has its own SQL table. All NetBox data models can be categorized by type. +A NetBox model represents a discrete object type such as a device or IP address. Per [Django convention](https://docs.djangoproject.com/en/stable/topics/db/models/), each model is defined as a Python class and has its own table in the PostgreSQL database. All NetBox data models can be categorized by type. -The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/contenttypes/) framework can be used to reference models within the database. A ContentType instance references a model by its `app_label` and `name`: For example, the Site model is referred to as `dcim.site`. The content type combined with an object's primary key form a globally unique identifier for the object (e.g. `dcim.site:123`). +The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/contenttypes/) framework is used to map Django models to database tables. A ContentType instance references a model by its `app_label` and `name`: For example, the Site model within the DCIM app is referred to as `dcim.site`. The content type combined with an object's primary key form a globally unique identifier for the object (e.g. `dcim.site:123`). ### Features Matrix -* [Change logging](../features/change-logging.md) - Changes to these objects are automatically recorded in the change log -* [Webhooks](../integrations/webhooks.md) - NetBox is capable of generating outgoing webhooks for these objects -* [Custom fields](../customization/custom-fields.md) - These models support the addition of user-defined fields -* [Export templates](../customization/export-templates.md) - Users can create custom export templates for these models -* [Tagging](../models/extras/tag.md) - The models can be tagged with user-defined tags -* [Journaling](../features/journaling.md) - These models support persistent historical commentary -* Nesting - These models can be nested recursively to create a hierarchy +Depending on its classification, each NetBox model may support various features which enhance its operation. Each feature is enabled by inheriting from its designated mixin class, and some features also make use of the [application registry](./application-registry.md#model_features). -| Type | Change Logging | Webhooks | Custom Fields | Export Templates | Tags | Journaling | Nesting | -| ------------------ | ---------------- | ---------------- |------------------| ---------------- | ---------------- | ---------------- | ---------------- | -| Primary | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | -| Organizational | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | | -| Nested Group | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | :material-check: | -| Component | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | | -| Component Template | :material-check: | :material-check: | | | | | | +| Feature | Feature Mixin | Registry Key | Description | +|------------------------------------------------------------|-------------------------|--------------------|--------------------------------------------------------------------------------| +| [Change logging](../features/change-logging.md) | `ChangeLoggingMixin` | - | Changes to these objects are automatically recorded in the change log | +| Cloning | `CloningMixin` | - | Provides the `clone()` method to prepare a copy | +| [Custom fields](../customization/custom-fields.md) | `CustomFieldsMixin` | `custom_fields` | These models support the addition of user-defined fields | +| [Custom links](../customization/custom-links.md) | `CustomLinksMixin` | `custom_links` | These models support the assignment of custom links | +| [Custom validation](../customization/custom-validation.md) | `CustomValidationMixin` | - | Supports the enforcement of custom validation rules | +| [Export templates](../customization/export-templates.md) | `ExportTemplatesMixin` | `export_templates` | Users can create custom export templates for these models | +| [Job results](../features/background-jobs.md) | `JobResultsMixin` | `job_results` | Users can create custom export templates for these models | +| [Journaling](../features/journaling.md) | `JournalingMixin` | `journaling` | These models support persistent historical commentary | +| [Synchronized data](../integrations/synchronized-data.md) | `SyncedDataMixin` | `synced_data` | Certain model data can be automatically synchronized from a remote data source | +| [Tagging](../models/extras/tag.md) | `TagsMixin` | `tags` | The models can be tagged with user-defined tags | +| [Webhooks](../integrations/webhooks.md) | `WebhooksMixin` | `webhooks` | NetBox is capable of generating outgoing webhooks for these objects | ## Models Index ### Primary Models +These are considered the "core" application models which are used to model network infrastructure. + * [circuits.Circuit](../models/circuits/circuit.md) * [circuits.Provider](../models/circuits/provider.md) * [circuits.ProviderNetwork](../models/circuits/providernetwork.md) +* [core.DataSource](../models/core/datasource.md) * [dcim.Cable](../models/dcim/cable.md) * [dcim.Device](../models/dcim/device.md) * [dcim.DeviceType](../models/dcim/devicetype.md) +* [dcim.Module](../models/dcim/module.md) +* [dcim.ModuleType](../models/dcim/moduletype.md) * [dcim.PowerFeed](../models/dcim/powerfeed.md) * [dcim.PowerPanel](../models/dcim/powerpanel.md) * [dcim.Rack](../models/dcim/rack.md) @@ -47,10 +52,10 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ * [ipam.IPAddress](../models/ipam/ipaddress.md) * [ipam.IPRange](../models/ipam/iprange.md) * [ipam.L2VPN](../models/ipam/l2vpn.md) -* [ipam.L2VPNTermination](../models/ipam/l2vpntermination.md) * [ipam.Prefix](../models/ipam/prefix.md) * [ipam.RouteTarget](../models/ipam/routetarget.md) * [ipam.Service](../models/ipam/service.md) +* [ipam.ServiceTemplate](../models/ipam/servicetemplate.md) * [ipam.VLAN](../models/ipam/vlan.md) * [ipam.VRF](../models/ipam/vrf.md) * [tenancy.Contact](../models/tenancy/contact.md) @@ -62,6 +67,8 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ ### Organizational Models +Organization models are used to organize and classify primary models. + * [circuits.CircuitType](../models/circuits/circuittype.md) * [dcim.DeviceRole](../models/dcim/devicerole.md) * [dcim.Manufacturer](../models/dcim/manufacturer.md) @@ -76,6 +83,8 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ ### Nested Group Models +Nested group models behave like organizational model, but self-nest within a recursive hierarchy. For example, the Region model can be used to represent a hierarchy of countries, states, and cities. + * [dcim.Location](../models/dcim/location.md) (formerly RackGroup) * [dcim.Region](../models/dcim/region.md) * [dcim.SiteGroup](../models/dcim/sitegroup.md) @@ -85,12 +94,15 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ ### Component Models +Component models represent individual physical or virtual components belonging to a device or virtual machine. + * [dcim.ConsolePort](../models/dcim/consoleport.md) * [dcim.ConsoleServerPort](../models/dcim/consoleserverport.md) * [dcim.DeviceBay](../models/dcim/devicebay.md) * [dcim.FrontPort](../models/dcim/frontport.md) * [dcim.Interface](../models/dcim/interface.md) * [dcim.InventoryItem](../models/dcim/inventoryitem.md) +* [dcim.ModuleBay](../models/dcim/modulebay.md) * [dcim.PowerOutlet](../models/dcim/poweroutlet.md) * [dcim.PowerPort](../models/dcim/powerport.md) * [dcim.RearPort](../models/dcim/rearport.md) @@ -98,11 +110,15 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ ### Component Template Models +These function as templates to effect the replication of device and virtual machine components. Component template models support a limited feature set, including change logging, custom validation, and webhooks. + * [dcim.ConsolePortTemplate](../models/dcim/consoleporttemplate.md) * [dcim.ConsoleServerPortTemplate](../models/dcim/consoleserverporttemplate.md) * [dcim.DeviceBayTemplate](../models/dcim/devicebaytemplate.md) * [dcim.FrontPortTemplate](../models/dcim/frontporttemplate.md) * [dcim.InterfaceTemplate](../models/dcim/interfacetemplate.md) +* [dcim.InventoryItemTemplate](../models/dcim/inventoryitemtemplate.md) +* [dcim.ModuleBayTemplate](../models/dcim/modulebaytemplate.md) * [dcim.PowerOutletTemplate](../models/dcim/poweroutlettemplate.md) * [dcim.PowerPortTemplate](../models/dcim/powerporttemplate.md) * [dcim.RearPortTemplate](../models/dcim/rearporttemplate.md) diff --git a/docs/features/background-jobs.md b/docs/features/background-jobs.md new file mode 100644 index 000000000..a36192ab3 --- /dev/null +++ b/docs/features/background-jobs.md @@ -0,0 +1,13 @@ +# Background Jobs + +NetBox includes the ability to execute certain functions as background tasks. These include: + +* [Report](../customization/reports.md) execution +* [Custom script](../customization/custom-scripts.md) execution +* Synchronization of [remote data sources](../integrations/synchronized-data.md) + +Additionally, NetBox plugins can enqueue their own background tasks. This is accomplished using the [JobResult model](../models/extras/jobresult.md). Background tasks are executed by the `rqworker` process(es). + +## Scheduled Jobs + +Background jobs can be configured to run immediately, or at a set time in the future. Scheduled jobs can also be configured to repeat at a set interval. diff --git a/docs/features/configuration-rendering.md b/docs/features/configuration-rendering.md new file mode 100644 index 000000000..2fe43f046 --- /dev/null +++ b/docs/features/configuration-rendering.md @@ -0,0 +1,38 @@ +# Configuration Rendering + +One of the critical aspects of operating a network is ensuring that every network node is configured correctly. By leveraging configuration templates and [context data](./context-data.md), NetBox can render complete configuration files for each device on your network. + +```mermaid +flowchart TD + ConfigContext & ConfigTemplate --> Config{{Rendered configuration}} + +click ConfigContext "../../models/extras/configcontext/" +click ConfigTemplate "../../models/extras/configtemplate/" +``` + +## Configuration Templates + +Configuration templates are written in the [Jinja2 templating language](https://jinja.palletsprojects.com/), and may be automatically populated from remote data sources. Context data is applied to a template during rendering to output a complete configuration file. Below is an example template. + +```jinja2 +{% extends 'base.j2' %} + +{% block content %} + system { + host-name {{ device.name }}; + domain-name example.com; + time-zone UTC; + authentication-order [ password radius ]; + ntp { + {% for server in ntp_servers %} + server {{ server }}; + {% endfor %} + } + } + {% for interface in device.interfaces.all() %} + {% include 'common/interface.j2' %} + {% endfor %} +{% endblock %} +``` + +When rendered for a specific NetBox device, the template's `device` variable will be populated with the device instance, and `ntp_servers` will be pulled from the device's available context data. The resulting output will be a valid configuration segment that can be applied directly to a compatible network device. diff --git a/docs/features/context-data.md b/docs/features/context-data.md index 04e795fd5..22aeccea2 100644 --- a/docs/features/context-data.md +++ b/docs/features/context-data.md @@ -11,6 +11,8 @@ Configuration context data (or "config contexts" for short) is a powerful featur } ``` +Context data can be consumed by remote API clients, or it can be employed natively to render [configuration templates](./configuration-rendering.md). + Config contexts can be computed for objects based on the following criteria: | Type | Devices | Virtual Machines | diff --git a/docs/index.md b/docs/index.md index d61465443..6a53403d6 100644 --- a/docs/index.md +++ b/docs/index.md @@ -52,4 +52,4 @@ NetBox is built on the enormously popular [Django](http://www.djangoproject.com/ * Try out our [public demo](https://demo.netbox.dev/) if you want to jump right in * The [installation guide](./installation/index.md) will help you get your own deployment up and running * Or try the community [Docker image](https://github.com/netbox-community/netbox-docker) for a low-touch approach -* [NetBox Cloud](https://www.getnetbox.io/) is a hosted solution offered by NS1 +* [NetBox Cloud](https://netboxlabs.com/netbox-cloud) is a managed solution offered by [NetBox Labs](https://netboxlabs.com/) diff --git a/docs/integrations/synchronized-data.md b/docs/integrations/synchronized-data.md new file mode 100644 index 000000000..805cbe15b --- /dev/null +++ b/docs/integrations/synchronized-data.md @@ -0,0 +1,9 @@ +# Synchronized Data + +Some NetBox models support automatic synchronization of certain attributes from remote [data sources](../models/core/datasource.md), such as a git repository hosted on GitHub or GitLab. Data from the authoritative remote source is synchronized locally in NetBox as [data files](../models/core/datafile.md). + +The following features support the use of synchronized data: + +* [Configuration templates](../features/configuration-rendering.md) +* [Configuration context data](../features/context-data.md) +* [Export templates](../customization/export-templates.md) diff --git a/docs/introduction.md b/docs/introduction.md index fe82e68aa..bfa0900cb 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -4,7 +4,7 @@ NetBox was originally developed by its lead maintainer, [Jeremy Stretch](https://github.com/jeremystretch), while he was working as a network engineer at [DigitalOcean](https://www.digitalocean.com/) in 2015 as part of an effort to automate their network provisioning. Recognizing the new tool's potential, DigitalOcean agreed to release it as an open source project in June 2016. -Since then, thousands of organizations around the world have embraced NetBox as their central network source of truth to empower both network operators and automation. +Since then, thousands of organizations around the world have embraced NetBox as their central network source of truth to empower both network operators and automation. Today, the open source project is stewarded by [NetBox Labs](https://netboxlabs.com/) and a team of volunteer maintainers. Beyond the core product, myriad [plugins](https://netbox.dev/plugins/) have been developed by the NetBox community to enhance and expand its feature set. ## Key Features @@ -17,6 +17,7 @@ NetBox was built specifically to serve the needs of network engineers and operat * AS number (ASN) management * Rack elevations with SVG rendering * Device modeling using pre-defined types +* Virtual chassis and device contexts * Network, power, and console cabling with SVG traces * Power distribution modeling * Data circuit and provider tracking @@ -29,12 +30,13 @@ NetBox was built specifically to serve the needs of network engineers and operat * Tenant ownership assignment * Device & VM configuration contexts for advanced configuration rendering * Custom fields for data model extension -* Support for custom validation rules +* Custom validation rules * Custom reports & scripts executable directly within the UI * Extensive plugin framework for adding custom functionality * Single sign-on (SSO) authentication * Robust object-based permissions * Detailed, automatic change logging +* Global search engine * NAPALM integration ## What NetBox Is Not diff --git a/docs/models/core/datafile.md b/docs/models/core/datafile.md new file mode 100644 index 000000000..3e2aa2f27 --- /dev/null +++ b/docs/models/core/datafile.md @@ -0,0 +1,25 @@ +# Data Files + +A data file object is the representation in NetBox's database of some file belonging to a remote [data source](./datasource.md). Data files are synchronized automatically, and cannot be modified locally (although they can be deleted). + +## Fields + +### Source + +The [data source](./datasource.md) to which this file belongs. + +### Path + +The path to the file, relative to its source's URL. For example, a file at `/opt/config-data/routing/bgp/peer.yaml` with a source URL of `file:///opt/config-data/` would have its path set to `routing/bgp/peer.yaml`. + +### Last Updated + +The date and time at which the file most recently updated from its source. Note that this attribute is updated only when the file's contents have been modified. Re-synchronizing the data source will not update this timestamp if the upstream file's data has not changed. + +### Size + +The file's size, in bytes. + +### Hash + +A [SHA256 hash](https://en.wikipedia.org/wiki/SHA-2) of the file's data. This can be compared to a hash taken from the original file to determine whether any changes have been made. diff --git a/docs/models/core/datasource.md b/docs/models/core/datasource.md new file mode 100644 index 000000000..d16abdd10 --- /dev/null +++ b/docs/models/core/datasource.md @@ -0,0 +1,47 @@ +# Data Sources + +A data source represents some external repository of data which NetBox can consume, such as a git repository. Files within the data source are synchronized to NetBox by saving them in the database as [data file](./datafile.md) objects. + +## Fields + +### Name + +The data source's human-friendly name. + +### Type + +The type of data source. Supported options include: + +* Local directory +* git repository + +### URL + +The URL identifying the remote source. Some examples are included below. + +| Type | Example URL | +|------|-------------| +| Local | file:///var/my/data/source/ | +| git | https://https://github.com/my-organization/my-repo | + +### Status + +The source's current synchronization status. Note that this cannot be set manually: It is updated automatically when the source is synchronized. + +### Enabled + +If false, synchronization will be disabled. + +### Ignore Rules + +A set of rules (one per line) identifying filenames to ignore during synchronization. Some examples are provided below. See Python's [`fnmatch()` documentation](https://docs.python.org/3/library/fnmatch.html) for a complete reference. + +| Rule | Description | +|----------------|------------------------------------------| +| `README` | Ignore any files named `README` | +| `*.txt` | Ignore any files with a `.txt` extension | +| `data???.json` | Ignore e.g. `data123.json` | + +### Last Synced + +The date and time at which the source was most recently synchronized successfully. diff --git a/docs/models/dcim/device.md b/docs/models/dcim/device.md index 33d07e07e..8f97b920b 100644 --- a/docs/models/dcim/device.md +++ b/docs/models/dcim/device.md @@ -72,6 +72,10 @@ The device's operational status. A device may be associated with a particular [platform](./platform.md) to indicate its operating system. Note that only platforms assigned to the associated manufacturer (or to no manufacturer) will be available for selection. +### Configuration Template + +The [configuration template](../extras/configtemplate.md) from which the configuration for this device can be rendered. If set, this will override any config template referenced by the device's role or platform. + ### Primary IPv4 & IPv6 Addresses Each device may designate one primary IPv4 address and/or one primary IPv6 address for management purposes. diff --git a/docs/models/dcim/devicerole.md b/docs/models/dcim/devicerole.md index e9bdc0fa6..786170f2b 100644 --- a/docs/models/dcim/devicerole.md +++ b/docs/models/dcim/devicerole.md @@ -19,3 +19,7 @@ The color used when displaying the role in the NetBox UI. ### VM Role If selected, this role may be assigned to [virtual machines](../virtualization/virtualmachine.md) + +### Configuration Template + +The default [configuration template](../extras/configtemplate.md) for devices assigned to this role. diff --git a/docs/models/dcim/platform.md b/docs/models/dcim/platform.md index d080f74a4..cea3efb55 100644 --- a/docs/models/dcim/platform.md +++ b/docs/models/dcim/platform.md @@ -22,6 +22,10 @@ A unique URL-friendly identifier. (This value can be used for filtering.) If designated, this platform will be available for use only to devices assigned to this [manufacturer](./manufacturer.md). This can be handy e.g. for limiting network operating systems to use on hardware produced by the relevant vendor. However, it should not be used when defining general-purpose software platforms. +### Configuration Template + +The default [configuration template](../extras/configtemplate.md) for devices assigned to this platform. + ### NAPALM Driver The [NAPALM driver](https://napalm.readthedocs.io/en/latest/support/index.html) associated with this platform. diff --git a/docs/models/extras/configcontext.md b/docs/models/extras/configcontext.md index 156b2d784..1e58b9e01 100644 --- a/docs/models/extras/configcontext.md +++ b/docs/models/extras/configcontext.md @@ -18,6 +18,10 @@ A numeric value which influences the order in which context data is merged. Cont The context data expressed in JSON format. +### Data File + +Config context data may optionally be sourced from a remote [data file](../core/datafile.md), which is synchronized from a remote data source. When designating a data file, there is no need to specify local data for the config context: It will be populated automatically from the data file. + ### Is Active If not selected, this config context will be excluded from rendering. This can be convenient to temporarily disable a config context. diff --git a/docs/models/extras/configtemplate.md b/docs/models/extras/configtemplate.md new file mode 100644 index 000000000..c3585dbdf --- /dev/null +++ b/docs/models/extras/configtemplate.md @@ -0,0 +1,29 @@ +# Configuration Templates + +Configuration templates can be used to render [devices](../dcim/device.md) configurations from [context data](../../features/context-data.md). Templates are written in the [Jinja2 language](https://jinja.palletsprojects.com/) and can be associated with devices roles, platforms, and/or individual devices. + +Context data is made available to [devices](../dcim/device.md) and/or [virtual machines](../virtualization/virtualmachine.md) based on their relationships to other objects in NetBox. For example, context data can be associated only with devices assigned to a particular site, or only to virtual machines in a certain cluster. + +See the [configuration rendering documentation](../../features/configuration-rendering.md) for more information. + +## Fields + +### Name + +A unique human-friendly name. + +### Weight + +A numeric value which influences the order in which context data is merged. Contexts with a lower weight are merged before those with a higher weight. + +### Data File + +Template code may optionally be sourced from a remote [data file](../core/datafile.md), which is synchronized from a remote data source. When designating a data file, there is no need to specify template code: It will be populated automatically from the data file. + +### Template Code + +Jinja2 template code, if being defined locally rather than replicated from a data file. + +### Environment Parameters + +A dictionary of any additional parameters to pass when instantiating the [Jinja2 environment](https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment). Jinja2 supports various optional parameters which can be used to modify its default behavior. diff --git a/docs/models/extras/exporttemplate.md b/docs/models/extras/exporttemplate.md index 3215201b3..d2f9292c6 100644 --- a/docs/models/extras/exporttemplate.md +++ b/docs/models/extras/exporttemplate.md @@ -12,6 +12,10 @@ The name of the export template. This will appear in the "export" dropdown list The type of NetBox object to which the export template applies. +### Data File + +Template code may optionally be sourced from a remote [data file](../core/datafile.md), which is synchronized from a remote data source. When designating a data file, there is no need to specify local content for the template: It will be populated automatically from the data file. + ### Template Code Jinja2 template code for rendering the exported data. diff --git a/docs/models/extras/jobresult.md b/docs/models/extras/jobresult.md new file mode 100644 index 000000000..81ab75745 --- /dev/null +++ b/docs/models/extras/jobresult.md @@ -0,0 +1,54 @@ +# Job Results + +The JobResult model is used to schedule and record the execution of [background tasks](../../features/background-jobs.md). + +## Fields + +### Name + +The name or other identifier of the NetBox object with which the job is associated. + +## Object Type + +The type of object (model) associated with this job. + +### Created + +The date and time at which the job itself was created. + +### Scheduled + +The date and time at which the job is/was scheduled to execute (if not submitted for immediate execution at the time of creation). + +### Interval + +The interval (in minutes) at which a scheduled job should re-execute. + +### Completed + +The date and time at which the job completed (if complete). + +### User + +The user who created the job. + +### Status + +The job's current status. Potential values include: + +| Value | Description | +|-------|-------------| +| Pending | Awaiting execution by an RQ worker process | +| Scheduled | Scheduled for a future date/time | +| Running | Currently executing | +| Completed | Successfully completed | +| Failed | The job did not complete successfully | +| Errored | An unexpected error was encountered during execution | + +### Data + +Any data associated with the execution of the job, such as log output. + +### Job ID + +The job's UUID, used for unique identification within a queue. diff --git a/docs/plugins/development/forms.md b/docs/plugins/development/forms.md index d819b76cb..db7adff23 100644 --- a/docs/plugins/development/forms.md +++ b/docs/plugins/development/forms.md @@ -170,6 +170,9 @@ In addition to the [form fields provided by Django](https://docs.djangoproject.c ## Choice Fields +!!! warning "Obsolete Fields" + NetBox's custom `ChoiceField` and `MultipleChoiceField` classes are no longer necessary thanks to improvements made to the user interface. Django's native form fields can be used instead. These custom field classes will be removed in NetBox v3.6. + ::: utilities.forms.ChoiceField options: members: false diff --git a/docs/plugins/development/navigation.md b/docs/plugins/development/navigation.md index 02ecdac74..3e7762184 100644 --- a/docs/plugins/development/navigation.md +++ b/docs/plugins/development/navigation.md @@ -48,7 +48,7 @@ menu_items = (item1, item2, item3) Each menu item represents a link and (optionally) a set of buttons comprising one entry in NetBox's navigation menu. Menu items are defined as PluginMenuItem instances. An example is shown below. -```python filename="navigation.py" +```python title="navigation.py" from extras.plugins import PluginMenuButton, PluginMenuItem from utilities.choices import ButtonColorChoices diff --git a/docs/reference/conditions.md b/docs/reference/conditions.md index fb8b66139..514006b01 100644 --- a/docs/reference/conditions.md +++ b/docs/reference/conditions.md @@ -97,7 +97,7 @@ Multiple conditions can be combined into nested sets using AND or OR logic. This ### Examples -`status` is "active" and `primary_ip` is defined _or_ the "exempt" tag is applied. +`status` is "active" and `primary_ip4` is defined _or_ the "exempt" tag is applied. ```json { @@ -109,8 +109,8 @@ Multiple conditions can be combined into nested sets using AND or OR logic. This "value": "active" }, { - "attr": "primary_ip", - "value": "", + "attr": "primary_ip4", + "value": null, "negate": true } ] diff --git a/docs/release-notes/version-3.4.md b/docs/release-notes/version-3.4.md index 4fedddab2..485b85719 100644 --- a/docs/release-notes/version-3.4.md +++ b/docs/release-notes/version-3.4.md @@ -1,19 +1,51 @@ # NetBox v3.4 -## v3.4.4 (FUTURE) +## v3.4.5 (FUTURE) + +### Enhancements + +* [#11110](https://github.com/netbox-community/netbox/issues/11110) - Add `start_address` and `end_address` filters for IP ranges +* [#11592](https://github.com/netbox-community/netbox/issues/11592) - Introduce `FILE_UPLOAD_MAX_MEMORY_SIZE` configuration parameter +* [#11685](https://github.com/netbox-community/netbox/issues/11685) - Match on containing prefixes and aggregates when querying for IP addresses using global search + +### Bug Fixes + +* [#11032](https://github.com/netbox-community/netbox/issues/11032) - Fix false custom validation errors during component creation +* [#11226](https://github.com/netbox-community/netbox/issues/11226) - Ensure scripts and reports within submodules are automatically reloaded +* [#11335](https://github.com/netbox-community/netbox/issues/11335) - Avoid exception when rendering change log after uninstalling a plugin +* [#11459](https://github.com/netbox-community/netbox/issues/11459) - Enable evaluating null values in custom validation rules +* [#11473](https://github.com/netbox-community/netbox/issues/11473) - GraphQL requests specifying an invalid filter should return an empty queryset +* [#11582](https://github.com/netbox-community/netbox/issues/11582) - Ensure form validation errors are displayed when adding virtual chassis members +* [#11601](https://github.com/netbox-community/netbox/issues/11601) - Fix partial matching of start/end addresses for IP range search +* [#11683](https://github.com/netbox-community/netbox/issues/11683) - Fix CSV header attribute detection when auto-detecting import format +* [#11711](https://github.com/netbox-community/netbox/issues/11711) - Fix CSV import for multiple-object custom fields +* [#11723](https://github.com/netbox-community/netbox/issues/11723) - Circuit terminations should link to their associated circuits (rather than site or provider network) +* [#11786](https://github.com/netbox-community/netbox/issues/11786) - List only applicable object types in form widget when filtering custom fields + +--- + +## v3.4.4 (2023-02-02) ### Enhancements * [#10762](https://github.com/netbox-community/netbox/issues/10762) - Permit selection custom fields to have only one choice +* [#11152](https://github.com/netbox-community/netbox/issues/11152) - Introduce AbortScript exception to elegantly abort scripts +* [#11554](https://github.com/netbox-community/netbox/issues/11554) - Add module types count to manufacturers list * [#11585](https://github.com/netbox-community/netbox/issues/11585) - Add IP address filters for services +* [#11598](https://github.com/netbox-community/netbox/issues/11598) - Add buttons to easily switch between rack list and elevations views ### Bug Fixes +* [#11267](https://github.com/netbox-community/netbox/issues/11267) - Avoid catching ImportErrors when loading plugin resources * [#11487](https://github.com/netbox-community/netbox/issues/11487) - Remove "set null" option from non-writable custom fields during bulk edit * [#11491](https://github.com/netbox-community/netbox/issues/11491) - Show edit/delete buttons in user tokens table * [#11528](https://github.com/netbox-community/netbox/issues/11528) - Permit import of devices using uploaded file * [#11555](https://github.com/netbox-community/netbox/issues/11555) - Avoid inadvertent interpretation of search query as regular expression under global search (previously [#11516](https://github.com/netbox-community/netbox/issues/11516)) * [#11562](https://github.com/netbox-community/netbox/issues/11562) - Correct ordering of virtual chassis interfaces with duplicate names +* [#11574](https://github.com/netbox-community/netbox/issues/11574) - Fix exception when attempting to schedule reports/scripts +* [#11620](https://github.com/netbox-community/netbox/issues/11620) - Correct available filter choices for interface PoE type +* [#11635](https://github.com/netbox-community/netbox/issues/11635) - Pre-populate assigned VRF when following "first available IP" link from prefix view +* [#11650](https://github.com/netbox-community/netbox/issues/11650) - Display error message when attempting to create device component with duplicate name --- diff --git a/docs/release-notes/version-3.5.md b/docs/release-notes/version-3.5.md index 6d0ab1834..6ca403ed9 100644 --- a/docs/release-notes/version-3.5.md +++ b/docs/release-notes/version-3.5.md @@ -4,13 +4,17 @@ ### Enhancements -* [#11517](https://github.com/netbox-community/netbox/issues/11517) - Standardize the inclusion of related objects across the entire UI -* [#11584](https://github.com/netbox-community/netbox/issues/11584) - Add a list view for contact assignments +* [#9073](https://github.com/netbox-community/netbox/issues/9073) - Enable syncing config context data from remote sources * [#11254](https://github.com/netbox-community/netbox/issues/11254) - Introduce the `X-Request-ID` HTTP header to annotate the unique ID of each request for change logging * [#11440](https://github.com/netbox-community/netbox/issues/11440) - Add an `enabled` field for device type interfaces +* [#11517](https://github.com/netbox-community/netbox/issues/11517) - Standardize the inclusion of related objects across the entire UI +* [#11584](https://github.com/netbox-community/netbox/issues/11584) - Add a list view for contact assignments +* [#11625](https://github.com/netbox-community/netbox/issues/11625) - Add HTMX support to ObjectEditView +* [#11693](https://github.com/netbox-community/netbox/issues/11693) - Enable syncing export template content from remote sources ### Other Changes * [#10604](https://github.com/netbox-community/netbox/issues/10604) - Remove unused `extra_tabs` block from `object.html` generic template * [#10923](https://github.com/netbox-community/netbox/issues/10923) - Remove unused `NetBoxModelCSVForm` class (replaced by `NetBoxModelImportForm`) * [#11611](https://github.com/netbox-community/netbox/issues/11611) - Refactor API viewset classes and introduce NetBoxReadOnlyModelViewSet +* diff --git a/mkdocs.yml b/mkdocs.yml index ff9174455..2487176d3 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -74,8 +74,10 @@ nav: - Contacts: 'features/contacts.md' - Search: 'features/search.md' - Context Data: 'features/context-data.md' + - Configuration Rendering: 'features/configuration-rendering.md' - Change Logging: 'features/change-logging.md' - Journaling: 'features/journaling.md' + - Background Jobs: 'features/background-jobs.md' - Auth & Permissions: 'features/authentication-permissions.md' - API & Integration: 'features/api-integration.md' - Customization: 'features/customization.md' @@ -116,6 +118,7 @@ nav: - REST API: 'integrations/rest-api.md' - GraphQL API: 'integrations/graphql-api.md' - Webhooks: 'integrations/webhooks.md' + - Synchronized Data: 'integrations/synchronized-data.md' - NAPALM: 'integrations/napalm.md' - Prometheus Metrics: 'integrations/prometheus-metrics.md' - Plugins: @@ -152,6 +155,9 @@ nav: - Circuit Type: 'models/circuits/circuittype.md' - Provider: 'models/circuits/provider.md' - Provider Network: 'models/circuits/providernetwork.md' + - Core: + - DataFile: 'models/core/datafile.md' + - DataSource: 'models/core/datasource.md' - DCIM: - Cable: 'models/dcim/cable.md' - ConsolePort: 'models/dcim/consoleport.md' @@ -196,10 +202,12 @@ nav: - Extras: - Branch: 'models/extras/branch.md' - ConfigContext: 'models/extras/configcontext.md' + - ConfigTemplate: 'models/extras/configtemplate.md' - CustomField: 'models/extras/customfield.md' - CustomLink: 'models/extras/customlink.md' - ExportTemplate: 'models/extras/exporttemplate.md' - ImageAttachment: 'models/extras/imageattachment.md' + - JobResult: 'models/extras/jobresult.md' - JournalEntry: 'models/extras/journalentry.md' - SavedFilter: 'models/extras/savedfilter.md' - StagedChange: 'models/extras/stagedchange.md' diff --git a/netbox/circuits/forms/bulk_edit.py b/netbox/circuits/forms/bulk_edit.py index e1fe6338d..a3e91c8ae 100644 --- a/netbox/circuits/forms/bulk_edit.py +++ b/netbox/circuits/forms/bulk_edit.py @@ -7,8 +7,7 @@ from ipam.models import ASN from netbox.forms import NetBoxModelBulkEditForm from tenancy.models import Tenant from utilities.forms import ( - add_blank_choice, CommentField, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField, SmallTextarea, - StaticSelect, + add_blank_choice, CommentField, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField, ) __all__ = ( @@ -35,7 +34,7 @@ class ProviderBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label=_('Comments') ) @@ -63,7 +62,7 @@ class ProviderNetworkBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label=_('Comments') ) @@ -101,8 +100,7 @@ class CircuitBulkEditForm(NetBoxModelBulkEditForm): status = forms.ChoiceField( choices=add_blank_choice(CircuitStatusChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) tenant = DynamicModelChoiceField( queryset=Tenant.objects.all(), @@ -125,7 +123,7 @@ class CircuitBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label=_('Comments') ) diff --git a/netbox/circuits/forms/filtersets.py b/netbox/circuits/forms/filtersets.py index d7cfc494d..05dacfd38 100644 --- a/netbox/circuits/forms/filtersets.py +++ b/netbox/circuits/forms/filtersets.py @@ -7,7 +7,7 @@ from dcim.models import Region, Site, SiteGroup from ipam.models import ASN from netbox.forms import NetBoxModelFilterSetForm from tenancy.forms import TenancyFilterForm, ContactModelFilterForm -from utilities.forms import DatePicker, DynamicModelMultipleChoiceField, MultipleChoiceField, TagFilterField +from utilities.forms import DatePicker, DynamicModelMultipleChoiceField, TagFilterField __all__ = ( 'CircuitFilterForm', @@ -107,7 +107,7 @@ class CircuitFilterForm(TenancyFilterForm, ContactModelFilterForm, NetBoxModelFi }, label=_('Provider network') ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=CircuitStatusChoices, required=False ) diff --git a/netbox/circuits/forms/model_forms.py b/netbox/circuits/forms/model_forms.py index cd73780fa..be0d39835 100644 --- a/netbox/circuits/forms/model_forms.py +++ b/netbox/circuits/forms/model_forms.py @@ -7,7 +7,6 @@ from netbox.forms import NetBoxModelForm from tenancy.forms import TenancyForm from utilities.forms import ( CommentField, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField, SelectSpeedWidget, SlugField, - StaticSelect, ) __all__ = ( @@ -102,7 +101,6 @@ class CircuitForm(TenancyForm, NetBoxModelForm): 'commit_rate': _("Committed rate"), } widgets = { - 'status': StaticSelect(), 'install_date': DatePicker(), 'termination_date': DatePicker(), 'commit_rate': SelectSpeedWidget(), @@ -174,7 +172,6 @@ class CircuitTerminationForm(NetBoxModelForm): 'pp_info': _("Patch panel ID and port number(s)") } widgets = { - 'term_side': StaticSelect(), 'port_speed': SelectSpeedWidget(), 'upstream_speed': SelectSpeedWidget(), } diff --git a/netbox/circuits/models/circuits.py b/netbox/circuits/models/circuits.py index 8ef5761fd..a04d78d9f 100644 --- a/netbox/circuits/models/circuits.py +++ b/netbox/circuits/models/circuits.py @@ -1,4 +1,3 @@ -from django.apps import apps from django.contrib.contenttypes.fields import GenericRelation from django.core.exceptions import ValidationError from django.db import models @@ -10,7 +9,6 @@ from dcim.models import CabledObjectModel from netbox.models import ( ChangeLoggedModel, CustomFieldsMixin, CustomLinksMixin, OrganizationalModel, PrimaryModel, TagsMixin, ) -from netbox.models.features import WebhooksMixin __all__ = ( 'Circuit', @@ -132,7 +130,6 @@ class CircuitTermination( CustomFieldsMixin, CustomLinksMixin, TagsMixin, - WebhooksMixin, ChangeLoggedModel, CabledObjectModel ): @@ -196,12 +193,10 @@ class CircuitTermination( ) def __str__(self): - return f'Termination {self.term_side}: {self.site or self.provider_network}' + return f'{self.circuit}: Termination {self.term_side}' def get_absolute_url(self): - if self.site: - return self.site.get_absolute_url() - return self.provider_network.get_absolute_url() + return self.circuit.get_absolute_url() def clean(self): super().clean() diff --git a/netbox/core/__init__.py b/netbox/core/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/core/api/__init__.py b/netbox/core/api/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/core/api/nested_serializers.py b/netbox/core/api/nested_serializers.py new file mode 100644 index 000000000..0a8351fec --- /dev/null +++ b/netbox/core/api/nested_serializers.py @@ -0,0 +1,25 @@ +from rest_framework import serializers + +from core.models import * +from netbox.api.serializers import WritableNestedSerializer + +__all__ = [ + 'NestedDataFileSerializer', + 'NestedDataSourceSerializer', +] + + +class NestedDataSourceSerializer(WritableNestedSerializer): + url = serializers.HyperlinkedIdentityField(view_name='core-api:datasource-detail') + + class Meta: + model = DataSource + fields = ['id', 'url', 'display', 'name'] + + +class NestedDataFileSerializer(WritableNestedSerializer): + url = serializers.HyperlinkedIdentityField(view_name='core-api:datafile-detail') + + class Meta: + model = DataFile + fields = ['id', 'url', 'display', 'path'] diff --git a/netbox/core/api/serializers.py b/netbox/core/api/serializers.py new file mode 100644 index 000000000..4c29fd69e --- /dev/null +++ b/netbox/core/api/serializers.py @@ -0,0 +1,51 @@ +from rest_framework import serializers + +from core.choices import * +from core.models import * +from netbox.api.fields import ChoiceField +from netbox.api.serializers import NetBoxModelSerializer +from .nested_serializers import * + +__all__ = ( + 'DataSourceSerializer', +) + + +class DataSourceSerializer(NetBoxModelSerializer): + url = serializers.HyperlinkedIdentityField( + view_name='core-api:datasource-detail' + ) + type = ChoiceField( + choices=DataSourceTypeChoices + ) + status = ChoiceField( + choices=DataSourceStatusChoices, + read_only=True + ) + + # Related object counts + file_count = serializers.IntegerField( + read_only=True + ) + + class Meta: + model = DataSource + fields = [ + 'id', 'url', 'display', 'name', 'type', 'source_url', 'enabled', 'status', 'description', 'comments', + 'parameters', 'ignore_rules', 'created', 'last_updated', 'file_count', + ] + + +class DataFileSerializer(NetBoxModelSerializer): + url = serializers.HyperlinkedIdentityField( + view_name='core-api:datafile-detail' + ) + source = NestedDataSourceSerializer( + read_only=True + ) + + class Meta: + model = DataFile + fields = [ + 'id', 'url', 'display', 'source', 'path', 'last_updated', 'size', 'hash', + ] diff --git a/netbox/core/api/urls.py b/netbox/core/api/urls.py new file mode 100644 index 000000000..364e5db55 --- /dev/null +++ b/netbox/core/api/urls.py @@ -0,0 +1,13 @@ +from netbox.api.routers import NetBoxRouter +from . import views + + +router = NetBoxRouter() +router.APIRootView = views.CoreRootView + +# Data sources +router.register('data-sources', views.DataSourceViewSet) +router.register('data-files', views.DataFileViewSet) + +app_name = 'core-api' +urlpatterns = router.urls diff --git a/netbox/core/api/views.py b/netbox/core/api/views.py new file mode 100644 index 000000000..b2d8c0ed4 --- /dev/null +++ b/netbox/core/api/views.py @@ -0,0 +1,52 @@ +from django.shortcuts import get_object_or_404 + +from rest_framework.decorators import action +from rest_framework.exceptions import PermissionDenied +from rest_framework.response import Response +from rest_framework.routers import APIRootView + +from core import filtersets +from core.models import * +from netbox.api.viewsets import NetBoxModelViewSet, NetBoxReadOnlyModelViewSet +from utilities.utils import count_related +from . import serializers + + +class CoreRootView(APIRootView): + """ + Core API root view + """ + def get_view_name(self): + return 'Core' + + +# +# Data sources +# + +class DataSourceViewSet(NetBoxModelViewSet): + queryset = DataSource.objects.annotate( + file_count=count_related(DataFile, 'source') + ) + serializer_class = serializers.DataSourceSerializer + filterset_class = filtersets.DataSourceFilterSet + + @action(detail=True, methods=['post']) + def sync(self, request, pk): + """ + Enqueue a job to synchronize the DataSource. + """ + if not request.user.has_perm('extras.sync_datasource'): + raise PermissionDenied("Syncing data sources requires the core.sync_datasource permission.") + + datasource = get_object_or_404(DataSource, pk=pk) + datasource.enqueue_sync_job(request) + serializer = serializers.DataSourceSerializer(datasource, context={'request': request}) + + return Response(serializer.data) + + +class DataFileViewSet(NetBoxReadOnlyModelViewSet): + queryset = DataFile.objects.defer('data').prefetch_related('source') + serializer_class = serializers.DataFileSerializer + filterset_class = filtersets.DataFileFilterSet diff --git a/netbox/core/apps.py b/netbox/core/apps.py new file mode 100644 index 000000000..c4886eb41 --- /dev/null +++ b/netbox/core/apps.py @@ -0,0 +1,8 @@ +from django.apps import AppConfig + + +class CoreConfig(AppConfig): + name = "core" + + def ready(self): + from . import data_backends, search diff --git a/netbox/core/choices.py b/netbox/core/choices.py new file mode 100644 index 000000000..6927c83fb --- /dev/null +++ b/netbox/core/choices.py @@ -0,0 +1,34 @@ +from django.utils.translation import gettext as _ + +from utilities.choices import ChoiceSet + + +# +# Data sources +# + +class DataSourceTypeChoices(ChoiceSet): + LOCAL = 'local' + GIT = 'git' + + CHOICES = ( + (LOCAL, _('Local'), 'gray'), + (GIT, _('Git'), 'blue'), + ) + + +class DataSourceStatusChoices(ChoiceSet): + + NEW = 'new' + QUEUED = 'queued' + SYNCING = 'syncing' + COMPLETED = 'completed' + FAILED = 'failed' + + CHOICES = ( + (NEW, _('New'), 'blue'), + (QUEUED, _('Queued'), 'orange'), + (SYNCING, _('Syncing'), 'cyan'), + (COMPLETED, _('Completed'), 'green'), + (FAILED, _('Failed'), 'red'), + ) diff --git a/netbox/core/data_backends.py b/netbox/core/data_backends.py new file mode 100644 index 000000000..5d0e80584 --- /dev/null +++ b/netbox/core/data_backends.py @@ -0,0 +1,117 @@ +import logging +import subprocess +import tempfile +from contextlib import contextmanager +from urllib.parse import quote, urlunparse, urlparse + +from django import forms +from django.conf import settings +from django.utils.translation import gettext as _ + +from netbox.registry import registry +from .choices import DataSourceTypeChoices +from .exceptions import SyncError + +__all__ = ( + 'LocalBackend', + 'GitBackend', +) + +logger = logging.getLogger('netbox.data_backends') + + +def register_backend(name): + """ + Decorator for registering a DataBackend class. + """ + def _wrapper(cls): + registry['data_backends'][name] = cls + return cls + + return _wrapper + + +class DataBackend: + parameters = {} + + def __init__(self, url, **kwargs): + self.url = url + self.params = kwargs + + @property + def url_scheme(self): + return urlparse(self.url).scheme.lower() + + @contextmanager + def fetch(self): + raise NotImplemented() + + +@register_backend(DataSourceTypeChoices.LOCAL) +class LocalBackend(DataBackend): + + @contextmanager + def fetch(self): + logger.debug(f"Data source type is local; skipping fetch") + local_path = urlparse(self.url).path # Strip file:// scheme + + yield local_path + + +@register_backend(DataSourceTypeChoices.GIT) +class GitBackend(DataBackend): + parameters = { + 'username': forms.CharField( + required=False, + label=_('Username'), + widget=forms.TextInput(attrs={'class': 'form-control'}) + ), + 'password': forms.CharField( + required=False, + label=_('Password'), + widget=forms.TextInput(attrs={'class': 'form-control'}) + ), + 'branch': forms.CharField( + required=False, + label=_('Branch'), + widget=forms.TextInput(attrs={'class': 'form-control'}) + ) + } + + @contextmanager + def fetch(self): + local_path = tempfile.TemporaryDirectory() + + # Add authentication credentials to URL (if specified) + username = self.params.get('username') + password = self.params.get('password') + if username and password: + url_components = list(urlparse(self.url)) + # Prepend username & password to netloc + url_components[1] = quote(f'{username}@{password}:') + url_components[1] + url = urlunparse(url_components) + else: + url = self.url + + # Compile git arguments + args = ['git', 'clone', '--depth', '1'] + if branch := self.params.get('branch'): + args.extend(['--branch', branch]) + args.extend([url, local_path.name]) + + # Prep environment variables + env_vars = {} + if settings.HTTP_PROXIES and self.url_scheme in ('http', 'https'): + env_vars['http_proxy'] = settings.HTTP_PROXIES.get(self.url_scheme) + + logger.debug(f"Cloning git repo: {' '.join(args)}") + try: + subprocess.run(args, check=True, capture_output=True, env=env_vars) + except subprocess.CalledProcessError as e: + raise SyncError( + f"Fetching remote data failed: {e.stderr}" + ) + + yield local_path.name + + local_path.cleanup() diff --git a/netbox/core/exceptions.py b/netbox/core/exceptions.py new file mode 100644 index 000000000..8412b0378 --- /dev/null +++ b/netbox/core/exceptions.py @@ -0,0 +1,2 @@ +class SyncError(Exception): + pass diff --git a/netbox/core/filtersets.py b/netbox/core/filtersets.py new file mode 100644 index 000000000..3bff34158 --- /dev/null +++ b/netbox/core/filtersets.py @@ -0,0 +1,64 @@ +from django.db.models import Q +from django.utils.translation import gettext as _ + +import django_filters + +from netbox.filtersets import ChangeLoggedModelFilterSet, NetBoxModelFilterSet +from .choices import * +from .models import * + +__all__ = ( + 'DataFileFilterSet', + 'DataSourceFilterSet', +) + + +class DataSourceFilterSet(NetBoxModelFilterSet): + type = django_filters.MultipleChoiceFilter( + choices=DataSourceTypeChoices, + null_value=None + ) + status = django_filters.MultipleChoiceFilter( + choices=DataSourceStatusChoices, + null_value=None + ) + + class Meta: + model = DataSource + fields = ('id', 'name', 'enabled') + + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(name__icontains=value) | + Q(description__icontains=value) | + Q(comments__icontains=value) + ) + + +class DataFileFilterSet(ChangeLoggedModelFilterSet): + q = django_filters.CharFilter( + method='search' + ) + source_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data source (ID)'), + ) + source = django_filters.ModelMultipleChoiceFilter( + field_name='source__name', + queryset=DataSource.objects.all(), + to_field_name='name', + label=_('Data source (name)'), + ) + + class Meta: + model = DataFile + fields = ('id', 'path', 'last_updated', 'size', 'hash') + + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(path__icontains=value) + ) diff --git a/netbox/core/forms/__init__.py b/netbox/core/forms/__init__.py new file mode 100644 index 000000000..1499f98b2 --- /dev/null +++ b/netbox/core/forms/__init__.py @@ -0,0 +1,4 @@ +from .bulk_edit import * +from .bulk_import import * +from .filtersets import * +from .model_forms import * diff --git a/netbox/core/forms/bulk_edit.py b/netbox/core/forms/bulk_edit.py new file mode 100644 index 000000000..f613785c5 --- /dev/null +++ b/netbox/core/forms/bulk_edit.py @@ -0,0 +1,47 @@ +from django import forms +from django.utils.translation import gettext as _ + +from core.choices import DataSourceTypeChoices +from core.models import * +from netbox.forms import NetBoxModelBulkEditForm +from utilities.forms import add_blank_choice, BulkEditNullBooleanSelect, CommentField + +__all__ = ( + 'DataSourceBulkEditForm', +) + + +class DataSourceBulkEditForm(NetBoxModelBulkEditForm): + type = forms.ChoiceField( + choices=add_blank_choice(DataSourceTypeChoices), + required=False, + initial='' + ) + enabled = forms.NullBooleanField( + required=False, + widget=BulkEditNullBooleanSelect(), + label=_('Enforce unique space') + ) + description = forms.CharField( + max_length=200, + required=False + ) + comments = CommentField( + widget=forms.Textarea, + label=_('Comments') + ) + parameters = forms.JSONField( + required=False + ) + ignore_rules = forms.CharField( + required=False, + widget=forms.Textarea() + ) + + model = DataSource + fieldsets = ( + (None, ('type', 'enabled', 'description', 'comments', 'parameters', 'ignore_rules')), + ) + nullable_fields = ( + 'description', 'description', 'parameters', 'comments', 'parameters', 'ignore_rules', + ) diff --git a/netbox/core/forms/bulk_import.py b/netbox/core/forms/bulk_import.py new file mode 100644 index 000000000..78a859dcb --- /dev/null +++ b/netbox/core/forms/bulk_import.py @@ -0,0 +1,15 @@ +from core.models import * +from netbox.forms import NetBoxModelImportForm + +__all__ = ( + 'DataSourceImportForm', +) + + +class DataSourceImportForm(NetBoxModelImportForm): + + class Meta: + model = DataSource + fields = ( + 'name', 'type', 'source_url', 'enabled', 'description', 'comments', 'parameters', 'ignore_rules', + ) diff --git a/netbox/core/forms/filtersets.py b/netbox/core/forms/filtersets.py new file mode 100644 index 000000000..a54941537 --- /dev/null +++ b/netbox/core/forms/filtersets.py @@ -0,0 +1,47 @@ +from django import forms +from django.utils.translation import gettext as _ + +from core.choices import * +from core.models import * +from netbox.forms import NetBoxModelFilterSetForm +from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, DynamicModelMultipleChoiceField + +__all__ = ( + 'DataFileFilterForm', + 'DataSourceFilterForm', +) + + +class DataSourceFilterForm(NetBoxModelFilterSetForm): + model = DataSource + fieldsets = ( + (None, ('q', 'filter_id')), + ('Data Source', ('type', 'status')), + ) + type = forms.MultipleChoiceField( + choices=DataSourceTypeChoices, + required=False + ) + status = forms.MultipleChoiceField( + choices=DataSourceStatusChoices, + required=False + ) + enabled = forms.NullBooleanField( + required=False, + widget=forms.Select( + choices=BOOLEAN_WITH_BLANK_CHOICES + ) + ) + + +class DataFileFilterForm(NetBoxModelFilterSetForm): + model = DataFile + fieldsets = ( + (None, ('q', 'filter_id')), + ('File', ('source_id',)), + ) + source_id = DynamicModelMultipleChoiceField( + queryset=DataSource.objects.all(), + required=False, + label=_('Data source') + ) diff --git a/netbox/core/forms/model_forms.py b/netbox/core/forms/model_forms.py new file mode 100644 index 000000000..464c3eb47 --- /dev/null +++ b/netbox/core/forms/model_forms.py @@ -0,0 +1,76 @@ +import copy + +from django import forms + +from core.models import * +from netbox.forms import NetBoxModelForm +from netbox.registry import registry +from utilities.forms import CommentField, get_field_value + +__all__ = ( + 'DataSourceForm', +) + + +class DataSourceForm(NetBoxModelForm): + comments = CommentField() + + class Meta: + model = DataSource + fields = [ + 'name', 'type', 'source_url', 'enabled', 'description', 'comments', 'ignore_rules', 'tags', + ] + widgets = { + 'type': forms.Select( + attrs={ + 'hx-get': '.', + 'hx-include': '#form_fields input', + 'hx-target': '#form_fields', + } + ), + 'ignore_rules': forms.Textarea( + attrs={ + 'rows': 5, + 'class': 'font-monospace', + 'placeholder': '.cache\n*.txt' + } + ), + } + + @property + def fieldsets(self): + fieldsets = [ + ('Source', ('name', 'type', 'source_url', 'enabled', 'description', 'tags', 'ignore_rules')), + ] + if self.backend_fields: + fieldsets.append( + ('Backend Parameters', self.backend_fields) + ) + + return fieldsets + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Determine the selected backend type + backend_type = get_field_value(self, 'type') + backend = registry['data_backends'].get(backend_type) + + # Add backend-specific form fields + self.backend_fields = [] + for name, form_field in backend.parameters.items(): + field_name = f'backend_{name}' + self.backend_fields.append(field_name) + self.fields[field_name] = copy.copy(form_field) + if self.instance and self.instance.parameters: + self.fields[field_name].initial = self.instance.parameters.get(name) + + def save(self, *args, **kwargs): + + parameters = {} + for name in self.fields: + if name.startswith('backend_'): + parameters[name[8:]] = self.cleaned_data[name] + self.instance.parameters = parameters + + return super().save(*args, **kwargs) diff --git a/netbox/core/graphql/__init__.py b/netbox/core/graphql/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/core/graphql/schema.py b/netbox/core/graphql/schema.py new file mode 100644 index 000000000..201965430 --- /dev/null +++ b/netbox/core/graphql/schema.py @@ -0,0 +1,12 @@ +import graphene + +from netbox.graphql.fields import ObjectField, ObjectListField +from .types import * + + +class CoreQuery(graphene.ObjectType): + data_file = ObjectField(DataFileType) + data_file_list = ObjectListField(DataFileType) + + data_source = ObjectField(DataSourceType) + data_source_list = ObjectListField(DataSourceType) diff --git a/netbox/core/graphql/types.py b/netbox/core/graphql/types.py new file mode 100644 index 000000000..402e36345 --- /dev/null +++ b/netbox/core/graphql/types.py @@ -0,0 +1,21 @@ +from core import filtersets, models +from netbox.graphql.types import BaseObjectType, NetBoxObjectType + +__all__ = ( + 'DataFileType', + 'DataSourceType', +) + + +class DataFileType(BaseObjectType): + class Meta: + model = models.DataFile + exclude = ('data',) + filterset_class = filtersets.DataFileFilterSet + + +class DataSourceType(NetBoxObjectType): + class Meta: + model = models.DataSource + fields = '__all__' + filterset_class = filtersets.DataSourceFilterSet diff --git a/netbox/core/jobs.py b/netbox/core/jobs.py new file mode 100644 index 000000000..ee285fa7c --- /dev/null +++ b/netbox/core/jobs.py @@ -0,0 +1,29 @@ +import logging + +from extras.choices import JobResultStatusChoices +from netbox.search.backends import search_backend +from .choices import * +from .exceptions import SyncError +from .models import DataSource + +logger = logging.getLogger(__name__) + + +def sync_datasource(job_result, *args, **kwargs): + """ + Call sync() on a DataSource. + """ + datasource = DataSource.objects.get(name=job_result.name) + + try: + job_result.start() + datasource.sync() + + # Update the search cache for DataFiles belonging to this source + search_backend.cache(datasource.datafiles.iterator()) + + except SyncError as e: + job_result.set_status(JobResultStatusChoices.STATUS_ERRORED) + job_result.save() + DataSource.objects.filter(pk=datasource.pk).update(status=DataSourceStatusChoices.FAILED) + logging.error(e) diff --git a/netbox/core/management/__init__.py b/netbox/core/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/core/management/commands/__init__.py b/netbox/core/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/core/management/commands/syncdatasource.py b/netbox/core/management/commands/syncdatasource.py new file mode 100644 index 000000000..3d73f70ab --- /dev/null +++ b/netbox/core/management/commands/syncdatasource.py @@ -0,0 +1,41 @@ +from django.core.management.base import BaseCommand, CommandError + +from core.models import DataSource + + +class Command(BaseCommand): + help = "Synchronize a data source from its remote upstream" + + def add_arguments(self, parser): + parser.add_argument('name', nargs='*', help="Data source(s) to synchronize") + parser.add_argument( + "--all", action='store_true', dest='sync_all', + help="Synchronize all data sources" + ) + + def handle(self, *args, **options): + + # Find DataSources to sync + if options['sync_all']: + datasources = DataSource.objects.all() + elif options['name']: + datasources = DataSource.objects.filter(name__in=options['name']) + # Check for invalid names + found_names = {ds['name'] for ds in datasources.values('name')} + if invalid_names := set(options['name']) - found_names: + raise CommandError(f"Invalid data source names: {', '.join(invalid_names)}") + else: + raise CommandError(f"Must specify at least one data source, or set --all.") + + if len(options['name']) > 1: + self.stdout.write(f"Syncing {len(datasources)} data sources.") + + for i, datasource in enumerate(datasources, start=1): + self.stdout.write(f"[{i}] Syncing {datasource}... ", ending='') + self.stdout.flush() + datasource.sync() + self.stdout.write(datasource.get_status_display()) + self.stdout.flush() + + if len(options['name']) > 1: + self.stdout.write(f"Finished.") diff --git a/netbox/core/migrations/0001_initial.py b/netbox/core/migrations/0001_initial.py new file mode 100644 index 000000000..0678d4a67 --- /dev/null +++ b/netbox/core/migrations/0001_initial.py @@ -0,0 +1,66 @@ +# Generated by Django 4.1.5 on 2023-02-02 02:37 + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion +import taggit.managers +import utilities.json + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('extras', '0084_staging'), + ] + + operations = [ + migrations.CreateModel( + name='DataSource', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), + ('created', models.DateTimeField(auto_now_add=True, null=True)), + ('last_updated', models.DateTimeField(auto_now=True, null=True)), + ('custom_field_data', models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder)), + ('description', models.CharField(blank=True, max_length=200)), + ('comments', models.TextField(blank=True)), + ('name', models.CharField(max_length=100, unique=True)), + ('type', models.CharField(default='local', max_length=50)), + ('source_url', models.CharField(max_length=200)), + ('status', models.CharField(default='new', editable=False, max_length=50)), + ('enabled', models.BooleanField(default=True)), + ('ignore_rules', models.TextField(blank=True)), + ('parameters', models.JSONField(blank=True, null=True)), + ('last_synced', models.DateTimeField(blank=True, editable=False, null=True)), + ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), + ], + options={ + 'ordering': ('name',), + }, + ), + migrations.CreateModel( + name='DataFile', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), + ('created', models.DateTimeField(auto_now_add=True)), + ('last_updated', models.DateTimeField(editable=False)), + ('path', models.CharField(editable=False, max_length=1000)), + ('size', models.PositiveIntegerField(editable=False)), + ('hash', models.CharField(editable=False, max_length=64, validators=[django.core.validators.RegexValidator(message='Length must be 64 hexadecimal characters.', regex='^[0-9a-f]{64}$')])), + ('data', models.BinaryField()), + ('source', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='datafiles', to='core.datasource')), + ], + options={ + 'ordering': ('source', 'path'), + }, + ), + migrations.AddConstraint( + model_name='datafile', + constraint=models.UniqueConstraint(fields=('source', 'path'), name='core_datafile_unique_source_path'), + ), + migrations.AddIndex( + model_name='datafile', + index=models.Index(fields=['source', 'path'], name='core_datafile_source_path'), + ), + ] diff --git a/netbox/core/migrations/__init__.py b/netbox/core/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/core/models/__init__.py b/netbox/core/models/__init__.py new file mode 100644 index 000000000..df22d8bbb --- /dev/null +++ b/netbox/core/models/__init__.py @@ -0,0 +1 @@ +from .data import * diff --git a/netbox/core/models/data.py b/netbox/core/models/data.py new file mode 100644 index 000000000..eaeef5f88 --- /dev/null +++ b/netbox/core/models/data.py @@ -0,0 +1,316 @@ +import logging +import os +import yaml +from fnmatch import fnmatchcase +from urllib.parse import urlparse + +from django.conf import settings +from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import ValidationError +from django.core.validators import RegexValidator +from django.db import models +from django.urls import reverse +from django.utils import timezone +from django.utils.module_loading import import_string +from django.utils.translation import gettext as _ + +from extras.models import JobResult +from netbox.models import PrimaryModel +from netbox.models.features import ChangeLoggingMixin +from netbox.registry import registry +from utilities.files import sha256_hash +from utilities.querysets import RestrictedQuerySet +from ..choices import * +from ..exceptions import SyncError +from ..signals import post_sync, pre_sync + +__all__ = ( + 'DataFile', + 'DataSource', +) + +logger = logging.getLogger('netbox.core.data') + + +class DataSource(PrimaryModel): + """ + A remote source, such as a git repository, from which DataFiles are synchronized. + """ + name = models.CharField( + max_length=100, + unique=True + ) + type = models.CharField( + max_length=50, + choices=DataSourceTypeChoices, + default=DataSourceTypeChoices.LOCAL + ) + source_url = models.CharField( + max_length=200, + verbose_name=_('URL') + ) + status = models.CharField( + max_length=50, + choices=DataSourceStatusChoices, + default=DataSourceStatusChoices.NEW, + editable=False + ) + enabled = models.BooleanField( + default=True + ) + ignore_rules = models.TextField( + blank=True, + help_text=_("Patterns (one per line) matching files to ignore when syncing") + ) + parameters = models.JSONField( + blank=True, + null=True + ) + last_synced = models.DateTimeField( + blank=True, + null=True, + editable=False + ) + + class Meta: + ordering = ('name',) + + def __str__(self): + return f'{self.name}' + + def get_absolute_url(self): + return reverse('core:datasource', args=[self.pk]) + + @property + def docs_url(self): + return f'{settings.STATIC_URL}docs/models/{self._meta.app_label}/{self._meta.model_name}/' + + def get_type_color(self): + return DataSourceTypeChoices.colors.get(self.type) + + def get_status_color(self): + return DataSourceStatusChoices.colors.get(self.status) + + @property + def url_scheme(self): + return urlparse(self.source_url).scheme.lower() + + @property + def ready_for_sync(self): + return self.enabled and self.status not in ( + DataSourceStatusChoices.QUEUED, + DataSourceStatusChoices.SYNCING + ) + + def clean(self): + + # Ensure URL scheme matches selected type + if self.type == DataSourceTypeChoices.LOCAL and self.url_scheme not in ('file', ''): + raise ValidationError({ + 'url': f"URLs for local sources must start with file:// (or omit the scheme)" + }) + + def enqueue_sync_job(self, request): + """ + Enqueue a background job to synchronize the DataSource by calling sync(). + """ + # Set the status to "syncing" + self.status = DataSourceStatusChoices.QUEUED + + # Enqueue a sync job + job_result = JobResult.enqueue_job( + import_string('core.jobs.sync_datasource'), + name=self.name, + obj_type=ContentType.objects.get_for_model(DataSource), + user=request.user, + ) + + return job_result + + def get_backend(self): + backend_cls = registry['data_backends'].get(self.type) + backend_params = self.parameters or {} + + return backend_cls(self.source_url, **backend_params) + + def sync(self): + """ + Create/update/delete child DataFiles as necessary to synchronize with the remote source. + """ + if not self.ready_for_sync: + raise SyncError(f"Cannot initiate sync; data source not ready/enabled") + + # Emit the pre_sync signal + pre_sync.send(sender=self.__class__, instance=self) + + self.status = DataSourceStatusChoices.SYNCING + DataSource.objects.filter(pk=self.pk).update(status=self.status) + + # Replicate source data locally + backend = self.get_backend() + with backend.fetch() as local_path: + + logger.debug(f'Syncing files from source root {local_path}') + data_files = self.datafiles.all() + known_paths = {df.path for df in data_files} + logger.debug(f'Starting with {len(known_paths)} known files') + + # Check for any updated/deleted files + updated_files = [] + deleted_file_ids = [] + for datafile in data_files: + + try: + if datafile.refresh_from_disk(source_root=local_path): + updated_files.append(datafile) + except FileNotFoundError: + # File no longer exists + deleted_file_ids.append(datafile.pk) + continue + + # Bulk update modified files + updated_count = DataFile.objects.bulk_update(updated_files, ('last_updated', 'size', 'hash', 'data')) + logger.debug(f"Updated {updated_count} files") + + # Bulk delete deleted files + deleted_count, _ = DataFile.objects.filter(pk__in=deleted_file_ids).delete() + logger.debug(f"Deleted {updated_count} files") + + # Walk the local replication to find new files + new_paths = self._walk(local_path) - known_paths + + # Bulk create new files + new_datafiles = [] + for path in new_paths: + datafile = DataFile(source=self, path=path) + datafile.refresh_from_disk(source_root=local_path) + datafile.full_clean() + new_datafiles.append(datafile) + created_count = len(DataFile.objects.bulk_create(new_datafiles, batch_size=100)) + logger.debug(f"Created {created_count} data files") + + # Update status & last_synced time + self.status = DataSourceStatusChoices.COMPLETED + self.last_synced = timezone.now() + DataSource.objects.filter(pk=self.pk).update(status=self.status, last_synced=self.last_synced) + + # Emit the post_sync signal + post_sync.send(sender=self.__class__, instance=self) + + def _walk(self, root): + """ + Return a set of all non-excluded files within the root path. + """ + logger.debug(f"Walking {root}...") + paths = set() + + for path, dir_names, file_names in os.walk(root): + path = path.split(root)[1].lstrip('/') # Strip root path + if path.startswith('.'): + continue + for file_name in file_names: + if not self._ignore(file_name): + paths.add(os.path.join(path, file_name)) + + logger.debug(f"Found {len(paths)} files") + return paths + + def _ignore(self, filename): + """ + Returns a boolean indicating whether the file should be ignored per the DataSource's configured + ignore rules. + """ + if filename.startswith('.'): + return True + for rule in self.ignore_rules.splitlines(): + if fnmatchcase(filename, rule): + return True + return False + + +class DataFile(models.Model): + """ + The database representation of a remote file fetched from a remote DataSource. DataFile instances should be created, + updated, or deleted only by calling DataSource.sync(). + """ + created = models.DateTimeField( + auto_now_add=True + ) + last_updated = models.DateTimeField( + editable=False + ) + source = models.ForeignKey( + to='core.DataSource', + on_delete=models.CASCADE, + related_name='datafiles', + editable=False + ) + path = models.CharField( + max_length=1000, + editable=False, + help_text=_("File path relative to the data source's root") + ) + size = models.PositiveIntegerField( + editable=False + ) + hash = models.CharField( + max_length=64, + editable=False, + validators=[ + RegexValidator(regex='^[0-9a-f]{64}$', message=_("Length must be 64 hexadecimal characters.")) + ], + help_text=_("SHA256 hash of the file data") + ) + data = models.BinaryField() + + objects = RestrictedQuerySet.as_manager() + + class Meta: + ordering = ('source', 'path') + constraints = ( + models.UniqueConstraint( + fields=('source', 'path'), + name='%(app_label)s_%(class)s_unique_source_path' + ), + ) + indexes = [ + models.Index(fields=('source', 'path'), name='core_datafile_source_path'), + ] + + def __str__(self): + return self.path + + def get_absolute_url(self): + return reverse('core:datafile', args=[self.pk]) + + @property + def data_as_string(self): + try: + return self.data.tobytes().decode('utf-8') + except UnicodeDecodeError: + return None + + def get_data(self): + """ + Attempt to read the file data as JSON/YAML and return a native Python object. + """ + # TODO: Something more robust + return yaml.safe_load(self.data_as_string) + + def refresh_from_disk(self, source_root): + """ + Update instance attributes from the file on disk. Returns True if any attribute + has changed. + """ + file_path = os.path.join(source_root, self.path) + file_hash = sha256_hash(file_path).hexdigest() + + # Update instance file attributes & data + if is_modified := file_hash != self.hash: + self.last_updated = timezone.now() + self.size = os.path.getsize(file_path) + self.hash = file_hash + with open(file_path, 'rb') as f: + self.data = f.read() + + return is_modified diff --git a/netbox/core/search.py b/netbox/core/search.py new file mode 100644 index 000000000..e6d3005e6 --- /dev/null +++ b/netbox/core/search.py @@ -0,0 +1,21 @@ +from netbox.search import SearchIndex, register_search +from . import models + + +@register_search +class DataSourceIndex(SearchIndex): + model = models.DataSource + fields = ( + ('name', 100), + ('source_url', 300), + ('description', 500), + ('comments', 5000), + ) + + +@register_search +class DataFileIndex(SearchIndex): + model = models.DataFile + fields = ( + ('path', 200), + ) diff --git a/netbox/core/signals.py b/netbox/core/signals.py new file mode 100644 index 000000000..65ca293f5 --- /dev/null +++ b/netbox/core/signals.py @@ -0,0 +1,10 @@ +import django.dispatch + +__all__ = ( + 'post_sync', + 'pre_sync', +) + +# DataSource signals +pre_sync = django.dispatch.Signal() +post_sync = django.dispatch.Signal() diff --git a/netbox/core/tables/__init__.py b/netbox/core/tables/__init__.py new file mode 100644 index 000000000..df22d8bbb --- /dev/null +++ b/netbox/core/tables/__init__.py @@ -0,0 +1 @@ +from .data import * diff --git a/netbox/core/tables/data.py b/netbox/core/tables/data.py new file mode 100644 index 000000000..8409e3b82 --- /dev/null +++ b/netbox/core/tables/data.py @@ -0,0 +1,52 @@ +import django_tables2 as tables + +from core.models import * +from netbox.tables import NetBoxTable, columns + +__all__ = ( + 'DataFileTable', + 'DataSourceTable', +) + + +class DataSourceTable(NetBoxTable): + name = tables.Column( + linkify=True + ) + type = columns.ChoiceFieldColumn() + status = columns.ChoiceFieldColumn() + enabled = columns.BooleanColumn() + tags = columns.TagColumn( + url_name='core:datasource_list' + ) + file_count = tables.Column( + verbose_name='Files' + ) + + class Meta(NetBoxTable.Meta): + model = DataSource + fields = ( + 'pk', 'id', 'name', 'type', 'status', 'enabled', 'source_url', 'description', 'comments', 'parameters', 'created', + 'last_updated', 'file_count', + ) + default_columns = ('pk', 'name', 'type', 'status', 'enabled', 'description', 'file_count') + + +class DataFileTable(NetBoxTable): + source = tables.Column( + linkify=True + ) + path = tables.Column( + linkify=True + ) + last_updated = columns.DateTimeColumn() + actions = columns.ActionsColumn( + actions=('delete',) + ) + + class Meta(NetBoxTable.Meta): + model = DataFile + fields = ( + 'pk', 'id', 'source', 'path', 'last_updated', 'size', 'hash', + ) + default_columns = ('pk', 'source', 'path', 'size', 'last_updated') diff --git a/netbox/core/tests/__init__.py b/netbox/core/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/core/tests/test_api.py b/netbox/core/tests/test_api.py new file mode 100644 index 000000000..dc6d6a5ce --- /dev/null +++ b/netbox/core/tests/test_api.py @@ -0,0 +1,93 @@ +from django.urls import reverse +from django.utils import timezone + +from utilities.testing import APITestCase, APIViewTestCases +from ..choices import * +from ..models import * + + +class AppTest(APITestCase): + + def test_root(self): + url = reverse('core-api:api-root') + response = self.client.get('{}?format=api'.format(url), **self.header) + + self.assertEqual(response.status_code, 200) + + +class DataSourceTest(APIViewTestCases.APIViewTestCase): + model = DataSource + brief_fields = ['display', 'id', 'name', 'url'] + bulk_update_data = { + 'enabled': False, + 'description': 'foo bar baz', + } + + @classmethod + def setUpTestData(cls): + data_sources = ( + DataSource(name='Data Source 1', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source1/'), + DataSource(name='Data Source 2', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source2/'), + DataSource(name='Data Source 3', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source3/'), + ) + DataSource.objects.bulk_create(data_sources) + + cls.create_data = [ + { + 'name': 'Data Source 4', + 'type': DataSourceTypeChoices.GIT, + 'source_url': 'https://example.com/git/source4' + }, + { + 'name': 'Data Source 5', + 'type': DataSourceTypeChoices.GIT, + 'source_url': 'https://example.com/git/source5' + }, + { + 'name': 'Data Source 6', + 'type': DataSourceTypeChoices.GIT, + 'source_url': 'https://example.com/git/source6' + }, + ] + + +class DataFileTest( + APIViewTestCases.GetObjectViewTestCase, + APIViewTestCases.ListObjectsViewTestCase, + APIViewTestCases.GraphQLTestCase +): + model = DataFile + brief_fields = ['display', 'id', 'path', 'url'] + + @classmethod + def setUpTestData(cls): + datasource = DataSource.objects.create( + name='Data Source 1', + type=DataSourceTypeChoices.LOCAL, + source_url='file:///var/tmp/source1/' + ) + + data_files = ( + DataFile( + source=datasource, + path='dir1/file1.txt', + last_updated=timezone.now(), + size=1000, + hash='442da078f0111cbdf42f21903724f6597c692535f55bdfbbea758a1ae99ad9e1' + ), + DataFile( + source=datasource, + path='dir1/file2.txt', + last_updated=timezone.now(), + size=2000, + hash='a78168c7c97115bafd96450ed03ea43acec495094c5caa28f0d02e20e3a76cc2' + ), + DataFile( + source=datasource, + path='dir1/file3.txt', + last_updated=timezone.now(), + size=3000, + hash='12b8827a14c4d5a2f30b6c6e2b7983063988612391c6cbe8ee7493b59054827a' + ), + ) + DataFile.objects.bulk_create(data_files) diff --git a/netbox/core/tests/test_filtersets.py b/netbox/core/tests/test_filtersets.py new file mode 100644 index 000000000..e1e916f70 --- /dev/null +++ b/netbox/core/tests/test_filtersets.py @@ -0,0 +1,120 @@ +from datetime import datetime + +from django.test import TestCase +from django.utils import timezone + +from utilities.testing import ChangeLoggedFilterSetTests +from ..choices import * +from ..filtersets import * +from ..models import * + + +class DataSourceTestCase(TestCase, ChangeLoggedFilterSetTests): + queryset = DataSource.objects.all() + filterset = DataSourceFilterSet + + @classmethod + def setUpTestData(cls): + data_sources = ( + DataSource( + name='Data Source 1', + type=DataSourceTypeChoices.LOCAL, + source_url='file:///var/tmp/source1/', + status=DataSourceStatusChoices.NEW, + enabled=True + ), + DataSource( + name='Data Source 2', + type=DataSourceTypeChoices.LOCAL, + source_url='file:///var/tmp/source2/', + status=DataSourceStatusChoices.SYNCING, + enabled=True + ), + DataSource( + name='Data Source 3', + type=DataSourceTypeChoices.GIT, + source_url='https://example.com/git/source3', + status=DataSourceStatusChoices.COMPLETED, + enabled=False + ), + ) + DataSource.objects.bulk_create(data_sources) + + def test_name(self): + params = {'name': ['Data Source 1', 'Data Source 2']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_type(self): + params = {'type': [DataSourceTypeChoices.LOCAL]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_enabled(self): + params = {'enabled': 'true'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'enabled': 'false'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) + + def test_status(self): + params = {'status': [DataSourceStatusChoices.NEW, DataSourceStatusChoices.SYNCING]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + +class DataFileTestCase(TestCase, ChangeLoggedFilterSetTests): + queryset = DataFile.objects.all() + filterset = DataFileFilterSet + + @classmethod + def setUpTestData(cls): + data_sources = ( + DataSource(name='Data Source 1', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source1/'), + DataSource(name='Data Source 2', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source2/'), + DataSource(name='Data Source 3', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source3/'), + ) + DataSource.objects.bulk_create(data_sources) + + data_files = ( + DataFile( + source=data_sources[0], + path='dir1/file1.txt', + last_updated=datetime(2023, 1, 1, 0, 0, 0, tzinfo=timezone.utc), + size=1000, + hash='442da078f0111cbdf42f21903724f6597c692535f55bdfbbea758a1ae99ad9e1' + ), + DataFile( + source=data_sources[1], + path='dir1/file2.txt', + last_updated=datetime(2023, 1, 2, 0, 0, 0, tzinfo=timezone.utc), + size=2000, + hash='a78168c7c97115bafd96450ed03ea43acec495094c5caa28f0d02e20e3a76cc2' + ), + DataFile( + source=data_sources[2], + path='dir1/file3.txt', + last_updated=datetime(2023, 1, 3, 0, 0, 0, tzinfo=timezone.utc), + size=3000, + hash='12b8827a14c4d5a2f30b6c6e2b7983063988612391c6cbe8ee7493b59054827a' + ), + ) + DataFile.objects.bulk_create(data_files) + + def test_source(self): + sources = DataSource.objects.all() + params = {'source_id': [sources[0].pk, sources[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'source': [sources[0].name, sources[1].name]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_path(self): + params = {'path': ['dir1/file1.txt', 'dir1/file2.txt']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_size(self): + params = {'size': [1000, 2000]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_hash(self): + params = {'hash': [ + '442da078f0111cbdf42f21903724f6597c692535f55bdfbbea758a1ae99ad9e1', + 'a78168c7c97115bafd96450ed03ea43acec495094c5caa28f0d02e20e3a76cc2', + ]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) diff --git a/netbox/core/tests/test_views.py b/netbox/core/tests/test_views.py new file mode 100644 index 000000000..4a50a8d05 --- /dev/null +++ b/netbox/core/tests/test_views.py @@ -0,0 +1,90 @@ +from django.utils import timezone + +from utilities.testing import ViewTestCases, create_tags +from ..choices import * +from ..models import * + + +class DataSourceTestCase(ViewTestCases.PrimaryObjectViewTestCase): + model = DataSource + + @classmethod + def setUpTestData(cls): + data_sources = ( + DataSource(name='Data Source 1', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source1/'), + DataSource(name='Data Source 2', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source2/'), + DataSource(name='Data Source 3', type=DataSourceTypeChoices.LOCAL, source_url='file:///var/tmp/source3/'), + ) + DataSource.objects.bulk_create(data_sources) + + tags = create_tags('Alpha', 'Bravo', 'Charlie') + + cls.form_data = { + 'name': 'Data Source X', + 'type': DataSourceTypeChoices.GIT, + 'source_url': 'http:///exmaple/com/foo/bar/', + 'description': 'Something', + 'comments': 'Foo bar baz', + 'tags': [t.pk for t in tags], + } + + cls.csv_data = ( + f"name,type,source_url,enabled", + f"Data Source 4,{DataSourceTypeChoices.LOCAL},file:///var/tmp/source4/,true", + f"Data Source 5,{DataSourceTypeChoices.LOCAL},file:///var/tmp/source4/,true", + f"Data Source 6,{DataSourceTypeChoices.GIT},http:///exmaple/com/foo/bar/,false", + ) + + cls.csv_update_data = ( + "id,name,description", + f"{data_sources[0].pk},Data Source 7,New description7", + f"{data_sources[1].pk},Data Source 8,New description8", + f"{data_sources[2].pk},Data Source 9,New description9", + ) + + cls.bulk_edit_data = { + 'enabled': False, + 'description': 'New description', + } + + +class DataFileTestCase( + ViewTestCases.GetObjectViewTestCase, + ViewTestCases.DeleteObjectViewTestCase, + ViewTestCases.ListObjectsViewTestCase, + ViewTestCases.BulkDeleteObjectsViewTestCase, +): + model = DataFile + + @classmethod + def setUpTestData(cls): + datasource = DataSource.objects.create( + name='Data Source 1', + type=DataSourceTypeChoices.LOCAL, + source_url='file:///var/tmp/source1/' + ) + + data_files = ( + DataFile( + source=datasource, + path='dir1/file1.txt', + last_updated=timezone.now(), + size=1000, + hash='442da078f0111cbdf42f21903724f6597c692535f55bdfbbea758a1ae99ad9e1' + ), + DataFile( + source=datasource, + path='dir1/file2.txt', + last_updated=timezone.now(), + size=2000, + hash='a78168c7c97115bafd96450ed03ea43acec495094c5caa28f0d02e20e3a76cc2' + ), + DataFile( + source=datasource, + path='dir1/file3.txt', + last_updated=timezone.now(), + size=3000, + hash='12b8827a14c4d5a2f30b6c6e2b7983063988612391c6cbe8ee7493b59054827a' + ), + ) + DataFile.objects.bulk_create(data_files) diff --git a/netbox/core/urls.py b/netbox/core/urls.py new file mode 100644 index 000000000..128020890 --- /dev/null +++ b/netbox/core/urls.py @@ -0,0 +1,22 @@ +from django.urls import include, path + +from utilities.urls import get_model_urls +from . import views + +app_name = 'core' +urlpatterns = ( + + # Data sources + path('data-sources/', views.DataSourceListView.as_view(), name='datasource_list'), + path('data-sources/add/', views.DataSourceEditView.as_view(), name='datasource_add'), + path('data-sources/import/', views.DataSourceBulkImportView.as_view(), name='datasource_import'), + path('data-sources/edit/', views.DataSourceBulkEditView.as_view(), name='datasource_bulk_edit'), + path('data-sources/delete/', views.DataSourceBulkDeleteView.as_view(), name='datasource_bulk_delete'), + path('data-sources//', include(get_model_urls('core', 'datasource'))), + + # Data files + path('data-files/', views.DataFileListView.as_view(), name='datafile_list'), + path('data-files/delete/', views.DataFileBulkDeleteView.as_view(), name='datafile_bulk_delete'), + path('data-files//', include(get_model_urls('core', 'datafile'))), + +) diff --git a/netbox/core/views.py b/netbox/core/views.py new file mode 100644 index 000000000..63905228e --- /dev/null +++ b/netbox/core/views.py @@ -0,0 +1,118 @@ +from django.contrib import messages +from django.shortcuts import get_object_or_404, redirect + +from netbox.views import generic +from netbox.views.generic.base import BaseObjectView +from utilities.utils import count_related +from utilities.views import register_model_view +from . import filtersets, forms, tables +from .models import * + + +# +# Data sources +# + +class DataSourceListView(generic.ObjectListView): + queryset = DataSource.objects.annotate( + file_count=count_related(DataFile, 'source') + ) + filterset = filtersets.DataSourceFilterSet + filterset_form = forms.DataSourceFilterForm + table = tables.DataSourceTable + + +@register_model_view(DataSource) +class DataSourceView(generic.ObjectView): + queryset = DataSource.objects.all() + + def get_extra_context(self, request, instance): + related_models = ( + (DataFile.objects.restrict(request.user, 'view').filter(source=instance), 'source_id'), + ) + + return { + 'related_models': related_models, + } + + +@register_model_view(DataSource, 'sync') +class DataSourceSyncView(BaseObjectView): + queryset = DataSource.objects.all() + + def get_required_permission(self): + return 'core.sync_datasource' + + def get(self, request, pk): + # Redirect GET requests to the object view + datasource = get_object_or_404(self.queryset, pk=pk) + return redirect(datasource.get_absolute_url()) + + def post(self, request, pk): + datasource = get_object_or_404(self.queryset, pk=pk) + job_result = datasource.enqueue_sync_job(request) + + messages.success(request, f"Queued job #{job_result.pk} to sync {datasource}") + return redirect(datasource.get_absolute_url()) + + +@register_model_view(DataSource, 'edit') +class DataSourceEditView(generic.ObjectEditView): + queryset = DataSource.objects.all() + form = forms.DataSourceForm + + +@register_model_view(DataSource, 'delete') +class DataSourceDeleteView(generic.ObjectDeleteView): + queryset = DataSource.objects.all() + + +class DataSourceBulkImportView(generic.BulkImportView): + queryset = DataSource.objects.all() + model_form = forms.DataSourceImportForm + table = tables.DataSourceTable + + +class DataSourceBulkEditView(generic.BulkEditView): + queryset = DataSource.objects.annotate( + count_files=count_related(DataFile, 'source') + ) + filterset = filtersets.DataSourceFilterSet + table = tables.DataSourceTable + form = forms.DataSourceBulkEditForm + + +class DataSourceBulkDeleteView(generic.BulkDeleteView): + queryset = DataSource.objects.annotate( + count_files=count_related(DataFile, 'source') + ) + filterset = filtersets.DataSourceFilterSet + table = tables.DataSourceTable + + +# +# Data files +# + +class DataFileListView(generic.ObjectListView): + queryset = DataFile.objects.defer('data') + filterset = filtersets.DataFileFilterSet + filterset_form = forms.DataFileFilterForm + table = tables.DataFileTable + actions = ('bulk_delete',) + + +@register_model_view(DataFile) +class DataFileView(generic.ObjectView): + queryset = DataFile.objects.all() + + +@register_model_view(DataFile, 'delete') +class DataFileDeleteView(generic.ObjectDeleteView): + queryset = DataFile.objects.all() + + +class DataFileBulkDeleteView(generic.BulkDeleteView): + queryset = DataFile.objects.defer('data') + filterset = filtersets.DataFileFilterSet + table = tables.DataFileTable diff --git a/netbox/dcim/api/serializers.py b/netbox/dcim/api/serializers.py index 184cb4577..8038ac248 100644 --- a/netbox/dcim/api/serializers.py +++ b/netbox/dcim/api/serializers.py @@ -10,6 +10,7 @@ from timezone_field.rest_framework import TimeZoneSerializerField from dcim.choices import * from dcim.constants import * from dcim.models import * +from extras.api.nested_serializers import NestedConfigTemplateSerializer from ipam.api.nested_serializers import ( NestedASNSerializer, NestedIPAddressSerializer, NestedL2VPNTerminationSerializer, NestedVLANSerializer, NestedVRFSerializer, @@ -313,6 +314,7 @@ class ManufacturerSerializer(NetBoxModelSerializer): class DeviceTypeSerializer(NetBoxModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='dcim-api:devicetype-detail') manufacturer = NestedManufacturerSerializer() + default_platform = NestedPlatformSerializer(required=False, allow_null=True) u_height = serializers.DecimalField( max_digits=4, decimal_places=1, @@ -328,7 +330,7 @@ class DeviceTypeSerializer(NetBoxModelSerializer): class Meta: model = DeviceType fields = [ - 'id', 'url', 'display', 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', + 'id', 'url', 'display', 'manufacturer', 'default_platform', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', 'weight', 'weight_unit', 'front_image', 'rear_image', 'description', 'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'device_count', ] @@ -491,7 +493,7 @@ class InterfaceTemplateSerializer(ValidatedModelSerializer): class Meta: model = InterfaceTemplate fields = [ - 'id', 'url', 'display', 'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'description', + 'id', 'url', 'display', 'device_type', 'module_type', 'name', 'label', 'type', 'enabled', 'mgmt_only', 'description', 'poe_mode', 'poe_type', 'created', 'last_updated', ] @@ -608,8 +610,8 @@ class DeviceRoleSerializer(NetBoxModelSerializer): class Meta: model = DeviceRole fields = [ - 'id', 'url', 'display', 'name', 'slug', 'color', 'vm_role', 'description', 'tags', 'custom_fields', - 'created', 'last_updated', 'device_count', 'virtualmachine_count', + 'id', 'url', 'display', 'name', 'slug', 'color', 'vm_role', 'config_template', 'description', 'tags', + 'custom_fields', 'created', 'last_updated', 'device_count', 'virtualmachine_count', ] @@ -622,8 +624,8 @@ class PlatformSerializer(NetBoxModelSerializer): class Meta: model = Platform fields = [ - 'id', 'url', 'display', 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', - 'tags', 'custom_fields', 'created', 'last_updated', 'device_count', 'virtualmachine_count', + 'id', 'url', 'display', 'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args', + 'description', 'tags', 'custom_fields', 'created', 'last_updated', 'device_count', 'virtualmachine_count', ] @@ -654,6 +656,7 @@ class DeviceSerializer(NetBoxModelSerializer): cluster = NestedClusterSerializer(required=False, allow_null=True) virtual_chassis = NestedVirtualChassisSerializer(required=False, allow_null=True, default=None) vc_position = serializers.IntegerField(allow_null=True, max_value=255, min_value=0, default=None) + config_template = NestedConfigTemplateSerializer(required=False, allow_null=True, default=None) class Meta: model = Device @@ -661,7 +664,7 @@ class DeviceSerializer(NetBoxModelSerializer): 'id', 'url', 'display', 'name', 'device_type', 'device_role', 'tenant', 'platform', 'serial', 'asset_tag', 'site', 'location', 'rack', 'position', 'face', 'parent_device', 'status', 'airflow', 'primary_ip', 'primary_ip4', 'primary_ip6', 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'description', - 'comments', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated', + 'comments', 'config_template', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated', ] @extend_schema_field(NestedDeviceSerializer) diff --git a/netbox/dcim/api/views.py b/netbox/dcim/api/views.py index 0b467c60f..09614bb8b 100644 --- a/netbox/dcim/api/views.py +++ b/netbox/dcim/api/views.py @@ -361,7 +361,7 @@ class InventoryItemTemplateViewSet(NetBoxModelViewSet): # class DeviceRoleViewSet(NetBoxModelViewSet): - queryset = DeviceRole.objects.prefetch_related('tags').annotate( + queryset = DeviceRole.objects.prefetch_related('config_template', 'tags').annotate( device_count=count_related(Device, 'device_role'), virtualmachine_count=count_related(VirtualMachine, 'role') ) @@ -374,7 +374,7 @@ class DeviceRoleViewSet(NetBoxModelViewSet): # class PlatformViewSet(NetBoxModelViewSet): - queryset = Platform.objects.prefetch_related('tags').annotate( + queryset = Platform.objects.prefetch_related('config_template', 'tags').annotate( device_count=count_related(Device, 'platform'), virtualmachine_count=count_related(VirtualMachine, 'platform') ) diff --git a/netbox/dcim/filtersets.py b/netbox/dcim/filtersets.py index 83ae8bcc9..fd3f9425e 100644 --- a/netbox/dcim/filtersets.py +++ b/netbox/dcim/filtersets.py @@ -3,6 +3,7 @@ from django.contrib.auth.models import User from django.utils.translation import gettext as _ from extras.filtersets import LocalConfigContextFilterSet +from extras.models import ConfigTemplate from ipam.models import ASN, L2VPN, IPAddress, VRF from netbox.filtersets import ( BaseFilterSet, ChangeLoggedModelFilterSet, OrganizationalModelFilterSet, NetBoxModelFilterSet, @@ -436,6 +437,16 @@ class DeviceTypeFilterSet(NetBoxModelFilterSet): to_field_name='slug', label=_('Manufacturer (slug)'), ) + default_platform_id = django_filters.ModelMultipleChoiceFilter( + queryset=Platform.objects.all(), + label=_('Default platform (ID)'), + ) + default_platform = django_filters.ModelMultipleChoiceFilter( + field_name='default_platform__slug', + queryset=Platform.objects.all(), + to_field_name='slug', + label=_('Default platform (slug)'), + ) has_front_image = django_filters.BooleanFilter( label=_('Has a front image'), method='_has_front_image' @@ -766,6 +777,10 @@ class InventoryItemTemplateFilterSet(ChangeLoggedModelFilterSet, DeviceTypeCompo class DeviceRoleFilterSet(OrganizationalModelFilterSet): + config_template_id = django_filters.ModelMultipleChoiceFilter( + queryset=ConfigTemplate.objects.all(), + label=_('Config template (ID)'), + ) class Meta: model = DeviceRole @@ -784,6 +799,10 @@ class PlatformFilterSet(OrganizationalModelFilterSet): to_field_name='slug', label=_('Manufacturer (slug)'), ) + config_template_id = django_filters.ModelMultipleChoiceFilter( + queryset=ConfigTemplate.objects.all(), + label=_('Config template (ID)'), + ) class Meta: model = Platform @@ -926,6 +945,10 @@ class DeviceFilterSet(NetBoxModelFilterSet, TenancyFilterSet, ContactModelFilter method='_virtual_chassis_member', label=_('Is a virtual chassis member') ) + config_template_id = django_filters.ModelMultipleChoiceFilter( + queryset=ConfigTemplate.objects.all(), + label=_('Config template (ID)'), + ) console_ports = django_filters.BooleanFilter( method='_console_ports', label=_('Has console ports'), diff --git a/netbox/dcim/forms/bulk_edit.py b/netbox/dcim/forms/bulk_edit.py index 8969b1e69..0bd5764a0 100644 --- a/netbox/dcim/forms/bulk_edit.py +++ b/netbox/dcim/forms/bulk_edit.py @@ -6,12 +6,13 @@ from timezone_field import TimeZoneFormField from dcim.choices import * from dcim.constants import * from dcim.models import * +from extras.models import ConfigTemplate from ipam.models import ASN, VLAN, VLANGroup, VRF from netbox.forms import NetBoxModelBulkEditForm from tenancy.models import Tenant from utilities.forms import ( add_blank_choice, BulkEditForm, BulkEditNullBooleanSelect, ColorField, CommentField, DynamicModelChoiceField, - DynamicModelMultipleChoiceField, form_from_model, SmallTextarea, StaticSelect, SelectSpeedWidget, + DynamicModelMultipleChoiceField, form_from_model, SelectSpeedWidget, ) __all__ = ( @@ -96,8 +97,7 @@ class SiteBulkEditForm(NetBoxModelBulkEditForm): status = forms.ChoiceField( choices=add_blank_choice(SiteStatusChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) region = DynamicModelChoiceField( queryset=Region.objects.all(), @@ -130,15 +130,14 @@ class SiteBulkEditForm(NetBoxModelBulkEditForm): ) time_zone = TimeZoneFormField( choices=add_blank_choice(TimeZoneFormField().choices), - required=False, - widget=StaticSelect() + required=False ) description = forms.CharField( max_length=200, required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -166,8 +165,7 @@ class LocationBulkEditForm(NetBoxModelBulkEditForm): status = forms.ChoiceField( choices=add_blank_choice(LocationStatusChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) tenant = DynamicModelChoiceField( queryset=Tenant.objects.all(), @@ -238,8 +236,7 @@ class RackBulkEditForm(NetBoxModelBulkEditForm): status = forms.ChoiceField( choices=add_blank_choice(RackStatusChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) role = DynamicModelChoiceField( queryset=RackRole.objects.all(), @@ -256,13 +253,11 @@ class RackBulkEditForm(NetBoxModelBulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(RackTypeChoices), - required=False, - widget=StaticSelect() + required=False ) width = forms.ChoiceField( choices=add_blank_choice(RackWidthChoices), - required=False, - widget=StaticSelect() + required=False ) u_height = forms.IntegerField( required=False, @@ -283,8 +278,7 @@ class RackBulkEditForm(NetBoxModelBulkEditForm): ) outer_unit = forms.ChoiceField( choices=add_blank_choice(RackDimensionUnitChoices), - required=False, - widget=StaticSelect() + required=False ) mounting_depth = forms.IntegerField( required=False, @@ -301,15 +295,14 @@ class RackBulkEditForm(NetBoxModelBulkEditForm): weight_unit = forms.ChoiceField( choices=add_blank_choice(WeightUnitChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) description = forms.CharField( max_length=200, required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -333,8 +326,7 @@ class RackReservationBulkEditForm(NetBoxModelBulkEditForm): queryset=User.objects.order_by( 'username' ), - required=False, - widget=StaticSelect() + required=False ) tenant = DynamicModelChoiceField( queryset=Tenant.objects.all(), @@ -345,7 +337,7 @@ class RackReservationBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -374,6 +366,10 @@ class DeviceTypeBulkEditForm(NetBoxModelBulkEditForm): queryset=Manufacturer.objects.all(), required=False ) + default_platform = DynamicModelChoiceField( + queryset=Platform.objects.all(), + required=False + ) part_number = forms.CharField( required=False ) @@ -388,8 +384,7 @@ class DeviceTypeBulkEditForm(NetBoxModelBulkEditForm): ) airflow = forms.ChoiceField( choices=add_blank_choice(DeviceAirflowChoices), - required=False, - widget=StaticSelect() + required=False ) weight = forms.DecimalField( min_value=0, @@ -398,21 +393,20 @@ class DeviceTypeBulkEditForm(NetBoxModelBulkEditForm): weight_unit = forms.ChoiceField( choices=add_blank_choice(WeightUnitChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) description = forms.CharField( max_length=200, required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) model = DeviceType fieldsets = ( - ('Device Type', ('manufacturer', 'part_number', 'u_height', 'is_full_depth', 'airflow', 'description')), + ('Device Type', ('manufacturer', 'default_platform', 'part_number', 'u_height', 'is_full_depth', 'airflow', 'description')), ('Weight', ('weight', 'weight_unit')), ) nullable_fields = ('part_number', 'airflow', 'weight', 'weight_unit', 'description', 'comments') @@ -433,15 +427,14 @@ class ModuleTypeBulkEditForm(NetBoxModelBulkEditForm): weight_unit = forms.ChoiceField( choices=add_blank_choice(WeightUnitChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) description = forms.CharField( max_length=200, required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -462,6 +455,10 @@ class DeviceRoleBulkEditForm(NetBoxModelBulkEditForm): widget=BulkEditNullBooleanSelect, label=_('VM role') ) + config_template = DynamicModelChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False + ) description = forms.CharField( max_length=200, required=False @@ -469,9 +466,9 @@ class DeviceRoleBulkEditForm(NetBoxModelBulkEditForm): model = DeviceRole fieldsets = ( - (None, ('color', 'vm_role', 'description')), + (None, ('color', 'vm_role', 'config_template', 'description')), ) - nullable_fields = ('color', 'description') + nullable_fields = ('color', 'config_template', 'description') class PlatformBulkEditForm(NetBoxModelBulkEditForm): @@ -483,7 +480,10 @@ class PlatformBulkEditForm(NetBoxModelBulkEditForm): max_length=50, required=False ) - # TODO: Bulk edit support for napalm_args + config_template = DynamicModelChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False + ) description = forms.CharField( max_length=200, required=False @@ -491,9 +491,9 @@ class PlatformBulkEditForm(NetBoxModelBulkEditForm): model = Platform fieldsets = ( - (None, ('manufacturer', 'napalm_driver', 'description')), + (None, ('manufacturer', 'config_template', 'napalm_driver', 'description')), ) - nullable_fields = ('manufacturer', 'napalm_driver', 'description') + nullable_fields = ('manufacturer', 'config_template', 'napalm_driver', 'description') class DeviceBulkEditForm(NetBoxModelBulkEditForm): @@ -533,13 +533,11 @@ class DeviceBulkEditForm(NetBoxModelBulkEditForm): ) status = forms.ChoiceField( choices=add_blank_choice(DeviceStatusChoices), - required=False, - widget=StaticSelect() + required=False ) airflow = forms.ChoiceField( choices=add_blank_choice(DeviceAirflowChoices), - required=False, - widget=StaticSelect() + required=False ) serial = forms.CharField( max_length=50, @@ -550,8 +548,12 @@ class DeviceBulkEditForm(NetBoxModelBulkEditForm): max_length=200, required=False ) + config_template = DynamicModelChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False + ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -560,6 +562,7 @@ class DeviceBulkEditForm(NetBoxModelBulkEditForm): ('Device', ('device_role', 'status', 'tenant', 'platform', 'description')), ('Location', ('site', 'location')), ('Hardware', ('manufacturer', 'device_type', 'airflow', 'serial')), + ('Configuration', ('config_template',)), ) nullable_fields = ( 'location', 'tenant', 'platform', 'serial', 'airflow', 'description', 'comments', @@ -581,8 +584,7 @@ class ModuleBulkEditForm(NetBoxModelBulkEditForm): status = forms.ChoiceField( choices=add_blank_choice(ModuleStatusChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) serial = forms.CharField( max_length=50, @@ -594,7 +596,7 @@ class ModuleBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -609,13 +611,11 @@ class CableBulkEditForm(NetBoxModelBulkEditForm): type = forms.ChoiceField( choices=add_blank_choice(CableTypeChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) status = forms.ChoiceField( choices=add_blank_choice(LinkStatusChoices), required=False, - widget=StaticSelect(), initial='' ) tenant = DynamicModelChoiceField( @@ -636,15 +636,14 @@ class CableBulkEditForm(NetBoxModelBulkEditForm): length_unit = forms.ChoiceField( choices=add_blank_choice(CableLengthUnitChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) description = forms.CharField( max_length=200, required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -668,7 +667,7 @@ class VirtualChassisBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -714,7 +713,7 @@ class PowerPanelBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -737,26 +736,22 @@ class PowerFeedBulkEditForm(NetBoxModelBulkEditForm): status = forms.ChoiceField( choices=add_blank_choice(PowerFeedStatusChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) type = forms.ChoiceField( choices=add_blank_choice(PowerFeedTypeChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) supply = forms.ChoiceField( choices=add_blank_choice(PowerFeedSupplyChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) phase = forms.ChoiceField( choices=add_blank_choice(PowerFeedPhaseChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) voltage = forms.IntegerField( required=False @@ -776,7 +771,7 @@ class PowerFeedBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label=_('Comments') ) @@ -803,8 +798,7 @@ class ConsolePortTemplateBulkEditForm(BulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(ConsolePortTypeChoices), - required=False, - widget=StaticSelect() + required=False ) nullable_fields = ('label', 'type', 'description') @@ -821,8 +815,7 @@ class ConsoleServerPortTemplateBulkEditForm(BulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(ConsolePortTypeChoices), - required=False, - widget=StaticSelect() + required=False ) description = forms.CharField( required=False @@ -842,8 +835,7 @@ class PowerPortTemplateBulkEditForm(BulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(PowerPortTypeChoices), - required=False, - widget=StaticSelect() + required=False ) maximum_draw = forms.IntegerField( min_value=1, @@ -879,8 +871,7 @@ class PowerOutletTemplateBulkEditForm(BulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(PowerOutletTypeChoices), - required=False, - widget=StaticSelect() + required=False ) power_port = forms.ModelChoiceField( queryset=PowerPortTemplate.objects.all(), @@ -888,8 +879,7 @@ class PowerOutletTemplateBulkEditForm(BulkEditForm): ) feed_leg = forms.ChoiceField( choices=add_blank_choice(PowerOutletFeedLegChoices), - required=False, - widget=StaticSelect() + required=False ) description = forms.CharField( required=False @@ -920,8 +910,11 @@ class InterfaceTemplateBulkEditForm(BulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(InterfaceTypeChoices), + required=False + ) + enabled = forms.NullBooleanField( required=False, - widget=StaticSelect() + widget=BulkEditNullBooleanSelect ) enabled = forms.NullBooleanField( required=False, @@ -939,14 +932,12 @@ class InterfaceTemplateBulkEditForm(BulkEditForm): choices=add_blank_choice(InterfacePoEModeChoices), required=False, initial='', - widget=StaticSelect(), label=_('PoE mode') ) poe_type = forms.ChoiceField( choices=add_blank_choice(InterfacePoETypeChoices), required=False, initial='', - widget=StaticSelect(), label=_('PoE type') ) @@ -964,8 +955,7 @@ class FrontPortTemplateBulkEditForm(BulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(PortTypeChoices), - required=False, - widget=StaticSelect() + required=False ) color = ColorField( required=False @@ -988,8 +978,7 @@ class RearPortTemplateBulkEditForm(BulkEditForm): ) type = forms.ChoiceField( choices=add_blank_choice(PortTypeChoices), - required=False, - widget=StaticSelect() + required=False ) color = ColorField( required=False @@ -1204,14 +1193,12 @@ class InterfaceBulkEditForm( choices=add_blank_choice(InterfacePoEModeChoices), required=False, initial='', - widget=StaticSelect(), label=_('PoE mode') ) poe_type = forms.ChoiceField( choices=add_blank_choice(InterfacePoETypeChoices), required=False, initial='', - widget=StaticSelect(), label=_('PoE type') ) mark_connected = forms.NullBooleanField( @@ -1221,8 +1208,7 @@ class InterfaceBulkEditForm( mode = forms.ChoiceField( choices=add_blank_choice(InterfaceModeChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) vlan_group = DynamicModelChoiceField( queryset=VLANGroup.objects.all(), @@ -1422,8 +1408,7 @@ class VirtualDeviceContextBulkEditForm(NetBoxModelBulkEditForm): ) status = forms.ChoiceField( required=False, - choices=add_blank_choice(VirtualDeviceContextStatusChoices), - widget=StaticSelect() + choices=add_blank_choice(VirtualDeviceContextStatusChoices) ) tenant = DynamicModelChoiceField( queryset=Tenant.objects.all(), diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index 3f016899e..e495ec34d 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -8,6 +8,7 @@ from django.utils.translation import gettext as _ from dcim.choices import * from dcim.constants import * from dcim.models import * +from extras.models import ConfigTemplate from ipam.models import VRF from netbox.forms import NetBoxModelImportForm from tenancy.models import Tenant @@ -281,12 +282,17 @@ class DeviceTypeImportForm(NetBoxModelImportForm): queryset=Manufacturer.objects.all(), to_field_name='name' ) + default_platform = forms.ModelChoiceField( + queryset=Platform.objects.all(), + to_field_name='name', + required=False, + ) class Meta: model = DeviceType fields = [ - 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', - 'description', 'comments', + 'manufacturer', 'default_platform', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', + 'subdevice_role', 'airflow', 'description', 'comments', ] @@ -302,11 +308,17 @@ class ModuleTypeImportForm(NetBoxModelImportForm): class DeviceRoleImportForm(NetBoxModelImportForm): + config_template = CSVModelChoiceField( + queryset=ConfigTemplate.objects.all(), + to_field_name='name', + required=False, + help_text=_('Config template') + ) slug = SlugField() class Meta: model = DeviceRole - fields = ('name', 'slug', 'color', 'vm_role', 'description', 'tags') + fields = ('name', 'slug', 'color', 'vm_role', 'config_template', 'description', 'tags') help_texts = { 'color': mark_safe(_('RGB color in hexadecimal (e.g. 00ff00)')), } @@ -320,10 +332,18 @@ class PlatformImportForm(NetBoxModelImportForm): to_field_name='name', help_text=_('Limit platform assignments to this manufacturer') ) + config_template = CSVModelChoiceField( + queryset=ConfigTemplate.objects.all(), + to_field_name='name', + required=False, + help_text=_('Config template') + ) class Meta: model = Platform - fields = ('name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags') + fields = ( + 'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args', 'description', 'tags', + ) class BaseDeviceImportForm(NetBoxModelImportForm): @@ -429,12 +449,18 @@ class DeviceImportForm(BaseDeviceImportForm): required=False, help_text=_('Airflow direction') ) + config_template = CSVModelChoiceField( + queryset=ConfigTemplate.objects.all(), + to_field_name='name', + required=False, + help_text=_('Config template') + ) class Meta(BaseDeviceImportForm.Meta): fields = [ 'name', 'device_role', 'tenant', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'status', 'site', 'location', 'rack', 'position', 'face', 'parent', 'device_bay', 'airflow', 'virtual_chassis', - 'vc_position', 'vc_priority', 'cluster', 'description', 'comments', 'tags', + 'vc_position', 'vc_priority', 'cluster', 'description', 'config_template', 'comments', 'tags', ] def __init__(self, data=None, *args, **kwargs): diff --git a/netbox/dcim/forms/common.py b/netbox/dcim/forms/common.py index a2243ce2d..f047d621b 100644 --- a/netbox/dcim/forms/common.py +++ b/netbox/dcim/forms/common.py @@ -3,6 +3,7 @@ from django.utils.translation import gettext as _ from dcim.choices import * from dcim.constants import * +from utilities.forms.utils import get_field_value __all__ = ( 'InterfaceCommonForm', @@ -23,6 +24,20 @@ class InterfaceCommonForm(forms.Form): label=_('MTU') ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Determine the selected 802.1Q mode + interface_mode = get_field_value(self, 'mode') + + # Delete VLAN tagging fields which are not relevant for the selected mode + if interface_mode in (InterfaceModeChoices.MODE_ACCESS, InterfaceModeChoices.MODE_TAGGED_ALL): + del self.fields['tagged_vlans'] + elif not interface_mode: + del self.fields['vlan_group'] + del self.fields['untagged_vlan'] + del self.fields['tagged_vlans'] + def clean(self): super().clean() diff --git a/netbox/dcim/forms/filtersets.py b/netbox/dcim/forms/filtersets.py index c00e83672..4ccc2fe54 100644 --- a/netbox/dcim/forms/filtersets.py +++ b/netbox/dcim/forms/filtersets.py @@ -6,12 +6,13 @@ from dcim.choices import * from dcim.constants import * from dcim.models import * from extras.forms import LocalConfigContextFilterForm +from extras.models import ConfigTemplate from ipam.models import ASN, L2VPN, VRF from netbox.forms import NetBoxModelFilterSetForm from tenancy.forms import ContactModelFilterForm, TenancyFilterForm from utilities.forms import ( - APISelectMultiple, add_blank_choice, ColorField, DynamicModelMultipleChoiceField, FilterForm, MultipleChoiceField, - StaticSelect, TagFilterField, BOOLEAN_WITH_BLANK_CHOICES, SelectSpeedWidget, + APISelectMultiple, add_blank_choice, ColorField, DynamicModelMultipleChoiceField, FilterForm, + TagFilterField, BOOLEAN_WITH_BLANK_CHOICES, SelectSpeedWidget, ) from wireless.choices import * @@ -150,7 +151,7 @@ class SiteFilterForm(TenancyFilterForm, ContactModelFilterForm, NetBoxModelFilte ('Tenant', ('tenant_group_id', 'tenant_id')), ('Contacts', ('contact', 'contact_role', 'contact_group')), ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=SiteStatusChoices, required=False ) @@ -208,7 +209,7 @@ class LocationFilterForm(TenancyFilterForm, ContactModelFilterForm, NetBoxModelF }, label=_('Parent') ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=LocationStatusChoices, required=False ) @@ -258,15 +259,15 @@ class RackFilterForm(TenancyFilterForm, ContactModelFilterForm, NetBoxModelFilte }, label=_('Location') ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=RackStatusChoices, required=False ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=RackTypeChoices, required=False ) - width = MultipleChoiceField( + width = forms.MultipleChoiceField( choices=RackWidthChoices, required=False ) @@ -378,7 +379,7 @@ class DeviceTypeFilterForm(NetBoxModelFilterSetForm): model = DeviceType fieldsets = ( (None, ('q', 'filter_id', 'tag')), - ('Hardware', ('manufacturer_id', 'part_number', 'subdevice_role', 'airflow')), + ('Hardware', ('manufacturer_id', 'default_platform_id', 'part_number', 'subdevice_role', 'airflow')), ('Images', ('has_front_image', 'has_rear_image')), ('Components', ( 'console_ports', 'console_server_ports', 'power_ports', 'power_outlets', 'interfaces', @@ -391,91 +392,96 @@ class DeviceTypeFilterForm(NetBoxModelFilterSetForm): required=False, label=_('Manufacturer') ) + default_platform_id = DynamicModelMultipleChoiceField( + queryset=Platform.objects.all(), + required=False, + label=_('Default platform') + ) part_number = forms.CharField( required=False ) - subdevice_role = MultipleChoiceField( + subdevice_role = forms.MultipleChoiceField( choices=add_blank_choice(SubdeviceRoleChoices), required=False ) - airflow = MultipleChoiceField( + airflow = forms.MultipleChoiceField( choices=add_blank_choice(DeviceAirflowChoices), required=False ) has_front_image = forms.NullBooleanField( required=False, label='Has a front image', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) has_rear_image = forms.NullBooleanField( required=False, label='Has a rear image', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) console_ports = forms.NullBooleanField( required=False, label='Has console ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) console_server_ports = forms.NullBooleanField( required=False, label='Has console server ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) power_ports = forms.NullBooleanField( required=False, label='Has power ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) power_outlets = forms.NullBooleanField( required=False, label='Has power outlets', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) interfaces = forms.NullBooleanField( required=False, label='Has interfaces', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) pass_through_ports = forms.NullBooleanField( required=False, label='Has pass-through ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) device_bays = forms.NullBooleanField( required=False, label='Has device bays', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) module_bays = forms.NullBooleanField( required=False, label='Has module bays', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) inventory_items = forms.NullBooleanField( required=False, label='Has inventory items', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -512,42 +518,42 @@ class ModuleTypeFilterForm(NetBoxModelFilterSetForm): console_ports = forms.NullBooleanField( required=False, label='Has console ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) console_server_ports = forms.NullBooleanField( required=False, label='Has console server ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) power_ports = forms.NullBooleanField( required=False, label='Has power ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) power_outlets = forms.NullBooleanField( required=False, label='Has power outlets', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) interfaces = forms.NullBooleanField( required=False, label='Has interfaces', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) pass_through_ports = forms.NullBooleanField( required=False, label='Has pass-through ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -563,6 +569,11 @@ class ModuleTypeFilterForm(NetBoxModelFilterSetForm): class DeviceRoleFilterForm(NetBoxModelFilterSetForm): model = DeviceRole + config_template_id = DynamicModelMultipleChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False, + label=_('Config template') + ) tag = TagFilterField(model) @@ -573,6 +584,11 @@ class PlatformFilterForm(NetBoxModelFilterSetForm): required=False, label=_('Manufacturer') ) + config_template_id = DynamicModelMultipleChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False, + label=_('Config template') + ) tag = TagFilterField(model) @@ -593,7 +609,7 @@ class DeviceFilterForm( ('Components', ( 'console_ports', 'console_server_ports', 'power_ports', 'power_outlets', 'interfaces', 'pass_through_ports', )), - ('Miscellaneous', ('has_primary_ip', 'virtual_chassis_member', 'local_context_data')) + ('Miscellaneous', ('has_primary_ip', 'virtual_chassis_member', 'config_template_id', 'local_context_data')) ) region_id = DynamicModelMultipleChoiceField( queryset=Region.objects.all(), @@ -657,11 +673,11 @@ class DeviceFilterForm( null_option='None', label=_('Platform') ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=DeviceStatusChoices, required=False ) - airflow = MultipleChoiceField( + airflow = forms.MultipleChoiceField( choices=add_blank_choice(DeviceAirflowChoices), required=False ) @@ -675,59 +691,64 @@ class DeviceFilterForm( required=False, label='MAC address' ) + config_template_id = DynamicModelMultipleChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False, + label=_('Config template') + ) has_primary_ip = forms.NullBooleanField( required=False, label='Has a primary IP', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) virtual_chassis_member = forms.NullBooleanField( required=False, label='Virtual chassis member', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) console_ports = forms.NullBooleanField( required=False, label='Has console ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) console_server_ports = forms.NullBooleanField( required=False, label='Has console server ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) power_ports = forms.NullBooleanField( required=False, label='Has power ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) power_outlets = forms.NullBooleanField( required=False, label='Has power outlets', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) interfaces = forms.NullBooleanField( required=False, label='Has interfaces', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) pass_through_ports = forms.NullBooleanField( required=False, label='Has pass-through ports', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -750,14 +771,14 @@ class VirtualDeviceContextFilterForm( label=_('Device'), fetch_trigger='open' ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( required=False, choices=add_blank_choice(VirtualDeviceContextStatusChoices) ) has_primary_ip = forms.NullBooleanField( required=False, label='Has a primary IP', - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -785,7 +806,7 @@ class ModuleFilterForm(LocalConfigContextFilterForm, TenancyFilterForm, NetBoxMo label=_('Type'), fetch_trigger='open' ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=ModuleStatusChoices, required=False ) @@ -878,11 +899,11 @@ class CableFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): }, label=_('Device') ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=add_blank_choice(CableTypeChoices), required=False ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( required=False, choices=add_blank_choice(LinkStatusChoices) ) @@ -980,24 +1001,21 @@ class PowerFeedFilterForm(NetBoxModelFilterSetForm): }, label=_('Rack') ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=PowerFeedStatusChoices, required=False ) type = forms.ChoiceField( choices=add_blank_choice(PowerFeedTypeChoices), - required=False, - widget=StaticSelect() + required=False ) supply = forms.ChoiceField( choices=add_blank_choice(PowerFeedSupplyChoices), - required=False, - widget=StaticSelect() + required=False ) phase = forms.ChoiceField( choices=add_blank_choice(PowerFeedPhaseChoices), - required=False, - widget=StaticSelect() + required=False ) voltage = forms.IntegerField( required=False @@ -1018,13 +1036,13 @@ class PowerFeedFilterForm(NetBoxModelFilterSetForm): class CabledFilterForm(forms.Form): cabled = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) occupied = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -1033,7 +1051,7 @@ class CabledFilterForm(forms.Form): class PathEndpointFilterForm(CabledFilterForm): connected = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -1047,11 +1065,11 @@ class ConsolePortFilterForm(PathEndpointFilterForm, DeviceComponentFilterForm): ('Device', ('region_id', 'site_group_id', 'site_id', 'location_id', 'rack_id', 'virtual_chassis_id', 'device_id')), ('Connection', ('cabled', 'connected', 'occupied')), ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=ConsolePortTypeChoices, required=False ) - speed = MultipleChoiceField( + speed = forms.MultipleChoiceField( choices=ConsolePortSpeedChoices, required=False ) @@ -1066,11 +1084,11 @@ class ConsoleServerPortFilterForm(PathEndpointFilterForm, DeviceComponentFilterF ('Device', ('region_id', 'site_group_id', 'site_id', 'location_id', 'rack_id', 'virtual_chassis_id', 'device_id')), ('Connection', ('cabled', 'connected', 'occupied')), ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=ConsolePortTypeChoices, required=False ) - speed = MultipleChoiceField( + speed = forms.MultipleChoiceField( choices=ConsolePortSpeedChoices, required=False ) @@ -1085,7 +1103,7 @@ class PowerPortFilterForm(PathEndpointFilterForm, DeviceComponentFilterForm): ('Device', ('region_id', 'site_group_id', 'site_id', 'location_id', 'rack_id', 'virtual_chassis_id', 'device_id')), ('Connection', ('cabled', 'connected', 'occupied')), ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=PowerPortTypeChoices, required=False ) @@ -1100,7 +1118,7 @@ class PowerOutletFilterForm(PathEndpointFilterForm, DeviceComponentFilterForm): ('Device', ('region_id', 'site_group_id', 'site_id', 'location_id', 'rack_id', 'virtual_chassis_id', 'device_id')), ('Connection', ('cabled', 'connected', 'occupied')), ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=PowerOutletTypeChoices, required=False ) @@ -1127,11 +1145,11 @@ class InterfaceFilterForm(PathEndpointFilterForm, DeviceComponentFilterForm): }, label=_('Virtual Device Context') ) - kind = MultipleChoiceField( + kind = forms.MultipleChoiceField( choices=InterfaceKindChoices, required=False ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=InterfaceTypeChoices, required=False ) @@ -1140,19 +1158,19 @@ class InterfaceFilterForm(PathEndpointFilterForm, DeviceComponentFilterForm): label='Speed', widget=SelectSpeedWidget() ) - duplex = MultipleChoiceField( + duplex = forms.MultipleChoiceField( choices=InterfaceDuplexChoices, required=False ) enabled = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) mgmt_only = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -1164,22 +1182,22 @@ class InterfaceFilterForm(PathEndpointFilterForm, DeviceComponentFilterForm): required=False, label='WWN' ) - poe_mode = MultipleChoiceField( + poe_mode = forms.MultipleChoiceField( choices=InterfacePoEModeChoices, required=False, label='PoE mode' ) - poe_type = MultipleChoiceField( - choices=InterfacePoEModeChoices, + poe_type = forms.MultipleChoiceField( + choices=InterfacePoETypeChoices, required=False, label='PoE type' ) - rf_role = MultipleChoiceField( + rf_role = forms.MultipleChoiceField( choices=WirelessRoleChoices, required=False, label='Wireless role' ) - rf_channel = MultipleChoiceField( + rf_channel = forms.MultipleChoiceField( choices=WirelessChannelChoices, required=False, label='Wireless channel' @@ -1219,7 +1237,7 @@ class FrontPortFilterForm(CabledFilterForm, DeviceComponentFilterForm): ('Cable', ('cabled', 'occupied')), ) model = FrontPort - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=PortTypeChoices, required=False ) @@ -1237,7 +1255,7 @@ class RearPortFilterForm(CabledFilterForm, DeviceComponentFilterForm): ('Device', ('region_id', 'site_group_id', 'site_id', 'location_id', 'rack_id', 'virtual_chassis_id', 'device_id')), ('Cable', ('cabled', 'occupied')), ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=PortTypeChoices, required=False ) @@ -1296,7 +1314,7 @@ class InventoryItemFilterForm(DeviceComponentFilterForm): ) discovered = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) diff --git a/netbox/dcim/forms/model_forms.py b/netbox/dcim/forms/model_forms.py index 703a7a6b4..34f91bbe8 100644 --- a/netbox/dcim/forms/model_forms.py +++ b/netbox/dcim/forms/model_forms.py @@ -7,13 +7,14 @@ from timezone_field import TimeZoneFormField from dcim.choices import * from dcim.constants import * from dcim.models import * +from extras.models import ConfigTemplate from ipam.models import ASN, IPAddress, VLAN, VLANGroup, VRF from netbox.forms import NetBoxModelForm from tenancy.forms import TenancyForm from utilities.forms import ( APISelect, add_blank_choice, BootstrapMixin, ClearableFileInput, CommentField, ContentTypeChoiceField, - DynamicModelChoiceField, DynamicModelMultipleChoiceField, JSONField, NumericArrayField, SelectWithPK, SmallTextarea, - SlugField, StaticSelect, SelectSpeedWidget, + DynamicModelChoiceField, DynamicModelMultipleChoiceField, JSONField, NumericArrayField, SelectWithPK, + SlugField, SelectSpeedWidget, ) from virtualization.models import Cluster, ClusterGroup from wireless.models import WirelessLAN, WirelessLANGroup @@ -129,8 +130,7 @@ class SiteForm(TenancyForm, NetBoxModelForm): slug = SlugField() time_zone = TimeZoneFormField( choices=add_blank_choice(TimeZoneFormField().choices), - required=False, - widget=StaticSelect() + required=False ) comments = CommentField() @@ -149,18 +149,16 @@ class SiteForm(TenancyForm, NetBoxModelForm): 'description', 'physical_address', 'shipping_address', 'latitude', 'longitude', 'comments', 'tags', ) widgets = { - 'physical_address': SmallTextarea( + 'physical_address': forms.Textarea( attrs={ 'rows': 3, } ), - 'shipping_address': SmallTextarea( + 'shipping_address': forms.Textarea( attrs={ 'rows': 3, } ), - 'status': StaticSelect(), - 'time_zone': StaticSelect(), } help_texts = { 'name': _("Full name of the site"), @@ -218,9 +216,6 @@ class LocationForm(TenancyForm, NetBoxModelForm): 'region', 'site_group', 'site', 'parent', 'name', 'slug', 'status', 'description', 'tenant_group', 'tenant', 'tags', ) - widgets = { - 'status': StaticSelect(), - } class RackRoleForm(NetBoxModelForm): @@ -287,13 +282,6 @@ class RackForm(TenancyForm, NetBoxModelForm): 'facility_id': _("The unique rack ID assigned by the facility"), 'u_height': _("Height in rack units"), } - widgets = { - 'status': StaticSelect(), - 'type': StaticSelect(), - 'width': StaticSelect(), - 'outer_unit': StaticSelect(), - 'weight_unit': StaticSelect(), - } class RackReservationForm(TenancyForm, NetBoxModelForm): @@ -340,8 +328,7 @@ class RackReservationForm(TenancyForm, NetBoxModelForm): user = forms.ModelChoiceField( queryset=User.objects.order_by( 'username' - ), - widget=StaticSelect() + ) ) comments = CommentField() @@ -378,13 +365,17 @@ class DeviceTypeForm(NetBoxModelForm): manufacturer = DynamicModelChoiceField( queryset=Manufacturer.objects.all() ) + default_platform = DynamicModelChoiceField( + queryset=Platform.objects.all(), + required=False + ) slug = SlugField( slug_source='model' ) comments = CommentField() fieldsets = ( - ('Device Type', ('manufacturer', 'model', 'slug', 'description', 'tags')), + ('Device Type', ('manufacturer', 'model', 'slug', 'description', 'tags', 'default_platform')), ('Chassis', ( 'u_height', 'is_full_depth', 'part_number', 'subdevice_role', 'airflow', 'weight', 'weight_unit', )), @@ -395,18 +386,15 @@ class DeviceTypeForm(NetBoxModelForm): model = DeviceType fields = [ 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', - 'weight', 'weight_unit', 'front_image', 'rear_image', 'description', 'comments', 'tags', + 'weight', 'weight_unit', 'front_image', 'rear_image', 'description', 'comments', 'tags', 'default_platform' ] widgets = { - 'airflow': StaticSelect(), - 'subdevice_role': StaticSelect(), 'front_image': ClearableFileInput(attrs={ 'accept': DEVICETYPE_IMAGE_FORMATS }), 'rear_image': ClearableFileInput(attrs={ 'accept': DEVICETYPE_IMAGE_FORMATS }), - 'weight_unit': StaticSelect(), } @@ -427,24 +415,24 @@ class ModuleTypeForm(NetBoxModelForm): 'manufacturer', 'model', 'part_number', 'weight', 'weight_unit', 'description', 'comments', 'tags', ] - widgets = { - 'weight_unit': StaticSelect(), - } - class DeviceRoleForm(NetBoxModelForm): + config_template = DynamicModelChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False + ) slug = SlugField() fieldsets = ( ('Device Role', ( - 'name', 'slug', 'color', 'vm_role', 'description', 'tags', + 'name', 'slug', 'color', 'vm_role', 'config_template', 'description', 'tags', )), ) class Meta: model = DeviceRole fields = [ - 'name', 'slug', 'color', 'vm_role', 'description', 'tags', + 'name', 'slug', 'color', 'vm_role', 'config_template', 'description', 'tags', ] @@ -453,13 +441,17 @@ class PlatformForm(NetBoxModelForm): queryset=Manufacturer.objects.all(), required=False ) + config_template = DynamicModelChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False + ) slug = SlugField( max_length=64 ) fieldsets = ( ('Platform', ( - 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags', + 'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args', 'description', 'tags', )), ) @@ -467,10 +459,10 @@ class PlatformForm(NetBoxModelForm): class Meta: model = Platform fields = [ - 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags', + 'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args', 'description', 'tags', ] widgets = { - 'napalm_args': SmallTextarea(), + 'napalm_args': forms.Textarea(), } @@ -582,6 +574,10 @@ class DeviceForm(TenancyForm, NetBoxModelForm): label=_('Priority'), help_text=_("The priority of the device in the virtual chassis") ) + config_template = DynamicModelChoiceField( + queryset=ConfigTemplate.objects.all(), + required=False + ) class Meta: model = Device @@ -589,7 +585,7 @@ class DeviceForm(TenancyForm, NetBoxModelForm): 'name', 'device_role', 'device_type', 'serial', 'asset_tag', 'region', 'site_group', 'site', 'rack', 'location', 'position', 'face', 'status', 'airflow', 'platform', 'primary_ip4', 'primary_ip6', 'cluster_group', 'cluster', 'tenant_group', 'tenant', 'virtual_chassis', 'vc_position', 'vc_priority', - 'description', 'comments', 'tags', 'local_context_data' + 'description', 'config_template', 'comments', 'tags', 'local_context_data' ] help_texts = { 'device_role': _("The function this device serves"), @@ -597,13 +593,6 @@ class DeviceForm(TenancyForm, NetBoxModelForm): 'local_context_data': _("Local config context data overwrites all source contexts in the final rendered " "config context"), } - widgets = { - 'face': StaticSelect(), - 'status': StaticSelect(), - 'airflow': StaticSelect(), - 'primary_ip4': StaticSelect(), - 'primary_ip6': StaticSelect(), - } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -737,11 +726,6 @@ class CableForm(TenancyForm, NetBoxModelForm): 'type', 'status', 'tenant_group', 'tenant', 'label', 'color', 'length', 'length_unit', 'description', 'comments', 'tags', ] - widgets = { - 'status': StaticSelect, - 'type': StaticSelect, - 'length_unit': StaticSelect, - } error_messages = { 'length': { 'max_value': 'Maximum length is 32767 (any unit)' @@ -856,12 +840,6 @@ class PowerFeedForm(NetBoxModelForm): 'mark_connected', 'supply', 'phase', 'voltage', 'amperage', 'max_utilization', 'description', 'comments', 'tags', ] - widgets = { - 'status': StaticSelect(), - 'type': StaticSelect(), - 'supply': StaticSelect(), - 'phase': StaticSelect(), - } # @@ -1025,9 +1003,6 @@ class ConsolePortTemplateForm(ModularComponentTemplateForm): fields = [ 'device_type', 'module_type', 'name', 'label', 'type', 'description', ] - widgets = { - 'type': StaticSelect, - } class ConsoleServerPortTemplateForm(ModularComponentTemplateForm): @@ -1040,9 +1015,6 @@ class ConsoleServerPortTemplateForm(ModularComponentTemplateForm): fields = [ 'device_type', 'module_type', 'name', 'label', 'type', 'description', ] - widgets = { - 'type': StaticSelect, - } class PowerPortTemplateForm(ModularComponentTemplateForm): @@ -1057,9 +1029,6 @@ class PowerPortTemplateForm(ModularComponentTemplateForm): fields = [ 'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description', ] - widgets = { - 'type': StaticSelect(), - } class PowerOutletTemplateForm(ModularComponentTemplateForm): @@ -1080,10 +1049,6 @@ class PowerOutletTemplateForm(ModularComponentTemplateForm): fields = [ 'device_type', 'module_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description', ] - widgets = { - 'type': StaticSelect(), - 'feed_leg': StaticSelect(), - } class InterfaceTemplateForm(ModularComponentTemplateForm): @@ -1097,11 +1062,6 @@ class InterfaceTemplateForm(ModularComponentTemplateForm): fields = [ 'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'enabled', 'description', 'poe_mode', 'poe_type', ] - widgets = { - 'type': StaticSelect(), - 'poe_mode': StaticSelect(), - 'poe_type': StaticSelect(), - } class FrontPortTemplateForm(ModularComponentTemplateForm): @@ -1127,9 +1087,6 @@ class FrontPortTemplateForm(ModularComponentTemplateForm): 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'description', ] - widgets = { - 'type': StaticSelect(), - } class RearPortTemplateForm(ModularComponentTemplateForm): @@ -1142,9 +1099,6 @@ class RearPortTemplateForm(ModularComponentTemplateForm): fields = [ 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'positions', 'description', ] - widgets = { - 'type': StaticSelect(), - } class ModuleBayTemplateForm(ComponentTemplateForm): @@ -1252,10 +1206,6 @@ class ConsolePortForm(ModularDeviceComponentForm): fields = [ 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags', ] - widgets = { - 'type': StaticSelect(), - 'speed': StaticSelect(), - } class ConsoleServerPortForm(ModularDeviceComponentForm): @@ -1271,10 +1221,6 @@ class ConsoleServerPortForm(ModularDeviceComponentForm): fields = [ 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags', ] - widgets = { - 'type': StaticSelect(), - 'speed': StaticSelect(), - } class PowerPortForm(ModularDeviceComponentForm): @@ -1292,9 +1238,6 @@ class PowerPortForm(ModularDeviceComponentForm): 'device', 'module', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected', 'description', 'tags', ] - widgets = { - 'type': StaticSelect(), - } class PowerOutletForm(ModularDeviceComponentForm): @@ -1319,10 +1262,6 @@ class PowerOutletForm(ModularDeviceComponentForm): 'device', 'module', 'name', 'label', 'type', 'power_port', 'feed_leg', 'mark_connected', 'description', 'tags', ] - widgets = { - 'type': StaticSelect(), - 'feed_leg': StaticSelect(), - } class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm): @@ -1427,14 +1366,14 @@ class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm): 'untagged_vlan', 'tagged_vlans', 'vrf', 'tags', ] widgets = { - 'type': StaticSelect(), 'speed': SelectSpeedWidget(), - 'poe_mode': StaticSelect(), - 'poe_type': StaticSelect(), - 'duplex': StaticSelect(), - 'mode': StaticSelect(), - 'rf_role': StaticSelect(), - 'rf_channel': StaticSelect(), + 'mode': forms.Select( + attrs={ + 'hx-get': '.', + 'hx-include': '#form_fields input', + 'hx-target': '#form_fields', + } + ), } labels = { 'mode': '802.1Q Mode', @@ -1467,9 +1406,6 @@ class FrontPortForm(ModularDeviceComponentForm): 'device', 'module', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'mark_connected', 'description', 'tags', ] - widgets = { - 'type': StaticSelect(), - } class RearPortForm(ModularDeviceComponentForm): @@ -1484,9 +1420,6 @@ class RearPortForm(ModularDeviceComponentForm): fields = [ 'device', 'module', 'name', 'label', 'type', 'color', 'positions', 'mark_connected', 'description', 'tags', ] - widgets = { - 'type': StaticSelect(), - } class ModuleBayForm(DeviceComponentForm): @@ -1517,8 +1450,7 @@ class PopulateDeviceBayForm(BootstrapMixin, forms.Form): installed_device = forms.ModelChoiceField( queryset=Device.objects.all(), label=_('Child Device'), - help_text=_("Child devices must first be created and assigned to the site/rack of the parent device."), - widget=StaticSelect(), + help_text=_("Child devices must first be created and assigned to the site/rack of the parent device.") ) def __init__(self, device_bay, *args, **kwargs): @@ -1767,8 +1699,3 @@ class VirtualDeviceContextForm(TenancyForm, NetBoxModelForm): 'region', 'site_group', 'site', 'location', 'rack', 'device', 'name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tenant_group', 'tenant', 'comments', 'tags' ] - widgets = { - 'status': StaticSelect(), - 'primary_ip4': StaticSelect(), - 'primary_ip6': StaticSelect(), - } diff --git a/netbox/dcim/migrations/0169_devicetype_default_platform.py b/netbox/dcim/migrations/0169_devicetype_default_platform.py new file mode 100644 index 000000000..a143f2c62 --- /dev/null +++ b/netbox/dcim/migrations/0169_devicetype_default_platform.py @@ -0,0 +1,19 @@ +# Generated by Django 4.1.6 on 2023-02-10 18:06 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('dcim', '0168_interface_template_enabled'), + ] + + operations = [ + migrations.AddField( + model_name='devicetype', + name='default_platform', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.platform'), + ), + ] diff --git a/netbox/dcim/migrations/0170_configtemplate.py b/netbox/dcim/migrations/0170_configtemplate.py new file mode 100644 index 000000000..b1aac0ad2 --- /dev/null +++ b/netbox/dcim/migrations/0170_configtemplate.py @@ -0,0 +1,28 @@ +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('extras', '0086_configtemplate'), + ('dcim', '0169_devicetype_default_platform'), + ] + + operations = [ + migrations.AddField( + model_name='device', + name='config_template', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='devices', to='extras.configtemplate'), + ), + migrations.AddField( + model_name='devicerole', + name='config_template', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='device_roles', to='extras.configtemplate'), + ), + migrations.AddField( + model_name='platform', + name='config_template', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='platforms', to='extras.configtemplate'), + ), + ] diff --git a/netbox/dcim/models/device_component_templates.py b/netbox/dcim/models/device_component_templates.py index 3d2d32509..be17627fb 100644 --- a/netbox/dcim/models/device_component_templates.py +++ b/netbox/dcim/models/device_component_templates.py @@ -9,7 +9,6 @@ from mptt.models import MPTTModel, TreeForeignKey from dcim.choices import * from dcim.constants import * from netbox.models import ChangeLoggedModel -from netbox.models.features import WebhooksMixin from utilities.fields import ColorField, NaturalOrderingField from utilities.mptt import TreeManager from utilities.ordering import naturalize_interface @@ -33,7 +32,7 @@ __all__ = ( ) -class ComponentTemplateModel(WebhooksMixin, ChangeLoggedModel): +class ComponentTemplateModel(ChangeLoggedModel): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.CASCADE, diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 603129228..7ce1a2388 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -82,6 +82,14 @@ class DeviceType(PrimaryModel, WeightMixin): slug = models.SlugField( max_length=100 ) + default_platform = models.ForeignKey( + to='dcim.Platform', + on_delete=models.SET_NULL, + related_name='+', + blank=True, + null=True, + verbose_name='Default platform' + ) part_number = models.CharField( max_length=50, blank=True, @@ -121,7 +129,7 @@ class DeviceType(PrimaryModel, WeightMixin): ) clone_fields = ( - 'manufacturer', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', 'weight', 'weight_unit' + 'manufacturer', 'default_platform', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', 'weight', 'weight_unit' ) prerequisite_models = ( 'dcim.Manufacturer', @@ -165,6 +173,7 @@ class DeviceType(PrimaryModel, WeightMixin): 'manufacturer': self.manufacturer.name, 'model': self.model, 'slug': self.slug, + 'default_platform': self.default_platform.name if self.default_platform else None, 'part_number': self.part_number, 'u_height': float(self.u_height), 'is_full_depth': self.is_full_depth, @@ -401,6 +410,13 @@ class DeviceRole(OrganizationalModel): verbose_name='VM Role', help_text=_('Virtual machines may be assigned to this role') ) + config_template = models.ForeignKey( + to='extras.ConfigTemplate', + on_delete=models.PROTECT, + related_name='device_roles', + blank=True, + null=True + ) def get_absolute_url(self): return reverse('dcim:devicerole', args=[self.pk]) @@ -420,6 +436,13 @@ class Platform(OrganizationalModel): null=True, help_text=_('Optionally limit this platform to devices of a certain manufacturer') ) + config_template = models.ForeignKey( + to='extras.ConfigTemplate', + on_delete=models.PROTECT, + related_name='platforms', + blank=True, + null=True + ) napalm_driver = models.CharField( max_length=50, blank=True, @@ -581,6 +604,13 @@ class Device(PrimaryModel, ConfigContextModel): null=True, validators=[MaxValueValidator(255)] ) + config_template = models.ForeignKey( + to='extras.ConfigTemplate', + on_delete=models.PROTECT, + related_name='devices', + blank=True, + null=True + ) # Generic relations contacts = GenericRelation( @@ -801,6 +831,10 @@ class Device(PrimaryModel, ConfigContextModel): if is_new and not self.airflow: self.airflow = self.device_type.airflow + # Inherit default_platform from DeviceType if not set + if is_new and not self.platform: + self.platform = self.device_type.default_platform + super().save(*args, **kwargs) # If this is a new Device, instantiate all the related components per the DeviceType definition @@ -849,6 +883,17 @@ class Device(PrimaryModel, ConfigContextModel): def interfaces_count(self): return self.vc_interfaces().count() + def get_config_template(self): + """ + Return the appropriate ConfigTemplate (if any) for this Device. + """ + if self.config_template: + return self.config_template + if self.device_role.config_template: + return self.device_role.config_template + if self.platform and self.platform.config_template: + return self.platform.config_template + def get_vc_master(self): """ If this Device is a VirtualChassis member, return the VC master. Otherwise, return None. diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py index 904e96b83..f68960965 100644 --- a/netbox/dcim/tables/devices.py +++ b/netbox/dcim/tables/devices.py @@ -86,6 +86,9 @@ class DeviceRoleTable(NetBoxTable): ) color = columns.ColorColumn() vm_role = columns.BooleanColumn() + config_template = tables.Column( + linkify=True + ) tags = columns.TagColumn( url_name='dcim:devicerole_list' ) @@ -93,8 +96,8 @@ class DeviceRoleTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = models.DeviceRole fields = ( - 'pk', 'id', 'name', 'device_count', 'vm_count', 'color', 'vm_role', 'description', 'slug', 'tags', - 'actions', 'created', 'last_updated', + 'pk', 'id', 'name', 'device_count', 'vm_count', 'color', 'vm_role', 'config_template', 'description', + 'slug', 'tags', 'actions', 'created', 'last_updated', ) default_columns = ('pk', 'name', 'device_count', 'vm_count', 'color', 'vm_role', 'description') @@ -110,6 +113,9 @@ class PlatformTable(NetBoxTable): manufacturer = tables.Column( linkify=True ) + config_template = tables.Column( + linkify=True + ) device_count = columns.LinkedCountColumn( viewname='dcim:device_list', url_params={'platform_id': 'pk'}, @@ -127,8 +133,8 @@ class PlatformTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = models.Platform fields = ( - 'pk', 'id', 'name', 'manufacturer', 'device_count', 'vm_count', 'slug', 'napalm_driver', 'napalm_args', - 'description', 'tags', 'actions', 'created', 'last_updated', + 'pk', 'id', 'name', 'manufacturer', 'device_count', 'vm_count', 'slug', 'config_template', 'napalm_driver', + 'napalm_args', 'description', 'tags', 'actions', 'created', 'last_updated', ) default_columns = ( 'pk', 'name', 'manufacturer', 'device_count', 'vm_count', 'napalm_driver', 'description', @@ -203,6 +209,9 @@ class DeviceTable(TenancyColumnsMixin, ContactsColumnMixin, NetBoxTable): vc_priority = tables.Column( verbose_name='VC Priority' ) + config_template = tables.Column( + linkify=True + ) comments = columns.MarkdownColumn() tags = columns.TagColumn( url_name='dcim:device_list' @@ -214,7 +223,7 @@ class DeviceTable(TenancyColumnsMixin, ContactsColumnMixin, NetBoxTable): 'pk', 'id', 'name', 'status', 'tenant', 'tenant_group', 'device_role', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'region', 'site_group', 'site', 'location', 'rack', 'position', 'face', 'airflow', 'primary_ip', 'primary_ip4', 'primary_ip6', 'cluster', 'virtual_chassis', 'vc_position', - 'vc_priority', 'description', 'comments', 'contacts', 'tags', 'created', 'last_updated', + 'vc_priority', 'description', 'config_template', 'comments', 'contacts', 'tags', 'created', 'last_updated', ) default_columns = ( 'pk', 'name', 'status', 'tenant', 'site', 'location', 'rack', 'device_role', 'manufacturer', 'device_type', diff --git a/netbox/dcim/tables/devicetypes.py b/netbox/dcim/tables/devicetypes.py index dff697588..91a37fab3 100644 --- a/netbox/dcim/tables/devicetypes.py +++ b/netbox/dcim/tables/devicetypes.py @@ -77,6 +77,9 @@ class DeviceTypeTable(NetBoxTable): manufacturer = tables.Column( linkify=True ) + default_platform = tables.Column( + linkify=True + ) is_full_depth = columns.BooleanColumn( verbose_name='Full Depth' ) @@ -100,7 +103,7 @@ class DeviceTypeTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = models.DeviceType fields = ( - 'pk', 'id', 'model', 'manufacturer', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role', + 'pk', 'id', 'model', 'manufacturer', 'default_platform', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', 'weight', 'description', 'comments', 'instance_count', 'tags', 'created', 'last_updated', ) default_columns = ( diff --git a/netbox/dcim/tests/test_filtersets.py b/netbox/dcim/tests/test_filtersets.py index 45d5797bd..c78b592d3 100644 --- a/netbox/dcim/tests/test_filtersets.py +++ b/netbox/dcim/tests/test_filtersets.py @@ -699,9 +699,16 @@ class DeviceTypeTestCase(TestCase, ChangeLoggedFilterSetTests): ) Manufacturer.objects.bulk_create(manufacturers) + platforms = ( + Platform(name='Platform 1', slug='platform-1', manufacturer=manufacturers[0]), + Platform(name='Platform 2', slug='platform-2', manufacturer=manufacturers[1]), + Platform(name='Platform 3', slug='platform-3', manufacturer=manufacturers[2]), + ) + Platform.objects.bulk_create(platforms) + device_types = ( - DeviceType(manufacturer=manufacturers[0], model='Model 1', slug='model-1', part_number='Part Number 1', u_height=1, is_full_depth=True, front_image='front.png', rear_image='rear.png', weight=10, weight_unit=WeightUnitChoices.UNIT_POUND), - DeviceType(manufacturer=manufacturers[1], model='Model 2', slug='model-2', part_number='Part Number 2', u_height=2, is_full_depth=True, subdevice_role=SubdeviceRoleChoices.ROLE_PARENT, airflow=DeviceAirflowChoices.AIRFLOW_FRONT_TO_REAR, weight=20, weight_unit=WeightUnitChoices.UNIT_POUND), + DeviceType(manufacturer=manufacturers[0], default_platform=platforms[0], model='Model 1', slug='model-1', part_number='Part Number 1', u_height=1, is_full_depth=True, front_image='front.png', rear_image='rear.png', weight=10, weight_unit=WeightUnitChoices.UNIT_POUND), + DeviceType(manufacturer=manufacturers[1], default_platform=platforms[1], model='Model 2', slug='model-2', part_number='Part Number 2', u_height=2, is_full_depth=True, subdevice_role=SubdeviceRoleChoices.ROLE_PARENT, airflow=DeviceAirflowChoices.AIRFLOW_FRONT_TO_REAR, weight=20, weight_unit=WeightUnitChoices.UNIT_POUND), DeviceType(manufacturer=manufacturers[2], model='Model 3', slug='model-3', part_number='Part Number 3', u_height=3, is_full_depth=False, subdevice_role=SubdeviceRoleChoices.ROLE_CHILD, airflow=DeviceAirflowChoices.AIRFLOW_REAR_TO_FRONT, weight=30, weight_unit=WeightUnitChoices.UNIT_KILOGRAM), ) DeviceType.objects.bulk_create(device_types) @@ -785,6 +792,13 @@ class DeviceTypeTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'manufacturer': [manufacturers[0].slug, manufacturers[1].slug]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_default_platform(self): + platforms = Platform.objects.all()[:2] + params = {'default_platform_id': [platforms[0].pk, platforms[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'default_platform': [platforms[0].slug, platforms[1].slug]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_has_front_image(self): params = {'has_front_image': True} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) diff --git a/netbox/dcim/tests/test_views.py b/netbox/dcim/tests/test_views.py index 6ea935bc8..bba91412d 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -503,6 +503,12 @@ class DeviceTypeTestCase( ) Manufacturer.objects.bulk_create(manufacturers) + platforms = ( + Platform(name='Platform 1', slug='platform-1', manufacturer=manufacturers[0]), + Platform(name='Platform 2', slug='platform-3', manufacturer=manufacturers[1]), + ) + Platform.objects.bulk_create(platforms) + DeviceType.objects.bulk_create([ DeviceType(model='Device Type 1', slug='device-type-1', manufacturer=manufacturers[0]), DeviceType(model='Device Type 2', slug='device-type-2', manufacturer=manufacturers[0]), @@ -513,6 +519,7 @@ class DeviceTypeTestCase( cls.form_data = { 'manufacturer': manufacturers[1].pk, + 'default_platform': platforms[0].pk, 'model': 'Device Type X', 'slug': 'device-type-x', 'part_number': '123ABC', @@ -525,6 +532,7 @@ class DeviceTypeTestCase( cls.bulk_edit_data = { 'manufacturer': manufacturers[1].pk, + 'default_platform': platforms[1].pk, 'u_height': 3, 'is_full_depth': False, } @@ -673,6 +681,7 @@ class DeviceTypeTestCase( """ IMPORT_DATA = """ manufacturer: Generic +default_platform: Platform model: TEST-1000 slug: test-1000 u_height: 2 @@ -755,8 +764,11 @@ inventory-items: manufacturer: Generic """ - # Create the manufacturer - Manufacturer(name='Generic', slug='generic').save() + # Create the manufacturer and platform + manufacturer = Manufacturer(name='Generic', slug='generic') + manufacturer.save() + platform = Platform(name='Platform', slug='test-platform', manufacturer=manufacturer) + platform.save() # Add all required permissions to the test user self.add_permissions( @@ -783,6 +795,7 @@ inventory-items: device_type = DeviceType.objects.get(model='TEST-1000') self.assertEqual(device_type.comments, 'Test comment') + self.assertEqual(device_type.default_platform.pk, platform.pk) # Verify all of the components were created self.assertEqual(device_type.consoleporttemplates.count(), 3) diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index d50aec73a..093cb90c5 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -1,3 +1,5 @@ +import traceback + from django.contrib import messages from django.contrib.contenttypes.models import ContentType from django.core.paginator import EmptyPage, PageNotAnInteger @@ -10,6 +12,7 @@ from django.utils.html import escape from django.utils.safestring import mark_safe from django.utils.translation import gettext as _ from django.views.generic import View +from jinja2.exceptions import TemplateError from circuits.models import Circuit, CircuitTermination from extras.views import ObjectConfigContextView @@ -595,6 +598,7 @@ class RackListView(generic.ObjectListView): filterset = filtersets.RackFilterSet filterset_form = forms.RackFilterForm table = tables.RackTable + template_name = 'dcim/rack_list.html' class RackElevationListView(generic.ObjectListView): @@ -1996,6 +2000,39 @@ class DeviceInventoryView(DeviceComponentsView): ) +@register_model_view(Device, 'render-config') +class DeviceRenderConfigView(generic.ObjectView): + queryset = Device.objects.all() + template_name = 'dcim/device/render_config.html' + tab = ViewTab( + label=_('Render Config'), + permission='extras.view_configtemplate', + weight=2000 + ) + + def get_extra_context(self, request, instance): + # Compile context data + context_data = { + 'device': instance, + } + context_data.update(**instance.get_config_context()) + + # Render the config template + rendered_config = None + if config_template := instance.get_config_template(): + try: + rendered_config = config_template.render(context=context_data) + except TemplateError as e: + messages.error(request, f"An error occurred while rendering the template: {e}") + rendered_config = traceback.format_exc() + + return { + 'config_template': config_template, + 'context_data': context_data, + 'rendered_config': rendered_config, + } + + @register_model_view(Device, 'configcontext', path='config-context') class DeviceConfigContextView(ObjectConfigContextView): queryset = Device.objects.annotate_config_context_data() @@ -2003,7 +2040,7 @@ class DeviceConfigContextView(ObjectConfigContextView): tab = ViewTab( label=_('Config Context'), permission='extras.view_configcontext', - weight=2000 + weight=2100 ) diff --git a/netbox/extras/api/nested_serializers.py b/netbox/extras/api/nested_serializers.py index 5644b0b4e..dab0798fe 100644 --- a/netbox/extras/api/nested_serializers.py +++ b/netbox/extras/api/nested_serializers.py @@ -7,6 +7,7 @@ from users.api.nested_serializers import NestedUserSerializer __all__ = [ 'NestedConfigContextSerializer', + 'NestedConfigTemplateSerializer', 'NestedCustomFieldSerializer', 'NestedCustomLinkSerializer', 'NestedExportTemplateSerializer', @@ -51,6 +52,14 @@ class NestedConfigContextSerializer(WritableNestedSerializer): fields = ['id', 'url', 'display', 'name'] +class NestedConfigTemplateSerializer(WritableNestedSerializer): + url = serializers.HyperlinkedIdentityField(view_name='extras-api:configtemplate-detail') + + class Meta: + model = models.ConfigTemplate + fields = ['id', 'url', 'display', 'name'] + + class NestedExportTemplateSerializer(WritableNestedSerializer): url = serializers.HyperlinkedIdentityField(view_name='extras-api:exporttemplate-detail') diff --git a/netbox/extras/api/serializers.py b/netbox/extras/api/serializers.py index 5e66ffee3..1cbd8333a 100644 --- a/netbox/extras/api/serializers.py +++ b/netbox/extras/api/serializers.py @@ -3,6 +3,7 @@ from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist from rest_framework import serializers +from core.api.nested_serializers import NestedDataSourceSerializer, NestedDataFileSerializer from dcim.api.nested_serializers import ( NestedDeviceRoleSerializer, NestedDeviceTypeSerializer, NestedLocationSerializer, NestedPlatformSerializer, NestedRegionSerializer, NestedSiteSerializer, NestedSiteGroupSerializer, @@ -16,6 +17,7 @@ from extras.utils import FeatureQuery from netbox.api.exceptions import SerializerNotFound from netbox.api.fields import ChoiceField, ContentTypeField, SerializedPKRelatedField from netbox.api.serializers import BaseModelSerializer, NetBoxModelSerializer, ValidatedModelSerializer +from netbox.api.serializers.features import TaggableModelSerializer from netbox.constants import NESTED_SERIALIZER_PREFIX from tenancy.api.nested_serializers import NestedTenantSerializer, NestedTenantGroupSerializer from tenancy.models import Tenant, TenantGroup @@ -29,6 +31,7 @@ from .nested_serializers import * __all__ = ( 'ConfigContextSerializer', + 'ConfigTemplateSerializer', 'ContentTypeSerializer', 'CustomFieldSerializer', 'CustomLinkSerializer', @@ -143,12 +146,19 @@ class ExportTemplateSerializer(ValidatedModelSerializer): queryset=ContentType.objects.filter(FeatureQuery('export_templates').get_query()), many=True ) + data_source = NestedDataSourceSerializer( + required=False + ) + data_file = NestedDataFileSerializer( + read_only=True + ) class Meta: model = ExportTemplate fields = [ 'id', 'url', 'display', 'content_types', 'name', 'description', 'template_code', 'mime_type', - 'file_extension', 'as_attachment', 'created', 'last_updated', + 'file_extension', 'as_attachment', 'data_source', 'data_path', 'data_file', 'data_synced', 'created', + 'last_updated', ] @@ -360,13 +370,41 @@ class ConfigContextSerializer(ValidatedModelSerializer): required=False, many=True ) + data_source = NestedDataSourceSerializer( + required=False + ) + data_file = NestedDataFileSerializer( + read_only=True + ) class Meta: model = ConfigContext fields = [ 'id', 'url', 'display', 'name', 'weight', 'description', 'is_active', 'regions', 'site_groups', 'sites', 'locations', 'device_types', 'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', - 'tenant_groups', 'tenants', 'tags', 'data', 'created', 'last_updated', + 'tenant_groups', 'tenants', 'tags', 'data_source', 'data_path', 'data_file', 'data_synced', 'data', + 'created', 'last_updated', + ] + + +# +# Config templates +# + +class ConfigTemplateSerializer(TaggableModelSerializer, ValidatedModelSerializer): + url = serializers.HyperlinkedIdentityField(view_name='extras-api:configtemplate-detail') + data_source = NestedDataSourceSerializer( + required=False + ) + data_file = NestedDataFileSerializer( + read_only=True + ) + + class Meta: + model = ConfigTemplate + fields = [ + 'id', 'url', 'display', 'name', 'description', 'environment_params', 'template_code', 'data_source', + 'data_path', 'data_file', 'data_synced', 'tags', 'created', 'last_updated', ] diff --git a/netbox/extras/api/urls.py b/netbox/extras/api/urls.py index 91067d40d..f01cdcd00 100644 --- a/netbox/extras/api/urls.py +++ b/netbox/extras/api/urls.py @@ -14,6 +14,7 @@ router.register('tags', views.TagViewSet) router.register('image-attachments', views.ImageAttachmentViewSet) router.register('journal-entries', views.JournalEntryViewSet) router.register('config-contexts', views.ConfigContextViewSet) +router.register('config-templates', views.ConfigTemplateViewSet) router.register('reports', views.ReportViewSet, basename='report') router.register('scripts', views.ScriptViewSet, basename='script') router.register('object-changes', views.ObjectChangeViewSet) diff --git a/netbox/extras/api/views.py b/netbox/extras/api/views.py index edf483482..5d2138123 100644 --- a/netbox/extras/api/views.py +++ b/netbox/extras/api/views.py @@ -5,6 +5,7 @@ from rest_framework import status from rest_framework.decorators import action from rest_framework.exceptions import PermissionDenied from rest_framework.permissions import IsAuthenticated +from rest_framework.renderers import JSONRenderer from rest_framework.response import Response from rest_framework.routers import APIRootView from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet @@ -17,11 +18,14 @@ from extras.models import CustomField from extras.reports import get_report, get_reports, run_report from extras.scripts import get_script, get_scripts, run_script from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired +from netbox.api.features import SyncedDataMixin from netbox.api.metadata import ContentTypeMetadata +from netbox.api.renderers import TextRenderer from netbox.api.viewsets import NetBoxModelViewSet from utilities.exceptions import RQWorkerNotRunningException from utilities.utils import copy_safe_request, count_related from . import serializers +from .nested_serializers import NestedConfigTemplateSerializer class ExtrasRootView(APIRootView): @@ -91,9 +95,9 @@ class CustomLinkViewSet(NetBoxModelViewSet): # Export templates # -class ExportTemplateViewSet(NetBoxModelViewSet): +class ExportTemplateViewSet(SyncedDataMixin, NetBoxModelViewSet): metadata_class = ContentTypeMetadata - queryset = ExportTemplate.objects.all() + queryset = ExportTemplate.objects.prefetch_related('data_source', 'data_file') serializer_class = serializers.ExportTemplateSerializer filterset_class = filtersets.ExportTemplateFilterSet @@ -147,14 +151,44 @@ class JournalEntryViewSet(NetBoxModelViewSet): # Config contexts # -class ConfigContextViewSet(NetBoxModelViewSet): +class ConfigContextViewSet(SyncedDataMixin, NetBoxModelViewSet): queryset = ConfigContext.objects.prefetch_related( - 'regions', 'site_groups', 'sites', 'locations', 'roles', 'platforms', 'tenant_groups', 'tenants', + 'regions', 'site_groups', 'sites', 'locations', 'roles', 'platforms', 'tenant_groups', 'tenants', 'data_source', + 'data_file', ) serializer_class = serializers.ConfigContextSerializer filterset_class = filtersets.ConfigContextFilterSet +# +# Config templates +# + +class ConfigTemplateViewSet(SyncedDataMixin, NetBoxModelViewSet): + queryset = ConfigTemplate.objects.prefetch_related('data_source', 'data_file') + serializer_class = serializers.ConfigTemplateSerializer + filterset_class = filtersets.ConfigTemplateFilterSet + + @action(detail=True, methods=['post'], renderer_classes=[JSONRenderer, TextRenderer]) + def render(self, request, pk): + """ + Render a ConfigTemplate using the context data provided (if any). If the client requests "text/plain" data, + return the raw rendered content, rather than serialized JSON. + """ + configtemplate = self.get_object() + output = configtemplate.render(context=request.data) + + # If the client has requested "text/plain", return the raw content. + if request.accepted_renderer.format == 'txt': + return Response(output) + + template_serializer = NestedConfigTemplateSerializer(configtemplate, context={'request': request}) + return Response({ + 'configtemplate': template_serializer.data, + 'content': output + }) + + # # Reports # diff --git a/netbox/extras/conditions.py b/netbox/extras/conditions.py index 965488c3a..c6744e524 100644 --- a/netbox/extras/conditions.py +++ b/netbox/extras/conditions.py @@ -44,7 +44,8 @@ class Condition: bool: (EQ, CONTAINS), int: (EQ, GT, GTE, LT, LTE, CONTAINS), float: (EQ, GT, GTE, LT, LTE, CONTAINS), - list: (EQ, IN, CONTAINS) + list: (EQ, IN, CONTAINS), + type(None): (EQ,) } def __init__(self, attr, value, op=EQ, negate=False): diff --git a/netbox/extras/constants.py b/netbox/extras/constants.py index 123eb0a45..d65fb9612 100644 --- a/netbox/extras/constants.py +++ b/netbox/extras/constants.py @@ -1,13 +1,2 @@ # Webhook content types HTTP_CONTENT_TYPE_JSON = 'application/json' - -# Registerable extras features -EXTRAS_FEATURES = [ - 'custom_fields', - 'custom_links', - 'export_templates', - 'job_results', - 'journaling', - 'tags', - 'webhooks' -] diff --git a/netbox/extras/fields.py b/netbox/extras/fields.py new file mode 100644 index 000000000..6cd44432f --- /dev/null +++ b/netbox/extras/fields.py @@ -0,0 +1,8 @@ +from django.db.models import TextField + + +class CachedValueField(TextField): + """ + Currently a dummy field to prevent custom lookups being applied globally to TextField. + """ + pass diff --git a/netbox/extras/filtersets.py b/netbox/extras/filtersets.py index 74b98ccf6..816406647 100644 --- a/netbox/extras/filtersets.py +++ b/netbox/extras/filtersets.py @@ -4,17 +4,19 @@ from django.contrib.contenttypes.models import ContentType from django.db.models import Q from django.utils.translation import gettext as _ +from core.models import DataSource from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup from netbox.filtersets import BaseFilterSet, ChangeLoggedModelFilterSet, NetBoxModelFilterSet from tenancy.models import Tenant, TenantGroup from utilities.filters import ContentTypeFilter, MultiValueCharFilter, MultiValueNumberFilter from virtualization.models import Cluster, ClusterGroup, ClusterType from .choices import * +from .filters import TagFilter from .models import * - __all__ = ( 'ConfigContextFilterSet', + 'ConfigTemplateFilterSet', 'ContentTypeFilterSet', 'CustomFieldFilterSet', 'CustomLinkFilterSet', @@ -126,10 +128,18 @@ class ExportTemplateFilterSet(BaseFilterSet): field_name='content_types__id' ) content_types = ContentTypeFilter() + data_source_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data source (ID)'), + ) + data_file_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data file (ID)'), + ) class Meta: model = ExportTemplate - fields = ['id', 'content_types', 'name', 'description'] + fields = ['id', 'content_types', 'name', 'description', 'data_synced'] def search(self, queryset, name, value): if not value.strip(): @@ -422,10 +432,18 @@ class ConfigContextFilterSet(ChangeLoggedModelFilterSet): to_field_name='slug', label=_('Tag (slug)'), ) + data_source_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data source (ID)'), + ) + data_file_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data file (ID)'), + ) class Meta: model = ConfigContext - fields = ['id', 'name', 'is_active'] + fields = ['id', 'name', 'is_active', 'data_synced'] def search(self, queryset, name, value): if not value.strip(): @@ -437,6 +455,34 @@ class ConfigContextFilterSet(ChangeLoggedModelFilterSet): ) +class ConfigTemplateFilterSet(BaseFilterSet): + q = django_filters.CharFilter( + method='search', + label=_('Search'), + ) + data_source_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data source (ID)'), + ) + data_file_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data file (ID)'), + ) + tag = TagFilter() + + class Meta: + model = ConfigTemplate + fields = ['id', 'name', 'description', 'data_synced'] + + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(name__icontains=value) | + Q(description__icontains=value) + ) + + # # Filter for Local Config Context Data # diff --git a/netbox/extras/forms/bulk_edit.py b/netbox/extras/forms/bulk_edit.py index 6e245bcaf..bba585591 100644 --- a/netbox/extras/forms/bulk_edit.py +++ b/netbox/extras/forms/bulk_edit.py @@ -4,11 +4,12 @@ from django.utils.translation import gettext as _ from extras.choices import * from extras.models import * from utilities.forms import ( - add_blank_choice, BulkEditForm, BulkEditNullBooleanSelect, ColorField, StaticSelect, + add_blank_choice, BulkEditForm, BulkEditNullBooleanSelect, ColorField, ) __all__ = ( 'ConfigContextBulkEditForm', + 'ConfigTemplateBulkEditForm', 'CustomFieldBulkEditForm', 'CustomLinkBulkEditForm', 'ExportTemplateBulkEditForm', @@ -41,8 +42,7 @@ class CustomFieldBulkEditForm(BulkEditForm): label=_("UI visibility"), choices=add_blank_choice(CustomFieldVisibilityChoices), required=False, - initial='', - widget=StaticSelect() + initial='' ) nullable_fields = ('group_name', 'description',) @@ -66,8 +66,7 @@ class CustomLinkBulkEditForm(BulkEditForm): ) button_class = forms.ChoiceField( choices=add_blank_choice(CustomLinkButtonClassChoices), - required=False, - widget=StaticSelect() + required=False ) @@ -203,6 +202,19 @@ class ConfigContextBulkEditForm(BulkEditForm): nullable_fields = ('description',) +class ConfigTemplateBulkEditForm(BulkEditForm): + pk = forms.ModelMultipleChoiceField( + queryset=ConfigTemplate.objects.all(), + widget=forms.MultipleHiddenInput + ) + description = forms.CharField( + max_length=200, + required=False + ) + + nullable_fields = ('description',) + + class JournalEntryBulkEditForm(BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=JournalEntry.objects.all(), diff --git a/netbox/extras/forms/bulk_import.py b/netbox/extras/forms/bulk_import.py index cf723c4f7..b035c2579 100644 --- a/netbox/extras/forms/bulk_import.py +++ b/netbox/extras/forms/bulk_import.py @@ -10,6 +10,7 @@ from extras.utils import FeatureQuery from utilities.forms import CSVChoiceField, CSVContentTypeField, CSVModelForm, CSVMultipleContentTypeField, SlugField __all__ = ( + 'ConfigTemplateImportForm', 'CustomFieldImportForm', 'CustomLinkImportForm', 'ExportTemplateImportForm', @@ -83,6 +84,15 @@ class ExportTemplateImportForm(CSVModelForm): ) +class ConfigTemplateImportForm(CSVModelForm): + + class Meta: + model = ConfigTemplate + fields = ( + 'name', 'description', 'environment_params', 'template_code', 'tags', + ) + + class SavedFilterImportForm(CSVModelForm): content_types = CSVMultipleContentTypeField( queryset=ContentType.objects.all(), diff --git a/netbox/extras/forms/filtersets.py b/netbox/extras/forms/filtersets.py index e6a9089bc..114eb1a59 100644 --- a/netbox/extras/forms/filtersets.py +++ b/netbox/extras/forms/filtersets.py @@ -3,6 +3,7 @@ from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.utils.translation import gettext as _ +from core.models import DataFile, DataSource from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup from extras.choices import * from extras.models import * @@ -11,18 +12,19 @@ from netbox.forms.base import NetBoxModelFilterSetForm from tenancy.models import Tenant, TenantGroup from utilities.forms import ( add_blank_choice, APISelectMultiple, BOOLEAN_WITH_BLANK_CHOICES, ContentTypeChoiceField, - ContentTypeMultipleChoiceField, DateTimePicker, DynamicModelMultipleChoiceField, FilterForm, MultipleChoiceField, - StaticSelect, TagFilterField, + ContentTypeMultipleChoiceField, DateTimePicker, DynamicModelMultipleChoiceField, FilterForm, + TagFilterField, ) from virtualization.models import Cluster, ClusterGroup, ClusterType from .mixins import SavedFiltersMixin __all__ = ( 'ConfigContextFilterForm', + 'ConfigTemplateFilterForm', 'CustomFieldFilterForm', - 'JobResultFilterForm', 'CustomLinkFilterForm', 'ExportTemplateFilterForm', + 'JobResultFilterForm', 'JournalEntryFilterForm', 'LocalConfigContextFilterForm', 'ObjectChangeFilterForm', @@ -38,12 +40,11 @@ class CustomFieldFilterForm(SavedFiltersMixin, FilterForm): ('Attributes', ('type', 'content_type_id', 'group_name', 'weight', 'required', 'ui_visibility')), ) content_type_id = ContentTypeMultipleChoiceField( - queryset=ContentType.objects.all(), - limit_choices_to=FeatureQuery('custom_fields'), + queryset=ContentType.objects.filter(FeatureQuery('custom_fields').get_query()), required=False, label=_('Object type') ) - type = MultipleChoiceField( + type = forms.MultipleChoiceField( choices=CustomFieldTypeChoices, required=False, label=_('Field type') @@ -56,15 +57,14 @@ class CustomFieldFilterForm(SavedFiltersMixin, FilterForm): ) required = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) ui_visibility = forms.ChoiceField( choices=add_blank_choice(CustomFieldVisibilityChoices), required=False, - label=_('UI visibility'), - widget=StaticSelect() + label=_('UI visibility') ) @@ -79,11 +79,10 @@ class JobResultFilterForm(SavedFiltersMixin, FilterForm): ) obj_type = ContentTypeChoiceField( label=_('Object Type'), - queryset=ContentType.objects.all(), - limit_choices_to=FeatureQuery('job_results'), # TODO: This doesn't actually work + queryset=ContentType.objects.filter(FeatureQuery('job_results').get_query()), required=False, ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=JobResultStatusChoices, required=False ) @@ -135,19 +134,18 @@ class CustomLinkFilterForm(SavedFiltersMixin, FilterForm): ('Attributes', ('content_types', 'enabled', 'new_window', 'weight')), ) content_types = ContentTypeMultipleChoiceField( - queryset=ContentType.objects.all(), - limit_choices_to=FeatureQuery('custom_links'), + queryset=ContentType.objects.filter(FeatureQuery('custom_links').get_query()), required=False ) enabled = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) new_window = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -159,11 +157,24 @@ class CustomLinkFilterForm(SavedFiltersMixin, FilterForm): class ExportTemplateFilterForm(SavedFiltersMixin, FilterForm): fieldsets = ( (None, ('q', 'filter_id')), + ('Data', ('data_source_id', 'data_file_id')), ('Attributes', ('content_types', 'mime_type', 'file_extension', 'as_attachment')), ) + data_source_id = DynamicModelMultipleChoiceField( + queryset=DataSource.objects.all(), + required=False, + label=_('Data source') + ) + data_file_id = DynamicModelMultipleChoiceField( + queryset=DataFile.objects.all(), + required=False, + label=_('Data file'), + query_params={ + 'source_id': '$data_source_id' + } + ) content_types = ContentTypeMultipleChoiceField( - queryset=ContentType.objects.all(), - limit_choices_to=FeatureQuery('export_templates'), + queryset=ContentType.objects.filter(FeatureQuery('export_templates').get_query()), required=False ) mime_type = forms.CharField( @@ -175,7 +186,7 @@ class ExportTemplateFilterForm(SavedFiltersMixin, FilterForm): ) as_attachment = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -187,19 +198,18 @@ class SavedFilterFilterForm(SavedFiltersMixin, FilterForm): ('Attributes', ('content_types', 'enabled', 'shared', 'weight')), ) content_types = ContentTypeMultipleChoiceField( - queryset=ContentType.objects.all(), - limit_choices_to=FeatureQuery('export_templates'), + queryset=ContentType.objects.filter(FeatureQuery('export_templates').get_query()), required=False ) enabled = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) shared = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -215,37 +225,36 @@ class WebhookFilterForm(SavedFiltersMixin, FilterForm): ('Events', ('type_create', 'type_update', 'type_delete')), ) content_type_id = ContentTypeMultipleChoiceField( - queryset=ContentType.objects.all(), - limit_choices_to=FeatureQuery('webhooks'), + queryset=ContentType.objects.filter(FeatureQuery('webhooks').get_query()), required=False, label=_('Object type') ) - http_method = MultipleChoiceField( + http_method = forms.MultipleChoiceField( choices=WebhookHttpMethodChoices, required=False, label=_('HTTP method') ) enabled = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) type_create = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) type_update = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) type_delete = forms.NullBooleanField( required=False, - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -263,11 +272,25 @@ class TagFilterForm(SavedFiltersMixin, FilterForm): class ConfigContextFilterForm(SavedFiltersMixin, FilterForm): fieldsets = ( (None, ('q', 'filter_id', 'tag_id')), + ('Data', ('data_source_id', 'data_file_id')), ('Location', ('region_id', 'site_group_id', 'site_id', 'location_id')), ('Device', ('device_type_id', 'platform_id', 'role_id')), ('Cluster', ('cluster_type_id', 'cluster_group_id', 'cluster_id')), ('Tenant', ('tenant_group_id', 'tenant_id')) ) + data_source_id = DynamicModelMultipleChoiceField( + queryset=DataSource.objects.all(), + required=False, + label=_('Data source') + ) + data_file_id = DynamicModelMultipleChoiceField( + queryset=DataFile.objects.all(), + required=False, + label=_('Data file'), + query_params={ + 'source_id': '$data_source_id' + } + ) region_id = DynamicModelMultipleChoiceField( queryset=Region.objects.all(), required=False, @@ -336,11 +359,32 @@ class ConfigContextFilterForm(SavedFiltersMixin, FilterForm): ) +class ConfigTemplateFilterForm(SavedFiltersMixin, FilterForm): + fieldsets = ( + (None, ('q', 'filter_id', 'tag')), + ('Data', ('data_source_id', 'data_file_id')), + ) + data_source_id = DynamicModelMultipleChoiceField( + queryset=DataSource.objects.all(), + required=False, + label=_('Data source') + ) + data_file_id = DynamicModelMultipleChoiceField( + queryset=DataFile.objects.all(), + required=False, + label=_('Data file'), + query_params={ + 'source_id': '$data_source_id' + } + ) + tag = TagFilterField(ConfigTemplate) + + class LocalConfigContextFilterForm(forms.Form): local_context_data = forms.NullBooleanField( required=False, label=_('Has local config context data'), - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -381,8 +425,7 @@ class JournalEntryFilterForm(NetBoxModelFilterSetForm): ) kind = forms.ChoiceField( choices=add_blank_choice(JournalEntryKindChoices), - required=False, - widget=StaticSelect() + required=False ) tag = TagFilterField(model) @@ -406,8 +449,7 @@ class ObjectChangeFilterForm(SavedFiltersMixin, FilterForm): ) action = forms.ChoiceField( choices=add_blank_choice(ObjectChangeActionChoices), - required=False, - widget=StaticSelect() + required=False ) user_id = DynamicModelMultipleChoiceField( queryset=User.objects.all(), diff --git a/netbox/extras/forms/mixins.py b/netbox/extras/forms/mixins.py index 640bcc3dc..4e05e3a1e 100644 --- a/netbox/extras/forms/mixins.py +++ b/netbox/extras/forms/mixins.py @@ -2,13 +2,15 @@ from django.contrib.contenttypes.models import ContentType from django import forms from django.utils.translation import gettext as _ +from core.models import DataFile, DataSource from extras.models import * from extras.choices import CustomFieldVisibilityChoices -from utilities.forms.fields import DynamicModelMultipleChoiceField +from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField __all__ = ( 'CustomFieldsMixin', 'SavedFiltersMixin', + 'SyncedDataMixin', ) @@ -72,3 +74,19 @@ class SavedFiltersMixin(forms.Form): 'usable': True, } ) + + +class SyncedDataMixin(forms.Form): + data_source = DynamicModelChoiceField( + queryset=DataSource.objects.all(), + required=False, + label=_('Data source') + ) + data_file = DynamicModelChoiceField( + queryset=DataFile.objects.all(), + required=False, + label=_('File'), + query_params={ + 'source_id': '$data_source', + } + ) diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index a21cf21e2..4ce81c01b 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -5,18 +5,20 @@ from django.utils.translation import gettext as _ from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup from extras.choices import * +from extras.forms.mixins import SyncedDataMixin from extras.models import * from extras.utils import FeatureQuery from netbox.forms import NetBoxModelForm from tenancy.models import Tenant, TenantGroup from utilities.forms import ( add_blank_choice, BootstrapMixin, CommentField, ContentTypeChoiceField, ContentTypeMultipleChoiceField, - DynamicModelMultipleChoiceField, JSONField, SlugField, StaticSelect, + DynamicModelMultipleChoiceField, JSONField, SlugField, ) from virtualization.models import Cluster, ClusterGroup, ClusterType __all__ = ( 'ConfigContextForm', + 'ConfigTemplateForm', 'CustomFieldForm', 'CustomLinkForm', 'ExportTemplateForm', @@ -57,11 +59,6 @@ class CustomFieldForm(BootstrapMixin, forms.ModelForm): 'type': _("The type of data stored in this field. For object/multi-object fields, select the related object " "type below.") } - widgets = { - 'type': StaticSelect(), - 'filter_logic': StaticSelect(), - 'ui_visibility': StaticSelect(), - } class CustomLinkForm(BootstrapMixin, forms.ModelForm): @@ -79,7 +76,6 @@ class CustomLinkForm(BootstrapMixin, forms.ModelForm): model = CustomLink fields = '__all__' widgets = { - 'button_class': StaticSelect(), 'link_text': forms.Textarea(attrs={'class': 'font-monospace'}), 'link_url': forms.Textarea(attrs={'class': 'font-monospace'}), } @@ -95,19 +91,28 @@ class ExportTemplateForm(BootstrapMixin, forms.ModelForm): queryset=ContentType.objects.all(), limit_choices_to=FeatureQuery('export_templates') ) + template_code = forms.CharField( + required=False, + widget=forms.Textarea(attrs={'class': 'font-monospace'}) + ) fieldsets = ( ('Export Template', ('name', 'content_types', 'description')), - ('Template', ('template_code',)), + ('Content', ('data_source', 'data_file', 'template_code',)), ('Rendering', ('mime_type', 'file_extension', 'as_attachment')), ) class Meta: model = ExportTemplate fields = '__all__' - widgets = { - 'template_code': forms.Textarea(attrs={'class': 'font-monospace'}), - } + + def clean(self): + super().clean() + + if not self.cleaned_data.get('template_code') and not self.cleaned_data.get('data_file'): + raise forms.ValidationError("Must specify either local content or a data file") + + return self.cleaned_data class SavedFilterForm(BootstrapMixin, forms.ModelForm): @@ -162,7 +167,6 @@ class WebhookForm(BootstrapMixin, forms.ModelForm): 'type_delete': 'Deletions', } widgets = { - 'http_method': StaticSelect(), 'additional_headers': forms.Textarea(attrs={'class': 'font-monospace'}), 'body_template': forms.Textarea(attrs={'class': 'font-monospace'}), 'conditions': forms.Textarea(attrs={'class': 'font-monospace'}), @@ -183,7 +187,7 @@ class TagForm(BootstrapMixin, forms.ModelForm): ] -class ConfigContextForm(BootstrapMixin, forms.ModelForm): +class ConfigContextForm(BootstrapMixin, SyncedDataMixin, forms.ModelForm): regions = DynamicModelMultipleChoiceField( queryset=Region.objects.all(), required=False @@ -236,10 +240,13 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm): queryset=Tag.objects.all(), required=False ) - data = JSONField() + data = JSONField( + required=False + ) fieldsets = ( ('Config Context', ('name', 'weight', 'description', 'data', 'is_active')), + ('Data Source', ('data_source', 'data_file')), ('Assignment', ( 'regions', 'site_groups', 'sites', 'locations', 'device_types', 'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'tags', @@ -251,9 +258,45 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm): fields = ( 'name', 'weight', 'description', 'data', 'is_active', 'regions', 'site_groups', 'sites', 'locations', 'roles', 'device_types', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', - 'tenants', 'tags', + 'tenants', 'tags', 'data_source', 'data_file', ) + def clean(self): + super().clean() + + if not self.cleaned_data.get('data') and not self.cleaned_data.get('data_file'): + raise forms.ValidationError("Must specify either local data or a data file") + + return self.cleaned_data + + +class ConfigTemplateForm(BootstrapMixin, SyncedDataMixin, forms.ModelForm): + tags = DynamicModelMultipleChoiceField( + queryset=Tag.objects.all(), + required=False + ) + template_code = forms.CharField( + required=False, + widget=forms.Textarea(attrs={'class': 'font-monospace'}) + ) + + fieldsets = ( + ('Config Template', ('name', 'description', 'environment_params', 'tags')), + ('Content', ('data_source', 'data_file', 'template_code',)), + ) + + class Meta: + model = ConfigTemplate + fields = '__all__' + + def clean(self): + super().clean() + + if not self.cleaned_data.get('template_code') and not self.cleaned_data.get('data_file'): + raise forms.ValidationError("Must specify either local content or a data file") + + return self.cleaned_data + class ImageAttachmentForm(BootstrapMixin, forms.ModelForm): @@ -267,8 +310,7 @@ class ImageAttachmentForm(BootstrapMixin, forms.ModelForm): class JournalEntryForm(NetBoxModelForm): kind = forms.ChoiceField( choices=add_blank_choice(JournalEntryKindChoices), - required=False, - widget=StaticSelect() + required=False ) comments = CommentField() diff --git a/netbox/extras/forms/scripts.py b/netbox/extras/forms/scripts.py index 79dc8c869..8216c5413 100644 --- a/netbox/extras/forms/scripts.py +++ b/netbox/extras/forms/scripts.py @@ -45,12 +45,16 @@ class ScriptForm(BootstrapMixin, forms.Form): self.fields['_interval'] = interval self.fields['_commit'] = commit - def clean__schedule_at(self): + def clean(self): scheduled_time = self.cleaned_data['_schedule_at'] - if scheduled_time and scheduled_time < timezone.now(): + if scheduled_time and scheduled_time < local_now(): raise forms.ValidationError(_('Scheduled time must be in the future.')) - return scheduled_time + # When interval is used without schedule at, raise an exception + if self.cleaned_data['_interval'] and not scheduled_time: + raise forms.ValidationError(_('Scheduled time must be set when recurs is used.')) + + return self.cleaned_data @property def requires_input(self): diff --git a/netbox/extras/graphql/schema.py b/netbox/extras/graphql/schema.py index 0c3113879..3e116023f 100644 --- a/netbox/extras/graphql/schema.py +++ b/netbox/extras/graphql/schema.py @@ -8,6 +8,9 @@ class ExtrasQuery(graphene.ObjectType): config_context = ObjectField(ConfigContextType) config_context_list = ObjectListField(ConfigContextType) + config_template = ObjectField(ConfigTemplateType) + config_template_list = ObjectListField(ConfigTemplateType) + custom_field = ObjectField(CustomFieldType) custom_field_list = ObjectListField(CustomFieldType) diff --git a/netbox/extras/graphql/types.py b/netbox/extras/graphql/types.py index b5d4dffce..ba16ccd3e 100644 --- a/netbox/extras/graphql/types.py +++ b/netbox/extras/graphql/types.py @@ -4,6 +4,7 @@ from netbox.graphql.types import BaseObjectType, ObjectType __all__ = ( 'ConfigContextType', + 'ConfigTemplateType', 'CustomFieldType', 'CustomLinkType', 'ExportTemplateType', @@ -24,6 +25,14 @@ class ConfigContextType(ObjectType): filterset_class = filtersets.ConfigContextFilterSet +class ConfigTemplateType(ObjectType): + + class Meta: + model = models.ConfigTemplate + fields = '__all__' + filterset_class = filtersets.ConfigTemplateFilterSet + + class CustomFieldType(ObjectType): class Meta: diff --git a/netbox/extras/lookups.py b/netbox/extras/lookups.py index 7197efcfc..77fe2301e 100644 --- a/netbox/extras/lookups.py +++ b/netbox/extras/lookups.py @@ -1,4 +1,5 @@ -from django.db.models import CharField, Lookup +from django.db.models import CharField, TextField, Lookup +from .fields import CachedValueField class Empty(Lookup): @@ -14,4 +15,18 @@ class Empty(Lookup): return 'CAST(LENGTH(%s) AS BOOLEAN) != %s' % (lhs, rhs), params +class NetContainsOrEquals(Lookup): + """ + This lookup has the same functionality as the one from the ipam app except lhs is cast to inet + """ + lookup_name = 'net_contains_or_equals' + + def as_sql(self, qn, connection): + lhs, lhs_params = self.process_lhs(qn, connection) + rhs, rhs_params = self.process_rhs(qn, connection) + params = lhs_params + rhs_params + return 'CAST(%s AS INET) >>= %s' % (lhs, rhs), params + + CharField.register_lookup(Empty) +CachedValueField.register_lookup(NetContainsOrEquals) diff --git a/netbox/extras/management/commands/housekeeping.py b/netbox/extras/management/commands/housekeeping.py index 42690568d..172e26bf2 100644 --- a/netbox/extras/management/commands/housekeeping.py +++ b/netbox/extras/management/commands/housekeeping.py @@ -37,7 +37,7 @@ class Command(BaseCommand): f"clearing sessions; skipping." ) - # Delete expired ObjectRecords + # Delete expired ObjectChanges if options['verbosity']: self.stdout.write("[*] Checking for expired changelog records") if config.CHANGELOG_RETENTION: diff --git a/netbox/extras/management/commands/nbshell.py b/netbox/extras/management/commands/nbshell.py index 07f943d15..04a67eb49 100644 --- a/netbox/extras/management/commands/nbshell.py +++ b/netbox/extras/management/commands/nbshell.py @@ -9,7 +9,7 @@ from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.core.management.base import BaseCommand -APPS = ('circuits', 'dcim', 'extras', 'ipam', 'tenancy', 'users', 'virtualization', 'wireless') +APPS = ('circuits', 'core', 'dcim', 'extras', 'ipam', 'tenancy', 'users', 'virtualization', 'wireless') BANNER_TEXT = """### NetBox interactive shell ({node}) ### Python {python} | Django {django} | NetBox {netbox} diff --git a/netbox/extras/migrations/0083_search.py b/netbox/extras/migrations/0083_search.py index 0c53de638..4c7ae1084 100644 --- a/netbox/extras/migrations/0083_search.py +++ b/netbox/extras/migrations/0083_search.py @@ -1,25 +1,9 @@ -import sys import uuid import django.db.models.deletion import django.db.models.lookups -from django.core import management from django.db import migrations, models - - -def reindex(apps, schema_editor): - # Build the search index (except during tests) - if 'test' not in sys.argv: - management.call_command( - 'reindex', - 'circuits', - 'dcim', - 'extras', - 'ipam', - 'tenancy', - 'virtualization', - 'wireless', - ) +import extras.fields class Migration(migrations.Migration): @@ -49,7 +33,7 @@ class Migration(migrations.Migration): ('object_id', models.PositiveBigIntegerField()), ('field', models.CharField(max_length=200)), ('type', models.CharField(max_length=30)), - ('value', models.TextField()), + ('value', extras.fields.CachedValueField()), ('weight', models.PositiveSmallIntegerField(default=1000)), ('object_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype')), ], @@ -57,8 +41,4 @@ class Migration(migrations.Migration): 'ordering': ('weight', 'object_type', 'object_id'), }, ), - migrations.RunPython( - code=reindex, - reverse_code=migrations.RunPython.noop - ), ] diff --git a/netbox/extras/migrations/0085_synced_data.py b/netbox/extras/migrations/0085_synced_data.py new file mode 100644 index 000000000..4790cd51a --- /dev/null +++ b/netbox/extras/migrations/0085_synced_data.py @@ -0,0 +1,55 @@ +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0001_initial'), + ('extras', '0084_staging'), + ] + + operations = [ + # ConfigContexts + migrations.AddField( + model_name='configcontext', + name='data_file', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='core.datafile'), + ), + migrations.AddField( + model_name='configcontext', + name='data_path', + field=models.CharField(blank=True, editable=False, max_length=1000), + ), + migrations.AddField( + model_name='configcontext', + name='data_source', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='core.datasource'), + ), + migrations.AddField( + model_name='configcontext', + name='data_synced', + field=models.DateTimeField(blank=True, editable=False, null=True), + ), + # ExportTemplates + migrations.AddField( + model_name='exporttemplate', + name='data_file', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='core.datafile'), + ), + migrations.AddField( + model_name='exporttemplate', + name='data_path', + field=models.CharField(blank=True, editable=False, max_length=1000), + ), + migrations.AddField( + model_name='exporttemplate', + name='data_source', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='core.datasource'), + ), + migrations.AddField( + model_name='exporttemplate', + name='data_synced', + field=models.DateTimeField(blank=True, editable=False, null=True), + ), + ] diff --git a/netbox/extras/migrations/0086_configtemplate.py b/netbox/extras/migrations/0086_configtemplate.py new file mode 100644 index 000000000..bd47254e9 --- /dev/null +++ b/netbox/extras/migrations/0086_configtemplate.py @@ -0,0 +1,34 @@ +from django.db import migrations, models +import django.db.models.deletion +import taggit.managers + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0001_initial'), + ('extras', '0085_synced_data'), + ] + + operations = [ + migrations.CreateModel( + name='ConfigTemplate', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), + ('created', models.DateTimeField(auto_now_add=True, null=True)), + ('last_updated', models.DateTimeField(auto_now=True, null=True)), + ('data_path', models.CharField(blank=True, editable=False, max_length=1000)), + ('data_synced', models.DateTimeField(blank=True, editable=False, null=True)), + ('name', models.CharField(max_length=100)), + ('description', models.CharField(blank=True, max_length=200)), + ('template_code', models.TextField()), + ('environment_params', models.JSONField(blank=True, null=True)), + ('data_file', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='core.datafile')), + ('data_source', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='core.datasource')), + ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), + ], + options={ + 'ordering': ('name',), + }, + ), + ] diff --git a/netbox/extras/models/__init__.py b/netbox/extras/models/__init__.py index 9b5c660c4..33936cc4f 100644 --- a/netbox/extras/models/__init__.py +++ b/netbox/extras/models/__init__.py @@ -1,5 +1,5 @@ from .change_logging import ObjectChange -from .configcontexts import ConfigContext, ConfigContextModel +from .configs import * from .customfields import CustomField from .models import * from .search import * @@ -12,6 +12,7 @@ __all__ = ( 'ConfigContext', 'ConfigContextModel', 'ConfigRevision', + 'ConfigTemplate', 'CustomField', 'CustomLink', 'ExportTemplate', diff --git a/netbox/extras/models/configcontexts.py b/netbox/extras/models/configs.py similarity index 64% rename from netbox/extras/models/configcontexts.py rename to netbox/extras/models/configs.py index d8d3510d7..f2b50f161 100644 --- a/netbox/extras/models/configcontexts.py +++ b/netbox/extras/models/configs.py @@ -2,16 +2,22 @@ from django.conf import settings from django.core.validators import ValidationError from django.db import models from django.urls import reverse +from django.utils import timezone +from django.utils.translation import gettext as _ +from jinja2.loaders import BaseLoader +from jinja2.sandbox import SandboxedEnvironment from extras.querysets import ConfigContextQuerySet +from netbox.config import get_config from netbox.models import ChangeLoggedModel -from netbox.models.features import WebhooksMixin +from netbox.models.features import ExportTemplatesMixin, SyncedDataMixin, TagsMixin +from utilities.jinja2 import ConfigTemplateLoader from utilities.utils import deepmerge - __all__ = ( 'ConfigContext', 'ConfigContextModel', + 'ConfigTemplate', ) @@ -19,7 +25,7 @@ __all__ = ( # Config contexts # -class ConfigContext(WebhooksMixin, ChangeLoggedModel): +class ConfigContext(SyncedDataMixin, ChangeLoggedModel): """ A ConfigContext represents a set of arbitrary data available to any Device or VirtualMachine matching its assigned qualifiers (region, site, etc.). For example, the data stored in a ConfigContext assigned to site A and tenant B @@ -130,6 +136,13 @@ class ConfigContext(WebhooksMixin, ChangeLoggedModel): {'data': 'JSON data must be in object form. Example: {"foo": 123}'} ) + def sync_data(self): + """ + Synchronize context data from the designated DataFile (if any). + """ + self.data = self.data_file.get_data() + self.data_synced = timezone.now() + class ConfigContextModel(models.Model): """ @@ -175,3 +188,77 @@ class ConfigContextModel(models.Model): raise ValidationError( {'local_context_data': 'JSON data must be in object form. Example: {"foo": 123}'} ) + + +# +# Config templates +# + +class ConfigTemplate(SyncedDataMixin, ExportTemplatesMixin, TagsMixin, ChangeLoggedModel): + name = models.CharField( + max_length=100 + ) + description = models.CharField( + max_length=200, + blank=True + ) + template_code = models.TextField( + help_text=_('Jinja2 template code.') + ) + environment_params = models.JSONField( + blank=True, + null=True + ) + + class Meta: + ordering = ('name',) + + def __str__(self): + return self.name + + def get_absolute_url(self): + return reverse('extras:configtemplate', args=[self.pk]) + + def sync_data(self): + """ + Synchronize template content from the designated DataFile (if any). + """ + self.template_code = self.data_file.data_as_string + self.data_synced = timezone.now() + + def render(self, context=None): + """ + Render the contents of the template. + """ + context = context or {} + + # Initialize the Jinja2 environment and instantiate the Template + environment = self._get_environment() + if self.data_file: + template = environment.get_template(self.data_file.path) + else: + template = environment.from_string(self.template_code) + + output = template.render(**context) + + # Replace CRLF-style line terminators + return output.replace('\r\n', '\n') + + def _get_environment(self): + """ + Instantiate and return a Jinja2 environment suitable for rendering the ConfigTemplate. + """ + # Initialize the template loader & cache the base template code (if applicable) + if self.data_file: + loader = ConfigTemplateLoader(data_source=self.data_source) + loader.cache_templates({ + self.data_file.path: self.template_code + }) + else: + loader = BaseLoader() + + # Initialize the environment + environment = SandboxedEnvironment(loader=loader) + environment.filters.update(get_config().JINJA2_FILTERS) + + return environment diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 4842c0654..8141ca76d 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -1,6 +1,6 @@ +import decimal import re from datetime import datetime, date -import decimal import django_filters from django import forms @@ -17,17 +17,18 @@ from django.utils.translation import gettext as _ from extras.choices import * from extras.utils import FeatureQuery from netbox.models import ChangeLoggedModel -from netbox.models.features import CloningMixin, ExportTemplatesMixin, WebhooksMixin +from netbox.models.features import CloningMixin, ExportTemplatesMixin from netbox.search import FieldTypes from utilities import filters -from utilities.forms import ( - CSVChoiceField, CSVMultipleChoiceField, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField, - JSONField, LaxURLField, StaticSelectMultiple, StaticSelect, add_blank_choice, +from utilities.forms.fields import ( + CSVChoiceField, CSVModelChoiceField, CSVModelMultipleChoiceField, CSVMultipleChoiceField, DynamicModelChoiceField, + DynamicModelMultipleChoiceField, JSONField, LaxURLField, ) +from utilities.forms.utils import add_blank_choice +from utilities.forms.widgets import DatePicker from utilities.querysets import RestrictedQuerySet from utilities.validators import validate_regex - __all__ = ( 'CustomField', 'CustomFieldManager', @@ -54,7 +55,7 @@ class CustomFieldManager(models.Manager.from_queryset(RestrictedQuerySet)): return self.get_queryset().filter(content_types=content_type) -class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): +class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): content_types = models.ManyToManyField( to=ContentType, related_name='custom_fields', @@ -372,7 +373,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge (False, 'False'), ) field = forms.NullBooleanField( - required=required, initial=initial, widget=StaticSelect(choices=choices) + required=required, initial=initial, widget=forms.Select(choices=choices) ) # Date @@ -393,14 +394,10 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge if self.type == CustomFieldTypeChoices.TYPE_SELECT: field_class = CSVChoiceField if for_csv_import else forms.ChoiceField - field = field_class( - choices=choices, required=required, initial=initial, widget=StaticSelect() - ) + field = field_class(choices=choices, required=required, initial=initial) else: field_class = CSVMultipleChoiceField if for_csv_import else forms.MultipleChoiceField - field = field_class( - choices=choices, required=required, initial=initial, widget=StaticSelectMultiple() - ) + field = field_class(choices=choices, required=required, initial=initial) # URL elif self.type == CustomFieldTypeChoices.TYPE_URL: @@ -413,7 +410,8 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge # Object elif self.type == CustomFieldTypeChoices.TYPE_OBJECT: model = self.object_type.model_class() - field = DynamicModelChoiceField( + field_class = CSVModelChoiceField if for_csv_import else DynamicModelChoiceField + field = field_class( queryset=model.objects.all(), required=required, initial=initial @@ -422,10 +420,11 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge # Multiple objects elif self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT: model = self.object_type.model_class() - field = DynamicModelMultipleChoiceField( + field_class = CSVModelMultipleChoiceField if for_csv_import else DynamicModelMultipleChoiceField + field = field_class( queryset=model.objects.all(), required=required, - initial=initial + initial=initial, ) # Text diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index e608f81b1..1360904dc 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -11,6 +11,7 @@ from django.core.validators import MinValueValidator, ValidationError from django.db import models from django.http import HttpResponse, QueryDict from django.urls import reverse +from django.urls.exceptions import NoReverseMatch from django.utils import timezone from django.utils.formats import date_format from django.utils.translation import gettext as _ @@ -25,7 +26,8 @@ from netbox.config import get_config from netbox.constants import RQ_QUEUE_DEFAULT from netbox.models import ChangeLoggedModel from netbox.models.features import ( - CloningMixin, CustomFieldsMixin, CustomLinksMixin, ExportTemplatesMixin, JobResultsMixin, TagsMixin, WebhooksMixin, + CloningMixin, CustomFieldsMixin, CustomLinksMixin, ExportTemplatesMixin, JobResultsMixin, SyncedDataMixin, + TagsMixin, ) from utilities.querysets import RestrictedQuerySet from utilities.utils import render_jinja2 @@ -44,7 +46,7 @@ __all__ = ( ) -class Webhook(ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): +class Webhook(ExportTemplatesMixin, ChangeLoggedModel): """ A Webhook defines a request that will be sent to a remote application when an object is created, updated, and/or delete in NetBox. The request will contain a representation of the object, which the remote application can act on. @@ -201,7 +203,7 @@ class Webhook(ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): return render_jinja2(self.payload_url, context) -class CustomLink(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): +class CustomLink(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): """ A custom link to an external representation of a NetBox object. The link text and URL fields accept Jinja2 template code to be rendered with an object as context. @@ -280,7 +282,7 @@ class CustomLink(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogged } -class ExportTemplate(ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): +class ExportTemplate(SyncedDataMixin, ExportTemplatesMixin, ChangeLoggedModel): content_types = models.ManyToManyField( to=ContentType, related_name='export_templates', @@ -334,6 +336,13 @@ class ExportTemplate(ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): 'name': f'"{self.name}" is a reserved name. Please choose a different name.' }) + def sync_data(self): + """ + Synchronize template content from the designated DataFile (if any). + """ + self.template_code = self.data_file.data_as_string + self.data_synced = timezone.now() + def render(self, queryset): """ Render the contents of the template. @@ -367,7 +376,7 @@ class ExportTemplate(ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): return response -class SavedFilter(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel): +class SavedFilter(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): """ A set of predefined keyword parameters that can be reused to filter for specific objects. """ @@ -438,7 +447,7 @@ class SavedFilter(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge return qd.urlencode() -class ImageAttachment(WebhooksMixin, ChangeLoggedModel): +class ImageAttachment(ChangeLoggedModel): """ An uploaded image which is associated with an object. """ @@ -514,7 +523,7 @@ class ImageAttachment(WebhooksMixin, ChangeLoggedModel): return objectchange -class JournalEntry(CustomFieldsMixin, CustomLinksMixin, TagsMixin, WebhooksMixin, ExportTemplatesMixin, ChangeLoggedModel): +class JournalEntry(CustomFieldsMixin, CustomLinksMixin, TagsMixin, ExportTemplatesMixin, ChangeLoggedModel): """ A historical remark concerning an object; collectively, these form an object's journal. The journal is used to preserve historical context around an object, and complements NetBox's built-in change logging. For example, you @@ -634,7 +643,7 @@ class JobResult(models.Model): def delete(self, *args, **kwargs): super().delete(*args, **kwargs) - rq_queue_name = get_config().QUEUE_MAPPINGS.get(self.obj_type.name, RQ_QUEUE_DEFAULT) + rq_queue_name = get_config().QUEUE_MAPPINGS.get(self.obj_type.model, RQ_QUEUE_DEFAULT) queue = django_rq.get_queue(rq_queue_name) job = queue.fetch_job(str(self.job_id)) @@ -642,7 +651,10 @@ class JobResult(models.Model): job.cancel() def get_absolute_url(self): - return reverse(f'extras:{self.obj_type.name}_result', args=[self.pk]) + try: + return reverse(f'extras:{self.obj_type.model}_result', args=[self.pk]) + except NoReverseMatch: + return None def get_status_color(self): return JobResultStatusChoices.colors.get(self.status) @@ -693,7 +705,7 @@ class JobResult(models.Model): schedule_at: Schedule the job to be executed at the passed date and time interval: Recurrence interval (in minutes) """ - rq_queue_name = get_config().QUEUE_MAPPINGS.get(obj_type.name, RQ_QUEUE_DEFAULT) + rq_queue_name = get_config().QUEUE_MAPPINGS.get(obj_type.model, RQ_QUEUE_DEFAULT) queue = django_rq.get_queue(rq_queue_name) status = JobResultStatusChoices.STATUS_SCHEDULED if schedule_at else JobResultStatusChoices.STATUS_PENDING job_result: JobResult = JobResult.objects.create( diff --git a/netbox/extras/models/search.py b/netbox/extras/models/search.py index 7c5860e00..6d088abb0 100644 --- a/netbox/extras/models/search.py +++ b/netbox/extras/models/search.py @@ -4,6 +4,7 @@ from django.contrib.contenttypes.models import ContentType from django.db import models from utilities.fields import RestrictedGenericForeignKey +from ..fields import CachedValueField __all__ = ( 'CachedValue', @@ -36,7 +37,7 @@ class CachedValue(models.Model): type = models.CharField( max_length=30 ) - value = models.TextField() + value = CachedValueField() weight = models.PositiveSmallIntegerField( default=1000 ) diff --git a/netbox/extras/models/tags.py b/netbox/extras/models/tags.py index 827d969e3..b980f0709 100644 --- a/netbox/extras/models/tags.py +++ b/netbox/extras/models/tags.py @@ -5,7 +5,7 @@ from django.utils.text import slugify from taggit.models import TagBase, GenericTaggedItemBase from netbox.models import ChangeLoggedModel -from netbox.models.features import ExportTemplatesMixin, WebhooksMixin +from netbox.models.features import ExportTemplatesMixin from utilities.choices import ColorChoices from utilities.fields import ColorField @@ -14,7 +14,7 @@ from utilities.fields import ColorField # Tags # -class Tag(ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel, TagBase): +class Tag(ExportTemplatesMixin, ChangeLoggedModel, TagBase): id = models.BigAutoField( primary_key=True ) diff --git a/netbox/extras/plugins/__init__.py b/netbox/extras/plugins/__init__.py index 7694a1fbe..ee74ad88e 100644 --- a/netbox/extras/plugins/__init__.py +++ b/netbox/extras/plugins/__init__.py @@ -1,4 +1,5 @@ import collections +from importlib import import_module from django.apps import AppConfig from django.conf import settings @@ -13,12 +14,21 @@ from .registration import * from .templates import * # Initialize plugin registry -registry['plugins'] = { +registry['plugins'].update({ 'graphql_schemas': [], 'menus': [], 'menu_items': {}, 'preferences': {}, 'template_extensions': collections.defaultdict(list), +}) + +DEFAULT_RESOURCE_PATHS = { + 'search_indexes': 'search.indexes', + 'graphql_schema': 'graphql.schema', + 'menu': 'navigation.menu', + 'menu_items': 'navigation.menu_items', + 'template_extensions': 'template_content.template_extensions', + 'user_preferences': 'preferences.preferences', } @@ -58,58 +68,53 @@ class PluginConfig(AppConfig): # Django apps to append to INSTALLED_APPS when plugin requires them. django_apps = [] - # Default integration paths. Plugin authors can override these to customize the paths to - # integrated components. - search_indexes = 'search.indexes' - graphql_schema = 'graphql.schema' - menu = 'navigation.menu' - menu_items = 'navigation.menu_items' - template_extensions = 'template_content.template_extensions' - user_preferences = 'preferences.preferences' + # Optional plugin resources + search_indexes = None + graphql_schema = None + menu = None + menu_items = None + template_extensions = None + user_preferences = None + + def _load_resource(self, name): + # Import from the configured path, if defined. + if getattr(self, name): + return import_string(f"{self.__module__}.{self.name}") + + # Fall back to the resource's default path. Return None if the module has not been provided. + default_path = f'{self.__module__}.{DEFAULT_RESOURCE_PATHS[name]}' + default_module, resource_name = default_path.rsplit('.', 1) + try: + module = import_module(default_module) + return getattr(module, resource_name, None) + except ModuleNotFoundError: + pass def ready(self): plugin_name = self.name.rsplit('.', 1)[-1] # Register search extensions (if defined) - try: - search_indexes = import_string(f"{self.__module__}.{self.search_indexes}") - for idx in search_indexes: - register_search(idx) - except ImportError: - pass + search_indexes = self._load_resource('search_indexes') or [] + for idx in search_indexes: + register_search(idx) # Register template content (if defined) - try: - template_extensions = import_string(f"{self.__module__}.{self.template_extensions}") + if template_extensions := self._load_resource('template_extensions'): register_template_extensions(template_extensions) - except ImportError: - pass # Register navigation menu and/or menu items (if defined) - try: - menu = import_string(f"{self.__module__}.{self.menu}") + if menu := self._load_resource('menu'): register_menu(menu) - except ImportError: - pass - try: - menu_items = import_string(f"{self.__module__}.{self.menu_items}") + if menu_items := self._load_resource('menu_items'): register_menu_items(self.verbose_name, menu_items) - except ImportError: - pass # Register GraphQL schema (if defined) - try: - graphql_schema = import_string(f"{self.__module__}.{self.graphql_schema}") + if graphql_schema := self._load_resource('graphql_schema'): register_graphql_schema(graphql_schema) - except ImportError: - pass # Register user preferences (if defined) - try: - user_preferences = import_string(f"{self.__module__}.{self.user_preferences}") + if user_preferences := self._load_resource('user_preferences'): register_user_preferences(plugin_name, user_preferences) - except ImportError: - pass @classmethod def validate(cls, user_config, netbox_version): diff --git a/netbox/extras/plugins/urls.py b/netbox/extras/plugins/urls.py index b4360dc9e..2f237f56a 100644 --- a/netbox/extras/plugins/urls.py +++ b/netbox/extras/plugins/urls.py @@ -1,9 +1,11 @@ +from importlib import import_module + from django.apps import apps from django.conf import settings from django.conf.urls import include from django.contrib.admin.views.decorators import staff_member_required from django.urls import path -from django.utils.module_loading import import_string +from django.utils.module_loading import import_string, module_has_submodule from . import views @@ -19,24 +21,21 @@ plugin_admin_patterns = [ # Register base/API URL patterns for each plugin for plugin_path in settings.PLUGINS: + plugin = import_module(plugin_path) plugin_name = plugin_path.split('.')[-1] app = apps.get_app_config(plugin_name) base_url = getattr(app, 'base_url') or app.label # Check if the plugin specifies any base URLs - try: + if module_has_submodule(plugin, 'urls'): urlpatterns = import_string(f"{plugin_path}.urls.urlpatterns") plugin_patterns.append( path(f"{base_url}/", include((urlpatterns, app.label))) ) - except ImportError: - pass # Check if the plugin specifies any API URLs - try: + if module_has_submodule(plugin, 'api.urls'): urlpatterns = import_string(f"{plugin_path}.api.urls.urlpatterns") plugin_api_patterns.append( path(f"{base_url}/", include((urlpatterns, f"{app.label}-api"))) ) - except ImportError: - pass diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index 998d727a4..313058d57 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -21,7 +21,7 @@ from extras.models import JobResult from extras.signals import clear_webhooks from ipam.formfields import IPAddressFormField, IPNetworkFormField from ipam.validators import MaxPrefixLengthValidator, MinPrefixLengthValidator, prefix_validator -from utilities.exceptions import AbortTransaction +from utilities.exceptions import AbortScript, AbortTransaction from utilities.forms import add_blank_choice, DynamicModelChoiceField, DynamicModelMultipleChoiceField from .context_managers import change_logging from .forms import ScriptForm @@ -470,6 +470,14 @@ def run_script(data, request, commit=True, *args, **kwargs): except AbortTransaction: script.log_info("Database changes have been reverted automatically.") clear_webhooks.send(request) + except AbortScript as e: + script.log_failure( + f"Script aborted with error: {e}" + ) + script.log_info("Database changes have been reverted due to error.") + logger.error(f"Script aborted with error: {e}") + job_result.set_status(JobResultStatusChoices.STATUS_ERRORED) + clear_webhooks.send(request) except Exception as e: stacktrace = traceback.format_exc() script.log_failure( @@ -516,27 +524,39 @@ def get_scripts(use_names=False): defined name in place of the actual module name. """ scripts = {} - # Iterate through all modules within the scripts path. These are the user-created files in which reports are + + # Get all modules within the scripts path. These are the user-created files in which scripts are # defined. - for importer, module_name, _ in pkgutil.iter_modules([settings.SCRIPTS_ROOT]): - # Use a lock as removing and loading modules is not thread safe - with lock: - # Remove cached module to ensure consistency with filesystem - if module_name in sys.modules: + modules = list(pkgutil.iter_modules([settings.SCRIPTS_ROOT])) + modules_bases = set([name.split(".")[0] for _, name, _ in modules]) + + # Deleting from sys.modules needs to done behind a lock to prevent race conditions where a module is + # removed from sys.modules while another thread is importing + with lock: + for module_name in list(sys.modules.keys()): + # Everything sharing a base module path with a module in the script folder is removed. + # We also remove all modules with a base module called "scripts". This allows modifying imported + # non-script modules without having to reload the RQ worker. + module_base = module_name.split(".")[0] + if module_base == "scripts" or module_base in modules_bases: del sys.modules[module_name] - module = importer.find_module(module_name).load_module(module_name) + for importer, module_name, _ in modules: + module = importer.find_module(module_name).load_module(module_name) if use_names and hasattr(module, 'name'): module_name = module.name + module_scripts = {} script_order = getattr(module, "script_order", ()) ordered_scripts = [cls for cls in script_order if is_script(cls)] unordered_scripts = [cls for _, cls in inspect.getmembers(module, is_script) if cls not in script_order] + for cls in [*ordered_scripts, *unordered_scripts]: # For scripts in submodules use the full import path w/o the root module as the name script_name = cls.full_name.split(".", maxsplit=1)[1] module_scripts[script_name] = cls + if module_scripts: scripts[module_name] = module_scripts diff --git a/netbox/extras/tables/tables.py b/netbox/extras/tables/tables.py index c2b8c9424..5991203f2 100644 --- a/netbox/extras/tables/tables.py +++ b/netbox/extras/tables/tables.py @@ -8,6 +8,7 @@ from .template_code import * __all__ = ( 'ConfigContextTable', + 'ConfigTemplateTable', 'CustomFieldTable', 'CustomLinkTable', 'ExportTemplateTable', @@ -90,15 +91,24 @@ class ExportTemplateTable(NetBoxTable): ) content_types = columns.ContentTypesColumn() as_attachment = columns.BooleanColumn() + data_source = tables.Column( + linkify=True + ) + data_file = tables.Column( + linkify=True + ) + is_synced = columns.BooleanColumn( + verbose_name='Synced' + ) class Meta(NetBoxTable.Meta): model = ExportTemplate fields = ( 'pk', 'id', 'name', 'content_types', 'description', 'mime_type', 'file_extension', 'as_attachment', - 'created', 'last_updated', + 'data_source', 'data_file', 'data_synced', 'created', 'last_updated', ) default_columns = ( - 'pk', 'name', 'content_types', 'description', 'mime_type', 'file_extension', 'as_attachment', + 'pk', 'name', 'content_types', 'description', 'mime_type', 'file_extension', 'as_attachment', 'is_synced', ) @@ -188,21 +198,58 @@ class TaggedItemTable(NetBoxTable): class ConfigContextTable(NetBoxTable): + data_source = tables.Column( + linkify=True + ) + data_file = tables.Column( + linkify=True + ) name = tables.Column( linkify=True ) is_active = columns.BooleanColumn( verbose_name='Active' ) + is_synced = columns.BooleanColumn( + verbose_name='Synced' + ) class Meta(NetBoxTable.Meta): model = ConfigContext fields = ( - 'pk', 'id', 'name', 'weight', 'is_active', 'description', 'regions', 'sites', 'locations', 'roles', - 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'created', - 'last_updated', + 'pk', 'id', 'name', 'weight', 'is_active', 'is_synced', 'description', 'regions', 'sites', 'locations', + 'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', + 'data_source', 'data_file', 'data_synced', 'created', 'last_updated', + ) + default_columns = ('pk', 'name', 'weight', 'is_active', 'is_synced', 'description') + + +class ConfigTemplateTable(NetBoxTable): + name = tables.Column( + linkify=True + ) + data_source = tables.Column( + linkify=True + ) + data_file = tables.Column( + linkify=True + ) + is_synced = columns.BooleanColumn( + verbose_name='Synced' + ) + tags = columns.TagColumn( + url_name='extras:configtemplate_list' + ) + + class Meta(NetBoxTable.Meta): + model = ConfigTemplate + fields = ( + 'pk', 'id', 'name', 'description', 'data_source', 'data_file', 'data_synced', 'created', 'last_updated', + 'tags', + ) + default_columns = ( + 'pk', 'name', 'description', 'is_synced', ) - default_columns = ('pk', 'name', 'weight', 'is_active', 'description') class ObjectChangeTable(NetBoxTable): diff --git a/netbox/extras/tests/test_conditions.py b/netbox/extras/tests/test_conditions.py index 8e02eb75d..e7275482a 100644 --- a/netbox/extras/tests/test_conditions.py +++ b/netbox/extras/tests/test_conditions.py @@ -126,6 +126,16 @@ class ConditionSetTest(TestCase): with self.assertRaises(ValueError): ConditionSet({'foo': []}) + def test_null_value(self): + cs = ConditionSet({ + 'and': [ + {'attr': 'a', 'value': None, 'op': 'eq', 'negate': True}, + ] + }) + self.assertFalse(cs.eval({'a': None})) + self.assertTrue(cs.eval({'a': "string"})) + self.assertTrue(cs.eval({'a': {"key": "value"}})) + def test_and_single_depth(self): cs = ConditionSet({ 'and': [ diff --git a/netbox/extras/urls.py b/netbox/extras/urls.py index f41a45f5a..dfbaa1bc6 100644 --- a/netbox/extras/urls.py +++ b/netbox/extras/urls.py @@ -29,6 +29,7 @@ urlpatterns = [ path('export-templates/import/', views.ExportTemplateBulkImportView.as_view(), name='exporttemplate_import'), path('export-templates/edit/', views.ExportTemplateBulkEditView.as_view(), name='exporttemplate_bulk_edit'), path('export-templates/delete/', views.ExportTemplateBulkDeleteView.as_view(), name='exporttemplate_bulk_delete'), + path('export-templates/sync/', views.ExportTemplateBulkSyncDataView.as_view(), name='exporttemplate_bulk_sync'), path('export-templates//', include(get_model_urls('extras', 'exporttemplate'))), # Saved filters @@ -60,8 +61,17 @@ urlpatterns = [ path('config-contexts/add/', views.ConfigContextEditView.as_view(), name='configcontext_add'), path('config-contexts/edit/', views.ConfigContextBulkEditView.as_view(), name='configcontext_bulk_edit'), path('config-contexts/delete/', views.ConfigContextBulkDeleteView.as_view(), name='configcontext_bulk_delete'), + path('config-contexts/sync/', views.ConfigContextBulkSyncDataView.as_view(), name='configcontext_bulk_sync'), path('config-contexts//', include(get_model_urls('extras', 'configcontext'))), + # Config templates + path('config-templates/', views.ConfigTemplateListView.as_view(), name='configtemplate_list'), + path('config-templates/add/', views.ConfigTemplateEditView.as_view(), name='configtemplate_add'), + path('config-templates/edit/', views.ConfigTemplateBulkEditView.as_view(), name='configtemplate_bulk_edit'), + path('config-templates/delete/', views.ConfigTemplateBulkDeleteView.as_view(), name='configtemplate_bulk_delete'), + path('config-templates/sync/', views.ConfigTemplateBulkSyncDataView.as_view(), name='configtemplate_bulk_sync'), + path('config-templates//', include(get_model_urls('extras', 'configtemplate'))), + # Image attachments path('image-attachments/add/', views.ImageAttachmentEditView.as_view(), name='imageattachment_add'), path('image-attachments//', include(get_model_urls('extras', 'imageattachment'))), diff --git a/netbox/extras/utils.py b/netbox/extras/utils.py index 268bf9e80..f90858bcf 100644 --- a/netbox/extras/utils.py +++ b/netbox/extras/utils.py @@ -2,7 +2,6 @@ from django.db.models import Q from django.utils.deconstruct import deconstructible from taggit.managers import _TaggableManager -from extras.constants import EXTRAS_FEATURES from netbox.registry import registry @@ -18,7 +17,7 @@ def is_taggable(obj): def image_upload(instance, filename): """ - Return a path for uploading image attchments. + Return a path for uploading image attachments. """ path = 'image-attachments/' @@ -56,8 +55,14 @@ class FeatureQuery: def register_features(model, features): + """ + Register model features in the application registry. + """ + app_label, model_name = model._meta.label_lower.split('.') for feature in features: - if feature not in EXTRAS_FEATURES: - raise ValueError(f"{feature} is not a valid extras feature!") - app_label, model_name = model._meta.label_lower.split('.') - registry['model_features'][feature][app_label].add(model_name) + try: + registry['model_features'][feature][app_label].add(model_name) + except KeyError: + raise KeyError( + f"{feature} is not a valid model feature! Valid keys are: {registry['model_features'].keys()}" + ) diff --git a/netbox/extras/views.py b/netbox/extras/views.py index 2d2608ae8..3edb70cf1 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -121,6 +121,8 @@ class ExportTemplateListView(generic.ObjectListView): filterset = filtersets.ExportTemplateFilterSet filterset_form = forms.ExportTemplateFilterForm table = tables.ExportTemplateTable + template_name = 'extras/exporttemplate_list.html' + actions = ('add', 'import', 'export', 'bulk_edit', 'bulk_delete', 'bulk_sync') @register_model_view(ExportTemplate) @@ -158,6 +160,10 @@ class ExportTemplateBulkDeleteView(generic.BulkDeleteView): table = tables.ExportTemplateTable +class ExportTemplateBulkSyncDataView(generic.BulkSyncDataView): + queryset = ExportTemplate.objects.all() + + # # Saved filters # @@ -352,7 +358,8 @@ class ConfigContextListView(generic.ObjectListView): filterset = filtersets.ConfigContextFilterSet filterset_form = forms.ConfigContextFilterForm table = tables.ConfigContextTable - actions = ('add', 'bulk_edit', 'bulk_delete') + template_name = 'extras/configcontext_list.html' + actions = ('add', 'bulk_edit', 'bulk_delete', 'bulk_sync') @register_model_view(ConfigContext) @@ -416,6 +423,10 @@ class ConfigContextBulkDeleteView(generic.BulkDeleteView): table = tables.ConfigContextTable +class ConfigContextBulkSyncDataView(generic.BulkSyncDataView): + queryset = ConfigContext.objects.all() + + class ObjectConfigContextView(generic.ObjectView): base_template = None template_name = 'extras/object_configcontext.html' @@ -441,6 +452,58 @@ class ObjectConfigContextView(generic.ObjectView): } +# +# Config templates +# + +class ConfigTemplateListView(generic.ObjectListView): + queryset = ConfigTemplate.objects.all() + filterset = filtersets.ConfigTemplateFilterSet + filterset_form = forms.ConfigTemplateFilterForm + table = tables.ConfigTemplateTable + template_name = 'extras/configtemplate_list.html' + actions = ('add', 'import', 'export', 'bulk_edit', 'bulk_delete', 'bulk_sync') + + +@register_model_view(ConfigTemplate) +class ConfigTemplateView(generic.ObjectView): + queryset = ConfigTemplate.objects.all() + + +@register_model_view(ConfigTemplate, 'edit') +class ConfigTemplateEditView(generic.ObjectEditView): + queryset = ConfigTemplate.objects.all() + form = forms.ConfigTemplateForm + + +@register_model_view(ConfigTemplate, 'delete') +class ConfigTemplateDeleteView(generic.ObjectDeleteView): + queryset = ConfigTemplate.objects.all() + + +class ConfigTemplateBulkImportView(generic.BulkImportView): + queryset = ConfigTemplate.objects.all() + model_form = forms.ConfigTemplateImportForm + table = tables.ConfigTemplateTable + + +class ConfigTemplateBulkEditView(generic.BulkEditView): + queryset = ConfigTemplate.objects.all() + filterset = filtersets.ConfigTemplateFilterSet + table = tables.ConfigTemplateTable + form = forms.ConfigTemplateBulkEditForm + + +class ConfigTemplateBulkDeleteView(generic.BulkDeleteView): + queryset = ConfigTemplate.objects.all() + filterset = filtersets.ConfigTemplateFilterSet + table = tables.ConfigTemplateTable + + +class ConfigTemplateBulkSyncDataView(generic.BulkSyncDataView): + queryset = ConfigTemplate.objects.all() + + # # Change logging # diff --git a/netbox/ipam/filtersets.py b/netbox/ipam/filtersets.py index 4bb431481..a1362803a 100644 --- a/netbox/ipam/filtersets.py +++ b/netbox/ipam/filtersets.py @@ -407,6 +407,14 @@ class IPRangeFilterSet(TenancyFilterSet, NetBoxModelFilterSet): field_name='start_address', lookup_expr='family' ) + start_address = MultiValueCharFilter( + method='filter_address', + label=_('Address'), + ) + end_address = MultiValueCharFilter( + method='filter_address', + label=_('Address'), + ) contains = django_filters.CharFilter( method='search_contains', label=_('Ranges which contain this prefix or IP'), @@ -443,9 +451,9 @@ class IPRangeFilterSet(TenancyFilterSet, NetBoxModelFilterSet): def search(self, queryset, name, value): if not value.strip(): return queryset - qs_filter = Q(description__icontains=value) + qs_filter = Q(description__icontains=value) | Q(start_address__contains=value) | Q(end_address__contains=value) try: - ipaddress = str(netaddr.IPNetwork(value.strip()).cidr) + ipaddress = str(netaddr.IPNetwork(value.strip())) qs_filter |= Q(start_address=ipaddress) qs_filter |= Q(end_address=ipaddress) except (AddrFormatError, ValueError): @@ -463,6 +471,12 @@ class IPRangeFilterSet(TenancyFilterSet, NetBoxModelFilterSet): except (AddrFormatError, ValueError): return queryset.none() + def filter_address(self, queryset, name, value): + try: + return queryset.filter(**{f'{name}__net_in': value}) + except ValidationError: + return queryset.none() + class IPAddressFilterSet(NetBoxModelFilterSet, TenancyFilterSet): family = django_filters.NumberFilter( diff --git a/netbox/ipam/forms/bulk_edit.py b/netbox/ipam/forms/bulk_edit.py index d0af43975..e63b34d75 100644 --- a/netbox/ipam/forms/bulk_edit.py +++ b/netbox/ipam/forms/bulk_edit.py @@ -9,8 +9,8 @@ from ipam.models import ASN from netbox.forms import NetBoxModelBulkEditForm from tenancy.models import Tenant from utilities.forms import ( - add_blank_choice, BulkEditNullBooleanSelect, CommentField, DynamicModelChoiceField, NumericArrayField, - SmallTextarea, StaticSelect, DynamicModelMultipleChoiceField, + add_blank_choice, BulkEditNullBooleanSelect, CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, + NumericArrayField, ) __all__ = ( @@ -48,7 +48,7 @@ class VRFBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -69,7 +69,7 @@ class RouteTargetBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -116,7 +116,7 @@ class ASNBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -145,7 +145,7 @@ class AggregateBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -205,8 +205,7 @@ class PrefixBulkEditForm(NetBoxModelBulkEditForm): ) status = forms.ChoiceField( choices=add_blank_choice(PrefixStatusChoices), - required=False, - widget=StaticSelect() + required=False ) role = DynamicModelChoiceField( queryset=Role.objects.all(), @@ -227,7 +226,7 @@ class PrefixBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -254,8 +253,7 @@ class IPRangeBulkEditForm(NetBoxModelBulkEditForm): ) status = forms.ChoiceField( choices=add_blank_choice(IPRangeStatusChoices), - required=False, - widget=StaticSelect() + required=False ) role = DynamicModelChoiceField( queryset=Role.objects.all(), @@ -266,7 +264,7 @@ class IPRangeBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -296,13 +294,11 @@ class IPAddressBulkEditForm(NetBoxModelBulkEditForm): ) status = forms.ChoiceField( choices=add_blank_choice(IPAddressStatusChoices), - required=False, - widget=StaticSelect() + required=False ) role = forms.ChoiceField( choices=add_blank_choice(IPAddressRoleChoices), - required=False, - widget=StaticSelect() + required=False ) dns_name = forms.CharField( max_length=255, @@ -314,7 +310,7 @@ class IPAddressBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -331,8 +327,7 @@ class IPAddressBulkEditForm(NetBoxModelBulkEditForm): class FHRPGroupBulkEditForm(NetBoxModelBulkEditForm): protocol = forms.ChoiceField( choices=add_blank_choice(FHRPGroupProtocolChoices), - required=False, - widget=StaticSelect() + required=False ) group_id = forms.IntegerField( min_value=0, @@ -342,7 +337,6 @@ class FHRPGroupBulkEditForm(NetBoxModelBulkEditForm): auth_type = forms.ChoiceField( choices=add_blank_choice(FHRPGroupAuthTypeChoices), required=False, - widget=StaticSelect(), label=_('Authentication type') ) auth_key = forms.CharField( @@ -359,7 +353,7 @@ class FHRPGroupBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -430,8 +424,7 @@ class VLANBulkEditForm(NetBoxModelBulkEditForm): ) status = forms.ChoiceField( choices=add_blank_choice(VLANStatusChoices), - required=False, - widget=StaticSelect() + required=False ) role = DynamicModelChoiceField( queryset=Role.objects.all(), @@ -442,7 +435,7 @@ class VLANBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -459,8 +452,7 @@ class VLANBulkEditForm(NetBoxModelBulkEditForm): class ServiceTemplateBulkEditForm(NetBoxModelBulkEditForm): protocol = forms.ChoiceField( choices=add_blank_choice(ServiceProtocolChoices), - required=False, - widget=StaticSelect() + required=False ) ports = NumericArrayField( base_field=forms.IntegerField( @@ -474,7 +466,7 @@ class ServiceTemplateBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) @@ -492,8 +484,7 @@ class ServiceBulkEditForm(ServiceTemplateBulkEditForm): class L2VPNBulkEditForm(NetBoxModelBulkEditForm): type = forms.ChoiceField( choices=add_blank_choice(L2VPNTypeChoices), - required=False, - widget=StaticSelect() + required=False ) tenant = DynamicModelChoiceField( queryset=Tenant.objects.all(), @@ -504,7 +495,7 @@ class L2VPNBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) diff --git a/netbox/ipam/forms/filtersets.py b/netbox/ipam/forms/filtersets.py index 7e790a68a..1d505a168 100644 --- a/netbox/ipam/forms/filtersets.py +++ b/netbox/ipam/forms/filtersets.py @@ -10,7 +10,7 @@ from netbox.forms import NetBoxModelFilterSetForm from tenancy.forms import TenancyFilterForm from utilities.forms import ( add_blank_choice, ContentTypeMultipleChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, - MultipleChoiceField, StaticSelect, TagFilterField, BOOLEAN_WITH_BLANK_CHOICES, + TagFilterField, BOOLEAN_WITH_BLANK_CHOICES, ) from virtualization.models import VirtualMachine @@ -87,7 +87,7 @@ class RIRFilterForm(NetBoxModelFilterSetForm): is_private = forms.NullBooleanField( required=False, label=_('Private'), - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -104,8 +104,7 @@ class AggregateFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): family = forms.ChoiceField( required=False, choices=add_blank_choice(IPAddressFamilyChoices), - label=_('Address family'), - widget=StaticSelect() + label=_('Address family') ) rir_id = DynamicModelMultipleChoiceField( queryset=RIR.objects.all(), @@ -164,10 +163,9 @@ class PrefixFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): family = forms.ChoiceField( required=False, choices=add_blank_choice(IPAddressFamilyChoices), - label=_('Address family'), - widget=StaticSelect() + label=_('Address family') ) - mask_length = MultipleChoiceField( + mask_length = forms.MultipleChoiceField( required=False, choices=PREFIX_MASK_LENGTH_CHOICES, label=_('Mask length') @@ -183,7 +181,7 @@ class PrefixFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): required=False, label=_('Present in VRF') ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=PrefixStatusChoices, required=False ) @@ -215,14 +213,14 @@ class PrefixFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): is_pool = forms.NullBooleanField( required=False, label=_('Is a pool'), - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) mark_utilized = forms.NullBooleanField( required=False, label=_('Marked as 100% utilized'), - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -239,8 +237,7 @@ class IPRangeFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): family = forms.ChoiceField( required=False, choices=add_blank_choice(IPAddressFamilyChoices), - label=_('Address family'), - widget=StaticSelect() + label=_('Address family') ) vrf_id = DynamicModelMultipleChoiceField( queryset=VRF.objects.all(), @@ -248,7 +245,7 @@ class IPRangeFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): label=_('Assigned VRF'), null_option='Global' ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=IPRangeStatusChoices, required=False ) @@ -282,14 +279,12 @@ class IPAddressFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): family = forms.ChoiceField( required=False, choices=add_blank_choice(IPAddressFamilyChoices), - label=_('Address family'), - widget=StaticSelect() + label=_('Address family') ) mask_length = forms.ChoiceField( required=False, choices=IPADDRESS_MASK_LENGTH_CHOICES, - label=_('Mask length'), - widget=StaticSelect() + label=_('Mask length') ) vrf_id = DynamicModelMultipleChoiceField( queryset=VRF.objects.all(), @@ -312,18 +307,18 @@ class IPAddressFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): required=False, label=_('Assigned VM'), ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=IPAddressStatusChoices, required=False ) - role = MultipleChoiceField( + role = forms.MultipleChoiceField( choices=IPAddressRoleChoices, required=False ) assigned_to_interface = forms.NullBooleanField( required=False, label=_('Assigned to an interface'), - widget=StaticSelect( + widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) @@ -340,7 +335,7 @@ class FHRPGroupFilterForm(NetBoxModelFilterSetForm): name = forms.CharField( required=False ) - protocol = MultipleChoiceField( + protocol = forms.MultipleChoiceField( choices=FHRPGroupProtocolChoices, required=False ) @@ -349,7 +344,7 @@ class FHRPGroupFilterForm(NetBoxModelFilterSetForm): required=False, label='Group ID' ) - auth_type = MultipleChoiceField( + auth_type = forms.MultipleChoiceField( choices=FHRPGroupAuthTypeChoices, required=False, label='Authentication type' @@ -444,7 +439,7 @@ class VLANFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): }, label=_('VLAN group') ) - status = MultipleChoiceField( + status = forms.MultipleChoiceField( choices=VLANStatusChoices, required=False ) @@ -474,8 +469,7 @@ class ServiceTemplateFilterForm(NetBoxModelFilterSetForm): ) protocol = forms.ChoiceField( choices=add_blank_choice(ServiceProtocolChoices), - required=False, - widget=StaticSelect() + required=False ) port = forms.IntegerField( required=False, @@ -497,8 +491,7 @@ class L2VPNFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): ) type = forms.ChoiceField( choices=add_blank_choice(L2VPNTypeChoices), - required=False, - widget=StaticSelect() + required=False ) import_target_id = DynamicModelMultipleChoiceField( queryset=RouteTarget.objects.all(), diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index 430a4b2f8..4e50c4949 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -13,7 +13,7 @@ from tenancy.forms import TenancyForm from utilities.exceptions import PermissionsViolation from utilities.forms import ( add_blank_choice, BootstrapMixin, CommentField, ContentTypeChoiceField, DatePicker, DynamicModelChoiceField, - DynamicModelMultipleChoiceField, NumericArrayField, SlugField, StaticSelect, StaticSelectMultiple, + DynamicModelMultipleChoiceField, NumericArrayField, SlugField, ) from virtualization.models import Cluster, ClusterGroup, VirtualMachine, VMInterface @@ -254,9 +254,6 @@ class PrefixForm(TenancyForm, NetBoxModelForm): 'prefix', 'vrf', 'site', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized', 'tenant_group', 'tenant', 'description', 'comments', 'tags', ] - widgets = { - 'status': StaticSelect(), - } class IPRangeForm(TenancyForm, NetBoxModelForm): @@ -282,9 +279,6 @@ class IPRangeForm(TenancyForm, NetBoxModelForm): 'vrf', 'start_address', 'end_address', 'status', 'role', 'tenant_group', 'tenant', 'description', 'comments', 'tags', ] - widgets = { - 'status': StaticSelect(), - } class IPAddressForm(TenancyForm, NetBoxModelForm): @@ -411,10 +405,6 @@ class IPAddressForm(TenancyForm, NetBoxModelForm): 'nat_cluster', 'nat_virtual_machine', 'nat_vrf', 'nat_inside', 'tenant_group', 'tenant', 'description', 'comments', 'tags', ] - widgets = { - 'status': StaticSelect(), - 'role': StaticSelect(), - } def __init__(self, *args, **kwargs): @@ -510,10 +500,6 @@ class IPAddressBulkAddForm(TenancyForm, NetBoxModelForm): fields = [ 'address', 'vrf', 'status', 'role', 'dns_name', 'description', 'tenant_group', 'tenant', 'tags', ] - widgets = { - 'status': StaticSelect(), - 'role': StaticSelect(), - } class IPAddressAssignForm(BootstrapMixin, forms.Form): @@ -559,11 +545,6 @@ class FHRPGroupForm(NetBoxModelForm): 'protocol', 'group_id', 'auth_type', 'auth_key', 'name', 'ip_vrf', 'ip_address', 'ip_status', 'description', 'comments', 'tags', ) - widgets = { - 'protocol': StaticSelect(), - 'auth_type': StaticSelect(), - 'ip_status': StaticSelect(), - } def save(self, *args, **kwargs): instance = super().save(*args, **kwargs) @@ -700,9 +681,6 @@ class VLANGroupForm(NetBoxModelForm): 'name', 'slug', 'description', 'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack', 'clustergroup', 'cluster', 'min_vid', 'max_vid', 'tags', ] - widgets = { - 'scope_type': StaticSelect, - } def __init__(self, *args, **kwargs): instance = kwargs.get('instance') @@ -740,7 +718,6 @@ class VLANForm(TenancyForm, NetBoxModelForm): ('virtualization.cluster', 'Cluster'), ), required=False, - widget=StaticSelect, label=_('Group scope') ) group = DynamicModelChoiceField( @@ -800,9 +777,6 @@ class VLANForm(TenancyForm, NetBoxModelForm): 'status': _("Operational status of this VLAN"), 'role': _("The primary function of this VLAN"), } - widgets = { - 'status': StaticSelect(), - } class ServiceTemplateForm(NetBoxModelForm): @@ -824,9 +798,6 @@ class ServiceTemplateForm(NetBoxModelForm): class Meta: model = ServiceTemplate fields = ('name', 'protocol', 'ports', 'description', 'comments', 'tags') - widgets = { - 'protocol': StaticSelect(), - } class ServiceForm(NetBoxModelForm): @@ -865,10 +836,6 @@ class ServiceForm(NetBoxModelForm): 'ipaddresses': _("IP address assignment is optional. If no IPs are selected, the service is assumed to be " "reachable via all IPs assigned to the device."), } - widgets = { - 'protocol': StaticSelect(), - 'ipaddresses': StaticSelectMultiple(), - } class ServiceCreateForm(ServiceForm): @@ -934,9 +901,6 @@ class L2VPNForm(TenancyForm, NetBoxModelForm): 'name', 'slug', 'type', 'identifier', 'import_targets', 'export_targets', 'tenant', 'description', 'comments', 'tags' ) - widgets = { - 'type': StaticSelect(), - } class L2VPNTerminationForm(NetBoxModelForm): diff --git a/netbox/ipam/models/fhrp.py b/netbox/ipam/models/fhrp.py index 759a6e1d3..1044a5cde 100644 --- a/netbox/ipam/models/fhrp.py +++ b/netbox/ipam/models/fhrp.py @@ -5,7 +5,6 @@ from django.db import models from django.urls import reverse from netbox.models import ChangeLoggedModel, PrimaryModel -from netbox.models.features import WebhooksMixin from ipam.choices import * from ipam.constants import * @@ -73,7 +72,7 @@ class FHRPGroup(PrimaryModel): return reverse('ipam:fhrpgroup', args=[self.pk]) -class FHRPGroupAssignment(WebhooksMixin, ChangeLoggedModel): +class FHRPGroupAssignment(ChangeLoggedModel): interface_type = models.ForeignKey( to=ContentType, on_delete=models.CASCADE diff --git a/netbox/ipam/search.py b/netbox/ipam/search.py index fd6db6a63..ad4403321 100644 --- a/netbox/ipam/search.py +++ b/netbox/ipam/search.py @@ -6,7 +6,7 @@ from netbox.search import SearchIndex, register_search class AggregateIndex(SearchIndex): model = models.Aggregate fields = ( - ('prefix', 100), + ('prefix', 120), ('description', 500), ('date_added', 2000), ('comments', 5000), @@ -70,7 +70,7 @@ class L2VPNIndex(SearchIndex): class PrefixIndex(SearchIndex): model = models.Prefix fields = ( - ('prefix', 100), + ('prefix', 110), ('description', 500), ('comments', 5000), ) diff --git a/netbox/ipam/tests/test_filtersets.py b/netbox/ipam/tests/test_filtersets.py index 711009a7e..13b3ae163 100644 --- a/netbox/ipam/tests/test_filtersets.py +++ b/netbox/ipam/tests/test_filtersets.py @@ -680,6 +680,14 @@ class IPRangeTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'family': '6'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) + def test_start_address(self): + params = {'start_address': ['10.0.1.100', '10.0.2.100']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_end_address(self): + params = {'end_address': ['10.0.1.199', '10.0.2.199']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_contains(self): params = {'contains': '10.0.1.150/24'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) diff --git a/netbox/netbox/api/features.py b/netbox/netbox/api/features.py new file mode 100644 index 000000000..db018ca12 --- /dev/null +++ b/netbox/netbox/api/features.py @@ -0,0 +1,30 @@ +from django.shortcuts import get_object_or_404 +from rest_framework.decorators import action +from rest_framework.exceptions import PermissionDenied +from rest_framework.response import Response + +from utilities.permissions import get_permission_for_model + +__all__ = ( + 'SyncedDataMixin', +) + + +class SyncedDataMixin: + + @action(detail=True, methods=['post']) + def sync(self, request, pk): + """ + Provide a /sync API endpoint to synchronize an object's data from its associated DataFile (if any). + """ + permission = get_permission_for_model(self.queryset.model, 'sync') + if not request.user.has_perm(permission): + raise PermissionDenied(f"Missing permission: {permission}") + + obj = get_object_or_404(self.queryset, pk=pk) + if obj.data_file: + obj.sync_data() + obj.save() + serializer = self.serializer_class(obj, context={'request': request}) + + return Response(serializer.data) diff --git a/netbox/netbox/api/renderers.py b/netbox/netbox/api/renderers.py index c492510fb..e41fbe450 100644 --- a/netbox/netbox/api/renderers.py +++ b/netbox/netbox/api/renderers.py @@ -1,4 +1,9 @@ -from rest_framework.renderers import BrowsableAPIRenderer +from rest_framework.renderers import BaseRenderer, BrowsableAPIRenderer + +__all__ = ( + 'FormlessBrowsableAPIRenderer', + 'TextRenderer', +) class FormlessBrowsableAPIRenderer(BrowsableAPIRenderer): @@ -10,3 +15,14 @@ class FormlessBrowsableAPIRenderer(BrowsableAPIRenderer): def get_filter_form(self, data, view, request): return None + + +class TextRenderer(BaseRenderer): + """ + Return raw data as plain text. + """ + media_type = 'text/plain' + format = 'txt' + + def render(self, data, accepted_media_type=None, renderer_context=None): + return str(data) diff --git a/netbox/netbox/api/views.py b/netbox/netbox/api/views.py index fecf6d985..5c55697ff 100644 --- a/netbox/netbox/api/views.py +++ b/netbox/netbox/api/views.py @@ -29,6 +29,7 @@ class APIRootView(APIView): return Response({ 'circuits': reverse('circuits-api:api-root', request=request, format=format), + 'core': reverse('core-api:api-root', request=request, format=format), 'dcim': reverse('dcim-api:api-root', request=request, format=format), 'extras': reverse('extras-api:api-root', request=request, format=format), 'ipam': reverse('ipam-api:api-root', request=request, format=format), diff --git a/netbox/netbox/configuration_example.py b/netbox/netbox/configuration_example.py index 14fcde022..7158308af 100644 --- a/netbox/netbox/configuration_example.py +++ b/netbox/netbox/configuration_example.py @@ -217,6 +217,10 @@ RQ_DEFAULT_TIMEOUT = 300 # this setting is derived from the installed location. # SCRIPTS_ROOT = '/opt/netbox/netbox/scripts' +# The maximum size (in bytes) that an upload will be before it gets streamed to the file system. +# Useful to be able to upload files bigger than 2.5Mbyte to custom scripts for processing. +# FILE_UPLOAD_MAX_MEMORY_SIZE = 2621440 + # The name to use for the csrf token cookie. CSRF_COOKIE_NAME = 'csrftoken' diff --git a/netbox/netbox/forms/__init__.py b/netbox/netbox/forms/__init__.py index d8acef94c..65460ebf1 100644 --- a/netbox/netbox/forms/__init__.py +++ b/netbox/netbox/forms/__init__.py @@ -5,7 +5,7 @@ from django.utils.translation import gettext as _ from netbox.search import LookupTypes from netbox.search.backends import search_backend -from utilities.forms import BootstrapMixin, StaticSelect, StaticSelectMultiple +from utilities.forms import BootstrapMixin from .base import * @@ -32,14 +32,12 @@ class SearchForm(BootstrapMixin, forms.Form): obj_types = forms.MultipleChoiceField( choices=[], required=False, - label=_('Object type(s)'), - widget=StaticSelectMultiple() + label=_('Object type(s)') ) lookup = forms.ChoiceField( choices=LOOKUP_CHOICES, initial=LookupTypes.PARTIAL, - required=False, - widget=StaticSelect() + required=False ) def __init__(self, *args, **kwargs): diff --git a/netbox/netbox/graphql/fields.py b/netbox/netbox/graphql/fields.py index 5b8e6cc5b..7c359e82e 100644 --- a/netbox/netbox/graphql/fields.py +++ b/netbox/netbox/graphql/fields.py @@ -60,6 +60,8 @@ class ObjectListField(DjangoListField): filterset_class = django_object_type._meta.filterset_class if filterset_class: filterset = filterset_class(data=args, queryset=queryset, request=info.context) + if not filterset.is_valid(): + return queryset.none() return filterset.qs return queryset diff --git a/netbox/netbox/graphql/schema.py b/netbox/netbox/graphql/schema.py index 82abfb4d5..7224f3c38 100644 --- a/netbox/netbox/graphql/schema.py +++ b/netbox/netbox/graphql/schema.py @@ -1,6 +1,7 @@ import graphene from circuits.graphql.schema import CircuitsQuery +from core.graphql.schema import CoreQuery from dcim.graphql.schema import DCIMQuery from extras.graphql.schema import ExtrasQuery from ipam.graphql.schema import IPAMQuery @@ -14,6 +15,7 @@ from wireless.graphql.schema import WirelessQuery class Query( UsersQuery, CircuitsQuery, + CoreQuery, DCIMQuery, ExtrasQuery, IPAMQuery, diff --git a/netbox/netbox/models/__init__.py b/netbox/netbox/models/__init__.py index a4c8e0ec2..db8179fdc 100644 --- a/netbox/netbox/models/__init__.py +++ b/netbox/netbox/models/__init__.py @@ -38,7 +38,7 @@ class NetBoxFeatureSet( # Base model classes # -class ChangeLoggedModel(ChangeLoggingMixin, CustomValidationMixin, models.Model): +class ChangeLoggedModel(ChangeLoggingMixin, CustomValidationMixin, WebhooksMixin, models.Model): """ Base model for ancillary models; provides limited functionality for models which don't support NetBox's full feature set. diff --git a/netbox/netbox/models/features.py b/netbox/netbox/models/features.py index 8e5af0ab5..e70d3df7b 100644 --- a/netbox/netbox/models/features.py +++ b/netbox/netbox/models/features.py @@ -2,15 +2,17 @@ from collections import defaultdict from functools import cached_property from django.contrib.contenttypes.fields import GenericRelation -from django.db.models.signals import class_prepared -from django.dispatch import receiver - from django.core.validators import ValidationError from django.db import models +from django.db.models.signals import class_prepared +from django.dispatch import receiver +from django.utils.translation import gettext as _ + from taggit.managers import TaggableManager from extras.choices import CustomFieldVisibilityChoices, ObjectChangeActionChoices from extras.utils import is_taggable, register_features +from netbox.registry import registry from netbox.signals import post_clean from utilities.json import CustomFieldJSONEncoder from utilities.utils import serialize_object @@ -25,6 +27,7 @@ __all__ = ( 'ExportTemplatesMixin', 'JobResultsMixin', 'JournalingMixin', + 'SyncedDataMixin', 'TagsMixin', 'WebhooksMixin', ) @@ -257,6 +260,10 @@ class CustomValidationMixin(models.Model): def clean(self): super().clean() + # If the instance is a base for replications, skip custom validation + if getattr(self, '_replicated_base', False): + return + # Send the post_clean signal post_clean.send(sender=self.__class__, instance=self) @@ -313,21 +320,95 @@ class WebhooksMixin(models.Model): abstract = True -FEATURES_MAP = ( - ('custom_fields', CustomFieldsMixin), - ('custom_links', CustomLinksMixin), - ('export_templates', ExportTemplatesMixin), - ('job_results', JobResultsMixin), - ('journaling', JournalingMixin), - ('tags', TagsMixin), - ('webhooks', WebhooksMixin), -) +class SyncedDataMixin(models.Model): + """ + Enables population of local data from a DataFile object, synchronized from a remote DatSource. + """ + data_source = models.ForeignKey( + to='core.DataSource', + on_delete=models.PROTECT, + blank=True, + null=True, + related_name='+', + help_text=_("Remote data source") + ) + data_file = models.ForeignKey( + to='core.DataFile', + on_delete=models.SET_NULL, + blank=True, + null=True, + related_name='+' + ) + data_path = models.CharField( + max_length=1000, + blank=True, + editable=False, + help_text=_("Path to remote file (relative to data source root)") + ) + data_synced = models.DateTimeField( + blank=True, + null=True, + editable=False + ) + + class Meta: + abstract = True + + @property + def is_synced(self): + return self.data_file and self.data_synced >= self.data_file.last_updated + + def clean(self): + if self.data_file: + self.sync_data() + self.data_path = self.data_file.path + + if self.data_source and not self.data_file: + raise ValidationError({ + 'data_file': _(f"Must specify a data file when designating a data source.") + }) + if self.data_file and not self.data_source: + self.data_source = self.data_file.source + + super().clean() + + def resolve_data_file(self): + """ + Determine the designated DataFile object identified by its parent DataSource and its path. Returns None if + either attribute is unset, or if no matching DataFile is found. + """ + from core.models import DataFile + + if self.data_source and self.data_path: + try: + return DataFile.objects.get(source=self.data_source, path=self.data_path) + except DataFile.DoesNotExist: + pass + + def sync_data(self): + raise NotImplementedError(f"{self.__class__} must implement a sync_data() method.") + + +FEATURES_MAP = { + 'custom_fields': CustomFieldsMixin, + 'custom_links': CustomLinksMixin, + 'export_templates': ExportTemplatesMixin, + 'job_results': JobResultsMixin, + 'journaling': JournalingMixin, + 'synced_data': SyncedDataMixin, + 'tags': TagsMixin, + 'webhooks': WebhooksMixin, +} + +registry['model_features'].update({ + feature: defaultdict(set) for feature in FEATURES_MAP.keys() +}) @receiver(class_prepared) def _register_features(sender, **kwargs): features = { - feature for feature, cls in FEATURES_MAP if issubclass(sender, cls) + feature for feature, cls in FEATURES_MAP.items() if issubclass(sender, cls) } register_features(sender, features) @@ -344,3 +425,9 @@ def _register_features(sender, **kwargs): 'changelog', kwargs={'model': sender} )('netbox.views.generic.ObjectChangeLogView') + if issubclass(sender, SyncedDataMixin): + register_model_view( + sender, + 'sync', + kwargs={'model': sender} + )('netbox.views.generic.ObjectSyncDataView') diff --git a/netbox/netbox/navigation/menu.py b/netbox/netbox/navigation/menu.py index 83a81690f..03c361002 100644 --- a/netbox/netbox/navigation/menu.py +++ b/netbox/netbox/navigation/menu.py @@ -287,6 +287,7 @@ OTHER_MENU = Menu( MenuGroup( label=_('Integrations'), items=( + get_model_item('core', 'datasource', _('Data Sources')), get_model_item('extras', 'webhook', _('Webhooks')), MenuItem( link='extras:report_list', @@ -310,6 +311,7 @@ OTHER_MENU = Menu( items=( get_model_item('extras', 'tag', 'Tags'), get_model_item('extras', 'configcontext', _('Config Contexts'), actions=['add']), + get_model_item('extras', 'configtemplate', _('Config Templates'), actions=['add']), ), ), ), diff --git a/netbox/netbox/registry.py b/netbox/netbox/registry.py index 76886e791..e37ee0d0c 100644 --- a/netbox/netbox/registry.py +++ b/netbox/netbox/registry.py @@ -1,12 +1,10 @@ import collections -from extras.constants import EXTRAS_FEATURES - 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). + Central registry for registration of functionality. Once a Registry is initialized, keys cannot be added or + removed (though the value of each key is mutable). """ def __getitem__(self, key): try: @@ -15,19 +13,18 @@ class Registry(dict): raise KeyError(f"Invalid store: {key}") def __setitem__(self, key, value): - if key in self: - raise KeyError(f"Store already set: {key}") - super().__setitem__(key, value) + raise TypeError("Cannot add stores to registry after initialization") def __delitem__(self, key): raise TypeError("Cannot delete stores from registry") # Initialize the global registry -registry = Registry() -registry['model_features'] = { - feature: collections.defaultdict(set) for feature in EXTRAS_FEATURES -} -registry['denormalized_fields'] = collections.defaultdict(list) -registry['search'] = dict() -registry['views'] = collections.defaultdict(dict) +registry = Registry({ + 'data_backends': dict(), + 'denormalized_fields': collections.defaultdict(list), + 'model_features': dict(), + 'plugins': dict(), + 'search': dict(), + 'views': collections.defaultdict(dict), +}) diff --git a/netbox/netbox/search/__init__.py b/netbox/netbox/search/__init__.py index 1eec8e097..6d53e9a97 100644 --- a/netbox/netbox/search/__init__.py +++ b/netbox/netbox/search/__init__.py @@ -2,6 +2,7 @@ from collections import namedtuple from django.db import models +from ipam.fields import IPAddressField, IPNetworkField from netbox.registry import registry ObjectFieldValue = namedtuple('ObjectFieldValue', ('name', 'type', 'weight', 'value')) @@ -11,6 +12,8 @@ class FieldTypes: FLOAT = 'float' INTEGER = 'int' STRING = 'str' + INET = 'inet' + CIDR = 'cidr' class LookupTypes: @@ -43,6 +46,10 @@ class SearchIndex: field_cls = instance._meta.get_field(field_name).__class__ if issubclass(field_cls, (models.FloatField, models.DecimalField)): return FieldTypes.FLOAT + if issubclass(field_cls, IPAddressField): + return FieldTypes.INET + if issubclass(field_cls, IPNetworkField): + return FieldTypes.CIDR if issubclass(field_cls, models.IntegerField): return FieldTypes.INTEGER return FieldTypes.STRING diff --git a/netbox/netbox/search/backends.py b/netbox/netbox/search/backends.py index d659a7abb..10e164c09 100644 --- a/netbox/netbox/search/backends.py +++ b/netbox/netbox/search/backends.py @@ -3,10 +3,12 @@ from collections import defaultdict from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ImproperlyConfigured -from django.db.models import F, Window +from django.db.models import F, Window, Q from django.db.models.functions import window from django.db.models.signals import post_delete, post_save from django.utils.module_loading import import_string +import netaddr +from netaddr.core import AddrFormatError from extras.models import CachedValue, CustomField from netbox.registry import registry @@ -95,18 +97,24 @@ class CachedValueSearchBackend(SearchBackend): def search(self, value, user=None, object_types=None, lookup=DEFAULT_LOOKUP_TYPE): - # Define the search parameters - params = { - f'value__{lookup}': value - } + query_filter = Q(**{f'value__{lookup}': value}) + + if object_types: + query_filter &= Q(object_type__in=object_types) + if lookup in (LookupTypes.STARTSWITH, LookupTypes.ENDSWITH): # Partial string matches are valid only on string values - params['type'] = FieldTypes.STRING - if object_types: - params['object_type__in'] = object_types + query_filter &= Q(type=FieldTypes.STRING) + + if lookup == LookupTypes.PARTIAL: + try: + address = str(netaddr.IPNetwork(value.strip()).cidr) + query_filter |= Q(type=FieldTypes.CIDR) & Q(value__net_contains_or_equals=address) + except (AddrFormatError, ValueError): + pass # Construct the base queryset to retrieve matching results - queryset = CachedValue.objects.filter(**params).annotate( + queryset = CachedValue.objects.filter(query_filter).annotate( # Annotate the rank of each result for its object according to its weight row_number=Window( expression=window.RowNumber(), diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 224d3617b..4562b4906 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -91,6 +91,7 @@ DOCS_ROOT = getattr(configuration, 'DOCS_ROOT', os.path.join(os.path.dirname(BAS EMAIL = getattr(configuration, 'EMAIL', {}) EXEMPT_VIEW_PERMISSIONS = getattr(configuration, 'EXEMPT_VIEW_PERMISSIONS', []) FIELD_CHOICES = getattr(configuration, 'FIELD_CHOICES', {}) +FILE_UPLOAD_MAX_MEMORY_SIZE = getattr(configuration, 'FILE_UPLOAD_MAX_MEMORY_SIZE', 2621440) HTTP_PROXIES = getattr(configuration, 'HTTP_PROXIES', None) INTERNAL_IPS = getattr(configuration, 'INTERNAL_IPS', ('127.0.0.1', '::1')) JINJA2_FILTERS = getattr(configuration, 'JINJA2_FILTERS', {}) @@ -331,6 +332,7 @@ INSTALLED_APPS = [ 'social_django', 'taggit', 'timezone_field', + 'core', 'circuits', 'dcim', 'ipam', diff --git a/netbox/netbox/tests/test_registry.py b/netbox/netbox/tests/test_registry.py index 25f9e43ec..e834c4356 100644 --- a/netbox/netbox/tests/test_registry.py +++ b/netbox/netbox/tests/test_registry.py @@ -5,29 +5,23 @@ from netbox.registry import Registry class RegistryTest(TestCase): - def test_add_store(self): - reg = Registry() - reg['foo'] = 123 + def test_set_store(self): + reg = Registry({ + 'foo': 123, + }) + with self.assertRaises(TypeError): + reg['bar'] = 456 - self.assertEqual(reg['foo'], 123) - - def test_manipulate_store(self): - reg = Registry() - reg['foo'] = [1, 2] + def test_mutate_store(self): + reg = Registry({ + '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 - + reg = Registry({ + 'foo': 123, + }) with self.assertRaises(TypeError): del reg['foo'] diff --git a/netbox/netbox/urls.py b/netbox/netbox/urls.py index d67525e93..6f3a01e16 100644 --- a/netbox/netbox/urls.py +++ b/netbox/netbox/urls.py @@ -27,6 +27,7 @@ _patterns = [ # Apps path('circuits/', include('circuits.urls')), + path('core/', include('core.urls')), path('dcim/', include('dcim.urls')), path('extras/', include('extras.urls')), path('ipam/', include('ipam.urls')), @@ -38,6 +39,7 @@ _patterns = [ # API path('api/', APIRootView.as_view(), name='api-root'), path('api/circuits/', include('circuits.api.urls')), + path('api/core/', include('core.api.urls')), path('api/dcim/', include('dcim.api.urls')), path('api/extras/', include('extras.api.urls')), path('api/ipam/', include('ipam.api.urls')), diff --git a/netbox/netbox/views/generic/bulk_views.py b/netbox/netbox/views/generic/bulk_views.py index 8dfd2c1e3..325d10338 100644 --- a/netbox/netbox/views/generic/bulk_views.py +++ b/netbox/netbox/views/generic/bulk_views.py @@ -389,8 +389,8 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): 'data': record, 'instance': instance, } - if form.cleaned_data['format'] == ImportFormatChoices.CSV: - model_form_kwargs['headers'] = form._csv_headers + if hasattr(form, '_csv_headers'): + model_form_kwargs['headers'] = form._csv_headers # Add CSV headers model_form = self.model_form(**model_form_kwargs) # When updating, omit all form fields other than those specified in the record. (No diff --git a/netbox/netbox/views/generic/feature_views.py b/netbox/netbox/views/generic/feature_views.py index d4d02ee4e..6e310c97a 100644 --- a/netbox/netbox/views/generic/feature_views.py +++ b/netbox/netbox/views/generic/feature_views.py @@ -1,16 +1,22 @@ from django.contrib.contenttypes.models import ContentType +from django.contrib import messages +from django.db import transaction from django.db.models import Q -from django.shortcuts import get_object_or_404, render +from django.shortcuts import get_object_or_404, redirect, render from django.utils.translation import gettext as _ from django.views.generic import View from extras import forms, tables from extras.models import * -from utilities.views import ViewTab +from utilities.permissions import get_permission_for_model +from utilities.views import GetReturnURLMixin, ViewTab +from .base import BaseMultiObjectView __all__ = ( + 'BulkSyncDataView', 'ObjectChangeLogView', 'ObjectJournalView', + 'ObjectSyncDataView', ) @@ -126,3 +132,49 @@ class ObjectJournalView(View): 'base_template': self.base_template, 'tab': self.tab, }) + + +class ObjectSyncDataView(View): + + def post(self, request, model, **kwargs): + """ + Synchronize data from the DataFile associated with this object. + """ + qs = model.objects.all() + if hasattr(model.objects, 'restrict'): + qs = qs.restrict(request.user, 'sync') + obj = get_object_or_404(qs, **kwargs) + + if not obj.data_file: + messages.error(request, f"Unable to synchronize data: No data file set.") + return redirect(obj.get_absolute_url()) + + obj.sync_data() + obj.save() + messages.success(request, f"Synchronized data for {model._meta.verbose_name} {obj}.") + + return redirect(obj.get_absolute_url()) + + +class BulkSyncDataView(GetReturnURLMixin, BaseMultiObjectView): + """ + Synchronize multiple instances of a model inheriting from SyncedDataMixin. + """ + def get_required_permission(self): + return get_permission_for_model(self.queryset.model, 'sync') + + def post(self, request): + selected_objects = self.queryset.filter( + pk__in=request.POST.getlist('pk'), + data_file__isnull=False + ) + + with transaction.atomic(): + for obj in selected_objects: + obj.sync_data() + obj.save() + + model_name = self.queryset.model._meta.verbose_name_plural + messages.success(request, f"Synced {len(selected_objects)} {model_name}") + + return redirect(self.get_return_url(request)) diff --git a/netbox/netbox/views/generic/object_views.py b/netbox/netbox/views/generic/object_views.py index 795f4ad56..1ba789cf1 100644 --- a/netbox/netbox/views/generic/object_views.py +++ b/netbox/netbox/views/generic/object_views.py @@ -218,6 +218,12 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView): form = self.form(instance=obj, initial=initial_data) restrict_form_fields(form, request.user) + # If this is an HTMX request, return only the rendered form HTML + if is_htmx(request): + return render(request, 'htmx/form.html', { + 'form': form, + }) + return render(request, self.template_name, { 'model': model, 'object': obj, @@ -425,6 +431,12 @@ class ComponentCreateView(GetReturnURLMixin, BaseObjectView): form = self.initialize_form(request) instance = self.alter_object(self.queryset.model(), request) + # If this is an HTMX request, return only the rendered form HTML + if is_htmx(request): + return render(request, 'htmx/form.html', { + 'form': form, + }) + return render(request, self.template_name, { 'object': instance, 'form': form, @@ -436,6 +448,10 @@ class ComponentCreateView(GetReturnURLMixin, BaseObjectView): form = self.initialize_form(request) instance = self.alter_object(self.queryset.model(), request) + # Note that the form instance is a replicated field base + # This is needed to avoid running custom validators multiple times + form.instance._replicated_base = hasattr(self.form, "replication_fields") + if form.is_valid(): new_components = [] data = deepcopy(request.POST) @@ -453,6 +469,9 @@ class ComponentCreateView(GetReturnURLMixin, BaseObjectView): if component_form.is_valid(): new_components.append(component_form) + else: + form.errors.update(component_form.errors) + break if not form.errors and not component_form.errors: try: diff --git a/netbox/project-static/dist/netbox.js b/netbox/project-static/dist/netbox.js index 19cdae0bd..d0058eae9 100644 Binary files a/netbox/project-static/dist/netbox.js and b/netbox/project-static/dist/netbox.js differ diff --git a/netbox/project-static/dist/netbox.js.map b/netbox/project-static/dist/netbox.js.map index d0563b9fc..384195df5 100644 Binary files a/netbox/project-static/dist/netbox.js.map and b/netbox/project-static/dist/netbox.js.map differ diff --git a/netbox/project-static/src/forms/index.ts b/netbox/project-static/src/forms/index.ts index 1ef8540fd..f166c75a8 100644 --- a/netbox/project-static/src/forms/index.ts +++ b/netbox/project-static/src/forms/index.ts @@ -1,10 +1,9 @@ import { initFormElements } from './elements'; import { initSpeedSelector } from './speedSelector'; import { initScopeSelector } from './scopeSelector'; -import { initVlanTags } from './vlanTags'; export function initForms(): void { - for (const func of [initFormElements, initSpeedSelector, initScopeSelector, initVlanTags]) { + for (const func of [initFormElements, initSpeedSelector, initScopeSelector]) { func(); } } diff --git a/netbox/project-static/src/forms/vlanTags.ts b/netbox/project-static/src/forms/vlanTags.ts deleted file mode 100644 index 4ad97c363..000000000 --- a/netbox/project-static/src/forms/vlanTags.ts +++ /dev/null @@ -1,148 +0,0 @@ -import { all, getElement, resetSelect, toggleVisibility as _toggleVisibility } from '../util'; - -/** - * Get a select element's containing `.row` element. - * - * @param element Select element. - * @returns Containing row element. - */ -function fieldContainer(element: Nullable): Nullable { - const container = element?.parentElement?.parentElement ?? null; - if (container !== null && container.classList.contains('row')) { - return container; - } - return null; -} - -/** - * Toggle visibility of the select element's container and disable the select element itself. - * - * @param element Select element. - * @param action 'show' or 'hide' - */ -function toggleVisibility>( - element: E, - action: 'show' | 'hide', -): void { - // Find the select element's containing element. - const parent = fieldContainer(element); - if (element !== null && parent !== null) { - // Toggle container visibility to visually remove it from the form. - _toggleVisibility(parent, action); - // Create a new event so that the APISelect instance properly handles the enable/disable - // action. - const event = new Event(`netbox.select.disabled.${element.name}`); - switch (action) { - case 'hide': - // Disable the native select element and dispatch the event APISelect is listening for. - element.disabled = true; - element.dispatchEvent(event); - break; - case 'show': - // Enable the native select element and dispatch the event APISelect is listening for. - element.disabled = false; - element.dispatchEvent(event); - } - } -} - -/** - * Toggle element visibility when the mode field does not have a value. - */ -function handleModeNone(): void { - const elements = [ - getElement('id_tagged_vlans'), - getElement('id_untagged_vlan'), - getElement('id_vlan_group'), - ]; - - if (all(elements)) { - const [taggedVlans, untaggedVlan] = elements; - resetSelect(untaggedVlan); - resetSelect(taggedVlans); - for (const element of elements) { - toggleVisibility(element, 'hide'); - } - } -} - -/** - * Toggle element visibility when the mode field's value is Access. - */ -function handleModeAccess(): void { - const elements = [ - getElement('id_tagged_vlans'), - getElement('id_untagged_vlan'), - getElement('id_vlan_group'), - ]; - if (all(elements)) { - const [taggedVlans, untaggedVlan, vlanGroup] = elements; - resetSelect(taggedVlans); - toggleVisibility(vlanGroup, 'show'); - toggleVisibility(untaggedVlan, 'show'); - toggleVisibility(taggedVlans, 'hide'); - } -} - -/** - * Toggle element visibility when the mode field's value is Tagged. - */ -function handleModeTagged(): void { - const elements = [ - getElement('id_tagged_vlans'), - getElement('id_untagged_vlan'), - getElement('id_vlan_group'), - ]; - if (all(elements)) { - const [taggedVlans, untaggedVlan, vlanGroup] = elements; - toggleVisibility(taggedVlans, 'show'); - toggleVisibility(vlanGroup, 'show'); - toggleVisibility(untaggedVlan, 'show'); - } -} - -/** - * Toggle element visibility when the mode field's value is Tagged (All). - */ -function handleModeTaggedAll(): void { - const elements = [ - getElement('id_tagged_vlans'), - getElement('id_untagged_vlan'), - getElement('id_vlan_group'), - ]; - if (all(elements)) { - const [taggedVlans, untaggedVlan, vlanGroup] = elements; - resetSelect(taggedVlans); - toggleVisibility(vlanGroup, 'show'); - toggleVisibility(untaggedVlan, 'show'); - toggleVisibility(taggedVlans, 'hide'); - } -} - -/** - * Reset field visibility when the mode field's value changes. - */ -function handleModeChange(element: HTMLSelectElement): void { - switch (element.value) { - case 'access': - handleModeAccess(); - break; - case 'tagged': - handleModeTagged(); - break; - case 'tagged-all': - handleModeTaggedAll(); - break; - case '': - handleModeNone(); - break; - } -} - -export function initVlanTags(): void { - const element = getElement('id_mode'); - if (element !== null) { - element.addEventListener('change', () => handleModeChange(element)); - handleModeChange(element); - } -} diff --git a/netbox/project-static/src/htmx.ts b/netbox/project-static/src/htmx.ts index 70ed4f534..bd80a0c49 100644 --- a/netbox/project-static/src/htmx.ts +++ b/netbox/project-static/src/htmx.ts @@ -1,8 +1,10 @@ import { getElements, isTruthy } from './util'; import { initButtons } from './buttons'; +import { initSelect } from './select'; function initDepedencies(): void { - for (const init of [initButtons]) { + console.log('initDepedencies()'); + for (const init of [initButtons, initSelect]) { init(); } } diff --git a/netbox/templates/core/datafile.html b/netbox/templates/core/datafile.html new file mode 100644 index 000000000..a10f5039d --- /dev/null +++ b/netbox/templates/core/datafile.html @@ -0,0 +1,81 @@ +{% extends 'generic/object.html' %} +{% load buttons %} +{% load custom_links %} +{% load helpers %} +{% load perms %} +{% load plugins %} + +{% block breadcrumbs %} + {{ block.super }} + +{% endblock %} + +{% block controls %} +
+
+ {% plugin_buttons object %} +
+ {% if request.user|can_delete:object %} + {% delete_button object %} + {% endif %} +
+ {% custom_links object %} +
+
+{% endblock controls %} + +{% block content %} +
+
+
+
Data File
+
+ + + + + + + + + + + + + + + + + + + + + +
Source{{ object.source }}
Path + {{ object.path }} + + + +
Last Updated{{ object.last_updated }}
Size{{ object.size }} byte{{ object.size|pluralize }}
SHA256 Hash + {{ object.hash }} + + + +
+
+
+
+
Content
+
+
{{ object.data_as_string }}
+
+
+ {% plugin_left_page object %} +
+
+
+
+ {% plugin_full_width_page object %} +
+
+{% endblock %} diff --git a/netbox/templates/core/datasource.html b/netbox/templates/core/datasource.html new file mode 100644 index 000000000..061017ad7 --- /dev/null +++ b/netbox/templates/core/datasource.html @@ -0,0 +1,114 @@ +{% extends 'generic/object.html' %} +{% load static %} +{% load helpers %} +{% load plugins %} +{% load render_table from django_tables2 %} + +{% block extra_controls %} + {% if perms.core.sync_datasource %} + {% if object.ready_for_sync %} +
+ {% csrf_token %} + +
+ {% else %} + + {% endif %} + {% endif %} +{% endblock %} + +{% block content %} +
+
+
+
Data Source
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name{{ object.name }}
Type{{ object.get_type_display }}
Enabled{% checkmark object.enabled %}
Status{% badge object.get_status_display bg_color=object.get_status_color %}
Last synced{{ object.last_synced|placeholder }}
Description{{ object.description|placeholder }}
URL + {{ object.source_url }} +
Ignore rules + {% if object.ignore_rules %} +
{{ object.ignore_rules }}
+ {% else %} + {{ ''|placeholder }} + {% endif %}
+
+
+ {% include 'inc/panels/tags.html' %} + {% include 'inc/panels/comments.html' %} + {% plugin_left_page object %} +
+
+
+
Backend
+
+ + {% for name, field in object.get_backend.parameters.items %} + + + + + {% empty %} + + + + {% endfor %} +
{{ field.label }}{{ object.parameters|get_key:name|placeholder }}
+ No parameters defined +
+
+
+ {% include 'inc/panels/related_objects.html' %} + {% include 'inc/panels/custom_fields.html' %} + {% plugin_right_page object %} +
+
+
+
+
+
Files
+
+
+ {% plugin_full_width_page object %} +
+
+{% endblock %} diff --git a/netbox/templates/dcim/device.html b/netbox/templates/dcim/device.html index 3c2cc6299..3af7814ae 100644 --- a/netbox/templates/dcim/device.html +++ b/netbox/templates/dcim/device.html @@ -9,9 +9,7 @@
-
- Device -
+
Device
@@ -111,6 +109,10 @@ + + + +
Asset Tag {{ object.asset_tag|placeholder }}
Config Template{{ object.config_template|linkify|placeholder }}
diff --git a/netbox/templates/dcim/device/render_config.html b/netbox/templates/dcim/device/render_config.html new file mode 100644 index 000000000..bc5642e2e --- /dev/null +++ b/netbox/templates/dcim/device/render_config.html @@ -0,0 +1,47 @@ +{% extends 'dcim/device/base.html' %} +{% load static %} + +{% block title %}{{ object }} - Config{% endblock %} + +{% block content %} +
+
+
+
Config Template
+
+ + + + + + + + + + + + + +
Config Template{{ config_template|linkify|placeholder }}
Data Source{{ config_template.data_file.source|linkify|placeholder }}
Data File{{ config_template.data_file|linkify|placeholder }}
+
+
+
+
+
+
Context Data
+
{{ context_data|pprint }}
+
+
+
+
+
+
+ {% if config_template %} +
{{ rendered_config }}
+ {% else %} +
No configuration template found
+ {% endif %} +
+
+
+{% endblock %} diff --git a/netbox/templates/dcim/device_edit.html b/netbox/templates/dcim/device_edit.html index b814e65ef..07e3bbdc9 100644 --- a/netbox/templates/dcim/device_edit.html +++ b/netbox/templates/dcim/device_edit.html @@ -65,6 +65,7 @@
{% render_field form.status %} {% render_field form.platform %} + {% render_field form.config_template %} {% if object.pk %} {% render_field form.primary_ip4 %} {% render_field form.primary_ip6 %} diff --git a/netbox/templates/dcim/devicerole.html b/netbox/templates/dcim/devicerole.html index bc01dbdb7..7c0bf67f6 100644 --- a/netbox/templates/dcim/devicerole.html +++ b/netbox/templates/dcim/devicerole.html @@ -42,6 +42,10 @@ VM Role {% checkmark object.vm_role %} + + Config Template + {{ object.config_template|linkify|placeholder }} +
diff --git a/netbox/templates/dcim/devicetype.html b/netbox/templates/dcim/devicetype.html index 73c82ddae..984898caa 100644 --- a/netbox/templates/dcim/devicetype.html +++ b/netbox/templates/dcim/devicetype.html @@ -27,6 +27,10 @@ Part Number {{ object.part_number|placeholder }} + + Default Platform + {{ object.default_platform|linkify }} + Description {{ object.description|placeholder }} diff --git a/netbox/templates/dcim/interface_edit.html b/netbox/templates/dcim/interface_edit.html deleted file mode 100644 index a044de660..000000000 --- a/netbox/templates/dcim/interface_edit.html +++ /dev/null @@ -1,101 +0,0 @@ -{% extends 'generic/object_edit.html' %} -{% load form_helpers %} - -{% block form %} - {# Render hidden fields #} - {% for field in form.hidden_fields %} - {{ field }} - {% endfor %} - -
-
-
Interface
-
- {% if form.instance.device %} -
- -
- -
-
- {% endif %} - {% render_field form.module %} - {% render_field form.name %} - {% render_field form.type %} - {% render_field form.speed %} - {% render_field form.duplex %} - {% render_field form.label %} - {% render_field form.description %} - {% render_field form.tags %} -
- -
-
-
Addressing
-
- {% render_field form.vrf %} - {% render_field form.mac_address %} - {% render_field form.wwn %} -
- -
-
-
Operation
-
- {% render_field form.mtu %} - {% render_field form.tx_power %} - {% render_field form.enabled %} - {% render_field form.mgmt_only %} - {% render_field form.mark_connected %} -
- -
-
-
Related Interfaces
-
- {% render_field form.parent %} - {% render_field form.bridge %} - {% render_field form.lag %} -
- - {% if form.instance.is_wireless %} -
-
-
Wireless
-
- {% render_field form.rf_role %} - {% render_field form.rf_channel %} - {% render_field form.rf_channel_frequency %} - {% render_field form.rf_channel_width %} - {% render_field form.wireless_lan_group %} - {% render_field form.wireless_lans %} -
- {% endif %} - -
-
-
Power over Ethernet (PoE)
-
- {% render_field form.poe_mode %} - {% render_field form.poe_type %} -
- -
-
-
802.1Q Switching
-
- {% render_field form.mode %} - {% render_field form.vlan_group %} - {% render_field form.untagged_vlan %} - {% render_field form.tagged_vlans %} -
- - {% if form.custom_fields %} -
-
-
Custom Fields
-
- {% render_custom_fields form %} -
- {% endif %} -{% endblock %} diff --git a/netbox/templates/dcim/platform.html b/netbox/templates/dcim/platform.html index 17a313d82..5123699d4 100644 --- a/netbox/templates/dcim/platform.html +++ b/netbox/templates/dcim/platform.html @@ -39,6 +39,10 @@ Manufacturer {{ object.manufacturer|linkify|placeholder }} + + Config Template + {{ object.config_template|linkify|placeholder }} + NAPALM Driver {{ object.napalm_driver|placeholder }} diff --git a/netbox/templates/dcim/rack_elevation_list.html b/netbox/templates/dcim/rack_elevation_list.html index c9d9a248a..bd02c9f74 100644 --- a/netbox/templates/dcim/rack_elevation_list.html +++ b/netbox/templates/dcim/rack_elevation_list.html @@ -5,31 +5,34 @@ {% block title %}Rack Elevations{% endblock %} {% block controls %} -
-
-
- -
-
- Front - Rear -
- -
+
+
+ + View List + +
+ +
+
+ Front + Rear +
+
+
{% endblock %} {% block content-wrapper %} diff --git a/netbox/templates/dcim/rack_list.html b/netbox/templates/dcim/rack_list.html new file mode 100644 index 000000000..897625af6 --- /dev/null +++ b/netbox/templates/dcim/rack_list.html @@ -0,0 +1,9 @@ +{% extends 'generic/object_list.html' %} +{% load helpers %} +{% load static %} + +{% block extra_controls %} + + View Elevations + +{% endblock %} \ No newline at end of file diff --git a/netbox/templates/dcim/virtualchassis_add_member.html b/netbox/templates/dcim/virtualchassis_add_member.html index 17ffd64d9..bc2ba2f55 100644 --- a/netbox/templates/dcim/virtualchassis_add_member.html +++ b/netbox/templates/dcim/virtualchassis_add_member.html @@ -5,6 +5,8 @@ {% block content %}
+ {% render_errors membership_form %} + {% csrf_token %}
Add New Member
diff --git a/netbox/templates/dcim/virtualchassis_edit.html b/netbox/templates/dcim/virtualchassis_edit.html index f98a9fe64..433837cf5 100644 --- a/netbox/templates/dcim/virtualchassis_edit.html +++ b/netbox/templates/dcim/virtualchassis_edit.html @@ -8,6 +8,10 @@
+ {% for form in formset %} + {% render_errors form %} + {% endfor %} + {% csrf_token %} {{ pk_form.pk }} {{ formset.management_form }} diff --git a/netbox/templates/extras/configcontext.html b/netbox/templates/extras/configcontext.html index 56ec52c07..e9513a3a8 100644 --- a/netbox/templates/extras/configcontext.html +++ b/netbox/templates/extras/configcontext.html @@ -3,81 +3,93 @@ {% load static %} {% block content %} -
-
-
-
- Config Context -
-
- - - - - - - - - - - - - - - - - -
Name - {{ object.name }} -
Weight - {{ object.weight }} -
Description{{ object.description|placeholder }}
Active - {% if object.is_active %} - - - - {% else %} - - - - {% endif %} -
-
-
-
-
- Assignment -
-
- - {% for title, objects in assigned_objects %} - - - - - {% endfor %} -
{{ title }} -
    - {% for object in objects %} -
  • {{ object|linkify }}
  • - {% empty %} -
  • None
  • - {% endfor %} -
-
-
-
+
+
+
+
Config Context
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name{{ object.name }}
Weight{{ object.weight }}
Description{{ object.description|placeholder }}
Active{% checkmark object.is_active %}
Data Source + {% if object.data_source %} + {{ object.data_source }} + {% else %} + {{ ''|placeholder }} + {% endif %} +
Data File + {% if object.data_file %} + {{ object.data_file }} + {% elif object.data_path %} +
+ +
+ {{ object.data_path }} + {% else %} + {{ ''|placeholder }} + {% endif %} +
Data Synced{{ object.data_synced|placeholder }}
-
-
-
-
Data
- {% include 'extras/inc/configcontext_format.html' %} -
-
- {% include 'extras/inc/configcontext_data.html' with data=object.data format=format %} -
-
+
+
+
Assignment
+
+ + {% for title, objects in assigned_objects %} + + + + + {% endfor %} +
{{ title }} +
    + {% for object in objects %} +
  • {{ object|linkify }}
  • + {% empty %} +
  • None
  • + {% endfor %} +
+
+
+
+
+
+
Data
+ {% include 'extras/inc/configcontext_format.html' %} +
+
+ {% include 'inc/sync_warning.html' %} + {% include 'extras/inc/configcontext_data.html' with data=object.data format=format %} +
+
+
+
{% endblock %} diff --git a/netbox/templates/extras/configcontext_list.html b/netbox/templates/extras/configcontext_list.html new file mode 100644 index 000000000..31e7087ad --- /dev/null +++ b/netbox/templates/extras/configcontext_list.html @@ -0,0 +1,10 @@ +{% extends 'generic/object_list.html' %} + +{% block bulk_buttons %} + {% if perms.extras.sync_configcontext %} + + {% endif %} + {{ block.super }} +{% endblock %} diff --git a/netbox/templates/extras/configtemplate.html b/netbox/templates/extras/configtemplate.html new file mode 100644 index 000000000..937789ee2 --- /dev/null +++ b/netbox/templates/extras/configtemplate.html @@ -0,0 +1,77 @@ +{% extends 'generic/object.html' %} +{% load helpers %} +{% load plugins %} + +{% block content %} +
+
+
+
Config Template
+
+ + + + + + + + + + + + + + + + + + + + + +
Name{{ object.name }}
Description{{ object.description|placeholder }}
Data Source + {% if object.data_source %} + {{ object.data_source }} + {% else %} + {{ ''|placeholder }} + {% endif %} +
Data File + {% if object.data_file %} + {{ object.data_file }} + {% elif object.data_path %} +
+ +
+ {{ object.data_path }} + {% else %} + {{ ''|placeholder }} + {% endif %} +
Data Synced{{ object.data_synced|placeholder }}
+
+
+ {% include 'inc/panels/tags.html' %} + {% plugin_left_page object %} +
+
+
+
Environment Parameters
+
+
{{ object.environment_params }}
+
+
+ {% plugin_right_page object %} +
+
+
+
+
+
Template
+
+ {% include 'inc/sync_warning.html' %} +
{{ object.template_code }}
+
+
+ {% plugin_full_width_page object %} +
+
+{% endblock %} diff --git a/netbox/templates/extras/configtemplate_list.html b/netbox/templates/extras/configtemplate_list.html new file mode 100644 index 000000000..5a3e8781e --- /dev/null +++ b/netbox/templates/extras/configtemplate_list.html @@ -0,0 +1,10 @@ +{% extends 'generic/object_list.html' %} + +{% block bulk_buttons %} + {% if perms.extras.sync_configtemplate %} + + {% endif %} + {{ block.super }} +{% endblock %} diff --git a/netbox/templates/extras/exporttemplate.html b/netbox/templates/extras/exporttemplate.html index d14294355..a80db8fca 100644 --- a/netbox/templates/extras/exporttemplate.html +++ b/netbox/templates/extras/exporttemplate.html @@ -10,66 +10,92 @@ {% endblock %} {% block content %} -
-
-
-
- Export Template -
-
- - - - - - - - - - - - - - - - - - - - - -
Name{{ object.name }}
Description{{ object.description|placeholder }}
MIME Type{{ object.mime_type|placeholder }}
File Extension{{ object.file_extension|placeholder }}
Attachment{% checkmark object.as_attachment %}
-
-
-
-
Assigned Models
-
- - {% for ct in object.content_types.all %} +
+
+
+
Export Template
+
+
- + + - {% endfor %} -
{{ ct }}Name{{ object.name }}
+ + Description + {{ object.description|placeholder }} + + + MIME Type + {{ object.mime_type|placeholder }} + + + File Extension + {{ object.file_extension|placeholder }} + + + Attachment + {% checkmark object.as_attachment %} + + + Data Source + + {% if object.data_source %} + {{ object.data_source }} + {% else %} + {{ ''|placeholder }} + {% endif %} + + + + Data File + + {% if object.data_file %} + {{ object.data_file }} + {% elif object.data_path %} +
+ +
+ {{ object.data_path }} + {% else %} + {{ ''|placeholder }} + {% endif %} + + + + Data Synced + {{ object.data_synced|placeholder }} + + +
-
- {% plugin_left_page object %} -
-
-
-
- Template -
-
-
{{ object.template_code }}
+
+
Assigned Models
+
+ + {% for ct in object.content_types.all %} + + + + {% endfor %} +
{{ ct }}
+
+ {% plugin_left_page object %} +
+
+
+
Template
+
+ {% include 'inc/sync_warning.html' %} +
{{ object.template_code }}
+
+
+ {% plugin_right_page object %}
- {% plugin_right_page object %}
-
-
+
- {% plugin_full_width_page object %} + {% plugin_full_width_page object %}
-
+
{% endblock %} diff --git a/netbox/templates/extras/exporttemplate_list.html b/netbox/templates/extras/exporttemplate_list.html new file mode 100644 index 000000000..c79f9259a --- /dev/null +++ b/netbox/templates/extras/exporttemplate_list.html @@ -0,0 +1,10 @@ +{% extends 'generic/object_list.html' %} + +{% block bulk_buttons %} + {% if perms.extras.sync_configcontext %} + + {% endif %} + {{ block.super }} +{% endblock %} diff --git a/netbox/templates/generic/object_edit.html b/netbox/templates/generic/object_edit.html index c61fb723f..8531ad6df 100644 --- a/netbox/templates/generic/object_edit.html +++ b/netbox/templates/generic/object_edit.html @@ -1,6 +1,4 @@ {% extends 'base/layout.html' %} -{% load form_helpers %} -{% load helpers %} {% comment %} Blocks: @@ -48,56 +46,11 @@ Context: {% csrf_token %} - {% block form %} - {% if form.fieldsets %} - - {# Render hidden fields #} - {% for field in form.hidden_fields %} - {{ field }} - {% endfor %} - - {# Render grouped fields according to Form #} - {% for group, fields in form.fieldsets %} -
- {% if group %} -
-
{{ group }}
-
- {% endif %} - {% for name in fields %} - {% with field=form|getfield:name %} - {% if not field.field.widget.is_hidden %} - {% render_field field %} - {% endif %} - {% endwith %} - {% endfor %} -
- {% endfor %} - - {% if form.custom_fields %} -
-
-
Custom Fields
-
- {% render_custom_fields form %} -
- {% endif %} - - {% if form.comments %} -
-
Comments
- {% render_field form.comments %} -
- {% endif %} - - {% else %} - {# Render all fields in a single group #} -
- {% render_form form %} -
- {% endif %} - - {% endblock form %} +
+ {% block form %} + {% include 'htmx/form.html' %} + {% endblock form %} +
{% block buttons %} diff --git a/netbox/templates/generic/object_list.html b/netbox/templates/generic/object_list.html index e9deb4ff8..b80fb7288 100644 --- a/netbox/templates/generic/object_list.html +++ b/netbox/templates/generic/object_list.html @@ -26,16 +26,15 @@ Context:
{% plugin_list_buttons model %} - {% block extra_controls %}{% endblock %} {% if 'add' in actions %} - {% add_button model %} + {% add_button model %} {% endif %} {% if 'import' in actions %} - {% import_button model %} + {% import_button model %} {% endif %} {% if 'export' in actions %} - {% export_button model %} + {% export_button model %} {% endif %}
diff --git a/netbox/templates/htmx/form.html b/netbox/templates/htmx/form.html new file mode 100644 index 000000000..e15df4706 --- /dev/null +++ b/netbox/templates/htmx/form.html @@ -0,0 +1,51 @@ +{% load form_helpers %} + +{% if form.fieldsets %} + + {# Render hidden fields #} + {% for field in form.hidden_fields %} + {{ field }} + {% endfor %} + + {# Render grouped fields according to Form #} + {% for group, fields in form.fieldsets %} +
+ {% if group %} +
+
{{ group }}
+
+ {% endif %} + {% for name in fields %} + {% with field=form|getfield:name %} + {% if field and not field.field.widget.is_hidden %} + {% render_field field %} + {% endif %} + {% endwith %} + {% endfor %} +
+ {% endfor %} + + {% if form.custom_fields %} +
+
+
Custom Fields
+
+ {% render_custom_fields form %} +
+ {% endif %} + + {% if form.comments %} +
+
Comments
+ {% render_field form.comments %} +
+ {% endif %} + +{% else %} + + {# Render all fields in a single group #} +
+ {% render_form form %} +
+ +{% endif %} diff --git a/netbox/templates/inc/sync_warning.html b/netbox/templates/inc/sync_warning.html new file mode 100644 index 000000000..1ffc77e15 --- /dev/null +++ b/netbox/templates/inc/sync_warning.html @@ -0,0 +1,13 @@ +{% load buttons %} +{% load perms %} + +{% if object.data_file and object.data_file.last_updated > object.data_synced %} + +{% endif %} diff --git a/netbox/templates/ipam/prefix.html b/netbox/templates/ipam/prefix.html index a0baf3325..6d986aed5 100644 --- a/netbox/templates/ipam/prefix.html +++ b/netbox/templates/ipam/prefix.html @@ -133,7 +133,7 @@ {% with first_available_ip=object.get_first_available_ip %} {% if first_available_ip %} {% if perms.ipam.add_ipaddress %} - {{ first_available_ip }} + {{ first_available_ip }} {% else %} {{ first_available_ip }} {% endif %} diff --git a/netbox/tenancy/filtersets.py b/netbox/tenancy/filtersets.py index ab74949ff..1edc8fdc8 100644 --- a/netbox/tenancy/filtersets.py +++ b/netbox/tenancy/filtersets.py @@ -81,6 +81,10 @@ class ContactFilterSet(NetBoxModelFilterSet): class ContactAssignmentFilterSet(ChangeLoggedModelFilterSet): + q = django_filters.CharFilter( + method='search', + label=_('Search'), + ) content_type = ContentTypeFilter() contact_id = django_filters.ModelMultipleChoiceFilter( queryset=Contact.objects.all(), @@ -101,6 +105,14 @@ class ContactAssignmentFilterSet(ChangeLoggedModelFilterSet): model = ContactAssignment fields = ['id', 'content_type_id', 'object_id', 'priority'] + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(contact__name__icontains=value) | + Q(role__name__icontains=value) + ) + class ContactModelFilterSet(django_filters.FilterSet): contact = django_filters.ModelMultipleChoiceFilter( diff --git a/netbox/tenancy/forms/bulk_edit.py b/netbox/tenancy/forms/bulk_edit.py index 183a8e851..eda256a57 100644 --- a/netbox/tenancy/forms/bulk_edit.py +++ b/netbox/tenancy/forms/bulk_edit.py @@ -2,7 +2,7 @@ from django import forms from netbox.forms import NetBoxModelBulkEditForm from tenancy.models import * -from utilities.forms import CommentField, DynamicModelChoiceField, SmallTextarea +from utilities.forms import CommentField, DynamicModelChoiceField __all__ = ( 'ContactBulkEditForm', @@ -106,7 +106,7 @@ class ContactBulkEditForm(NetBoxModelBulkEditForm): required=False ) comments = CommentField( - widget=SmallTextarea, + widget=forms.Textarea, label='Comments' ) diff --git a/netbox/tenancy/forms/filtersets.py b/netbox/tenancy/forms/filtersets.py index 7f843d9a4..3acd6e341 100644 --- a/netbox/tenancy/forms/filtersets.py +++ b/netbox/tenancy/forms/filtersets.py @@ -1,3 +1,4 @@ +from django import forms from django.contrib.contenttypes.models import ContentType from django.utils.translation import gettext as _ @@ -106,7 +107,7 @@ class ContactAssignmentFilterForm(NetBoxModelFilterSetForm): required=False, label=_('Role') ) - priority = MultipleChoiceField( + priority = forms.MultipleChoiceField( choices=ContactPriorityChoices, required=False ) diff --git a/netbox/tenancy/forms/model_forms.py b/netbox/tenancy/forms/model_forms.py index b466c94b2..a27e41f74 100644 --- a/netbox/tenancy/forms/model_forms.py +++ b/netbox/tenancy/forms/model_forms.py @@ -3,7 +3,7 @@ from django import forms from netbox.forms import NetBoxModelForm from tenancy.models import * from utilities.forms import ( - BootstrapMixin, CommentField, DynamicModelChoiceField, SlugField, SmallTextarea, StaticSelect, + BootstrapMixin, CommentField, DynamicModelChoiceField, SlugField, ) __all__ = ( @@ -112,7 +112,7 @@ class ContactForm(NetBoxModelForm): 'group', 'name', 'title', 'phone', 'email', 'address', 'link', 'description', 'comments', 'tags', ) widgets = { - 'address': SmallTextarea(attrs={'rows': 3}), + 'address': forms.Textarea(attrs={'rows': 3}), } @@ -142,5 +142,4 @@ class ContactAssignmentForm(BootstrapMixin, forms.ModelForm): widgets = { 'content_type': forms.HiddenInput(), 'object_id': forms.HiddenInput(), - 'priority': StaticSelect(), } diff --git a/netbox/tenancy/models/contacts.py b/netbox/tenancy/models/contacts.py index 4fa8d87cb..440541b5f 100644 --- a/netbox/tenancy/models/contacts.py +++ b/netbox/tenancy/models/contacts.py @@ -4,7 +4,6 @@ from django.db import models from django.urls import reverse from netbox.models import ChangeLoggedModel, NestedGroupModel, OrganizationalModel, PrimaryModel -from netbox.models.features import WebhooksMixin from tenancy.choices import * __all__ = ( @@ -93,7 +92,7 @@ class Contact(PrimaryModel): return reverse('tenancy:contact', args=[self.pk]) -class ContactAssignment(WebhooksMixin, ChangeLoggedModel): +class ContactAssignment(ChangeLoggedModel): content_type = models.ForeignKey( to=ContentType, on_delete=models.CASCADE diff --git a/netbox/tenancy/urls.py b/netbox/tenancy/urls.py index cb8715f70..6563eff4b 100644 --- a/netbox/tenancy/urls.py +++ b/netbox/tenancy/urls.py @@ -49,6 +49,7 @@ urlpatterns = [ # Contact assignments path('contact-assignments/', views.ContactAssignmentListView.as_view(), name='contactassignment_list'), path('contact-assignments/add/', views.ContactAssignmentEditView.as_view(), name='contactassignment_add'), + path('contact-assignments/delete/', views.ContactAssignmentBulkDeleteView.as_view(), name='contactassignment_bulk_delete'), path('contact-assignments//', include(get_model_urls('tenancy', 'contactassignment'))), ] diff --git a/netbox/tenancy/views.py b/netbox/tenancy/views.py index b7585b8d7..b71702d65 100644 --- a/netbox/tenancy/views.py +++ b/netbox/tenancy/views.py @@ -354,6 +354,7 @@ class ContactAssignmentListView(generic.ObjectListView): filterset = filtersets.ContactAssignmentFilterSet filterset_form = forms.ContactAssignmentFilterForm table = tables.ContactAssignmentTable + actions = ('export', 'bulk_delete') @register_model_view(ContactAssignment, 'edit') @@ -376,6 +377,12 @@ class ContactAssignmentEditView(generic.ObjectEditView): } +class ContactAssignmentBulkDeleteView(generic.BulkDeleteView): + queryset = ContactAssignment.objects.all() + filterset = filtersets.ContactAssignmentFilterSet + table = tables.ContactAssignmentTable + + @register_model_view(ContactAssignment, 'delete') class ContactAssignmentDeleteView(generic.ObjectDeleteView): queryset = ContactAssignment.objects.all() diff --git a/netbox/users/forms.py b/netbox/users/forms.py index e8647aa5f..0c7d7ea19 100644 --- a/netbox/users/forms.py +++ b/netbox/users/forms.py @@ -7,7 +7,7 @@ from django.utils.translation import gettext as _ from ipam.formfields import IPNetworkFormField from netbox.preferences import PREFERENCES -from utilities.forms import BootstrapMixin, DateTimePicker, StaticSelect +from utilities.forms import BootstrapMixin, DateTimePicker from utilities.utils import flatten_dict from .models import Token, UserConfig @@ -35,7 +35,7 @@ class UserConfigFormMetaclass(forms.models.ModelFormMetaclass): 'help_text': mark_safe(help_text), 'coerce': preference.coerce, 'required': False, - 'widget': StaticSelect, + 'widget': forms.Select, } preference_fields[field_name] = forms.TypedChoiceField(**field_kwargs) attrs.update(preference_fields) diff --git a/netbox/users/views.py b/netbox/users/views.py index 832a4e592..a82620914 100644 --- a/netbox/users/views.py +++ b/netbox/users/views.py @@ -96,7 +96,7 @@ class LoginView(View): # Authenticate user auth_login(request, form.get_user()) logger.info(f"User {request.user} successfully authenticated") - messages.info(request, f"Logged in as {request.user}.") + messages.success(request, f"Logged in as {request.user}.") # Ensure the user has a UserConfig defined. (This should normally be handled by # create_userconfig() on user creation.) diff --git a/netbox/utilities/exceptions.py b/netbox/utilities/exceptions.py index 657e90745..d7418d0cb 100644 --- a/netbox/utilities/exceptions.py +++ b/netbox/utilities/exceptions.py @@ -24,6 +24,13 @@ class AbortRequest(Exception): self.message = message +class AbortScript(Exception): + """ + Raised to cleanly abort a script. + """ + pass + + class PermissionsViolation(Exception): """ Raised when an operation was prevented because it would violate the diff --git a/netbox/utilities/files.py b/netbox/utilities/files.py new file mode 100644 index 000000000..68afe2962 --- /dev/null +++ b/netbox/utilities/files.py @@ -0,0 +1,9 @@ +import hashlib + + +def sha256_hash(filepath): + """ + Return the SHA256 hash of the file at the specified path. + """ + with open(filepath, 'rb') as f: + return hashlib.sha256(f.read()) diff --git a/netbox/utilities/forms/fields/content_types.py b/netbox/utilities/forms/fields/content_types.py index 80861166c..76efe9a7b 100644 --- a/netbox/utilities/forms/fields/content_types.py +++ b/netbox/utilities/forms/fields/content_types.py @@ -27,11 +27,11 @@ class ContentTypeChoiceField(ContentTypeChoiceMixin, forms.ModelChoiceField): """ Selection field for a single content type. """ - widget = widgets.StaticSelect + pass class ContentTypeMultipleChoiceField(ContentTypeChoiceMixin, forms.ModelMultipleChoiceField): """ Selection field for one or more content types. """ - widget = widgets.StaticSelectMultiple + pass diff --git a/netbox/utilities/forms/fields/fields.py b/netbox/utilities/forms/fields/fields.py index bb6c3f73b..c5d2d0a1f 100644 --- a/netbox/utilities/forms/fields/fields.py +++ b/netbox/utilities/forms/fields/fields.py @@ -68,7 +68,6 @@ class TagFilterField(forms.MultipleChoiceField): :param model: The model of the filter """ - widget = widgets.StaticSelectMultiple def __init__(self, model, *args, **kwargs): def get_choices(): @@ -137,13 +136,16 @@ class MACAddressField(forms.Field): class ChoiceField(forms.ChoiceField): """ - Overrides Django's built-in `ChoiceField` to use NetBox's `StaticSelect` widget + Previously used to override Django's built-in `ChoiceField` to use NetBox's now-obsolete `StaticSelect` widget. """ - widget = widgets.StaticSelect + # TODO: Remove in v3.6 + pass class MultipleChoiceField(forms.MultipleChoiceField): """ - Overrides Django's built-in `MultipleChoiceField` to use NetBox's `StaticSelectMultiple` widget + Previously used to override Django's built-in `MultipleChoiceField` to use NetBox's now-obsolete + `StaticSelectMultiple` widget. """ - widget = widgets.StaticSelectMultiple + # TODO: Remove in v3.6 + pass diff --git a/netbox/utilities/forms/forms.py b/netbox/utilities/forms/forms.py index 99d03f2a6..a008a2c8c 100644 --- a/netbox/utilities/forms/forms.py +++ b/netbox/utilities/forms/forms.py @@ -9,7 +9,7 @@ from django.utils.translation import gettext as _ from utilities.choices import ImportFormatChoices from utilities.forms.utils import parse_csv -from .widgets import APISelect, APISelectMultiple, ClearableFileInput, StaticSelect +from .widgets import APISelect, APISelectMultiple, ClearableFileInput __all__ = ( 'BootstrapMixin', @@ -37,27 +37,31 @@ class BootstrapMixin: super().__init__(*args, **kwargs) exempt_widgets = [ - forms.CheckboxInput, forms.FileInput, forms.RadioSelect, - forms.Select, APISelect, APISelectMultiple, ClearableFileInput, - StaticSelect, ] for field_name, field in self.fields.items(): css = field.widget.attrs.get('class', '') - if field.widget.__class__ not in exempt_widgets: - field.widget.attrs['class'] = f'{css} form-control' + if field.widget.__class__ in exempt_widgets: + continue elif isinstance(field.widget, forms.CheckboxInput): field.widget.attrs['class'] = f'{css} form-check-input' + elif isinstance(field.widget, forms.SelectMultiple): + if 'size' not in field.widget.attrs: + field.widget.attrs['class'] = f'{css} netbox-static-select' + elif isinstance(field.widget, forms.Select): - field.widget.attrs['class'] = f'{css} form-select' + field.widget.attrs['class'] = f'{css} netbox-static-select' + + else: + field.widget.attrs['class'] = f'{css} form-control' if field.required and not isinstance(field.widget, forms.FileInput): field.widget.attrs['required'] = 'required' @@ -165,8 +169,7 @@ class ImportForm(BootstrapMixin, forms.Form): ) format = forms.ChoiceField( choices=ImportFormatChoices, - initial=ImportFormatChoices.AUTO, - widget=StaticSelect() + initial=ImportFormatChoices.AUTO ) data_field = 'data' @@ -197,6 +200,8 @@ class ImportForm(BootstrapMixin, forms.Form): self.cleaned_data['data'] = self._clean_json(data) elif format == ImportFormatChoices.YAML: self.cleaned_data['data'] = self._clean_yaml(data) + else: + raise forms.ValidationError(f"Unknown data format: {format}") def _detect_format(self, data): """ diff --git a/netbox/utilities/forms/utils.py b/netbox/utilities/forms/utils.py index 1a2f62b2e..2f08a3cce 100644 --- a/netbox/utilities/forms/utils.py +++ b/netbox/utilities/forms/utils.py @@ -12,6 +12,7 @@ __all__ = ( 'expand_alphanumeric_pattern', 'expand_ipaddress_pattern', 'form_from_model', + 'get_field_value', 'get_selected_values', 'parse_alphanumeric_range', 'parse_numeric_range', @@ -113,6 +114,21 @@ def expand_ipaddress_pattern(string, family): yield ''.join([lead, format(i, 'x' if family == 6 else 'd'), remnant]) +def get_field_value(form, field_name): + """ + Return the current bound or initial value associated with a form field, prior to calling + clean() for the form. + """ + field = form.fields[field_name] + + if form.is_bound: + if data := form.data.get(field_name): + if field.valid_value(data): + return data + + return form.get_initial_for_field(field, field_name) + + def get_selected_values(form, field_name): """ Return the list of selected human-friendly values for a form field diff --git a/netbox/utilities/forms/widgets.py b/netbox/utilities/forms/widgets.py index 1802306f1..c7e1cfb81 100644 --- a/netbox/utilities/forms/widgets.py +++ b/netbox/utilities/forms/widgets.py @@ -21,9 +21,6 @@ __all__ = ( 'SelectSpeedWidget', 'SelectWithPK', 'SlugWidget', - 'SmallTextarea', - 'StaticSelect', - 'StaticSelectMultiple', 'TimePicker', ) @@ -33,13 +30,6 @@ QueryParam = Dict[str, QueryParamValue] ProcessedParams = Sequence[Dict[str, Sequence[JSONPrimitive]]] -class SmallTextarea(forms.Textarea): - """ - Subclass used for rendering a smaller textarea element. - """ - pass - - class SlugWidget(forms.TextInput): """ Subclass TextInput and add a slug regeneration button next to the form field. @@ -76,26 +66,7 @@ class BulkEditNullBooleanSelect(forms.NullBooleanSelect): self.attrs['class'] = 'netbox-static-select' -class StaticSelect(forms.Select): - """ - A static