This is the function I used to retrieve data. The same header works for machine addition, but it throws an Unrecognised Signature Error when used with GET requests. Please answer quickly; it would be appreciated.
from django.http import JsonResponse
import logging
from oauthlib.oauth1 import SIGNATURE_PLAINTEXT
from requests_oauthlib import OAuth1Session
import requests
Configure logging
logger = logging.getLogger(name)
def fetch_maas_nodes(maas_server_ip, api_key):
“”"
Fetch nodes from the MAAS API using OAuth1 authentication.
:param maas_server_ip: IP address of the MAAS server
:param api_key: API key in the format 'CONSUMER_KEY:CONSUMER_TOKEN:SECRET'
:return: JSON response from the MAAS API or raises an exception if the request fails
"""
# Extract credentials from the API key
consumer_key, consumer_token, secret = api_key.split(":")
# MAAS API URL
maas_host = f"http://{maas_server_ip}:5240/MAAS"
url = f"{maas_host}/api/2.0/machines/"
# Create an OAuth1 session
maas = OAuth1Session(
consumer_key,
resource_owner_key=consumer_token,
resource_owner_secret=secret,
signature_method=SIGNATURE_PLAINTEXT
)
try:
# Perform a GET request to fetch nodes
response = maas.get(url, params={"op": "list_allocated"})
response.raise_for_status() # Raise an exception for HTTP errors
return response.json() # Return JSON response
except requests.exceptions.RequestException as e:
# Handle and log exceptions
logger.error(f"Error while communicating with MAAS API: {e}")
raise # Re-raise the exception to be handled by the caller
data = fetch_maas_nodes(maas_server_ip, api_key)
logger.debug(f"API Response Data: {data}")
return JsonResponse(data, safe=False)
except Exception as e:
logger.error(f"Failed to fetch nodes: {e}")
return JsonResponse({'error': str(e)}, status=500)
return JsonResponse({'error': 'This view only supports GET requests'}, status=405)
<Response [200]>
I received a response with status 200, but the data returned is an empty list (DEBUG API Response Data: [];).
However, in the MAAS UI, I see that there is one data entry available under MAAS/r/machines.
please guide me if anything i missed