Hi there @Studmuffn1134 ,
I've looked at your code and identified a few issues:
- You're trying to use basic auth (username/password), but Xen Orchestra's API typically uses token authentication through cookies
- You're making a GET request to the base URL instead of a specific API endpoint
- You're not using the
/vms/{vm_id}/actions/hard_shutdown
or/vms/{vm_id}/actions/clean_shutdown
endpoints
I was able to call successfully the shutdown endpoints but with verify off. Also FYI, I was using a lab and I did not build it from source however if you are using the latest version the API should be available.
Link for the swagger documentation is: http://host/rest/v0/docs/
#!/usr/bin/env python3
import requests
import os
import sys
from urllib3.exceptions import InsecureRequestWarning
# Disable insecure HTTPS warnings.
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def shutdown_vm(vm_id, xo_url, auth_token, use_force=True):
action = "hard_shutdown" if use_force else "clean_shutdown"
endpoint = f"{xo_url}/rest/v0/vms/{vm_id}/actions/{action}"
print(f"Sending {action} request for VM {vm_id}...")
headers = {
'Cookie': f'authenticationToken={auth_token}',
'Content-Type': 'application/json'
}
try:
response = requests.post(
endpoint,
headers=headers,
verify=False
)
print(f"Status code: {response.status_code}")
if response.status_code in [200, 202, 204]:
print(f"â Successfully initiated {'force' if use_force else 'clean'} shutdown")
return True
else:
print(f"Error: {response.text}")
return False
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return False
VM_ID = 'your_vm_id_here'
XO_URL = 'https://your_xo_url_here'
# You can navigate to the XO GUI and in the user section create an auth token.
AUTH_TOKEN = 'your_auth_token_here'
shutdown_vm(VM_ID, XO_URL, AUTH_TOKEN)
I hope it will help you fix the issue on your side. Please let me know if I can help with anything else.