[lxc-devel] [pylxd/branch-2.0] Branch 2.0

ajkavanagh on Github lxc-bot at linuxcontainers.org
Thu Mar 8 17:07:06 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 676 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180308/82c6223e/attachment.bin>
-------------- next part --------------
From e20fef4f21e303538fb7ce95cd7bebc56549d958 Mon Sep 17 00:00:00 2001
From: Alex Kavanagh <alex at ajkavanagh.co.uk>
Date: Thu, 8 Mar 2018 16:46:12 +0100
Subject: [PATCH 1/2] Fix Operation class to allow unknown attributes

lxd 3.0.0-beta{x} grew a new feature in that the operation metadata
includes a 'description' tag.  This breaks pylxd, and so this fix adds
the 'description' key, and also then makes the class more robust to
handle unknown attributes in the future.  Note that ALL the other
classes got this fix in 2.2.4.

Fixes bug: #284

Signed-off-by: Alex Kavanagh <alex at ajkavanagh.co.uk>
---
 pylxd/operation.py            | 18 +++++++++++++++---
 pylxd/tests/test_operation.py | 16 ++++++++++++++++
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/pylxd/operation.py b/pylxd/operation.py
index 1594680..94e102d 100644
--- a/pylxd/operation.py
+++ b/pylxd/operation.py
@@ -11,6 +11,9 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
+
+import warnings
+
 from pylxd import exceptions
 
 
@@ -19,8 +22,8 @@ class Operation(object):
 
     __slots__ = [
         '_client',
-        'class', 'created_at', 'err', 'id', 'may_cancel', 'metadata',
-        'resources', 'status', 'status_code', 'updated_at']
+        'class', 'created_at', 'description', 'err', 'id', 'may_cancel',
+        'metadata', 'resources', 'status', 'status_code', 'updated_at']
 
     @classmethod
     def wait_for_operation(cls, client, operation_id):
@@ -40,7 +43,16 @@ def get(cls, client, operation_id):
     def __init__(self, **kwargs):
         super(Operation, self).__init__()
         for key, value in kwargs.items():
-            setattr(self, key, value)
+            try:
+                setattr(self, key, value)
+            except AttributeError:
+                # ignore attributes we don't know about -- prevent breakage
+                # in the future if new attributes are added.
+                warnings.warn(
+                    'Attempted to set unknown attribute "{}" '
+                    'on instance of "{}"'
+                    .format(key, self.__class__.__name__))
+                pass
 
     def wait(self):
         """Wait for the operation to complete and return."""
diff --git a/pylxd/tests/test_operation.py b/pylxd/tests/test_operation.py
index b250af9..055ec2b 100644
--- a/pylxd/tests/test_operation.py
+++ b/pylxd/tests/test_operation.py
@@ -12,6 +12,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import json
+
 from pylxd import exceptions, operation
 from pylxd.tests import testing
 
@@ -56,3 +58,17 @@ def error(request, context):
         an_operation = operation.Operation.get(self.client, name)
 
         self.assertRaises(exceptions.LXDAPIException, an_operation.wait)
+
+    def test_unknown_attribute(self):
+        self.add_rule({
+            'text': json.dumps({
+                'type': 'sync',
+                'metadata': {'id': 'operation-unknown',
+                             'metadata': {'return': 0},
+                             'unknown': False},
+                }),
+            'method': 'GET',
+            'url': r'^http://pylxd.test/1.0/operations/operation-unknown$',
+        })
+        url = '/1.0/operations/operation-unknown'
+        operation.Operation.get(self.client, url)

From a99ae53caf000fd1737cdc0d51888f19dd94624a Mon Sep 17 00:00:00 2001
From: Alex Kavanagh <alex at ajkavanagh.co.uk>
Date: Thu, 8 Mar 2018 18:05:04 +0100
Subject: [PATCH 2/2] Bump version to 2.0.7 for next point release

Signed-off-by: Alex Kavanagh <alex at ajkavanagh.co.uk>
---
 setup.cfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/setup.cfg b/setup.cfg
index 8ea40a9..fc1ed7f 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,7 +1,7 @@
 [metadata]
 name = pylxd
 summary = python library for lxd
-version = 2.0.6
+version = 2.0.7
 description-file =
     README.rst
 author = Paul Hummer


More information about the lxc-devel mailing list