Add option "--name" to command "openstack object create" · openstack/python-openstackclient@78312ca · GitHub
Skip to content

Commit 78312ca

Browse files
rajasi18stevemar
authored andcommitted
Add option "--name" to command "openstack object create"
Option "--name" can be used to set as the object name of the file to be uploaded in the container. Similar to option "--object-name" in command "swift upload". Added unit test case to ensure an exception is raised when using option "--name" for uploading multiple objects. Change-Id: Ied7827841f6ca1cf9d4b48e304cbe5d62eda38ab Closes-Bug: #1607972
1 parent f19240f commit 78312ca

6 files changed

Lines changed: 56 additions & 3 deletions

File tree

doc/source/command-objects/object.rst

Lines changed: 5 additions & 0 deletions

openstackclient/api/object_store_v1.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,16 @@ def object_create(
214214
self,
215215
container=None,
216216
object=None,
217+
name=None,
217218
):
218219
"""Create an object inside a container
219220
220221
:param string container:
221222
name of container to store object
222223
:param string object:
223224
local path to object
225+
:param string name:
226+
name of object to create
224227
:returns:
225228
dict of returned headers
226229
"""
@@ -229,8 +232,12 @@ def object_create(
229232
# TODO(dtroyer): What exception to raise here?
230233
return {}
231234

235+
# For uploading a file, if name is provided then set it as the
236+
# object's name in the container.
237+
object_name_str = name if name else object
238+
232239
full_url = "%s/%s" % (urllib.parse.quote(container),
233-
urllib.parse.quote(object))
240+
urllib.parse.quote(object_name_str))
234241
with io.open(object, 'rb') as f:
235242
response = self.create(
236243
full_url,
@@ -240,7 +247,7 @@ def object_create(
240247
data = {
241248
'account': self._find_account_id(),
242249
'container': container,
243-
'object': object,
250+
'object': object_name_str,
244251
'x-trans-id': response.headers.get('X-Trans-Id'),
245252
'etag': response.headers.get('Etag'),
246253
}

openstackclient/object/v1/object.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from osc_lib.cli import parseractions
2121
from osc_lib.command import command
22+
from osc_lib import exceptions
2223
from osc_lib import utils
2324
import six
2425

@@ -44,10 +45,20 @@ def get_parser(self, prog_name):
4445
nargs="+",
4546
help='Local filename(s) to upload',
4647
)
48+
parser.add_argument(
49+
'--name',
50+
metavar='<name>',
51+
help='Upload a file and rename it. '
52+
'Can only be used when uploading a single object'
53+
)
4754
return parser
4855

4956
def take_action(self, parsed_args):
50-
57+
if parsed_args.name:
58+
if len(parsed_args.objects) > 1:
59+
msg = _('Attempting to upload multiple objects and '
60+
'using --name is not permitted')
61+
raise exceptions.CommandError(msg)
5162
results = []
5263
for obj in parsed_args.objects:
5364
if len(obj) > 1024:
@@ -57,6 +68,7 @@ def take_action(self, parsed_args):
5768
data = self.app.client_manager.object_store.object_create(
5869
container=parsed_args.container,
5970
object=obj,
71+
name=parsed_args.name,
6072
)
6173
results.append(data)
6274

openstackclient/tests/unit/object/v1/fakes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
'last_modified': object_modified_2,
7676
}
7777

78+
object_upload_name = 'test-object-name'
79+
7880

7981
class TestObjectv1(utils.TestCommand):
8082

openstackclient/tests/unit/object/v1/test_object_all.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import copy
1515

16+
from osc_lib import exceptions
1617
from requests_mock.contrib import fixture
1718

1819
from openstackclient.object.v1 import object as object_cmds
@@ -35,6 +36,27 @@ def setUp(self):
3536
# Get the command object to test
3637
self.cmd = object_cmds.CreateObject(self.app, None)
3738

39+
def test_multiple_object_create_with_object_name(self):
40+
arglist = [
41+
object_fakes.container_name,
42+
object_fakes.object_name_1,
43+
object_fakes.object_name_2,
44+
'--name', object_fakes.object_upload_name,
45+
]
46+
47+
verifylist = [
48+
('container', object_fakes.container_name),
49+
('objects', [object_fakes.object_name_1,
50+
object_fakes.object_name_2]),
51+
('name', object_fakes.object_upload_name),
52+
]
53+
54+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
55+
56+
self.assertRaises(exceptions.CommandError,
57+
self.cmd.take_action,
58+
parsed_args)
59+
3860

3961
class TestObjectList(TestObjectAll):
4062

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)