MaaS API with Python - op-allocate - getting a 500 error - wrong content type

I’m trying to use the machine API to allocate a machine (MAAS | API).

My code looks like:

maas_machines = get_machines()
machine_id = next((m.id for m in maas_machines if m.fqdn == fqdn), None)

if machine_id is None:
    logger.error(f"Machine {fqdn} not found in MaaS")
    raise Exception(f"Machine {fqdn} not found in MaaS")

maas = OAuth1Session(
    MAAS_CONSUMER_KEY,
    resource_owner_key=MAAS_CONSUMER_TOKEN,
    resource_owner_secret=MAAS_CONSUMER_SECRET,
    signature_method=SIGNATURE_PLAINTEXT,
)

url = f"{MAAS_API_BASE_URL}/api/2.0/machines/op-allocate"

try:
    payload = {"system_id": machine_id}
    maas.post(
        url,
        json=payload,
        verify=False,
    ).raise_for_status()
except Exception as e:
    logger.error(f"Error allocating machine: {e}")
    raise e

But I’m getting a 500 error back:

500 Server Error: Internal Server Error for url: http://maas.domain.com:5240/MAAS/api/2.0/machines/op-allocate

I already know that:

  • the base url is correct (used to retrieve the list of machines)
  • the id of the machine I am retrieving is the correct one (same one as the one on the web interface)
  • the authentication is fine (used to retrieve the list of machines)

When looking at the MaaS logs, I get the following exception:

2024-02-27 15:37:37 maasserver: [error] ################################ Exception: ‘dict’ object has no attribute ‘lists’ ################################
2024-02-27 15:37:37 maasserver: [error] Traceback (most recent call last):
File “/usr/lib/python3/dist-packages/django/core/handlers/base.py”, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “/usr/lib/python3/dist-packages/maasserver/utils/views.py”, line 298, in view_atomic_with_post_commit_savepoint
return view_atomic(*args, **kwargs)
File “/usr/lib/python3.10/contextlib.py”, line 79, in inner
return func(*args, **kwds)
File “/usr/lib/python3/dist-packages/maasserver/api/support.py”, line 62, in call
response = super().call(request, *args, **kwargs)
File “/usr/lib/python3/dist-packages/django/views/decorators/vary.py”, line 20, in inner_func
response = func(*args, **kwargs)
File “/usr/lib/python3.10/dist-packages/piston3/resource.py”, line 197, in call
result = self.error_handler(e, request, meth, em_format)
File “/usr/lib/python3.10/dist-packages/piston3/resource.py”, line 195, in call
result = meth(request, *args, **kwargs)
File “/usr/lib/python3/dist-packages/maasserver/api/support.py”, line 371, in dispatch
return function(self, request, *args, **kwargs)
File “/usr/lib/python3/dist-packages/maasserver/api/machines.py”, line 2518, in allocate
param for param in request.data.lists() if param[0] != “op”
AttributeError: ‘dict’ object has no attribute ‘lists’

How can I troubleshoot that further?

I sorted it out by changing the type of data passed to requests:

payload = {"system_id": machine_id}
maas.post(
    url,
    data=payload,
    verify=False,
).raise_for_status()

Note the datainstead of the jsonpayload.

For the record, the V2 api are not consuming json but rather multipart/form-data. see https://maas.io/docs/api

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.