Add container create and delete support

Add basic container create and delete support to OSC.

Change-Id: Ia104db9d7e580d33097ea33a5690998f817995d1
implements: bp swift-client
This commit is contained in:
Steve Martinelli 2014-08-03 02:19:29 -04:00
parent 75e8490e54
commit be83ae763f
4 changed files with 107 additions and 3 deletions

View File

@ -189,7 +189,11 @@ class RESTApi(object):
:param \*\*kwargs: Optional arguments passed to ``request``
"""
return self.request('PATCH', url, data=data, json=json, **kwargs)
if json:
kwargs['json'] = json
if data:
kwargs['data'] = data
return self.request('PATCH', url, **kwargs)
def post(self, url, data=None, json=None, **kwargs):
"""Send a POST request. Returns :class:`requests.Response` object.
@ -201,7 +205,11 @@ class RESTApi(object):
:param \*\*kwargs: Optional arguments passed to ``request``
"""
return self.request('POST', url, data=data, json=json, **kwargs)
if json:
kwargs['json'] = json
if data:
kwargs['data'] = data
return self.request('POST', url, **kwargs)
def put(self, url, data=None, json=None, **kwargs):
"""Send a PUT request. Returns :class:`requests.Response` object.
@ -213,7 +221,11 @@ class RESTApi(object):
:param \*\*kwargs: Optional arguments passed to ``request``
"""
return self.request('PUT', url, data=data, json=json, **kwargs)
if json:
kwargs['json'] = json
if data:
kwargs['data'] = data
return self.request('PUT', url, **kwargs)
# Command verb methods

View File

@ -19,6 +19,7 @@
import logging
import six
from cliff import command
from cliff import lister
from cliff import show
@ -26,6 +27,56 @@ from openstackclient.common import utils
from openstackclient.object.v1.lib import container as lib_container
class CreateContainer(show.ShowOne):
"""Create a container"""
log = logging.getLogger(__name__ + '.CreateContainer')
def get_parser(self, prog_name):
parser = super(CreateContainer, self).get_parser(prog_name)
parser.add_argument(
'container',
metavar='<container>',
help='New container name',
)
return parser
def take_action(self, parsed_args):
self.log.debug('take_action(%s)', parsed_args)
data = lib_container.create_container(
self.app.restapi,
self.app.client_manager.object_store.endpoint,
parsed_args.container,
)
return zip(*sorted(six.iteritems(data)))
class DeleteContainer(command.Command):
"""Delete a container"""
log = logging.getLogger(__name__ + '.DeleteContainer')
def get_parser(self, prog_name):
parser = super(DeleteContainer, self).get_parser(prog_name)
parser.add_argument(
'container',
metavar='<container>',
help='Container name to delete',
)
return parser
def take_action(self, parsed_args):
self.log.debug('take_action(%s)', parsed_args)
lib_container.delete_container(
self.app.restapi,
self.app.client_manager.object_store.endpoint,
parsed_args.container,
)
class ListContainer(lister.Lister):
"""List containers"""

View File

@ -22,6 +22,45 @@ except ImportError:
from urlparse import urlparse # noqa
def create_container(
api,
url,
container,
):
"""Create a container
:param api: a restapi object
:param url: endpoint
:param container: name of container to create
:returns: dict of returned headers
"""
response = api.put("%s/%s" % (url, container))
url_parts = urlparse(url)
data = {
'account': url_parts.path.split('/')[-1],
'container': container,
}
data['x-trans-id'] = response.headers.get('x-trans-id', None)
return data
def delete_container(
api,
url,
container,
):
"""Delete a container
:param api: a restapi object
:param url: endpoint
:param container: name of container to delete
"""
api.delete("%s/%s" % (url, container))
def list_containers(
api,
url,

View File

@ -268,6 +268,8 @@ openstack.network.v2 =
network_show = openstackclient.network.v2.network:ShowNetwork
openstack.object_store.v1 =
container_create = openstackclient.object.v1.container:CreateContainer
container_delete = openstackclient.object.v1.container:DeleteContainer
container_list = openstackclient.object.v1.container:ListContainer
container_show = openstackclient.object.v1.container:ShowContainer
object_list = openstackclient.object.v1.object:ListObject