- Require user confirmation for "bin/glance clear" and "bin/glance delete <id>"

- Allow for override with -f/--force command-line option
This commit is contained in:
Brian Waldon 2011-04-21 02:17:43 +00:00 committed by Tarmac
commit 50a9f90b7d
2 changed files with 25 additions and 3 deletions

View File

@ -309,7 +309,6 @@ def image_delete(options, args):
%(prog)s delete [options] <ID>
Deletes an image from Glance"""
c = get_client(options)
try:
image_id = args.pop()
except IndexError:
@ -317,6 +316,13 @@ Deletes an image from Glance"""
print "as the first argument"
return FAILURE
if not options.force and \
not user_confirm("Delete image %s?" % (image_id,)):
print 'Not deleting image %s' % (image_id,)
return FAILURE
c = get_client(options)
try:
c.delete_image(image_id)
print "Deleted image %s" % image_id
@ -428,6 +434,10 @@ def images_clear(options, args):
%(prog)s clear [options]
Deletes all images from a Glance server"""
if not options.force and not user_confirm("Delete all images?"):
print 'Not deleting any images'
return FAILURE
c = get_client(options)
images = c.get_images()
for image in images:
@ -470,6 +480,10 @@ def create_options(parser):
type=int, default=9292,
help="Port the Glance API host listens on. "
"Default: %default")
parser.add_option('-f', '--force', dest="force", metavar="FORCE",
default=False, action="store_true",
help="Prevent select actions from requesting "
"user confirmation")
parser.add_option('--dry-run', default=False, action="store_true",
help="Don't actually execute the command, just print "
"output showing what WOULD happen.")
@ -532,6 +546,14 @@ def print_help(options, args):
print COMMANDS[command].__doc__ % {'prog': os.path.basename(sys.argv[0])}
def user_confirm(prompt):
try:
answer = raw_input("%s [Y/n] " % (prompt,))
return answer.lower() in ("yes", "y")
except Exception:
return False
if __name__ == '__main__':
usage = """
%prog <command> [options] [args]

View File

@ -72,7 +72,7 @@ class TestBinGlance(functional.FunctionalTest):
self.assertTrue('MyImage' in image_data_line)
# 3. Delete the image
cmd = "bin/glance --port=%d delete 1" % api_port
cmd = "bin/glance --port=%d --force delete 1" % api_port
exitcode, out, err = execute(cmd)
@ -195,7 +195,7 @@ class TestBinGlance(functional.FunctionalTest):
self.assertEqual('Added new image with ID: %i' % i, out.strip())
# 2. Clear all images
cmd = "bin/glance --port=%d clear" % api_port
cmd = "bin/glance --port=%d --force clear" % api_port
exitcode, out, err = execute(cmd)
self.assertEqual(0, exitcode)