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
This commit is contained in:
parent
f19240fc29
commit
78312ca9af
@ -13,9 +13,14 @@ Upload object to container
|
|||||||
.. code:: bash
|
.. code:: bash
|
||||||
|
|
||||||
os object create
|
os object create
|
||||||
|
[--name <name>]
|
||||||
<container>
|
<container>
|
||||||
<filename> [<filename> ...]
|
<filename> [<filename> ...]
|
||||||
|
|
||||||
|
.. option:: --name <name>
|
||||||
|
|
||||||
|
Upload a file and rename it. Can only be used when uploading a single object
|
||||||
|
|
||||||
.. describe:: <container>
|
.. describe:: <container>
|
||||||
|
|
||||||
Container for new object
|
Container for new object
|
||||||
|
@ -214,6 +214,7 @@ class APIv1(api.BaseAPI):
|
|||||||
self,
|
self,
|
||||||
container=None,
|
container=None,
|
||||||
object=None,
|
object=None,
|
||||||
|
name=None,
|
||||||
):
|
):
|
||||||
"""Create an object inside a container
|
"""Create an object inside a container
|
||||||
|
|
||||||
@ -221,6 +222,8 @@ class APIv1(api.BaseAPI):
|
|||||||
name of container to store object
|
name of container to store object
|
||||||
:param string object:
|
:param string object:
|
||||||
local path to object
|
local path to object
|
||||||
|
:param string name:
|
||||||
|
name of object to create
|
||||||
:returns:
|
:returns:
|
||||||
dict of returned headers
|
dict of returned headers
|
||||||
"""
|
"""
|
||||||
@ -229,8 +232,12 @@ class APIv1(api.BaseAPI):
|
|||||||
# TODO(dtroyer): What exception to raise here?
|
# TODO(dtroyer): What exception to raise here?
|
||||||
return {}
|
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),
|
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:
|
with io.open(object, 'rb') as f:
|
||||||
response = self.create(
|
response = self.create(
|
||||||
full_url,
|
full_url,
|
||||||
@ -240,7 +247,7 @@ class APIv1(api.BaseAPI):
|
|||||||
data = {
|
data = {
|
||||||
'account': self._find_account_id(),
|
'account': self._find_account_id(),
|
||||||
'container': container,
|
'container': container,
|
||||||
'object': object,
|
'object': object_name_str,
|
||||||
'x-trans-id': response.headers.get('X-Trans-Id'),
|
'x-trans-id': response.headers.get('X-Trans-Id'),
|
||||||
'etag': response.headers.get('Etag'),
|
'etag': response.headers.get('Etag'),
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import logging
|
|||||||
|
|
||||||
from osc_lib.cli import parseractions
|
from osc_lib.cli import parseractions
|
||||||
from osc_lib.command import command
|
from osc_lib.command import command
|
||||||
|
from osc_lib import exceptions
|
||||||
from osc_lib import utils
|
from osc_lib import utils
|
||||||
import six
|
import six
|
||||||
|
|
||||||
@ -44,10 +45,20 @@ class CreateObject(command.Lister):
|
|||||||
nargs="+",
|
nargs="+",
|
||||||
help='Local filename(s) to upload',
|
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
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
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 = []
|
results = []
|
||||||
for obj in parsed_args.objects:
|
for obj in parsed_args.objects:
|
||||||
if len(obj) > 1024:
|
if len(obj) > 1024:
|
||||||
@ -57,6 +68,7 @@ class CreateObject(command.Lister):
|
|||||||
data = self.app.client_manager.object_store.object_create(
|
data = self.app.client_manager.object_store.object_create(
|
||||||
container=parsed_args.container,
|
container=parsed_args.container,
|
||||||
object=obj,
|
object=obj,
|
||||||
|
name=parsed_args.name,
|
||||||
)
|
)
|
||||||
results.append(data)
|
results.append(data)
|
||||||
|
|
||||||
|
@ -75,6 +75,8 @@ OBJECT_2 = {
|
|||||||
'last_modified': object_modified_2,
|
'last_modified': object_modified_2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object_upload_name = 'test-object-name'
|
||||||
|
|
||||||
|
|
||||||
class TestObjectv1(utils.TestCommand):
|
class TestObjectv1(utils.TestCommand):
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
|
from osc_lib import exceptions
|
||||||
from requests_mock.contrib import fixture
|
from requests_mock.contrib import fixture
|
||||||
|
|
||||||
from openstackclient.object.v1 import object as object_cmds
|
from openstackclient.object.v1 import object as object_cmds
|
||||||
@ -35,6 +36,27 @@ class TestObjectCreate(TestObjectAll):
|
|||||||
# Get the command object to test
|
# Get the command object to test
|
||||||
self.cmd = object_cmds.CreateObject(self.app, None)
|
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):
|
class TestObjectList(TestObjectAll):
|
||||||
|
|
||||||
|
5
releasenotes/notes/bug-1607972-a910a9fbdb81da57.yaml
Normal file
5
releasenotes/notes/bug-1607972-a910a9fbdb81da57.yaml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- Add ``--name`` option to command ``object create``
|
||||||
|
for uploading a file and renaming it.
|
||||||
|
[Bug `1607972 <https://bugs.launchpad.net/bugs/1607972>`_]
|
Loading…
Reference in New Issue
Block a user