Commit 94bedc5b authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Add output support to create methods

Refs: #3756 #3732

In kamaki.clients
-------------------
- storage.create_directory returns response headers
- implement pithos.create_container

In kamaki.cli.pithos
-------------------
- add flag --with_output for optional output
- add (-j, --json) flag for optional json-formated output
- apply these changes to file_mkdir, file_touch, file_create
parent 0399ac7e
......@@ -34,4 +34,7 @@ Features:
- Add enumeration to all listing commands, make it optional [#3739]
- Add a download_to_string method in pithos client [#3608]
- Add an upload_from_string method in pithos client [#3608]
- Add pithos client method create_container [#3756]
- Add output for file methods [#3756, #3732]:
mkdir, touch, create
......@@ -432,6 +432,11 @@ class file_list(_file_container_command):
class file_mkdir(_file_container_command):
"""Create a directory"""
arguments = dict(
with_output=FlagArgument('show response headers', ('--with-output')),
json_output=FlagArgument('show headers in json', ('-j', '--json'))
)
__doc__ += '\n. '.join([
'Kamaki hanldes directories the same way as OOS Storage and Pithos+:',
'A directory is an object with type "application/directory"',
......@@ -443,7 +448,11 @@ class file_mkdir(_file_container_command):
@errors.pithos.connection
@errors.pithos.container
def _run(self):
self.client.create_directory(self.path)
r = self.client.create_directory(self.path)
if self['json_output']:
print_json(r)
elif self['with_output']:
print_dict(r)
def main(self, container___directory):
super(self.__class__, self)._run(
......@@ -462,14 +471,20 @@ class file_touch(_file_container_command):
content_type=ValueArgument(
'Set content type (default: application/octet-stream)',
'--content-type',
default='application/octet-stream')
default='application/octet-stream'),
with_output=FlagArgument('show response headers', ('--with-output')),
json_output=FlagArgument('show headers in json', ('-j', '--json'))
)
@errors.generic.all
@errors.pithos.connection
@errors.pithos.container
def _run(self):
self.client.create_object(self.path, self['content_type'])
r = self.client.create_object(self.path, self['content_type'])
if self['json_output']:
print_json(r)
elif self['with_output']:
print_dict(r)
def main(self, container___path):
super(file_touch, self)._run(
......@@ -489,17 +504,24 @@ class file_create(_file_container_command):
limit=IntArgument('set default container limit', '--limit'),
meta=KeyValueArgument(
'set container metadata (can be repeated)',
'--meta')
'--meta'),
with_output=FlagArgument('show request headers', ('--with-output')),
json_output=FlagArgument('show headers in json', ('-j', '--json'))
)
@errors.generic.all
@errors.pithos.connection
@errors.pithos.container
def _run(self):
self.client.container_put(
limit=self['limit'],
def _run(self, container):
r = self.client.create_container(
container=container,
sizelimit=self['limit'],
versioning=self['versioning'],
metadata=self['meta'])
if self['json_output']:
print_json(r)
elif self['with_output']:
print_dict(r)
def main(self, container=None):
super(self.__class__, self)._run(container)
......@@ -507,7 +529,7 @@ class file_create(_file_container_command):
raiseCLIError('Invalid container name %s' % container, details=[
'Did you mean "%s" ?' % self.container,
'Use --container for names containing :'])
self._run()
self._run(container)
class _source_destination_command(_file_container_command):
......
......@@ -71,6 +71,30 @@ class PithosClient(PithosRestClient):
def __init__(self, base_url, token, account=None, container=None):
super(PithosClient, self).__init__(base_url, token, account, container)
def create_container(
self,
container=None, sizelimit=None, versioning=None, metadata=None):
"""
:param container: (str) if not given, self.container is used instead
:param sizelimit: (int) container total size limit in bytes
:param versioning: (str) can be auto or whatever supported by server
:param metadata: (dict) Custom user-defined metadata of the form
{ 'name1': 'value1', 'name2': 'value2', ... }
:returns: (dict) response headers
"""
cnt_back_up = self.container
try:
self.container = container or cnt_back_up
r = self.container_put(
quota=sizelimit, versioning=versioning, metadata=metadata)
return r.headers
finally:
self.container = cnt_back_up
def purge_container(self, container=None):
"""Delete an empty container and destroy associated blocks
"""
......
......@@ -161,24 +161,32 @@ class StorageClient(Client):
content_type='application/octet-stream', content_length=0):
"""
:param obj: (str) directory-object name
:param content_type: (str) explicitly set content_type
:param content_length: (int) explicitly set content length
:returns: (dict) object creation headers
"""
self._assert_container()
path = path4url(self.account, self.container, obj)
self.set_header('Content-Type', content_type)
self.set_header('Content-length', str(content_length))
self.put(path, success=201)
r = self.put(path, success=201)
return r.headers
def create_directory(self, obj):
"""
:param obj: (str) directory-object name
:returns: (dict) request headers
"""
self._assert_container()
path = path4url(self.account, self.container, obj)
self.set_header('Content-Type', 'application/directory')
self.set_header('Content-length', '0')
self.put(path, success=201)
r = self.put(path, success=201)
return r.headers
def get_object_info(self, obj):
"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment