@@ -805,8 +805,8 @@ def take_action(self, parsed_args):
805805
806806 # Checks if anything that requires getting the image
807807 if not (kwargs or parsed_args .deactivate or parsed_args .activate ):
808- self . log . warning ( _ ("No arguments specified" ) )
809- return {}, {}
808+ msg = _ ("No arguments specified" )
809+ raise exceptions . CommandError ( msg )
810810
811811 image = utils .find_resource (
812812 image_client .images , parsed_args .image )
@@ -856,3 +856,88 @@ def take_action(self, parsed_args):
856856
857857 info = _format_image (image )
858858 return zip (* sorted (six .iteritems (info )))
859+
860+
861+ class UnsetImage (command .Command ):
862+ """Unset image tags and properties"""
863+
864+ def get_parser (self , prog_name ):
865+ parser = super (UnsetImage , self ).get_parser (prog_name )
866+ parser .add_argument (
867+ "image" ,
868+ metavar = "<image>" ,
869+ help = _ ("Image to modify (name or ID)" ),
870+ )
871+ parser .add_argument (
872+ "--tag" ,
873+ dest = "tags" ,
874+ metavar = "<tag>" ,
875+ default = [],
876+ action = 'append' ,
877+ help = _ ("Unset a tag on this image "
878+ "(repeat option to set multiple tags)" ),
879+ )
880+ parser .add_argument (
881+ "--property" ,
882+ dest = "properties" ,
883+ metavar = "<property_key>" ,
884+ default = [],
885+ action = 'append' ,
886+ help = _ ("Unset a property on this image "
887+ "(repeat option to set multiple properties)" ),
888+ )
889+ return parser
890+
891+ def take_action (self , parsed_args ):
892+ image_client = self .app .client_manager .image
893+ image = utils .find_resource (
894+ image_client .images ,
895+ parsed_args .image ,
896+ )
897+
898+ if not (parsed_args .tags or parsed_args .properties ):
899+ msg = _ ("No arguments specified" )
900+ raise exceptions .CommandError (msg )
901+
902+ kwargs = {}
903+ tagret = 0
904+ propret = 0
905+ if parsed_args .tags :
906+ for k in parsed_args .tags :
907+ try :
908+ image_client .image_tags .delete (image .id , k )
909+ except Exception :
910+ self .log .error (_ ("tag unset failed,"
911+ " '%s' is a nonexistent tag " ) % k )
912+ tagret += 1
913+
914+ if parsed_args .properties :
915+ for k in parsed_args .properties :
916+ try :
917+ assert (k in image .keys ())
918+ except AssertionError :
919+ self .log .error (_ ("property unset failed,"
920+ " '%s' is a nonexistent property " ) % k )
921+ propret += 1
922+ image_client .images .update (
923+ image .id ,
924+ parsed_args .properties ,
925+ ** kwargs )
926+
927+ tagtotal = len (parsed_args .tags )
928+ proptotal = len (parsed_args .properties )
929+ if (tagret > 0 and propret > 0 ):
930+ msg = (_ ("Failed to unset %(tagret)s of %(tagtotal)s tags,"
931+ "Failed to unset %(propret)s of %(proptotal)s properties." )
932+ % {'tagret' : tagret , 'tagtotal' : tagtotal ,
933+ 'propret' : propret , 'proptotal' : proptotal })
934+ raise exceptions .CommandError (msg )
935+ elif tagret > 0 :
936+ msg = (_ ("Failed to unset %(target)s of %(tagtotal)s tags." )
937+ % {'tagret' : tagret , 'tagtotal' : tagtotal })
938+ raise exceptions .CommandError (msg )
939+ elif propret > 0 :
940+ msg = (_ ("Failed to unset %(propret)s of %(proptotal)s"
941+ " properties." )
942+ % {'propret' : propret , 'proptotal' : proptotal })
943+ raise exceptions .CommandError (msg )
0 commit comments