lxdapi¶
This library includes basic helpers encapsulating boring business logic code for interacting with the lxd daemon. It is an experimental alternative to the pylxd approach. The purpose of this library is not to be a full blown framework for interacting with lxd completely abstracting HTTP transactions in an hermetic way, pylxd already does a good job at that.
Contents:
HTTP API transactions¶
API¶
HTTP API classes for interfacing with the LXD API.
This module leverages the composition pattern, providing 3 main classes:
API
: the entry point for dealing with the HTTP API,APIResult
: returned by requests made withAPI
, it represents an HTTP transaction.APIException
: raised when the server responded with HTTP 400.
The API
object wraps around requests, note that its constructor takes
a debug keyword argument to enable printouts of HTTP transactions, that can
also be enabled with the DEBUG
environment variable.
-
classmethod
API.
factory
(endpoint=None, default_version=None, **kwargs)[source]¶ Instanciate an
API
with the right endpoint and session.Example:
# Connect to a local socket api = lxd.API.factory() # Or, connect to a remote server (untested so far) api = lxd.API.factory(base_url='http://example.com:12345')
-
class
lxdapi.api.
API
(session, endpoint, default_version=None, debug=False)[source]¶ Main entry point to interact with the HTTP API.
Once you have an instance of
API
, which is easier to make withfactory()
than with the constructor, use theget()
,post()
,delete()
,put()
orrequest()
directly. Sincerequest()
is used by the other methods, refer to torequest()
for details.Example:
api = lxd.API.factory() api.post('images', json=data_dict).wait()
-
API.
request
(method, url, *args, **kwargs)[source]¶ Execute an HTTP request, return an
APIResult
.Note that it calls
APIResult.validate()
, which may raiseAPIException
orAPINotFoundException
.If
debug
is True, then this will dump HTTP request and response data.Extra args and kwargs are passed to
requests.Session.request()
.
If the server responds with HTTP/400 then an APIException will be raised. It
has the APIResult as result
attribute and has the error message from the
server as error. It looks like this:
APIException: GET http+unix://%2Fvar%2Flib%2Flxd%2Funix.socket/1.0/operations/1c34b923-57c8-4fce-b349-e4a1c61b8803/wait?timeout=30 Error calling 'lxd forkstart pylxd-test-container /var/lib/lxd/containers /var/log/lxd/pylxd-test-container/lxc.conf': err='exit status 1'
Debugging¶
The purpose of this lib is to help the user to have feedback from HTTP
transactions. If anything fails, it should help a lot to re-run the program
with the DEBUG
env var, and such output will be printed out for every
transaction:
PUT http+unix://%2Fvar%2Flib%2Flxd%2Funix.socket/1.0/containers/pylxd-test-container/state
{
"action": "stop",
"timeout": 30
} HTTP/202
{
"status": "Operation created",
"status_code": 100,
"operation": "/1.0/operations/70eb42bf-5e79-42de-8230-2162e9e8b612",
"type": "async",
"metadata": {
"status": "Running",
"err": "",
"status_code": 103,
"created_at": "2016-06-23T15:05:45.435413676+02:00",
"updated_at": "2016-06-23T15:05:45.435413676+02:00",
"class": "task",
"may_cancel": false,
"id": "70eb42bf-5e79-42de-8230-2162e9e8b612",
"resources": {
"containers": [
"/1.0/containers/pylxd-test-container"
]
},
"metadata": null
}
}
APIResult¶
-
class
lxdapi.api.
APIResult
(api, response)[source]¶ Represent an HTTP transaction, return by API calls using
API
.-
data
¶ JSON data from the response.
-
request
¶ Request object from the requests library.
-
response
¶ Response object from the requests library.
-
validate
()[source]¶ Recursive status code checker for this result’s response.
If the response’s status code is 404 then raise
APINotFoundException
.If the response’s status code is anything superior or equal to 400 then raise
APIException
It’ll use
validate_metadata()
to check metadata.
-
validate_metadata
(data)[source]¶ Recursive function to check status code for a metadata dict.
Each metadata may contain more metadata. Each metadata may have a status_code, if it’s superior or equal to 400 then an
APIException
is raised.This is used by
validate()
which should be used in general instead of this method.
-
Shortcut functions¶
Idempotent functions and shortcuts.
Functions here have the following similarities:
- take a
API
as first argument, - return True if something has changed, False otherwise,
- except
_get()
functions such ascontainer_get()
which returnAPIResult
for anlxdapi.api.API.get()
or False.
-
lxdapi.shortcuts.
container_absent
(api, container)[source]¶ Ensure a container is absent.
Container is an
APIResult
for the container, to be able to compare the configuration with.It is expected that the user manages the HTTP transactions, here’s an example usage:
container_absent(api, container_get('yourcontainer'))
-
lxdapi.shortcuts.
container_apply_config
(api, container, config)[source]¶ Apply a configuration on a container.
Container is an:class:`lxdapi.api.APIResult`for the container, to be able to compare the configuration with.
Config is the dict to pass as JSON to the HTTP API.
Example usage:
container_apply_config(api, container_get('yourcontainer'))
-
lxdapi.shortcuts.
container_apply_status
(api, container, status)[source]¶ Apply an LXD status to a container.
Container is an:class:`lxdapi.api.APIResult`for the container, to be able to compare the status with.
Status is a string, choices are: Running, Stopped, Frozen.
Example usage:
container_apply_status(api, container_get('yourcontainer'), 'Running')
-
lxdapi.shortcuts.
container_get
(api, name)[source]¶ Return the:class:`lxdapi.api.APIResult`for a container or False.
-
lxdapi.shortcuts.
image_absent
(api, fingerprint)[source]¶ Return False if the image is absent, otherwise delete it and return True.
-
lxdapi.shortcuts.
image_alias_present
(api, name, target, description=None)[source]¶ Ensure an image has an alias.