diff --git a/doc/source/command-objects/container.rst b/doc/source/command-objects/container.rst index 3372f4d9bd..3e52d56b05 100644 --- a/doc/source/command-objects/container.rst +++ b/doc/source/command-objects/container.rst @@ -89,6 +89,26 @@ Save container contents locally Container to save +container set +------------- + +Set container properties + +.. program:: container set +.. code:: bash + + os container set + [] + [--property [...] ] + +.. describe:: + + Container to modify + +.. option:: --property + + Set a property on this container (repeat option to set multiple properties) + container show -------------- @@ -103,3 +123,23 @@ Display container details .. describe:: Container to display + +container unset +--------------- + +Unset container properties + +.. program:: container unset +.. code:: bash + + os container unset + [] + [--property ] + +.. describe:: + + Container to modify + +.. option:: --property + + Property to remove from container (repeat option to remove multiple properties) diff --git a/openstackclient/api/object_store_v1.py b/openstackclient/api/object_store_v1.py index fab470e5ab..b47f556b09 100644 --- a/openstackclient/api/object_store_v1.py +++ b/openstackclient/api/object_store_v1.py @@ -139,6 +139,23 @@ class APIv1(api.BaseAPI): for object in objects: self.object_save(container=container, object=object['name']) + def container_set( + self, + container, + properties, + ): + """Set container properties + + :param string container: + name of container to modify + :param dict properties: + properties to add or update for the container + """ + + headers = self._set_properties(properties, 'X-Container-Meta-%s') + if headers: + self.create(container, headers=headers) + def container_show( self, container=None, @@ -168,6 +185,24 @@ class APIv1(api.BaseAPI): } return data + def container_unset( + self, + container, + properties, + ): + """Unset container properties + + :param string container: + name of container to modify + :param dict properties: + properties to remove from the container + """ + + headers = self._unset_properties(properties, + 'X-Remove-Container-Meta-%s') + if headers: + self.create(container, headers=headers) + def object_create( self, container=None, @@ -397,14 +432,7 @@ class APIv1(api.BaseAPI): properties to add or update for the account """ - # NOTE(stevemar): As per the API, the headers have to be in the form - # of "X-Account-Meta-Book: MobyDick" - - headers = {} - for k, v in properties.iteritems(): - header_name = 'X-Account-Meta-%s' % k - headers[header_name] = v - + headers = self._set_properties(properties, 'X-Account-Meta-%s') if headers: # NOTE(stevemar): The URL (first argument) in this case is already # set to the swift account endpoint, because that's how it's @@ -438,19 +466,37 @@ class APIv1(api.BaseAPI): properties to remove from the account """ - # NOTE(stevemar): As per the API, the headers have to be in the form - # of "X-Remove-Account-Meta-Book: x". In the case where metadata is - # removed, we can set the value of the header to anything, so it's - # set to 'x' - - headers = {} - for k in properties: - header_name = 'X-Remove-Account-Meta-%s' % k - headers[header_name] = "x" - + headers = self._unset_properties(properties, + 'X-Remove-Account-Meta-%s') if headers: self.create("", headers=headers) def _find_account_id(self): url_parts = urlparse(self.endpoint) return url_parts.path.split('/')[-1] + + def _unset_properties(self, properties, header_tag): + # NOTE(stevemar): As per the API, the headers have to be in the form + # of "X-Remove-Account-Meta-Book: x". In the case where metadata is + # removed, we can set the value of the header to anything, so it's + # set to 'x'. In the case of a Container property we use: + # "X-Remove-Container-Meta-Book: x", and the same logic applies for + # Object properties + + headers = {} + for k in properties: + header_name = header_tag % k + headers[header_name] = 'x' + return headers + + def _set_properties(self, properties, header_tag): + # NOTE(stevemar): As per the API, the headers have to be in the form + # of "X-Account-Meta-Book: MobyDick". In the case of a Container + # property we use: "X-Add-Container-Meta-Book: MobyDick", and the same + # logic applies for Object properties + + headers = {} + for k, v in properties.iteritems(): + header_name = header_tag % k + headers[header_name] = v + return headers diff --git a/openstackclient/object/v1/container.py b/openstackclient/object/v1/container.py index 49173debbc..b8eb4c254e 100644 --- a/openstackclient/object/v1/container.py +++ b/openstackclient/object/v1/container.py @@ -23,6 +23,7 @@ from cliff import command from cliff import lister from cliff import show +from openstackclient.common import parseractions from openstackclient.common import utils @@ -178,6 +179,36 @@ class SaveContainer(command.Command): ) +class SetContainer(command.Command): + """Set container properties""" + + log = logging.getLogger(__name__ + '.SetContainer') + + def get_parser(self, prog_name): + parser = super(SetContainer, self).get_parser(prog_name) + parser.add_argument( + 'container', + metavar='', + help='Container to modify', + ) + parser.add_argument( + "--property", + metavar="", + required=True, + action=parseractions.KeyValueAction, + help="Set a property on this container " + "(repeat option to set multiple properties)" + ) + return parser + + @utils.log_method(log) + def take_action(self, parsed_args): + self.app.client_manager.object_store.container_set( + parsed_args.container, + properties=parsed_args.property, + ) + + class ShowContainer(show.ShowOne): """Display container details""" @@ -200,3 +231,34 @@ class ShowContainer(show.ShowOne): ) return zip(*sorted(six.iteritems(data))) + + +class UnsetContainer(command.Command): + """Unset container properties""" + + log = logging.getLogger(__name__ + '.UnsetContainer') + + def get_parser(self, prog_name): + parser = super(UnsetContainer, self).get_parser(prog_name) + parser.add_argument( + 'container', + metavar='', + help='Container to modify', + ) + parser.add_argument( + '--property', + metavar='', + required=True, + action='append', + default=[], + help='Property to remove from container ' + '(repeat option to remove multiple properties)', + ) + return parser + + @utils.log_method(log) + def take_action(self, parsed_args): + self.app.client_manager.object_store.container_unset( + parsed_args.container, + properties=parsed_args.property, + ) diff --git a/setup.cfg b/setup.cfg index 2498a3d355..248c9c6604 100644 --- a/setup.cfg +++ b/setup.cfg @@ -339,7 +339,9 @@ openstack.object_store.v1 = container_delete = openstackclient.object.v1.container:DeleteContainer container_list = openstackclient.object.v1.container:ListContainer container_save = openstackclient.object.v1.container:SaveContainer + container_set = openstackclient.object.v1.container:SetContainer container_show = openstackclient.object.v1.container:ShowContainer + container_unset = openstackclient.object.v1.container:UnsetContainer object_create = openstackclient.object.v1.object:CreateObject object_delete = openstackclient.object.v1.object:DeleteObject object_list = openstackclient.object.v1.object:ListObject