[lxc-devel] [pylxd/master] Added compatibility with operations project values

felix-engelmann on Github lxc-bot at linuxcontainers.org
Thu Dec 13 15:50:37 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 744 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181213/e7f61739/attachment.bin>
-------------- next part --------------
From 98c484d8b0960efb1829ee53ea2ac7e1d7f0d9f0 Mon Sep 17 00:00:00 2001
From: Felix Engelmann <fe-github at nlogn.org>
Date: Thu, 13 Dec 2018 16:47:01 +0100
Subject: [PATCH] Added compatibility with operations project values

the LXD Rest api returns an operation id with the query string
?project=default to notify in which project the operation belongs. This
was not sufficiently cut away by .split('/')[-1]
but also needed a subsequent .split('?')[0]

The Mock_lxd has been adapted to test for this feature

In the container.execute the operation_id is needed directly, where it
is fixed too

Signed-off-by: Felix Engelmann <fe-github at nlogn.org>
---
 pylxd/models/container.py |  3 ++-
 pylxd/models/operation.py |  2 ++
 pylxd/tests/mock_lxd.py   | 38 +++++++++++++++++++++-----------------
 3 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/pylxd/models/container.py b/pylxd/models/container.py
index 354c8967..375c2e87 100644
--- a/pylxd/models/container.py
+++ b/pylxd/models/container.py
@@ -397,7 +397,8 @@ def execute(
         })
 
         fds = response.json()['metadata']['metadata']['fds']
-        operation_id = response.json()['operation'].split('/')[-1]
+        operation_id = response.json()['operation']\
+            .split('/')[-1].split('?')[0]
         parsed = parse.urlparse(
             self.client.api.operations[operation_id].websocket._api_endpoint)
 
diff --git a/pylxd/models/operation.py b/pylxd/models/operation.py
index 94e102d8..c8172f49 100644
--- a/pylxd/models/operation.py
+++ b/pylxd/models/operation.py
@@ -37,6 +37,8 @@ def get(cls, client, operation_id):
         """Get an operation."""
         if operation_id.startswith('/'):
             operation_id = operation_id.split('/')[-1]
+        if '?' in operation_id:
+            operation_id = operation_id.split('?')[0]
         response = client.api.operations[operation_id].get()
         return cls(_client=client, **response.json()['metadata'])
 
diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index 06f54667..7bd2e991 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -5,7 +5,7 @@ def containers_POST(request, context):
     context.status_code = 202
     return json.dumps({
         'type': 'async',
-        'operation': 'operation-abc'})
+        'operation': '/1.0/operations/operation-abc?project=default'})
 
 
 def container_POST(request, context):
@@ -13,11 +13,11 @@ def container_POST(request, context):
     if not request.json().get('migration', False):
         return {
             'type': 'async',
-            'operation': 'operation-abc'}
+            'operation': '/1.0/operations/operation-abc?project=default'}
     else:
         return {
             'type': 'async',
-            'operation': 'operation-abc',
+            'operation': '/1.0/operations/operation-abc?project=default',
             'metadata': {
                 'metadata': {
                     '0': 'abc',
@@ -32,21 +32,22 @@ def container_DELETE(request, context):
     context.status_code = 202
     return json.dumps({
         'type': 'async',
-        'operation': 'operation-abc'})
+        'operation': '/1.0/operations/operation-abc?project=default'})
 
 
 def images_POST(request, context):
     context.status_code = 202
     return json.dumps({
         'type': 'async',
-        'operation': 'images-create-operation'})
+        'operation': '/1.0/operations/images-create-operation?project=default'
+    })
 
 
 def image_DELETE(request, context):
     context.status_code = 202
     return json.dumps({
         'type': 'async',
-        'operation': 'operation-abc'})
+        'operation': '/1.0/operations/operation-abc?project=default'})
 
 
 def networks_GET(request, _):
