JSON responses for pylxd

I’m using pylxd latest version (2.3.1). I tried to get all instances and I’m receiving those. But I want to add a check on response like below:

response = client.instances.all()

if response.status_code != 200:
    print("Failed to get instances. Status code:", response.status_code)
    exit()

if len(response) < 0:
    print("No instance found")
    exit()

if len(response):
    further work...

More, How can we directly get the full details of each instance without creating custom dictionary/custom parsing like below:

instance_list = []

    for instance in instances:

        instance_dict = {

            'name': instance.name,

            'status': instance.status,

            'ipv4': [],

            'ipv6': [],

            'architecture': instance.architecture,

            'created_at': instance.created_at,

            'config': instance.config,

            'devices': instance.devices,

            'ephemeral': instance.ephemeral,

            'expanded_config': instance.expanded_config,

            'expanded_devices': instance.expanded_devices,

            'description': instance.description,

            'profiles': instance.profiles,

            'last_used_at': instance.last_used_at,

            'location': instance.location,

            'type':instance.type,

            'status_code':instance.status_code,

            'stateful':instance.stateful

        }

and lastly how can we get to know what is actually return if we call any api using pylxd client?

You want to use recursion parameter and set it to 1 or 2 (but this does come at a cost)

That will return a bigger struct with more details, but from memory pylxd doesn’t return “Objects”.

https://linuxcontainers.org/lxd/api/master/ will tell you whats returned

1 Like

I’m using pylxd client to call the endpoints like mentioned in the official documentation

In the below example they are showing how to get all containers using pylxd client but in response to

client.instances.all()

we get (response)

[<instance.Instance at 0x7f95d8af72b0>,]

not the response like below:

"status": "Success",
  "status_code": 200,
  "type": "sync"

image