@@ -1012,17 +1012,26 @@ def get_parser(self, prog_name):
10121012 membership_group = parser .add_mutually_exclusive_group ()
10131013 membership_group .add_argument (
10141014 "--accept" ,
1015- action = "store_true" ,
1015+ action = "store_const" ,
1016+ const = "accepted" ,
1017+ dest = "membership" ,
1018+ default = None ,
10161019 help = _ ("Accept the image membership" ),
10171020 )
10181021 membership_group .add_argument (
10191022 "--reject" ,
1020- action = "store_true" ,
1023+ action = "store_const" ,
1024+ const = "rejected" ,
1025+ dest = "membership" ,
1026+ default = None ,
10211027 help = _ ("Reject the image membership" ),
10221028 )
10231029 membership_group .add_argument (
10241030 "--pending" ,
1025- action = "store_true" ,
1031+ action = "store_const" ,
1032+ const = "pending" ,
1033+ dest = "membership" ,
1034+ default = None ,
10261035 help = _ ("Reset the image membership to 'pending'" ),
10271036 )
10281037
@@ -1053,6 +1062,43 @@ def take_action(self, parsed_args):
10531062 _ ("ERROR: --%s was given, which is an Image v1 option"
10541063 " that is no longer supported in Image v2" ) % deadopt )
10551064
1065+ image = image_client .find_image (
1066+ parsed_args .image , ignore_missing = False ,
1067+ )
1068+ project_id = None
1069+ if parsed_args .project :
1070+ project_id = common .find_project (
1071+ identity_client ,
1072+ parsed_args .project ,
1073+ parsed_args .project_domain ,
1074+ ).id
1075+
1076+ # handle activation status changes
1077+
1078+ activation_status = None
1079+ if parsed_args .deactivate or parsed_args .activate :
1080+ if parsed_args .deactivate :
1081+ image_client .deactivate_image (image .id )
1082+ activation_status = "deactivated"
1083+ if parsed_args .activate :
1084+ image_client .reactivate_image (image .id )
1085+ activation_status = "activated"
1086+
1087+ # handle membership changes
1088+
1089+ if parsed_args .membership :
1090+ # If a specific project is not passed, assume we want to update
1091+ # our own membership
1092+ if not project_id :
1093+ project_id = self .app .client_manager .auth_ref .project_id
1094+ image_client .update_member (
1095+ image = image .id ,
1096+ member = project_id ,
1097+ status = parsed_args .membership ,
1098+ )
1099+
1100+ # handle everything else
1101+
10561102 kwargs = {}
10571103 copy_attrs = ('architecture' , 'container_format' , 'disk_format' ,
10581104 'file' , 'instance_id' , 'kernel_id' , 'locations' ,
@@ -1089,48 +1135,12 @@ def take_action(self, parsed_args):
10891135 kwargs ['visibility' ] = 'community'
10901136 if parsed_args .shared :
10911137 kwargs ['visibility' ] = 'shared'
1092- project_id = None
10931138 if parsed_args .project :
1094- project_id = common .find_project (
1095- identity_client ,
1096- parsed_args .project ,
1097- parsed_args .project_domain ,
1098- ).id
1139+ # We already did the project lookup above
10991140 kwargs ['owner_id' ] = project_id
1100-
1101- image = image_client .find_image (parsed_args .image ,
1102- ignore_missing = False )
1103-
1104- # image = utils.find_resource(
1105- # image_client.images, parsed_args.image)
1106-
1107- activation_status = None
1108- if parsed_args .deactivate :
1109- image_client .deactivate_image (image .id )
1110- activation_status = "deactivated"
1111- if parsed_args .activate :
1112- image_client .reactivate_image (image .id )
1113- activation_status = "activated"
1114-
1115- membership_group_args = ('accept' , 'reject' , 'pending' )
1116- membership_status = [status for status in membership_group_args
1117- if getattr (parsed_args , status )]
1118- if membership_status :
1119- # If a specific project is not passed, assume we want to update
1120- # our own membership
1121- if not project_id :
1122- project_id = self .app .client_manager .auth_ref .project_id
1123- # The mutually exclusive group of the arg parser ensure we have at
1124- # most one item in the membership_status list.
1125- if membership_status [0 ] != 'pending' :
1126- membership_status [0 ] += 'ed' # Glance expects the past form
1127- image_client .update_member (
1128- image = image .id , member = project_id , status = membership_status [0 ])
1129-
11301141 if parsed_args .tags :
11311142 # Tags should be extended, but duplicates removed
11321143 kwargs ['tags' ] = list (set (image .tags ).union (set (parsed_args .tags )))
1133-
11341144 if parsed_args .hidden is not None :
11351145 kwargs ['is_hidden' ] = parsed_args .hidden
11361146
0 commit comments