Skip to content

Commit abaf711

Browse files
author
Steve Martinelli
committed
add support for set/unset of container properties
include docs and commands to set and unset container properties Partial-Bug: #1501945 Change-Id: I8d7e8cf356a2321a37ed940c4e10cae411b94dfd
1 parent bf11960 commit abaf711

4 files changed

Lines changed: 166 additions & 16 deletions

File tree

doc/source/command-objects/container.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ Save container contents locally
8989

9090
Container to save
9191

92+
container set
93+
-------------
94+
95+
Set container properties
96+
97+
.. program:: container set
98+
.. code:: bash
99+
100+
os container set
101+
[<container>]
102+
[--property <key=value> [...] ]
103+
104+
.. describe:: <container>
105+
106+
Container to modify
107+
108+
.. option:: --property <key=value>
109+
110+
Set a property on this container (repeat option to set multiple properties)
111+
92112
container show
93113
--------------
94114

@@ -103,3 +123,23 @@ Display container details
103123
.. describe:: <container>
104124

105125
Container to display
126+
127+
container unset
128+
---------------
129+
130+
Unset container properties
131+
132+
.. program:: container unset
133+
.. code:: bash
134+
135+
os container unset
136+
[<container>]
137+
[--property <key>]
138+
139+
.. describe:: <container>
140+
141+
Container to modify
142+
143+
.. option:: --property <key>
144+
145+
Property to remove from container (repeat option to remove multiple properties)