@@ -80,7 +81,7 @@ def networks_DELETE(_, context):
     context.status_code = 202
     return json.dumps({
         'type': 'sync',
-        'operation': 'operation-abc'})
+        'operation': '/1.0/operations/operation-abc?project=default'})
 
 
 def profile_GET(request, context):
@@ -108,14 +109,14 @@ def profile_DELETE(request, context):
     context.status_code = 200
     return json.dumps({
         'type': 'sync',
-        'operation': 'operation-abc'})
+        'operation': '/1.0/operations/operation-abc?project=default'})
 
 
 def snapshot_DELETE(request, context):
     context.status_code = 202
     return json.dumps({
         'type': 'async',
-        'operation': 'operation-abc'})
+        'operation': '/1.0/operations/operation-abc?project=default'})
 
 
 RULES = [
@@ -439,7 +440,7 @@ def snapshot_DELETE(request, context):
         'status_code': 202,
         'json': {
             'type': 'async',
-            'operation': 'operation-abc'},
+            'operation': '/1.0/operations/operation-abc?project=default'},
         'method': 'PUT',
         'url': r'^http://pylxd.test/1.0/containers/an-container/state$',  # NOQA
     },
@@ -451,7 +452,7 @@ def snapshot_DELETE(request, context):
     {
         'text': json.dumps({
             'type': 'async',
-            'operation': 'operation-abc'}),
+            'operation': '/1.0/operations/operation-abc?project=default'}),
         'status_code': 202,
         'method': 'PUT',
         'url': r'^http://pylxd.test/1.0/containers/an-container$',
@@ -474,7 +475,7 @@ def snapshot_DELETE(request, context):
                     }
                 },
             },
-            'operation': 'operation-abc'},
+            'operation': '/1.0/operations/operation-abc?project=default'},
         'status_code': 202,
         'method': 'POST',
         'url': r'^http://pylxd.test/1.0/containers/an-container/exec$',  # NOQA
@@ -493,7 +494,7 @@ def snapshot_DELETE(request, context):
     {
         'text': json.dumps({
             'type': 'async',
-            'operation': 'operation-abc'}),
+            'operation': '/1.0/operations/operation-abc?project=default'}),
         'status_code': 202,
         'method': 'POST',
         'url': r'^http://pylxd.test/1.0/containers/an-container/snapshots$',  # NOQA
@@ -511,7 +512,7 @@ def snapshot_DELETE(request, context):
     {
         'text': json.dumps({
             'type': 'async',
-            'operation': 'operation-abc'}),
+            'operation': '/1.0/operations/operation-abc?project=default'}),
         'status_code': 202,
         'method': 'POST',
         'url': r'^http://pylxd.test/1.0/containers/an-container/snapshots/an-snapshot$',  # NOQA
@@ -636,7 +637,9 @@ def snapshot_DELETE(request, context):
         'url': r'^http://pylxd2.test/1.0/images/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855$',  # NOQA
     },
     {
-        'text': json.dumps({'type': 'async', 'operation': 'operation-abc'}),
+        'text': json.dumps({
+            'type': 'async',
+            'operation': '/1.0/operations/operation-abc?project=default'}),
         'status_code': 202,
         'method': 'PUT',
         'url': r'^http://pylxd.test/1.0/images/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855$',  # NOQA
@@ -865,7 +868,8 @@ def snapshot_DELETE(request, context):
     },
     # create an async storage volume
     {
-        'json': {'type': 'async', 'operation': 'operation-abc'},
+        'json': {'type': 'async',
+                 'operation': '/1.0/operations/operation-abc?project=default'},
         'status_code': 202,
         'method': 'POST',
         'url': (r'^http://pylxd.test/1.0/storage-pools/'
@@ -909,7 +913,7 @@ def snapshot_DELETE(request, context):
     {
         'json': {
             "type": "async",
-            "operation": "operation-abc",
+            "operation": "/1.0/operations/operation-abc?project=default",
             "metadata": {
                 "control": "secret1",
                 "fs": "secret2"


More information about the lxc-devel mailing list