XCP-ng
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Python help

    Scheduled Pinned Locked Moved REST API
    28 Posts 5 Posters 1.0k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      Studmuffn1134 @olivierlambert
      last edited by olivierlambert

      @olivierlambert import requests

      import os
      from requests.auth import HTTPBasicAuth
      # Path to the certificate
      root_dir = os.path.dirname(os.path.abspath(__file__))
      file_path = os.path.join(root_dir, "Certs\\fullchain.pem")
      
      # Define the Xen Orchestra API endpoint
      # Define the ID of the VM you want to shut down (you can get this from the XO UI or API)
      VM_ID = '573914d5-69c4-9041-6d3f-6d01f6d29d69'
      
      def make_get_request():
          USERNAME = 'ThePlague'
          PASSWORD = 'NO'
          XO_SERVER_URL = 'https://nordstromfamily.net:2223/rest/v0'
          BEARER_TOKEN = "NO"
          try:
              # Make the GET request using Basic Authentication
              response = requests.get(XO_SERVER_URL, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False)  # Set verify=False if you have SSL issues
      
              if response.status_code == 200:
                  print("Response from the API:", response.json())  # Print the API response (if JSON)
              else:
                  print(f"Error: {response.status_code} - {response.text}")  # Print error if authentication fails
      
          except requests.exceptions.RequestException as e:
              print(f"Request failed: {e}")
      
      if __name__ == '__main__':
          make_get_request()
      

      Would code like that work i keep getting an authorizaiton error an i know for a fact that my username password or token are working

      1 Reply Last reply Reply Quote 0
      • olivierlambertO Offline
        olivierlambert Vates đŸȘ Co-Founder CEO
        last edited by olivierlambert

        Please use the markdown syntax (I edited your post) otherwise it's impossible to read your code.

        Also please paste the error. I'm asking the DevOps Tool team to take a look.

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          Studmuffn1134 @olivierlambert
          last edited by

          @olivierlambert how do i use the mark down tool hear do i do the '''?

          S 1 Reply Last reply Reply Quote 0
          • S Offline
            Studmuffn1134 @Studmuffn1134
            last edited by

            @Studmuffn1134

            import os
            from requests.auth import HTTPBasicAuth
            # Path to the certificate
            root_dir = os.path.dirname(os.path.abspath(__file__))
            file_path = os.path.join(root_dir, "Certs\\fullchain.pem")
            
            # Define the Xen Orchestra API endpoint
            # Define the ID of the VM you want to shut down (you can get this from the XO UI or API)
            VM_ID = '573914d5-69c4-9041-6d3f-6d01f6d29d69'
            
            def make_get_request():
                USERNAME = 'ThePlague'
                PASSWORD = 'NO'
                XO_SERVER_URL = 'https://nordstromfamily.net:2223/rest/v0'
                BEARER_TOKEN = "NO"
                try:
                    # Make the GET request using Basic Authentication
                    response = requests.get(XO_SERVER_URL, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False)  # Set verify=False if you have SSL issues
            
                    if response.status_code == 200:
                        print("Response from the API:", response.json())  # Print the API response (if JSON)
                    else:
                        print(f"Error: {response.status_code} - {response.text}")  # Print error if authentication fails
            
                except requests.exceptions.RequestException as e:
                    print(f"Request failed: {e}")
            
            if __name__ == '__main__':
                make_get_request()```
            S 1 Reply Last reply Reply Quote 0
            • S Offline
              Studmuffn1134 @Studmuffn1134
              last edited by

              @Studmuffn1134 stromfamily.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
              warnings.warn(
              Error: 401 - Unauthorized
              That is the output i know about the cert warnings so dont worry about that it is a red herring the 401 though i cant get it to pass any sort of username password or token. I built xo from sources so is there any thing i have to do to enable the rest api and or tell the api to transmit on that dedicated port

              1 Reply Last reply Reply Quote 0
              • B Offline
                Butcat DevOps Team Vates đŸȘ
                last edited by Butcat

                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.

                S 1 Reply Last reply Reply Quote 3
                • S Offline
                  Studmuffn1134 @Butcat
                  last edited by

                  @Butcat It worked thank you so much I never really worked with api's before so I am learning

                  S 1 Reply Last reply Reply Quote 0
                  • S Offline
                    Studmuffn1134 @Studmuffn1134
                    last edited by

                    @Studmuffn1134 said in Python help:

                    Reply

                    Is it the same code for the hosts or do I have to use different api links for host actions can you help me with that?

                    B 1 Reply Last reply Reply Quote 0
                    • B Offline
                      Butcat DevOps Team Vates đŸȘ @Studmuffn1134
                      last edited by Butcat

                      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! 🙂

                      S 1 Reply Last reply Reply Quote 1
                      • S Offline
                        Studmuffn1134 @Butcat
                        last edited by

                        @Butcat This was very very very helpful. https://192.168.100.30:2223/rest/v0/host/d2f1374c-728d-4905-85cc-e0d7166a3fbf/actions is there no way to turn off a single host through the api. The interesting part is I have no actions in the host section with that host id?

                        B 1 Reply Last reply Reply Quote 0
                        • B Offline
                          Butcat DevOps Team Vates đŸȘ @Studmuffn1134
                          last edited by

                          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)

                          S 1 Reply Last reply Reply Quote 0
                          • S Offline
                            Studmuffn1134 @Butcat
                            last edited by

                            @Butcat def shutdown_vm_hosts(host_id,xo_url,auth_token,use_force):
                            root_dir = os.path.dirname(os.path.abspath(file))
                            file_path = os.path.join((root_dir),"Certs\fullchain.pem")

                            headers = {
                                'Content-Type':'application/json',
                                'Authorization':f'Bearer {auth_token}'
                            }
                            data = {
                                "jsonrpc":"2.0",
                                "method":"host.shutdown",
                                "params":[host_id],
                                "id":1
                            }
                            try:
                                response = requests.post(
                                    xo_url,
                                    headers=headers,data=json.dumps(data),
                                    verify=file_path
                                )
                                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
                            
                            S 1 Reply Last reply Reply Quote 0
                            • S Offline
                              Studmuffn1134 @Studmuffn1134
                              last edited by Studmuffn1134

                              @Studmuffn1134 That is what i did and it still does not work I get a status code 200 but it never shuts the host off

                              1 Reply Last reply Reply Quote 0
                              • TheNorthernLightT Offline
                                TheNorthernLight @Studmuffn1134
                                last edited by

                                @Studmuffn1134 LOL at "ThePlague"... (hackers unite!)

                                S 1 Reply Last reply Reply Quote 0
                                • S Offline
                                  Studmuffn1134 @TheNorthernLight
                                  last edited by

                                  @TheNorthernLight Finally someone gets the reference But what do u think I am doing wrong

                                  TheNorthernLightT 1 Reply Last reply Reply Quote 0
                                  • TheNorthernLightT Offline
                                    TheNorthernLight @Studmuffn1134
                                    last edited by

                                    @Studmuffn1134 Sadly, I dont know squat about python, sorry!

                                    S 1 Reply Last reply Reply Quote 0
                                    • S Offline
                                      Studmuffn1134 @TheNorthernLight
                                      last edited by

                                      @TheNorthernLight Well could you do it in a language you know and maybe i would be able to convert it>?

                                      1 Reply Last reply Reply Quote 0
                                      • G Offline
                                        Gurve
                                        last edited by

                                        Did some "kicking" around in python, I don't python that much so "readers beware"

                                        How did i figure it out, the french blog from here was useful, but only showed listing methods. I was still very confused as to how to call the vm.stop and which parameters it took.

                                        Enter xo-cli ❀

                                        xo-cli uses jsonrpc but is CLi only, but you can get very nice info from it just have to register and call "list-methods". should be available on your xo VM

                                        xo-cli register http://[yourXO].example.com [yourusername]
                                        #after registering/authenticating
                                        xo-cli list-commands | egrep 'vm\.' --color=always
                                        

                                        here you will get a nice list of all "methods" in jsonrpc related to vm and a line about vm.stop:

                                        vm.stop id=<string> [force=<boolean>] [forceShutdownDelay=<number>] [bypassBlockedOperation=<boolean>]

                                        which was enough information to alter the french guy's (Baron) example into this:

                                        import aiohttp
                                        import asyncio
                                        
                                        from jsonrpc_websocket import Server
                                        
                                        async def routine():
                                            async with aiohttp.ClientSession() as client:
                                                server = Server('ws://[yourXO]/api/', client)
                                        
                                                await server.ws_connect()
                                        
                                                # signIn required
                                                result = await server.session.signIn(username='[xoAdmin]', password='[xoAdmin]') # email attribute is working in place of username
                                                
                                                #hard shutdown
                                                #result = await server.vm.stop(id='3f32beeb-ab3f-a8ac-087d-fdc7ed061b58', force=(bool(1)))
                                                
                                                #clean Shutdown
                                                result = await server.vm.stop(id='3f32beeb-ab3f-a8ac-087d-fdc7ed061b58', force=(bool(0)))
                                                
                                                print (result)
                                        
                                        asyncio.get_event_loop().run_until_complete(routine())
                                        
                                        
                                        
                                        S 1 Reply Last reply Reply Quote 0
                                        • S Offline
                                          Studmuffn1134 @Gurve
                                          last edited by

                                          @Gurve Is this for the vm's on the server or the host itself i need the host itself

                                          G 1 Reply Last reply Reply Quote 0
                                          • G Offline
                                            Gurve @Studmuffn1134
                                            last edited by

                                            @Studmuffn1134 Sorry, must have somehow read another reply about vm and mixed them. But pretty sure you should be able to utilise the steps I did for host shutdown

                                            xo-cli to get relevant api endpoints, xo-cli to get parameters for said endpoint and then press play

                                            S 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post