Skip to content

Custom model validation exposed through plugins #4393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lampwins opened this issue Mar 21, 2020 · 5 comments
Closed

Custom model validation exposed through plugins #4393

lampwins opened this issue Mar 21, 2020 · 5 comments
Labels
topic: plugins Relates to the plugins framework type: feature Introduction of new functionality to the application

Comments

@lampwins
Copy link
Contributor

lampwins commented Mar 21, 2020

Environment

  • Python version: 2.7
  • NetBox version: 2.7.10

Proposed Functionality

Expose a plugin interface that allows for custom model validation to be performed when the model's clean() method is called. This interface will look similar to the plugin template content injection interface in which plugin developers specify the model they wish to act on. In this case, the class will implement only a single method validate():

class DeviceModelValidator(ModelValidator):
    model = 'dcim.device'

    def validate(self, obj):
        if obj.name == 'cat':
            raise ValidationError({
                'name': 'No cats allowed on the network!'
            })

As you can see, this method can either raise VailidationError or simply return None if the object passes validation. In the code path, these custom validators will be run at the end of the model's standard clean() method.

Use Case

As users adopt NetBox as a core component in automation solutions, data validity is key. NetBox reports can be used to highlight data consistency and integrity issues, but users must still fix these problems after the fact. By enforcing business logic rules when an object is created or updated, the chances of producing bad data are reduced.

The desire to implement custom validation has come up several times in the community before, but we have not really had a clean enough way to implement this until the plugins framework was born.

See: this comment, #3974, #3163, etc.

Database Changes

None

External Dependencies

The plugins framework introduced in 2.8.

@lampwins lampwins added status: under review Further discussion is needed to determine this issue's scope and/or implementation type: feature Introduction of new functionality to the application labels Mar 26, 2020
@jeremystretch
Copy link
Member

I'm going to punt on this for the initial implementation as there's already a lot going on. Let's put a pin in this until we figure out how we're going to handle feature requests for the plugins framework after the v2.8 release.

@lampwins
Copy link
Contributor Author

lampwins commented May 5, 2020

@jeremystretch can we pick this back up given the relatively light load of plugins related requests? I am happy to take this on.

@lampwins
Copy link
Contributor Author

Blocked by #4640

@lampwins lampwins added status: accepted This issue has been accepted for implementation status: blocked Another issue or external requirement is preventing implementation and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels May 13, 2020
@jeremystretch jeremystretch removed the status: accepted This issue has been accepted for implementation label Jul 24, 2020
@itdependsnetworks
Copy link

itdependsnetworks commented Aug 6, 2020

To further provide some use cases, here are a few I have noted with customers recently

Enforcing Business logic

  • Ensure that a hostname adheres to the internal naming standard.
  • Lookup to external system for new site creation, such as salesforce or similar.
  • Custom field verification such as "owner" on an IP address
  • Ensure every object is tied to a Tenant
  • Ensure consistency within subnetworks, e.g. only allow a /24 to be created within this /16

Many users "expect" the IPAM component to operate in a certain way. While it is clear that the base implementation cannot support that logic in order to support many more use cases, within the custom model validation you can add these capabilities.

  • Ensure that any network that has child networks is a "container"
  • Ensuring that no IP address within a container but not an active network is allowed. e.g. 10.10.0.0/16 container and only a 10.10.50.0/24 network, you should not be able to create an IP of 10.10.20.20 IP address
  • Ensure that IP addresses subnet mask matches up with the most specific network it is contained within

@jeremystretch jeremystretch added the topic: plugins Relates to the plugins framework label Oct 14, 2020
@jeremystretch
Copy link
Member

Closing this in favor of #5963, since it doesn't seem practical to require users to develop an entire plugin just to enforce model validation.

@jeremystretch jeremystretch removed the status: blocked Another issue or external requirement is preventing implementation label Mar 29, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: plugins Relates to the plugins framework type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

3 participants