summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/api/object_store_v1.py11
-rw-r--r--openstackclient/object/v1/object.py14
-rw-r--r--openstackclient/tests/unit/object/v1/fakes.py2
-rw-r--r--openstackclient/tests/unit/object/v1/test_object_all.py22
4 files changed, 46 insertions, 3 deletions
diff --git a/openstackclient/api/object_store_v1.py b/openstackclient/api/object_store_v1.py
index faa55118..184814c6 100644
--- a/openstackclient/api/object_store_v1.py
+++ b/openstackclient/api/object_store_v1.py
@@ -214,6 +214,7 @@ class APIv1(api.BaseAPI):
self,
container=None,
object=None,
+ name=None,
):
"""Create an object inside a container
@@ -221,6 +222,8 @@ class APIv1(api.BaseAPI):
name of container to store object
:param string object:
local path to object
+ :param string name:
+ name of object to create
:returns:
dict of returned headers
"""
@@ -229,8 +232,12 @@ class APIv1(api.BaseAPI):
# TODO(dtroyer): What exception to raise here?
return {}
+ # For uploading a file, if name is provided then set it as the
+ # object's name in the container.
+ object_name_str = name if name else object
+
full_url = "%s/%s" % (urllib.parse.quote(container),
- urllib.parse.quote(object))
+ urllib.parse.quote(object_name_str))
with io.open(object, 'rb') as f:
response = self.create(
full_url,
@@ -240,7 +247,7 @@ class APIv1(api.BaseAPI):
data = {
'account': self._find_account_id(),
'container': container,
- 'object': object,
+ 'object': object_name_str,
'x-trans-id': response.headers.get('X-Trans-Id'),
'etag': response.headers.get('Etag'),
}
diff --git a/openstackclient/object/v1/object.py b/openstackclient/object/v1/object.py
index db61d638..88f6815e 100644
--- a/openstackclient/object/v1/object.py
+++ b/openstackclient/object/v1/object.py
@@ -19,6 +19,7 @@ import logging
from osc_lib.cli import parseractions
from osc_lib.command import command
+from osc_lib import exceptions
from osc_lib import utils
import six
@@ -44,10 +45,20 @@ class CreateObject(command.Lister):
nargs="+",
help='Local filename(s) to upload',
)
+ parser.add_argument(
+ '--name',
+ metavar='<name>',
+ help='Upload a file and rename it. '
+ 'Can only be used when uploading a single object'
+ )
return parser
def take_action(self, parsed_args):
-
+ if parsed_args.name:
+ if len(parsed_args.objects) > 1:
+ msg = _('Attempting to upload multiple objects and '
+ 'using --name is not permitted')
+ raise exceptions.CommandError(msg)
results = []
for obj in parsed_args.objects:
if len(obj) > 1024:
@@ -57,6 +68,7 @@ class CreateObject(command.Lister):
data = self.app.client_manager.object_store.object_create(
container=parsed_args.container,
object=obj,
+ name=parsed_args.name,
)
results.append(data)
diff --git a/openstackclient/tests/unit/object/v1/fakes.py b/openstackclient/tests/unit/object/v1/fakes.py
index 0ff594bc..72646d25 100644
--- a/openstackclient/tests/unit/object/v1/fakes.py
+++ b/openstackclient/tests/unit/object/v1/fakes.py
@@ -75,6 +75,8 @@ OBJECT_2 = {
'last_modified': object_modified_2,
}
+object_upload_name = 'test-object-name'
+
class TestObjectv1(utils.TestCommand):
diff --git a/openstackclient/tests/unit/object/v1/test_object_all.py b/openstackclient/tests/unit/object/v1/test_object_all.py
index a0948b1b..f215836e 100644
--- a/openstackclient/tests/unit/object/v1/test_object_all.py
+++ b/openstackclient/tests/unit/object/v1/test_object_all.py
@@ -13,6 +13,7 @@
import copy
+from osc_lib import exceptions
from requests_mock.contrib import fixture
from openstackclient.object.v1 import object as object_cmds
@@ -35,6 +36,27 @@ class TestObjectCreate(TestObjectAll):
# Get the command object to test
self.cmd = object_cmds.CreateObject(self.app, None)
+ def test_multiple_object_create_with_object_name(self):
+ arglist = [
+ object_fakes.container_name,
+ object_fakes.object_name_1,
+ object_fakes.object_name_2,
+ '--name', object_fakes.object_upload_name,
+ ]
+
+ verifylist = [
+ ('container', object_fakes.container_name),
+ ('objects', [object_fakes.object_name_1,
+ object_fakes.object_name_2]),
+ ('name', object_fakes.object_upload_name),
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.assertRaises(exceptions.CommandError,
+ self.cmd.take_action,
+ parsed_args)
+
class TestObjectList(TestObjectAll):