nautobot / nautobot-app-design-builder

https://docs.nautobot.com/projects/design-builder/en/latest/
Other
8 stars 2 forks source link

Using `post_implementation` alongside a `!ref:create:<attr>` runs at wrong time #153

Closed jeffkala closed 3 months ago

jeffkala commented 4 months ago

Environment

Expected Behavior

Having access to objects that you created during the run method in the post_implementation method.

Observed Behavior

When using !ref:create:name for example on a device, if you change it to !ref:create_or_update:name it works.

Steps to Reproduce

  1. Create a simple design to create a device.
    ---
    devices:
    - "!create:name": "{{ device_hostname }}"
    "location__name": "{{ site_name }}"
    "status__name": "{{ device_status }}"
    "device_type__model": "{{ device_model }}"
    "role__name": "{{ device_role }}"
    "platform__name": "{{ device_platform }}"
  2. Add a simple post_implementation method and try to get the device object that was just created.
    def post_implementation(self, context, environment):
        """Generate bootstrap config."""
        current_device = Device.objects.get(name=context["device_hostname"])
  3. Run the design job. Results in a exception
    Traceback (most recent call last):
    File "/usr/local/lib/python3.11/site-packages/celery/app/trace.py", line 477, in trace_task
    R = retval = fun(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/celery/app/trace.py", line 760, in __protected_call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/nautobot/extras/jobs.py", line 1136, in run_job
    result = job(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/nautobot/extras/jobs.py", line 149, in __call__
    return self.run(*args, **deserialized_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/nautobot_design_builder/design_job.py", line 143, in run
    return self._run_in_transaction(dryrun, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/nautobot_design_builder/design_job.py", line 212, in _run_in_transaction
    raise ex
    File "/usr/local/lib/python3.11/site-packages/nautobot_design_builder/design_job.py", line 192, in _run_in_transaction
    self.post_implementation(context, self.environment)
    File "/opt/nautobot/git/nb2_jobs/designs/new_device_registration/jobs.py", line 64, in post_implementation
    current_device = Device.objects.get(name=context["device_hostname"])
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 435, in get
    raise self.model.DoesNotExist(
    nautobot.dcim.models.devices.Device.DoesNotExist: Device matching query does not exist.
abates commented 3 months ago

I believe this is fixed in pull #156. Any chance you can test?