XCP-ng
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login
    1. Home
    2. Butcat
    B
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 0
    • Posts 4
    • Groups 2

    Butcat

    @Butcat

    DevOps Team Vates đŸȘ

    Building DevOps tools for Xen Orchestra & XCP-ng. Much love for Go & Rust, happy to contribute to open source projects

    7
    Reputation
    5
    Profile views
    4
    Posts
    0
    Followers
    0
    Following
    Joined
    Last Online
    Age 28
    Website www.munimentum.eu
    Location France

    Butcat Unfollow Follow
    DevOps Team Vates đŸȘ

    Best posts made by Butcat

    • RE: Python help

      Hi there @Studmuffn1134 ,

      I've looked at your code and identified a few issues:

      1. You're trying to use basic auth (username/password), but Xen Orchestra's API typically uses token authentication through cookies
      2. You're making a GET request to the base URL instead of a specific API endpoint
      3. 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.

      posted in REST API
      B
      Butcat
    • RE: Python help

      Hello @Studmuffn1134, welcome!

      The provided python code will resembled yes however the path will be specific to the type and actions you need to perform. Also the returned payload will also be different.
      You can find bellow some informations that can help you understand and use the Xen Orchestra API.

      Here is a summary provided when searching with google about REST APIs patterns:

      Understanding REST: A Guide to API Design Patterns

      REST (Representational State Transfer) is a widely-used software architectural style that defines how APIs should be designed. It emphasizes simplicity, scalability, and efficiency, making it a popular choice for building web services and applications. REST APIs use standard HTTP methods (such as GET, POST, PUT, and DELETE) to interact with resources, which are identified by URLs. This approach makes REST APIs intuitive and easy to work with.

      To get the most out of REST, it’s important to understand its patterns and conventions. These patterns will not only improve your foundational knowledge but also help you interact with APIs.

      REST API Path Structure

      A typical REST API path follows a structured format, often resembling this pattern:

      http://host_or_domain_name/<types>/<id>/<action_verb>
      

      Here’s a breakdown of the components:

      • <types>: Represents the type of resource you’re interacting with (e.g., vms, users, networks).

      • <id>: A unique identifier for a specific resource (e.g., a VM ID or user ID).

      • <action_verb>: Specifies the action to be performed on the resource (e.g., actions, start, stop).

      HTTP Methods in REST

      REST APIs use specific HTTP methods to perform operations on resources:

      • GET: Retrieves data. For example, fetching a list of resources or details of a specific resource.

      • POST: Creates a new resource.

      • PUT: Replaces or updates an entire resource.

      • PATCH: Updates specific parts of a resource.

      • DELETE: Removes a resource.

      Example: REST API Paths for Virtual Machines (VMs)
      Let’s look at some examples using a vms resource type:

      List all VMs:

      GET http://host/rest/v0/vms
      

      This returns a list of VM IDs.

      Get details of a specific VM:

      GET http://host/rest/v0/vms/<id>
      

      This returns detailed information about the VM with the specified ID.

      List available actions for a VM:

      GET http://host/rest/v0/vms/<id>/actions
      

      This returns all actions that can be performed on the specified VM (e.g., start, stop, reboot).

      Perform an action on a VM:

      POST http://host/rest/v0/vms/<id>/actions/<your-action>
      

      This performs the specified action (e.g., start, stop) on the VM.

      Exploring Available REST Endpoints
      If you’re working with a REST API like Xen Orchestra, you can explore the available endpoints by navigating to the base URL in your browser or using a tool like Postman. For example:

      GET http://host/rest/v0
      

      This will return a list of available resource types and operations, such as:

      [
        "/rest/v0/hosts",
        "/rest/v0/messages",
        "/rest/v0/networks",
        "/rest/v0/pifs",
        "/rest/v0/pools",
        "/rest/v0/srs",
        "/rest/v0/vbds",
        "/rest/v0/vdi-snapshots",
        "/rest/v0/vdis",
        "/rest/v0/vifs",
        "/rest/v0/vm-controllers",
        "/rest/v0/vm-snapshots",
        "/rest/v0/vm-templates",
        "/rest/v0/vms",
        "/rest/v0/backup",
        "/rest/v0/groups",
        "/rest/v0/restore",
        "/rest/v0/tasks",
        "/rest/v0/servers",
        "/rest/v0/users",
        "/rest/v0/dashboard",
        "/rest/v0/alarms",
        "/rest/v0/docs"
      ]
      

      From here, you can follow the REST patterns described above to query specific resources or perform actions.

      Hope this help! 🙂

      posted in REST API
      B
      Butcat

    Latest posts made by Butcat

    • RE: Kubernetes Recipe

      Hello @abreaux, for the moment I have no idea however I am going to search and if nothing is available a task will be created for this feature 👍

      posted in Infrastructure as Code
      B
      Butcat
    • RE: Python help

      Hi @Studmuffn1134,

      You’re right—some actions (like certain host operations) aren’t fully available in the REST API yet. Right now, most "GET" methods work, and some actions (like those for VMs and snapshots) are implemented. However, the REST API is still a work in progress.

      The Xen Orchestra team is currently focusing on:

      • Non-admin user management (coming soon).

      • Save/backup functionality (planned for the next few months).

      Since not everything is ready in REST yet, you might need to use both REST and JSON-RPC to access all operations.

      For JSON-RPC, check out:

      Official JSON-RPC Documentation

      Basic JSON-RPC Examples (Forum Discussion)

      posted in REST API
      B
      Butcat
    • RE: Python help

      Hello @Studmuffn1134, welcome!

      The provided python code will resembled yes however the path will be specific to the type and actions you need to perform. Also the returned payload will also be different.
      You can find bellow some informations that can help you understand and use the Xen Orchestra API.

      Here is a summary provided when searching with google about REST APIs patterns:

      Understanding REST: A Guide to API Design Patterns

      REST (Representational State Transfer) is a widely-used software architectural style that defines how APIs should be designed. It emphasizes simplicity, scalability, and efficiency, making it a popular choice for building web services and applications. REST APIs use standard HTTP methods (such as GET, POST, PUT, and DELETE) to interact with resources, which are identified by URLs. This approach makes REST APIs intuitive and easy to work with.

      To get the most out of REST, it’s important to understand its patterns and conventions. These patterns will not only improve your foundational knowledge but also help you interact with APIs.

      REST API Path Structure

      A typical REST API path follows a structured format, often resembling this pattern:

      http://host_or_domain_name/<types>/<id>/<action_verb>
      

      Here’s a breakdown of the components:

      • <types>: Represents the type of resource you’re interacting with (e.g., vms, users, networks).

      • <id>: A unique identifier for a specific resource (e.g., a VM ID or user ID).

      • <action_verb>: Specifies the action to be performed on the resource (e.g., actions, start, stop).

      HTTP Methods in REST

      REST APIs use specific HTTP methods to perform operations on resources:

      • GET: Retrieves data. For example, fetching a list of resources or details of a specific resource.

      • POST: Creates a new resource.

      • PUT: Replaces or updates an entire resource.

      • PATCH: Updates specific parts of a resource.

      • DELETE: Removes a resource.

      Example: REST API Paths for Virtual Machines (VMs)
      Let’s look at some examples using a vms resource type:

      List all VMs:

      GET http://host/rest/v0/vms
      

      This returns a list of VM IDs.

      Get details of a specific VM:

      GET http://host/rest/v0/vms/<id>
      

      This returns detailed information about the VM with the specified ID.

      List available actions for a VM:

      GET http://host/rest/v0/vms/<id>/actions
      

      This returns all actions that can be performed on the specified VM (e.g., start, stop, reboot).

      Perform an action on a VM:

      POST http://host/rest/v0/vms/<id>/actions/<your-action>
      

      This performs the specified action (e.g., start, stop) on the VM.

      Exploring Available REST Endpoints
      If you’re working with a REST API like Xen Orchestra, you can explore the available endpoints by navigating to the base URL in your browser or using a tool like Postman. For example:

      GET http://host/rest/v0
      

      This will return a list of available resource types and operations, such as:

      [
        "/rest/v0/hosts",
        "/rest/v0/messages",
        "/rest/v0/networks",
        "/rest/v0/pifs",
        "/rest/v0/pools",
        "/rest/v0/srs",
        "/rest/v0/vbds",
        "/rest/v0/vdi-snapshots",
        "/rest/v0/vdis",
        "/rest/v0/vifs",
        "/rest/v0/vm-controllers",
        "/rest/v0/vm-snapshots",
        "/rest/v0/vm-templates",
        "/rest/v0/vms",
        "/rest/v0/backup",
        "/rest/v0/groups",
        "/rest/v0/restore",
        "/rest/v0/tasks",
        "/rest/v0/servers",
        "/rest/v0/users",
        "/rest/v0/dashboard",
        "/rest/v0/alarms",
        "/rest/v0/docs"
      ]
      

      From here, you can follow the REST patterns described above to query specific resources or perform actions.

      Hope this help! 🙂

      posted in REST API
      B
      Butcat
    • RE: Python help

      Hi there @Studmuffn1134 ,

      I've looked at your code and identified a few issues:

      1. You're trying to use basic auth (username/password), but Xen Orchestra's API typically uses token authentication through cookies
      2. You're making a GET request to the base URL instead of a specific API endpoint
      3. 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.

      posted in REST API
      B
      Butcat