Move swift tempurl creation functionality to utils
Change-Id: I25b6670f62e8702bba875fb0c4aa36b39a4c40b3
This commit is contained in:
parent
a8dc3e7d7a
commit
163addcaa8
|
@ -13,13 +13,10 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import uuid
|
||||
|
||||
from mistral_lib import actions
|
||||
from six.moves import urllib
|
||||
from swiftclient import exceptions as swiftexceptions
|
||||
from swiftclient.utils import generate_temp_url
|
||||
|
||||
from tripleo_common.actions import base
|
||||
from tripleo_common.utils import swift as swiftutils
|
||||
|
||||
|
||||
class SwiftInformationAction(base.TripleOAction):
|
||||
|
@ -58,18 +55,6 @@ class SwiftTempUrlAction(base.TripleOAction):
|
|||
|
||||
def run(self, context):
|
||||
swift_client = self.get_object_client(context)
|
||||
|
||||
try:
|
||||
cont_stat = swift_client.head_container(self.container)
|
||||
except swiftexceptions.ClientException:
|
||||
cont_stat = {}
|
||||
|
||||
key = cont_stat.get('x-container-meta-temp-url-key')
|
||||
if not key:
|
||||
key = str(uuid.uuid4())
|
||||
cont_stat = swift_client.put_container(
|
||||
self.container, {'X-Container-Meta-Temp-Url-Key': key})
|
||||
parsed = urllib.parse.urlparse(swift_client.url)
|
||||
path = "%s/%s/%s" % (parsed.path, self.container, self.obj)
|
||||
temp_path = generate_temp_url(path, self.valid, key, self.method)
|
||||
return "%s://%s%s" % (parsed.scheme, parsed.netloc, temp_path)
|
||||
return swiftutils.get_temp_url(
|
||||
swift_client, self.container, self.obj,
|
||||
self.method, self.valid)
|
||||
|
|
|
@ -55,42 +55,3 @@ class SwiftInformationActionTest(base.TestCase):
|
|||
return_obj = actions.Result(data=None, error='failure')
|
||||
|
||||
self.assertEqual(return_obj, self.action.run(mock_ctx))
|
||||
|
||||
|
||||
class SwiftTempUrlActionTest(base.TestCase):
|
||||
@mock.patch('time.time')
|
||||
@mock.patch('uuid.uuid4')
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client')
|
||||
def _test_get_tempurl(self, secret, mock_get_object_client,
|
||||
mock_uuid, mock_time):
|
||||
mock_ctx = mock.MagicMock()
|
||||
|
||||
url = "http://swift:8080/v1/AUTH_test"
|
||||
swiftclient = mock.MagicMock(url=url)
|
||||
headers = {}
|
||||
if secret:
|
||||
headers['x-container-meta-temp-url-key'] = secret
|
||||
swiftclient.head_container.return_value = headers
|
||||
mock_get_object_client.return_value = swiftclient
|
||||
|
||||
mock_uuid.return_value = '1-2-3-4'
|
||||
mock_time.return_value = 1500000000
|
||||
|
||||
action = swifthelper.SwiftTempUrlAction("container", "obj")
|
||||
tempurl = action.run(mock_ctx)
|
||||
|
||||
expected = "%s/container/obj?temp_url_sig=%s&temp_url_expires=%d" % (
|
||||
url, "ea8fdc57e2b2b1fbb7210bddd40029a7c8d5e2ed", 1500086400)
|
||||
self.assertEqual(expected, tempurl)
|
||||
|
||||
if not secret:
|
||||
swiftclient.put_container.assert_called_with(
|
||||
'container', {'X-Container-Meta-Temp-Url-Key': '1-2-3-4'})
|
||||
|
||||
def test_get_tempurl(self):
|
||||
# temp-url-key already set on the container
|
||||
self._test_get_tempurl('1-2-3-4')
|
||||
|
||||
def test_get_tempurl_no_key(self):
|
||||
# temp-url-key not yet set
|
||||
self._test_get_tempurl(None)
|
||||
|
|
|
@ -86,3 +86,35 @@ class SwiftTest(base.TestCase):
|
|||
self.swiftclient.put_object = put_mock
|
||||
swift_utils.put_object_string(self.swiftclient, 'foo', 'bar', b'foo')
|
||||
put_mock.assert_called_once_with('foo', 'bar', str('foo'))
|
||||
|
||||
@mock.patch('time.time')
|
||||
@mock.patch('uuid.uuid4')
|
||||
def _test_get_tempurl(self, secret, mock_uuid, mock_time):
|
||||
url = "http://swift:8080/v1/AUTH_test"
|
||||
swiftclient = mock.MagicMock(url=url)
|
||||
headers = {}
|
||||
if secret:
|
||||
headers['x-container-meta-temp-url-key'] = secret
|
||||
swiftclient.head_container.return_value = headers
|
||||
|
||||
mock_uuid.return_value = '1-2-3-4'
|
||||
mock_time.return_value = 1500000000
|
||||
|
||||
tempurl = swift_utils.get_temp_url(swiftclient,
|
||||
"container", "obj")
|
||||
|
||||
expected = "%s/container/obj?temp_url_sig=%s&temp_url_expires=%d" % (
|
||||
url, "ea8fdc57e2b2b1fbb7210bddd40029a7c8d5e2ed", 1500086400)
|
||||
self.assertEqual(expected, tempurl)
|
||||
|
||||
if not secret:
|
||||
swiftclient.put_container.assert_called_with(
|
||||
'container', {'X-Container-Meta-Temp-Url-Key': '1-2-3-4'})
|
||||
|
||||
def test_get_tempurl(self):
|
||||
# temp-url-key already set on the container
|
||||
self._test_get_tempurl('1-2-3-4')
|
||||
|
||||
def test_get_tempurl_no_key(self):
|
||||
# temp-url-key not yet set
|
||||
self._test_get_tempurl(None)
|
||||
|
|
|
@ -18,10 +18,14 @@ import dateutil.parser
|
|||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
import uuid
|
||||
|
||||
import six
|
||||
from six.moves import urllib
|
||||
from swiftclient import exceptions as swiftexceptions
|
||||
from swiftclient.service import SwiftError
|
||||
from swiftclient.service import SwiftUploadObject
|
||||
from swiftclient.utils import generate_temp_url
|
||||
|
||||
from tripleo_common.utils import tarball
|
||||
|
||||
|
@ -190,3 +194,20 @@ def put_object_string(swift, container, object_name, contents):
|
|||
except AttributeError:
|
||||
pass
|
||||
return swift.put_object(container, object_name, contents)
|
||||
|
||||
|
||||
def get_temp_url(swift, container, object_name, method='GET', valid='86400'):
|
||||
try:
|
||||
cont_stat = swift.head_container(container)
|
||||
except swiftexceptions.ClientException:
|
||||
cont_stat = {}
|
||||
|
||||
key = cont_stat.get('x-container-meta-temp-url-key')
|
||||
if not key:
|
||||
key = str(uuid.uuid4())
|
||||
cont_stat = swift.put_container(
|
||||
container, {'X-Container-Meta-Temp-Url-Key': key})
|
||||
parsed = urllib.parse.urlparse(swift.url)
|
||||
path = "%s/%s/%s" % (parsed.path, container, object_name)
|
||||
temp_path = generate_temp_url(path, valid, key, method)
|
||||
return "%s://%s%s" % (parsed.scheme, parsed.netloc, temp_path)
|
||||
|
|
Loading…
Reference in New Issue