[lxc-devel] [pylxd/master] New example
rockstar on Github
lxc-bot at linuxcontainers.org
Mon May 23 04:05:47 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 435 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160523/197eff08/attachment.bin>
-------------- next part --------------
From d16123d776d8fe5b526b3ba81aa3ff9e8ddb87ce Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul at eventuallyanyway.com>
Date: Sun, 22 May 2016 22:02:57 -0600
Subject: [PATCH 1/2] Update api example for PyLXD 2.0 API
---
examples/api_test.py | 27 ++++++++++-----------------
1 file changed, 10 insertions(+), 17 deletions(-)
diff --git a/examples/api_test.py b/examples/api_test.py
index dcaeede..9ec7495 100755
--- a/examples/api_test.py
+++ b/examples/api_test.py
@@ -16,24 +16,17 @@
import uuid
-from pylxd import api
+from pylxd.client import Client
# Let's pick a random name, avoiding clashes
-CONTAINER_NAME = str(uuid.uuid1())
+CONTAINER_NAME = str(uuid.uuid1()).split('-')[0]
-lxd = api.API()
-try:
- lxd.container_defined(CONTAINER_NAME)
-except Exception as e:
- print("Container doesnt exist: %s" % e)
+lxd = Client()
-config = {'name': CONTAINER_NAME,
- 'source': {'type': 'none'}}
-lxd.container_init(config)
-if lxd.container_defined(CONTAINER_NAME):
- print("Container is running")
-else:
- print("Whoops!")
-containers = lxd.container_list()
-for x in containers:
- lxd.container_destroy(x)
+container_config = {
+ 'name': CONTAINER_NAME,
+ 'source': {'type': 'none'},
+}
+container = lxd.containers.create(container_config, wait=True)
+
+container.delete(wait=True)
From 2dc3f01206367dd7c18074880e1aac8fba62263f Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul at eventuallyanyway.com>
Date: Sun, 22 May 2016 22:04:12 -0600
Subject: [PATCH 2/2] Update tests and code for issues found in updating
example
---
pylxd/container.py | 10 ++++++----
pylxd/tests/mock_lxd.py | 9 +++++++--
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/pylxd/container.py b/pylxd/container.py
index 38bbe04..4701b41 100644
--- a/pylxd/container.py
+++ b/pylxd/container.py
@@ -64,9 +64,11 @@ def create(cls, client, config, wait=False):
"""Create a new container config."""
response = client.api.containers.post(json=config)
+ if response.status_code != 202:
+ raise RuntimeError('Error creating instance')
if wait:
Operation.wait_for_operation(client, response.json()['operation'])
- return cls(name=config['name'])
+ return cls(name=config['name'], _client=client)
def __init__(self, **kwargs):
super(Container, self).__init__()
@@ -108,6 +110,8 @@ def delete(self, wait=False):
"""Delete the container."""
response = self._client.api.containers[self.name].delete()
+ if response.status_code != 202:
+ raise RuntimeError('Error deleting instance {}'.format(self.name))
if wait:
self.wait_for_operation(response.json()['operation'])
@@ -179,9 +183,7 @@ def list_snapshots(self):
def rename_snapshot(self, old, new, wait=False):
"""Rename a snapshot."""
response = self._client.api.containers[
- self.name].snapshots[old].post(json={
- 'name': new
- })
+ self.name].snapshots[old].post(json={'name': new})
if wait:
self.wait_for_operation(response.json()['operation'])
diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index 76fa2a2..e580ffd 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -2,7 +2,7 @@
def containers_POST(request, context):
- context.status_code = 201
+ context.status_code = 202
return json.dumps({'operation': 'operation-abc'})
@@ -18,6 +18,11 @@ def container_GET(request, context):
context.status_code = 404
+def container_DELETE(request, context):
+ context.status_code = 202
+ return json.dumps({'operation': 'operation-abc'})
+
+
def profile_GET(request, context):
name = request.path.split('/')[-1]
if name in ('an-profile', 'an-new-profile'):
@@ -60,7 +65,7 @@ def profile_GET(request, context):
'url': r'^http://pylxd.test/1.0/containers/(?P<container_name>.*)$',
},
{
- 'text': json.dumps({'operation': 'operation-abc'}),
+ 'text': container_DELETE,
'method': 'DELETE',
'url': r'^http://pylxd.test/1.0/containers/(?P<container_name>.*)$',
},
More information about the lxc-devel
mailing list