From db78c4d03aebdf559d72395d8d6456b47ed190c1 Mon Sep 17 00:00:00 2001 From: smile-luobin Date: Fri, 1 Apr 2016 10:34:17 +0000 Subject: [PATCH] Fix SwiftBankPlugin Add serialize and deserialize in swift_bank_plugin; Fix bugs in list_objects; Add get_owner_id function; Change-Id: I916a6c8f3c02167fbd0e5f00ac94267ff04e11d4 --- .../bank_plugins/swift_bank_plugin.py | 23 +++++++++++++++---- .../unit/protection/fake_swift_client.py | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/smaug/services/protection/bank_plugins/swift_bank_plugin.py b/smaug/services/protection/bank_plugins/swift_bank_plugin.py index 3b7e5aac..bb356444 100644 --- a/smaug/services/protection/bank_plugins/swift_bank_plugin.py +++ b/smaug/services/protection/bank_plugins/swift_bank_plugin.py @@ -10,6 +10,10 @@ # License for the specific language governing permissions and limitations # under the License. +import json +import time +import uuid + from oslo_config import cfg from oslo_log import log as logging from oslo_service import loopingcall @@ -19,8 +23,6 @@ from smaug.services.protection.bank_plugin import BankPlugin from smaug.services.protection.bank_plugin import LeasePlugin from swiftclient import client as swift from swiftclient import ClientException -import time -import uuid swift_client_opts = [ cfg.StrOpt('bank_swift_url', @@ -132,11 +134,17 @@ class SwiftBankPlugin(BankPlugin, LeasePlugin): cacert=self.swift_ca_cert_file) return connection + def get_owner_id(self): + return self.owner_id + def create_object(self, key, value): try: + if not isinstance(value, str): + value = json.dumps(value) self._put_object(container=self.bank_object_container, obj=key, - contents=value) + contents=value, + headers={'x-object-meta-serialized': True}) except SwiftConnectionFailed as err: LOG.error(_LE("create object failed, err: %s."), err) raise exception.BankCreateObjectFailed(reasone=err, @@ -144,9 +152,12 @@ class SwiftBankPlugin(BankPlugin, LeasePlugin): def update_object(self, key, value): try: + if not isinstance(value, str): + value = json.dumps(value) self._put_object(container=self.bank_object_container, obj=key, - contents=value) + contents=value, + headers={'x-object-meta-serialized': True}) except SwiftConnectionFailed as err: LOG.error(_LE("update object failed, err: %s."), err) raise exception.BankUpdateObjectFailed(reasone=err, @@ -182,7 +193,7 @@ class SwiftBankPlugin(BankPlugin, LeasePlugin): raise exception.BankListObjectsFailed(reasone=err) for obj in body: if obj.get("name"): - object_names.append(obj.get("name")) + object_names.append(obj.get("name").lstrip(prefix)) return object_names def acquire_lease(self): @@ -234,6 +245,8 @@ class SwiftBankPlugin(BankPlugin, LeasePlugin): try: (_resp, body) = self.connection.get_object(container=container, obj=obj) + if _resp.get("x-object-meta-serialized") == "True": + body = json.loads(body) return body except ClientException as err: raise SwiftConnectionFailed(reason=err) diff --git a/smaug/tests/unit/protection/fake_swift_client.py b/smaug/tests/unit/protection/fake_swift_client.py index 8cc332bb..8f3f1d97 100644 --- a/smaug/tests/unit/protection/fake_swift_client.py +++ b/smaug/tests/unit/protection/fake_swift_client.py @@ -68,7 +68,7 @@ class FakeSwiftConnection(object): if os.path.exists(container_dir) is True: if os.path.exists(obj_file) is True: with open(obj_file, "r") as f: - return None, f.read() + return {}, f.read() else: raise ClientException("error_obj") else: