From 0774e28ada4fa903fa7cc29457ba8a2f30b23a13 Mon Sep 17 00:00:00 2001 From: Jason Dunsmore Date: Wed, 8 Oct 2014 13:32:24 -0500 Subject: [PATCH] Add custom get_temp_url to RackspaceSwiftClient Rackspace Cloud Files uses a non-OpenStack account identifier in the URL, so the Rackspace Swift client needs its own get_temp_url method. Change-Id: Ic90c26fca0200f1fba74d50b6aa8a75f8bcd7ef6 Closes-bug: #1378979 --- contrib/rackspace/rackspace/clients.py | 37 +++++++++++++++++++++----- contrib/rackspace/setup.cfg | 1 + 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/contrib/rackspace/rackspace/clients.py b/contrib/rackspace/rackspace/clients.py index 9d176a79a6..e04158fef7 100644 --- a/contrib/rackspace/rackspace/clients.py +++ b/contrib/rackspace/rackspace/clients.py @@ -13,6 +13,9 @@ """Client Libraries for Rackspace Resources.""" +import hashlib +import random +import time import urlparse from oslo.config import cfg @@ -23,11 +26,13 @@ from heat.engine.clients import client_plugin from heat.engine.clients.os import cinder from heat.engine.clients.os import glance from heat.engine.clients.os import nova +from heat.engine.clients.os import swift from heat.engine.clients.os import trove from heat.openstack.common import log as logging from glanceclient import client as gc +from swiftclient import utils as swiftclient_utils from troveclient import client as tc LOG = logging.getLogger(__name__) @@ -170,14 +175,32 @@ class RackspaceCinderClient(cinder.CinderClientPlugin): return client -class RackspaceSwiftClient(RackspaceClientPlugin): +class RackspaceSwiftClient(swift.SwiftClientPlugin): - def _create(self): - # Rackspace doesn't include object-store in the default catalog - # for "reasons". The pyrax client takes care of this, but it - # returns a wrapper over the upstream python-swiftclient so we - # unwrap here and things just work - return self._get_client("object_store").connection + def get_temp_url(self, container_name, obj_name, timeout=None): + ''' + Return a Swift TempURL. + ''' + def tenant_uuid(): + for role in self.context.auth_token_info['user']['roles']: + if role['name'] == 'object-store:default': + return role['tenantId'] + + key_header = 'x-account-meta-temp-url-key' + if key_header in self.client().head_account(): + key = self.client().head_account()[key_header] + else: + key = hashlib.sha224(str(random.getrandbits(256))).hexdigest()[:32] + self.client().post_account({key_header: key}) + + method = 'PUT' + path = '/v1/%s/%s/%s' % (tenant_uuid(), container_name, obj_name) + if timeout is None: + timeout = swift.MAX_EPOCH - 60 - time.time() + tempurl = swiftclient_utils.generate_temp_url(path, timeout, key, + method) + sw_url = urlparse.urlparse(self.client().url) + return '%s://%s%s' % (sw_url.scheme, sw_url.netloc, tempurl) class RackspaceGlanceClient(glance.GlanceClientPlugin): diff --git a/contrib/rackspace/setup.cfg b/contrib/rackspace/setup.cfg index 5922b32d51..957fe2cee0 100644 --- a/contrib/rackspace/setup.cfg +++ b/contrib/rackspace/setup.cfg @@ -36,6 +36,7 @@ heat.clients = glance = rackspace.clients:RackspaceGlanceClient nova = rackspace.clients:RackspaceNovaClient trove = rackspace.clients:RackspaceTroveClient + swift = rackspace.clients:RackspaceSwiftClient [global] setup-hooks =