canonical / pylxd

Python module for LXD
https://pylxd.readthedocs.io/en/latest/
Apache License 2.0
256 stars 134 forks source link

api should expose container uid in a clean fashion Edit #255

Open javacruft opened 6 years ago

javacruft commented 6 years ago

Proxied from Launchpad Ubuntu Bug:

https://bugs.launchpad.net/ubuntu/+source/python-pylxd/+bug/1546203

Currently I'm getting the container uid as follows, which is ugly and presumably fragile:

vsm = lxd_api.get_container_config(container_name)['config']['volatile.last_state.idmap'] return ast.literal_eval(vsm.replace('true', 'True').replace('false', 'False'))[0]["Hostid"]

python-pylxd should expose this in a clean way.

ajkavanagh commented 6 years ago

The nicer way to do this, for the moment, is to use json:

import json
import pylxd

client = pylxd.Client()
c = client.containers.get('test')
idmap = json.loads(c.config['volatile.last_state.idmap'])
hostid = idmap[0]['Hostid']

It would be much nicer if pylxd exposed the attributes as something like:

c.config.volatile.last_state.idmap[0].Hostid

However, this would be a major breaking change to the existing API/usage. Maybe a 3.0 option. Also related to this is #214 (Attributes always returned as string regardles of their real type) which is really the same issue.

I'm leaving this open, as it's got a slightly nicer example of how to access the string attribute from Python, but also marking it as a duplicate.