I have a custom script defined like this (I shortened the run function to eliminate as much of my code as possible as the source of the problem):
class ProbeDevice(Script):
class Meta:
name = "Probe Device"
description = "Populate basic device information"
field_order = ['device']
device = ObjectVar(
description = "Device to probe",
queryset = Device.objects.all()
)
def run(self, data, commit):
device = data['device']
self.log_success("Starting script for device [{}]({})".format(device, reverse('dcim:device', kwargs={'pk':
device.pk})))
When I attempt to call it via the API I get the following:
{"log":[{"status":"failure","message":"An exception occurred: `AttributeError: 'int' object has no attribute '_meta'`\n```\nTraceback (most recent call last):\n File \"/opt/netbox/netbox/extras/scripts.py\", line 408, in run_script\n output = script.run(**kwargs)\n File \"/etc/netbox/scripts/devices.py\", line 42, in run\n return serializers.serialize('json', [device])\n File \"/usr/local/lib/python3.7/site-packages/django/core/serializers/__init__.py\", line 128, in serialize\n s.serialize(queryset, **options)\n File \"/usr/local/lib/python3.7/site-packages/django/core/serializers/base.py\", line 94, in serialize\n concrete_model = obj._meta.concrete_model\nAttributeError: 'int' object has no attribute '_meta'\n\n```"},{"status":"info","message":"Database changes have been reverted automatically."}],"output":null}
Which seems to indicate that my data object is not the type it is expecting? How is one supposed to feed in values for an ObjectVar parameter type when executing a custom script via the API?
To sanity check my work, I have another script that takes only a string as input:
class FindDevice(Script):
class Meta:
name = "Find Device"
description = "Find a device by IP and populate basic device information"
field_order = ['ip_address']
ip_address = StringVar(
description = "IPv4 address of the device to probe",
min_length = 8,
max_length = 16
)
def run(self, data, commit):
self.log_success("Starting script for IP address: {}".format(data['ip_address']))
And when I call it like this:
I get a JSON object representing the response as expected