diff --git a/openstackclient/common/restapi.py b/openstackclient/common/restapi.py index a4822a1028..a646acb364 100644 --- a/openstackclient/common/restapi.py +++ b/openstackclient/common/restapi.py @@ -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 diff --git a/openstackclient/object/v1/container.py b/openstackclient/object/v1/container.py index 1e252aaf4f..ae4013fc0c 100644 --- a/openstackclient/object/v1/container.py +++ b/openstackclient/object/v1/container.py @@ -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='', + 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='', + 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""" diff --git a/openstackclient/object/v1/lib/container.py b/openstackclient/object/v1/lib/container.py index 72e97d4e1d..bd50955514 100644 --- a/openstackclient/object/v1/lib/container.py +++ b/openstackclient/object/v1/lib/container.py @@ -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, diff --git a/setup.cfg b/setup.cfg index 00c6904fc0..7f07286c0d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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