Support delete and cluster_delete with many resources

Not like nova-delete when use trove-delete there is only support
delete an instance everytime. With this patch, we can delete
many instances or clusters in the same call, like
trove delete instance1 instance2.

Change-Id: I6bb0c406ba7f4c0e43cddfdff1d156b7e82ffa7a
This commit is contained in:
wangyao 2017-11-20 17:57:17 +08:00
parent 34dbdd2198
commit 3d9576bfa5
3 changed files with 38 additions and 14 deletions

View File

@ -0,0 +1,6 @@
---
features:
- This change defines do_action_on_many function as a helper to run an
action on many resources (like trove delete). Use command like
``trove delete instance1 instance2 ...`` to delete many instances and the
same as cluster-delete.

View File

@ -334,3 +334,19 @@ def do_action_with_msg(action, success_msg):
action
print(success_msg)
def do_action_on_many(action, resources, success_msg, error_msg):
"""Helper to run an action on many resources."""
failure_flag = False
for resource in resources:
try:
action(resource)
print(success_msg % resource)
except Exception as e:
failure_flag = True
print(encodeutils.safe_encode(six.text_type(e)))
if failure_flag:
raise exceptions.CommandError(error_msg)

View File

@ -402,15 +402,16 @@ def do_cluster_shrink(cs, args):
cs.clusters.shrink(cluster, instances=instances)
@utils.arg('instance', metavar='<instance>',
help=_('ID or name of the instance.'))
@utils.arg('instance', metavar='<instance>', nargs='+',
help=_('ID or name of the instance(s).'))
@utils.service_type('database')
def do_delete(cs, args):
"""Deletes an instance."""
instance = _find_instance(cs, args.instance)
msg = _("Request to delete instance %s "
"has been accepted.") % instance.id
utils.do_action_with_msg(cs.instances.delete(instance), msg)
"""Delete specified instance(s)."""
utils.do_action_on_many(
lambda s: cs.instances.delete(_find_instance(cs, s)),
args.instance,
_("Request to delete instance %s has been accepted."),
_("Unable to delete the specified instance(s)."))
@utils.arg('instance', metavar='<instance>',
@ -437,15 +438,16 @@ def do_reset_status(cs, args):
cs.instances.reset_status(instance=instance)
@utils.arg('cluster', metavar='<cluster>',
help=_('ID or name of the cluster.'))
@utils.arg('cluster', metavar='<cluster>', nargs='+',
help=_('ID or name of the cluster(s).'))
@utils.service_type('database')
def do_cluster_delete(cs, args):
"""Deletes a cluster."""
cluster = _find_cluster(cs, args.cluster)
msg = _("Request to delete cluster %s "
"has been accepted.") % cluster.id
utils.do_action_with_msg(cs.clusters.delete(cluster), msg)
"""Delete specified cluster(s)."""
utils.do_action_on_many(
lambda s: cs.clusters.delete(_find_cluster(cs, s)),
args.cluster,
_("Request to delete cluster %s has been accepted."),
_("Unable to delete the specified cluster(s)."))
@utils.arg('cluster', metavar='<cluster>',