Skip to content
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

!next-prefix action tag using location and prefix type fails with prefix_length is null #154

Open
jeffkala opened this issue May 16, 2024 · 6 comments

Comments

@jeffkala
Copy link

Environment

  • Python version: 3.8
  • Nautobot version: 2.2.3
  • nautobot-design-builder version: 2.0.0

Expected Behavior

!next-prefix action tag will get-next properly when a filter criteria is specified to determine the "parent" prefixes to search in.

Observed Behavior

Prefix None failed validation

prefix_length: This field cannot be null.

Steps to Reproduce

  1. Create design
---
prefixes:
  - "!next_prefix":
      locations: "{{ site_name.parent.parent.parent.id }}"
      type: "container"
      length: "{{ ipam_templates[site_size | lower]['template_prefix_size'] | int }}"
    status__name: "Active"
    description: "{{ site_name }} Parent Prefix"
    type: "container"
    "!ref": "parent_prefix"
  1. Have available prefixes in the parent location
  2. Execute the job.
  3. Gives error
Traceback (most recent call last):
  File "/opt/nautobot/lib/python3.8/site-packages/celery/app/trace.py", line 477, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/celery/app/trace.py", line 760, in __protected_call__
    return self.run(*args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot/extras/jobs.py", line 1136, in run_job
    result = job(*args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot/extras/jobs.py", line 149, in __call__
    return self.run(*args, **deserialized_kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design_job.py", line 143, in run
    return self._run_in_transaction(dryrun, **kwargs)
  File "/usr/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design_job.py", line 209, in _run_in_transaction
    raise ex
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design_job.py", line 190, in _run_in_transaction
    self.implement_design(context, design_file, not dryrun)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design_job.py", line 138, in implement_design
    self.environment.implement_design(design, commit)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design.py", line 761, in implement_design
    raise ex
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design.py", line 746, in implement_design
    self._create_objects(self.model_map[key], value)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design.py", line 822, in _create_objects
    model.save()
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_design_builder/design.py", line 613, in save
    raise errors.DesignValidationError(self) from validation_error
nautobot_design_builder.errors.DesignValidationError: <unprintable DesignValidationError object>

For additonal testing did validate the same _get_next functionality from ORM returns a value.

>>> stuff = {"locations": l.parent.parent.parent.id, "type": "container"}
>>> query = Q(**stuff)
>>> query
<Q: (AND: ('locations', UUID('7e3cff0a-629a-42fe-b578-d02ab4cff4ae')), ('type', 'container'))>
>>> prefixes = Prefix.objects.filter(query)
>>> prefixes
<PrefixQuerySet [<Prefix: 10.34.0.0/16>, <Prefix: 1.34.0.0/16>, <Prefix: 1.41.0.0/16>]>
>>> length = 25
>>> _get_next(prefixes, length)
'10.34.0.0/25'
@abates
Copy link
Contributor

abates commented May 20, 2024

I think the issue here is that the length field is being passed in as a string, but it needs to be an integer. Try this:

---
prefixes:
  - "!next_prefix":
      locations: "{{ site_name.parent.parent.parent.id }}"
      type: "container"
      length: {{ ipam_templates[site_size | lower]['template_prefix_size'] | int }}
    status__name: "Active"
    description: "{{ site_name }} Parent Prefix"
    type: "container"
    "!ref": "parent_prefix"

@jeffkala
Copy link
Author

jeffkala commented May 22, 2024

Did try this and still got the same error. Even just hardcoded the length and it fails.

---
prefixes:
  - "!next_prefix":
      locations: "{{ site_name.parent.parent.parent.id }}"
      type: "container"
      length: 25
    status__name: "Active"
    description: "{{ site_name }} Parent Prefix"
    type: "container"
    "!ref": "parent_prefix"

@jeffkala
Copy link
Author

Does seem like its something about no prefixes available in certain circumstances. If I play around with it and make the length much smaller it results in the expected error around "no prefixes available for ..." still debugging further to see what is happening but if I ensure the prefix is available it works as expected.

@jeffkala
Copy link
Author

Still troubleshooting, but the culprit was a relationship with the attributes below. Once the relationship was deleted and nautobot restarted, the design job started working.
image

@itdependsnetworks
Copy link
Contributor

In a similar example, I just changed the source and destination to not have potential namepsace issues with the method names, such as prefix -> prefix1 and VLAN -> VLAN1 and that fixed the issue.

@abates
Copy link
Contributor

abates commented May 28, 2024

I was able to reproduce this in a test and it is exactly as @itdependsnetworks mentioned. Design Builder translates the source/destination labels of custom relationships to actual fields on a proxy model. If there is a clash then this type of issue is caused. If you look at the Prefix model it already has a vlan field:

In [4]: Prefix._meta.get_field("vlan")
Out[4]: <django.db.models.fields.related.ForeignKey: vlan>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants