diff --git a/bin/glance b/bin/glance index 42000009eb..6963e219d1 100755 --- a/bin/glance +++ b/bin/glance @@ -309,7 +309,6 @@ def image_delete(options, args): %(prog)s delete [options] 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 [options] [args] diff --git a/tests/functional/test_bin_glance.py b/tests/functional/test_bin_glance.py index 851f3c225c..9ad84b8cfc 100644 --- a/tests/functional/test_bin_glance.py +++ b/tests/functional/test_bin_glance.py @@ -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)