Skip to content

Commit a38a382

Browse files
14132 Add EventRule - change webhook and add in script processing to events (#14267)
--------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent b83fcc6 commit a38a382

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1564
-584
lines changed

docs/configuration/required-parameters.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ DATABASE = {
5959

6060
## REDIS
6161

62-
[Redis](https://redis.io/) is an in-memory data store similar to memcached. While Redis has been an optional component of
63-
NetBox since the introduction of webhooks in version 2.4, it is required starting in 2.6 to support NetBox's caching
64-
functionality (as well as other planned features). In 2.7, the connection settings were broken down into two sections for
65-
task queuing and caching, allowing the user to connect to different Redis instances/databases per feature.
62+
[Redis](https://redis.io/) is a lightweight in-memory data store similar to memcached. NetBox employs Redis for background task queuing and other features.
6663

6764
Redis is configured using a configuration setting similar to `DATABASE` and these settings are the same for both of the `tasks` and `caching` subsections:
6865

@@ -81,15 +78,15 @@ REDIS = {
8178
'tasks': {
8279
'HOST': 'redis.example.com',
8380
'PORT': 1234,
84-
'USERNAME': 'netbox'
81+
'USERNAME': 'netbox',
8582
'PASSWORD': 'foobar',
8683
'DATABASE': 0,
8784
'SSL': False,
8885
},
8986
'caching': {
9087
'HOST': 'localhost',
9188
'PORT': 6379,
92-
'USERNAME': ''
89+
'USERNAME': '',
9390
'PASSWORD': '',
9491
'DATABASE': 1,
9592
'SSL': False,

docs/development/application-registry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A dictionary of particular features (e.g. custom fields) mapped to the NetBox mo
3131
'dcim': ['site', 'rack', 'devicetype', ...],
3232
...
3333
},
34-
'webhooks': {
34+
'event_rules': {
3535
'extras': ['configcontext', 'tag', ...],
3636
'dcim': ['site', 'rack', 'devicetype', ...],
3737
},

docs/development/models.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/
1010

1111
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).
1212

13-
| Feature | Feature Mixin | Registry Key | Description |
14-
|------------------------------------------------------------|-------------------------|--------------------|--------------------------------------------------------------------------------|
15-
| [Change logging](../features/change-logging.md) | `ChangeLoggingMixin` | - | Changes to these objects are automatically recorded in the change log |
16-
| Cloning | `CloningMixin` | - | Provides the `clone()` method to prepare a copy |
17-
| [Custom fields](../customization/custom-fields.md) | `CustomFieldsMixin` | `custom_fields` | These models support the addition of user-defined fields |
18-
| [Custom links](../customization/custom-links.md) | `CustomLinksMixin` | `custom_links` | These models support the assignment of custom links |
19-
| [Custom validation](../customization/custom-validation.md) | `CustomValidationMixin` | - | Supports the enforcement of custom validation rules |
20-
| [Export templates](../customization/export-templates.md) | `ExportTemplatesMixin` | `export_templates` | Users can create custom export templates for these models |
21-
| [Job results](../features/background-jobs.md) | `JobsMixin` | `jobs` | Users can create custom export templates for these models |
22-
| [Journaling](../features/journaling.md) | `JournalingMixin` | `journaling` | These models support persistent historical commentary |
23-
| [Synchronized data](../integrations/synchronized-data.md) | `SyncedDataMixin` | `synced_data` | Certain model data can be automatically synchronized from a remote data source |
24-
| [Tagging](../models/extras/tag.md) | `TagsMixin` | `tags` | The models can be tagged with user-defined tags |
25-
| [Webhooks](../integrations/webhooks.md) | `WebhooksMixin` | `webhooks` | NetBox is capable of generating outgoing webhooks for these objects |
13+
| Feature | Feature Mixin | Registry Key | Description |
14+
|------------------------------------------------------------|-------------------------|--------------------|-----------------------------------------------------------------------------------------|
15+
| [Change logging](../features/change-logging.md) | `ChangeLoggingMixin` | - | Changes to these objects are automatically recorded in the change log |
16+
| Cloning | `CloningMixin` | - | Provides the `clone()` method to prepare a copy |
17+
| [Custom fields](../customization/custom-fields.md) | `CustomFieldsMixin` | `custom_fields` | These models support the addition of user-defined fields |
18+
| [Custom links](../customization/custom-links.md) | `CustomLinksMixin` | `custom_links` | These models support the assignment of custom links |
19+
| [Custom validation](../customization/custom-validation.md) | `CustomValidationMixin` | - | Supports the enforcement of custom validation rules |
20+
| [Export templates](../customization/export-templates.md) | `ExportTemplatesMixin` | `export_templates` | Users can create custom export templates for these models |
21+
| [Job results](../features/background-jobs.md) | `JobsMixin` | `jobs` | Users can create custom export templates for these models |
22+
| [Journaling](../features/journaling.md) | `JournalingMixin` | `journaling` | These models support persistent historical commentary |
23+
| [Synchronized data](../integrations/synchronized-data.md) | `SyncedDataMixin` | `synced_data` | Certain model data can be automatically synchronized from a remote data source |
24+
| [Tagging](../models/extras/tag.md) | `TagsMixin` | `tags` | The models can be tagged with user-defined tags |
25+
| [Event rules](../features/event-rules.md) | `EventRulesMixin` | `event_rules` | Event rules can send webhooks or run custom scripts automatically in response to events |
2626

2727
## Models Index
2828

@@ -111,7 +111,7 @@ Component models represent individual physical or virtual components belonging t
111111

112112
### Component Template Models
113113

114-
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.
114+
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 event rules.
115115

116116
* [dcim.ConsolePortTemplate](../models/dcim/consoleporttemplate.md)
117117
* [dcim.ConsoleServerPortTemplate](../models/dcim/consoleserverporttemplate.md)

docs/features/api-integration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ To learn more about this feature, check out the [GraphQL API documentation](../i
2626

2727
## Webhooks
2828

29-
A webhook is a mechanism for conveying to some external system a change that took place in NetBox. For example, you may want to notify a monitoring system whenever the status of a device is updated in NetBox. This can be done by creating a webhook for the device model in NetBox and identifying the webhook receiver. When NetBox detects a change to a device, an HTTP request containing the details of the change and who made it be sent to the specified receiver. Webhooks are an excellent mechanism for building event-based automation processes.
29+
A webhook is a mechanism for conveying to some external system a change that has taken place in NetBox. For example, you may want to notify a monitoring system whenever the status of a device is updated in NetBox. To do this, first create a [webhook](../models/extras/webhook.md) identifying the remote receiver (URL), HTTP method, and any other necessary parameters. Then, define an [event rule](../models/extras/eventrule.md) which is triggered by device changes to transmit the webhook.
3030

31-
To learn more about this feature, check out the [webhooks documentation](../integrations/webhooks.md).
31+
When NetBox detects a change to a device, an HTTP request containing the details of the change and who made it be sent to the specified receiver. Webhooks are an excellent mechanism for building event-based automation processes. To learn more about this feature, check out the [webhooks documentation](../integrations/webhooks.md).
3232

3333
## Prometheus Metrics
3434

docs/features/event-rules.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Event Rules
2+
3+
NetBox includes the ability to execute certain functions in response to internal object changes. These include:
4+
5+
* [Scripts](../customization/custom-scripts.md) execution
6+
* [Webhooks](../integrations/webhooks.md) execution
7+
8+
For example, suppose you want to automatically configure a monitoring system to start monitoring a device when its operational status is changed to active, and remove it from monitoring for any other status. You can create a webhook in NetBox for the device model and craft its content and destination URL to effect the desired change on the receiving system. You can then associate an event rule with this webhook and the webhook will be sent automatically by NetBox whenever the configured constraints are met.
9+
10+
Each event must be associated with at least one NetBox object type and at least one event (e.g. create, update, or delete).
11+
12+
## Conditional Event Rules
13+
14+
An event rule may include a set of conditional logic expressed in JSON used to control whether an event triggers for a specific object. For example, you may wish to trigger an event for devices only when the `status` field of an object is "active":
15+
16+
```json
17+
{
18+
"and": [
19+
{
20+
"attr": "status.value",
21+
"value": "active"
22+
}
23+
]
24+
}
25+
```
26+
27+
For more detail, see the reference documentation for NetBox's [conditional logic](../reference/conditions.md).
28+
29+
## Event Rule Processing
30+
31+
When a change is detected, any resulting events are placed into a Redis queue for processing. This allows the user's request to complete without needing to wait for the outgoing event(s) to be processed. The events are then extracted from the queue by the `rqworker` process. The current event queue and any failed events can be inspected in the admin UI under System > Background Tasks.

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ In addition to its expansive and robust data model, NetBox offers myriad mechani
3232
* Custom fields
3333
* Custom model validation
3434
* Export templates
35-
* Webhooks
35+
* Event rules
3636
* Plugins
3737
* REST & GraphQL APIs
3838

docs/integrations/webhooks.md

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Webhooks
22

3-
NetBox can be configured to transmit outgoing webhooks to remote systems in response to internal object changes. The receiver can act on the data in these webhook messages to perform related tasks.
3+
NetBox can be configured via [Event Rules](../features/event-rules.md) to transmit outgoing webhooks to remote systems in response to internal object changes. The receiver can act on the data in these webhook messages to perform related tasks.
44

55
For example, suppose you want to automatically configure a monitoring system to start monitoring a device when its operational status is changed to active, and remove it from monitoring for any other status. You can create a webhook in NetBox for the device model and craft its content and destination URL to effect the desired change on the receiving system. Webhooks will be sent automatically by NetBox whenever the configured constraints are met.
66

7-
Each webhook must be associated with at least one NetBox object type and at least one event (create, update, or delete). Users can specify the receiver URL, HTTP request type (`GET`, `POST`, etc.), content type, and headers. A request body can also be specified; if left blank, this will default to a serialized representation of the affected object.
8-
97
!!! warning "Security Notice"
108
Webhooks support the inclusion of user-submitted code to generate the URL, custom headers, and payloads, which may pose security risks under certain conditions. Only grant permission to create or modify webhooks to trusted users.
119

@@ -70,26 +68,12 @@ If no body template is specified, the request body will be populated with a JSON
7068
}
7169
```
7270

73-
## Conditional Webhooks
74-
75-
A webhook may include a set of conditional logic expressed in JSON used to control whether a webhook triggers for a specific object. For example, you may wish to trigger a webhook for devices only when the `status` field of an object is "active":
76-
77-
```json
78-
{
79-
"and": [
80-
{
81-
"attr": "status.value",
82-
"value": "active"
83-
}
84-
]
85-
}
86-
```
87-
88-
For more detail, see the reference documentation for NetBox's [conditional logic](../reference/conditions.md).
71+
!!! note
72+
The setting of conditional webhooks has been moved to [Event Rules](../features/event-rules.md) since NetBox 3.7
8973

9074
## Webhook Processing
9175

92-
When a change is detected, any resulting webhooks are placed into a Redis queue for processing. This allows the user's request to complete without needing to wait for the outgoing webhook(s) to be processed. The webhooks are then extracted from the queue by the `rqworker` process and HTTP requests are sent to their respective destinations. The current webhook queue and any failed webhooks can be inspected in the admin UI under System > Background Tasks.
76+
Using [Event Rules](../features/event-rules.md), when a change is detected, any resulting webhooks are placed into a Redis queue for processing. This allows the user's request to complete without needing to wait for the outgoing webhook(s) to be processed. The webhooks are then extracted from the queue by the `rqworker` process and HTTP requests are sent to their respective destinations. The current webhook queue and any failed webhooks can be inspected in the admin UI under System > Background Tasks.
9377

9478
A request is considered successful if the response has a 2XX status code; otherwise, the request is marked as having failed. Failed requests may be retried manually via the admin UI.
9579

docs/models/extras/eventrule.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# EventRule
2+
3+
An event rule is a mechanism for automatically taking an action (such as running a script or sending a webhook) in response to an event in NetBox. For example, you may want to notify a monitoring system whenever the status of a device is updated in NetBox. This can be done by creating an event for device objects and designating a webhook to be transmitted. When NetBox detects a change to a device, an HTTP request containing the details of the change and who made it be sent to the specified receiver.
4+
5+
See the [event rules documentation](../features/event-rules.md) for more information.
6+
7+
## Fields
8+
9+
### Name
10+
11+
A unique human-friendly name.
12+
13+
### Content Types
14+
15+
The type(s) of object in NetBox that will trigger the rule.
16+
17+
### Enabled
18+
19+
If not selected, the event rule will not be processed.
20+
21+
### Events
22+
23+
The events which will trigger the rule. At least one event type must be selected.
24+
25+
| Name | Description |
26+
|------------|--------------------------------------|
27+
| Creations | A new object has been created |
28+
| Updates | An existing object has been modified |
29+
| Deletions | An object has been deleted |
30+
| Job starts | A job for an object starts |
31+
| Job ends | A job for an object terminates |
32+
33+
### Conditions
34+
35+
A set of [prescribed conditions](../../reference/conditions.md) against which the triggering object will be evaluated. If the conditions are defined but not met by the object, no action will be taken. An event rule that does not define any conditions will _always_ trigger.

docs/plugins/development/models.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,17 @@ For more information about database migrations, see the [Django documentation](h
123123

124124
::: netbox.models.features.CustomValidationMixin
125125

126+
::: netbox.models.features.EventRulesMixin
127+
128+
!!! note
129+
`EventRulesMixin` was renamed from `WebhooksMixin` in NetBox v3.7.
130+
126131
::: netbox.models.features.ExportTemplatesMixin
127132

128133
::: netbox.models.features.JournalingMixin
129134

130135
::: netbox.models.features.TagsMixin
131136

132-
::: netbox.models.features.WebhooksMixin
133-
134137
## Choice Sets
135138

136139
For model fields which support the selection of one or more values from a predefined list of choices, NetBox provides the `ChoiceSet` utility class. This can be used in place of a regular choices tuple to provide enhanced functionality, namely dynamic configuration and colorization. (See [Django's documentation](https://docs.djangoproject.com/en/stable/ref/models/fields/#choices) on the `choices` parameter for supported model fields.)

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ nav:
8787
- Auth & Permissions: 'features/authentication-permissions.md'
8888
- API & Integration: 'features/api-integration.md'
8989
- Customization: 'features/customization.md'
90+
- Event Rules: 'features/event-rules.md'
9091
- Installation & Upgrade:
9192
- Installing NetBox: 'installation/index.md'
9293
- 1. PostgreSQL: 'installation/1-postgresql.md'
@@ -215,6 +216,7 @@ nav:
215216
- CustomField: 'models/extras/customfield.md'
216217
- CustomFieldChoiceSet: 'models/extras/customfieldchoiceset.md'
217218
- CustomLink: 'models/extras/customlink.md'
219+
- EventRule: 'models/extras/eventrule.md'
218220
- ExportTemplate: 'models/extras/exporttemplate.md'
219221
- ImageAttachment: 'models/extras/imageattachment.md'
220222
- JournalEntry: 'models/extras/journalentry.md'

netbox/core/models/contenttypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def with_feature(self, feature):
2626
Return the ContentTypes only for models which are registered as supporting the specified feature. For example,
2727
we can find all ContentTypes for models which support webhooks with
2828
29-
ContentType.objects.with_feature('webhooks')
29+
ContentType.objects.with_feature('event_rules')
3030
"""
3131
if feature not in registry['model_features']:
3232
raise KeyError(

0 commit comments

Comments
 (0)