openstackclient/api/object_store_v1.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ def container_save(
139139
for object in objects:
140140
self.object_save(container=container, object=object['name'])
141141

142+
def container_set(
143+
self,
144+
container,
145+
properties,
146+
):
147+
"""Set container properties
148+
149+
:param string container:
150+
name of container to modify
151+
:param dict properties:
152+
properties to add or update for the container
153+
"""
154+
155+
headers = self._set_properties(properties, 'X-Container-Meta-%s')
156+
if headers:
157+
self.create(container, headers=headers)
158+
142159
def container_show(
143160
self,
144161
container=None,
@@ -168,6 +185,24 @@ def container_show(
168185
}
169186
return data
170187

188+
def container_unset(
189+
self,
190+
container,
191+
properties,
192+
):
193+
"""Unset container properties
194+
195+
:param string container:
196+
name of container to modify
197+
:param dict properties:
198+
properties to remove from the container
199+
"""
200+
201+
headers = self._unset_properties(properties,
202+
'X-Remove-Container-Meta-%s')
203+
if headers:
204+
self.create(container, headers=headers)
205+
171206
def object_create(
172207
self,
173208
container=None,
@@ -397,14 +432,7 @@ def account_set(
397432
properties to add or update for the account
398433
"""
399434

400-
# NOTE(stevemar): As per the API, the headers have to be in the form
401-
# of "X-Account-Meta-Book: MobyDick"
402-
403-
headers = {}
404-
for k, v in properties.iteritems():
405-
header_name = 'X-Account-Meta-%s' % k
406-
headers[header_name] = v
407-
435+
headers = self._set_properties(properties, 'X-Account-Meta-%s')
408436
if headers:
409437
# NOTE(stevemar): The URL (first argument) in this case is already
410438
# set to the swift account endpoint, because that's how it's
@@ -438,19 +466,37 @@ def account_unset(
438466
properties to remove from the account
439467
"""
440468

469+
headers = self._unset_properties(properties,
470+
'X-Remove-Account-Meta-%s')
471+
if headers:
472+
self.create("", headers=headers)
473+
474+
def _find_account_id(self):
475+
url_parts = urlparse(self.endpoint)
476+
return url_parts.path.split('/')[-1]
477+
478+
def _unset_properties(self, properties, header_tag):
441479
# NOTE(stevemar): As per the API, the headers have to be in the form
442480
# of "X-Remove-Account-Meta-Book: x". In the case where metadata is
443481
# removed, we can set the value of the header to anything, so it's
444-
# set to 'x'
482+
# set to 'x'. In the case of a Container property we use:
483+
# "X-Remove-Container-Meta-Book: x", and the same logic applies for
484+
# Object properties
445485

446486
headers = {}
447487
for k in properties:
448-
header_name = 'X-Remove-Account-Meta-%s' % k
449-
headers[header_name] = "x"
488+
header_name = header_tag % k
489+
headers[header_name] = 'x'
490+
return headers
450491

451-
if headers:
452-
self.create("", headers=headers)
492+
def _set_properties(self, properties, header_tag):
493+
# NOTE(stevemar): As per the API, the headers have to be in the form
494+
# of "X-Account-Meta-Book: MobyDick". In the case of a Container
495+
# property we use: "X-Add-Container-Meta-Book: MobyDick", and the same
496+
# logic applies for Object properties
453497

454-
def _find_account_id(self):
455-
url_parts = urlparse(self.endpoint)
456-
return url_parts.path.split('/')[-1]
498+
headers = {}
499+
for k, v in properties.iteritems():
500+
header_name = header_tag % k
501+
headers[header_name] = v
502+
return headers

openstackclient/object/v1/container.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from cliff import lister
2424
from cliff import show
2525

26+
from openstackclient.common import parseractions
2627
from openstackclient.common import utils
2728

2829

@@ -178,6 +179,36 @@ def take_action(self, parsed_args):
178179
)
179180

180181

182+
class SetContainer(command.Command):
183+
"""Set container properties"""
184+
185+
log = logging.getLogger(__name__ + '.SetContainer')
186+
187+
def get_parser(self, prog_name):
188+
parser = super(SetContainer, self).get_parser(prog_name)
189+
parser.add_argument(
190+
'container',
191+
metavar='<container>',
192+
help='Container to modify',
193+
)
194+
parser.add_argument(
195+
"--property",
196+
metavar="<key=value>",
197+
required=True,
198+
action=parseractions.KeyValueAction,
199+
help="Set a property on this container "
200+
"(repeat option to set multiple properties)"
201+
)
202+
return parser
203+
204+
@utils.log_method(log)
205+
def take_action(self, parsed_args):
206+
self.app.client_manager.object_store.container_set(
207+
parsed_args.container,
208+
properties=parsed_args.property,
209+
)
210+
211+
181212
class ShowContainer(show.ShowOne):
182213
"""Display container details"""
183214

@@ -200,3 +231,34 @@ def take_action(self, parsed_args):
200231
)
201232

202233
return zip(*sorted(six.iteritems(data)))
234+
235+
236+
class UnsetContainer(command.Command):
237+
"""Unset container properties"""
238+
239+
log = logging.getLogger(__name__ + '.UnsetContainer')
240+
241+
def get_parser(self, prog_name):
242+
parser = super(UnsetContainer, self).get_parser(prog_name)
243+
parser.add_argument(
244+
'container',
245+
metavar='<container>',
246+
help='Container to modify',
247+
)
248+
parser.add_argument(
249+
'--property',
250+
metavar='<key>',
251+
required=True,
252+
action='append',
253+
default=[],
254+
help='Property to remove from container '
255+
'(repeat option to remove multiple properties)',
256+
)
257+
return parser
258+
259+
@utils.log_method(log)
260+
def take_action(self, parsed_args):
261+
self.app.client_manager.object_store.container_unset(
262+
parsed_args.container,
263+
properties=parsed_args.property,
264+
)

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ openstack.object_store.v1 =
339339
container_delete = openstackclient.object.v1.container:DeleteContainer
340340
container_list = openstackclient.object.v1.container:ListContainer
341341
container_save = openstackclient.object.v1.container:SaveContainer
342+
container_set = openstackclient.object.v1.container:SetContainer
342343
container_show = openstackclient.object.v1.container:ShowContainer
344+
container_unset = openstackclient.object.v1.container:UnsetContainer
343345
object_create = openstackclient.object.v1.object:CreateObject
344346
object_delete = openstackclient.object.v1.object:DeleteObject
345347
object_list = openstackclient.object.v1.object:ListObject

0 commit comments

Comments
 (0)