Extend webhooks to support basic conditional logic #4806

Closed
opened 2025-12-29 19:20:48 +01:00 by adam · 4 comments
Owner

Originally created by @jeremystretch on GitHub (Apr 22, 2021).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v2.11.1

Feature type

New functionality

Proposed functionality

Extend the Webhook model to support the attachment of arbitrary conditions that control whether a webhook fires. These attributes could be specified in a manner similar to how object permission constraints are defined.

For example, suppose we create a webhook for the IPAddress model, but we only want it to fire if the IP address is within 10.0.0.0/8. We could specify something like the following on the webhook:

{"address__net_contained": "10.0.0.0/8"}

(The net_contained lookup is used to specify a containing prefix.)

Use case

Eliminates many of the simpler use cases that currently require a middleware by handling basic conditional logic. The intent here is not to solve complex problems, but rather to expedite simplistic use cases where we only want to fire a webhook if an object meets certain conditions.

Database changes

Add a new JSON field (conditions?) to the Webhook model

External dependencies

No response

Originally created by @jeremystretch on GitHub (Apr 22, 2021). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v2.11.1 ### Feature type New functionality ### Proposed functionality Extend the Webhook model to support the attachment of arbitrary conditions that control whether a webhook fires. These attributes could be specified in a manner similar to how object permission constraints are defined. For example, suppose we create a webhook for the IPAddress model, but we only want it to fire if the IP address is within 10.0.0.0/8. We could specify something like the following on the webhook: ``` {"address__net_contained": "10.0.0.0/8"} ``` (The `net_contained` lookup is used to specify a containing prefix.) ### Use case Eliminates many of the simpler use cases that currently require a middleware by handling basic conditional logic. The intent here is not to solve complex problems, but rather to expedite simplistic use cases where we only want to fire a webhook if an object meets certain conditions. ### Database changes Add a new JSON field (`conditions`?) to the Webhook model ### External dependencies _No response_
adam added the status: acceptedtype: feature labels 2025-12-29 19:20:48 +01:00
adam closed this issue 2025-12-29 19:20:48 +01:00
Author
Owner

@ziggekatten commented on GitHub (Apr 22, 2021):

This would be a great feature. Usecase for us is when onboarding data for a new tenant, we don't want to fire any of our webhooks until the data is validated and tenant considered ready for production state.

And during the onboarding a lot of data changes are done that would fire unneeded web hooks.

This would remove need for maintaining business logic in our recieving endpoint which is really good.

@ziggekatten commented on GitHub (Apr 22, 2021): This would be a great feature. Usecase for us is when onboarding data for a new tenant, we don't want to fire any of our webhooks until the data is validated and tenant considered ready for production state. And during the onboarding a lot of data changes are done that would fire unneeded web hooks. This would remove need for maintaining business logic in our recieving endpoint which is really good.
Author
Owner

@Nothing4You commented on GitHub (Aug 30, 2021):

another use case that'd be useful for us:

we have a tag to identify prefixes used for a specific purpose.

if we could filter for changes only where the tag is removed or added (or the prefix with the tag is updated) this would be a great way to automatically notify that there's changes that need to be applied in other places.

@Nothing4You commented on GitHub (Aug 30, 2021): another use case that'd be useful for us: we have a tag to identify prefixes used for a specific purpose. if we could filter for changes only where the tag is removed or added (or the prefix with the tag is updated) this would be a great way to automatically notify that there's changes that need to be applied in other places.
Author
Owner

@jeremystretch commented on GitHub (Oct 15, 2021):

I'm still working on the implementation here. We need a mechanism for storing the rule(s) by which the webhook data will be evaluated, when determining whether to generate a webhook.

For example, say we have a webhook defined for sites, but we want it to trigger only for sites with a status of "active." We need a way to encapsulate and store that logic in the database so that it's associated with the webhook object. The most naive approach would be to store a mapping of attributes in JSON; for example:

{
    "status": "active"
}

However, this quickly becomes complicated when we want to check multiple values, or a range of values, or something else slightly more complex.

These attributes could be specified in a manner similar to how object permission constraints are defined.

While I originally envisioned replicating the approach we use for object permissions, this relies on having data in the database, as opposed to Python objects. (We can't rely on database queries as this logic will sometimes be evaluated after an object has been deleted.)

I'd really like to avoid creating a domain-specific language for this. Maybe something like JsonLogic would be suitable, but I'm still evaluating. Happy to hear any suggestions!

@jeremystretch commented on GitHub (Oct 15, 2021): I'm still working on the implementation here. We need a mechanism for storing the rule(s) by which the webhook data will be evaluated, when determining whether to generate a webhook. For example, say we have a webhook defined for sites, but we want it to trigger only for sites with a status of "active." We need a way to encapsulate and store that logic in the database so that it's associated with the webhook object. The most naive approach would be to store a mapping of attributes in JSON; for example: ```json { "status": "active" } ``` However, this quickly becomes complicated when we want to check multiple values, or a range of values, or something else slightly more complex. > These attributes could be specified in a manner similar to how object permission constraints are defined. While I originally envisioned replicating the approach we use for object permissions, this relies on having data in the database, as opposed to Python objects. (We can't rely on database queries as this logic will sometimes be evaluated _after_ an object has been deleted.) I'd **really** like to avoid creating a domain-specific language for this. Maybe something like [JsonLogic](https://jsonlogic.com/) would be suitable, but I'm still evaluating. Happy to hear any suggestions!
Author
Owner

@jeremystretch commented on GitHub (Oct 25, 2021):

I ended up developing a simple framework for expressing conditional logic. I expect we'll continue to flesh it out a bit, but it's working and should satisfy the most common use cases for now.

@jeremystretch commented on GitHub (Oct 25, 2021): I ended up developing a simple framework for expressing conditional logic. I expect we'll continue to flesh it out a bit, but it's working and should satisfy the most common use cases for now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4806