Просмотр исходного кода

Closes #23874: Document gaps in restricted rendering

List the IPAM methods which have no equivalent in restricted mode, and
warn that config templates silently omit objects the rendering user
can't view. The export template mappings for child IP addresses and
ranges now include the prefix's VRF, as the legacy methods do.
Jeremy Stretch 2 часов назад
Родитель
Сommit
bcb902c8b3

+ 11 - 0
docs/configuration/system.md

@@ -447,6 +447,17 @@ TEMPLATE_RENDERING = {
 
 Where a feature uses `legacy` mode, individual objects can opt into restricted mode by enabling their restricted rendering field. This allows templates to be converted and checked one at a time, before the whole feature is switched to `restricted` mode. The field has no effect on a feature which uses `restricted` mode: all of its objects are rendered in restricted mode. A template included by another (e.g. with `{% include %}`) is rendered in the mode of the template which includes it.
 
+Restricted templates can't call model methods or read model properties, and most have an equivalent field or `query()` filter. The following IPAM methods and properties, which compute availability and utilization, have no equivalent in restricted mode. Templates which depend on them can't yet be converted.
+
+| Model       | Methods and properties |
+|-------------|------------------------|
+| Aggregate   | `get_utilization()`, `get_available_prefixes()`, `get_first_available_prefix()` |
+| Prefix      | `get_utilization()`, `get_available_prefixes()`, `get_first_available_prefix()`, `get_available_ips()`, `get_available_ip_count()`, `get_first_available_ip()` |
+| IP range    | `utilization`, `first_available_ip`, `get_available_ips()`, `get_available_ip_count()`, `get_first_available_ip()` |
+| IP address  | `get_next_available_ip()` |
+| ASN range   | `get_available_asns()` |
+| VLAN group  | `get_available_vids()`, `get_next_available_vid()` |
+
 NetBox will fail to start if this parameter names an unknown feature or mode, or selects `restricted` for a feature that doesn't support it.
 
 !!! warning "Default changes in NetBox v5.0"

+ 2 - 0
docs/customization/custom-links.md

@@ -63,6 +63,8 @@ Most links work the same in both modes. Common changes needed when migrating a l
 | `{{ object.get_config_context() }}`     | `{{ object.config_context }}`                 |
 | `{% if request.GET.tab %}`              | `{% if request.GET.get('tab') %}`             |
 
+Some IPAM methods, such as `object.get_utilization()`, have [no equivalent](../configuration/system.md#template_rendering) in restricted mode.
+
 ## Conditional Rendering
 
 Only links which render with non-empty text are included on the page. You can employ conditional Jinja2 logic to control the conditions under which a link gets rendered.

+ 13 - 10
docs/customization/export-templates.md

@@ -53,16 +53,19 @@ Model classes (e.g. `dcim.Site`) aren't available, and each variable provided by
 
 In legacy mode, `objects` and `user` are also provided, so templates can be migrated one at a time before switching modes. Common changes needed when migrating a template:
 
-| Legacy                                                                 | Restricted                                                                     |
-|------------------------------------------------------------------------|--------------------------------------------------------------------------------|
-| `{% for d in queryset %}`                                              | `{% for d in objects %}`                                                       |
-| `{{ d.cf.foo }}`                                                       | `{{ d.custom_fields.foo }}`                                                    |
-| `{{ d.get_absolute_url() }}`                                           | `{{ d.display_url }}`                                                          |
-| `{{ d.get_status_display() }}`                                         | `{{ d.status.label }}`                                                         |
-| `{{ d.get_config_context() }}`                                         | `{{ d.config_context }}`                                                       |
-| `{{ prefix.get_child_ips().filter(interface__name='Bu1').first() }}`   | `{{ query('ipam.ipaddress', parent=prefix.prefix, interface='Bu1').first() }}` |
-| `{{ prefix.get_child_ranges().first() }}`                              | `{{ query('ipam.iprange', parent=prefix.prefix).first() }}`                    |
-| `{% if prefix.tags.contains(extras.Tag.objects.get(slug='static')) %}` | `{% if 'static' in prefix.tags \| map(attribute='slug') %}`                    |
+| Legacy                                                                 | Restricted                                                                                    |
+|------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------|
+| `{% for d in queryset %}`                                              | `{% for d in objects %}`                                                                      |
+| `{{ d.cf.foo }}`                                                       | `{{ d.custom_fields.foo }}`                                                                   |
+| `{{ d.get_absolute_url() }}`                                           | `{{ d.display_url }}`                                                                         |
+| `{{ d.get_status_display() }}`                                         | `{{ d.status.label }}`                                                                        |
+| `{{ d.get_config_context() }}`                                         | `{{ d.config_context }}`                                                                      |
+| `{{ prefix.get_child_ips().filter(interface__name='Bu1').first() }}`   | `{{ query('ipam.ipaddress', parent=prefix.prefix, vrf_id=vrf_id, interface='Bu1').first() }}` |
+| `{{ prefix.get_child_ranges().first() }}`                              | `{{ query('ipam.iprange', parent=prefix.prefix, vrf_id=vrf_id).first() }}`                    |
+| `{% if prefix.tags.contains(extras.Tag.objects.get(slug='static')) %}` | `{% if 'static' in prefix.tags \| map(attribute='slug') %}`                                   |
+| `{{ prefix.get_utilization() }}`                                       | No equivalent (see [`TEMPLATE_RENDERING`](../configuration/system.md#template_rendering))     |
+
+`get_child_ips()` and `get_child_ranges()` return only the objects in the prefix's VRF, so pass its ID as `vrf_id`, or `None` for the global table (e.g. `{% set vrf_id = prefix.vrf.id if prefix.vrf else None %}`). The exception is a container prefix in the global table, for which `get_child_ips()` returns IP addresses in any VRF: omit `vrf_id` to match it.
 
 ## REST API Integration
 

+ 5 - 0
docs/features/configuration-rendering.md

@@ -72,6 +72,11 @@ Each variable provided by a plugin is included only if its value can be serializ
 | `{{ device.interfaces.all()[0].name }}`  | `{{ device.interfaces.first().name }}`            |
 | `{{ dcim.Site.objects.count() }}`        | `{{ query('dcim.site').count() }}`                |
 
+Some IPAM methods, such as `prefix.get_first_available_ip()`, have [no equivalent](../configuration/system.md#template_rendering) in restricted mode.
+
+!!! warning "Permissions of the rendering user"
+    In restricted mode, objects which the rendering user isn't permitted to view are left out of the rendered configuration without an error. For example, an interface's IP addresses which the user can't view are omitted, and the configuration renders as though they weren't assigned. Ensure that every user who renders configurations, including the accounts behind API tokens used by automation, can view all of the objects the template reads.
+
 ## Rendering Templates
 
 ### Device Configurations