@@ -408,18 +408,10 @@ def get_parser(self, prog_name):
408408 )
409409 return parser
410410
411- def take_action (self , parsed_args ):
411+ def _take_action_image (self , parsed_args ):
412412 identity_client = self .app .client_manager .identity
413413 image_client = self .app .client_manager .image
414414
415- for deadopt in self .deadopts :
416- if getattr (parsed_args , deadopt .replace ('-' , '_' ), None ):
417- msg = _ (
418- "ERROR: --%s was given, which is an Image v1 option "
419- "that is no longer supported in Image v2"
420- )
421- raise exceptions .CommandError (msg % deadopt )
422-
423415 # Build an attribute dict from the parsed args, only include
424416 # attributes that were actually set on the command line
425417 kwargs = {'allow_duplicates' : True }
@@ -472,7 +464,6 @@ def take_action(self, parsed_args):
472464 # image is created. Get the file name (if it is file, and not stdin)
473465 # for easier further handling.
474466 fp , fname = get_data_file (parsed_args )
475- info = {}
476467
477468 if fp is not None and parsed_args .volume :
478469 msg = _ (
@@ -552,49 +543,92 @@ def take_action(self, parsed_args):
552543 if signer .padding_method :
553544 kwargs ['img_signature_key_type' ] = signer .padding_method
554545
555- # If a volume is specified.
556- if parsed_args .volume :
557- volume_client = self .app .client_manager .volume
558- source_volume = utils .find_resource (
559- volume_client .volumes ,
560- parsed_args .volume ,
561- )
562- mv_kwargs = {}
563- if volume_client .api_version >= api_versions .APIVersion ('3.1' ):
564- mv_kwargs .update (
565- visibility = kwargs .get ('visibility' , 'private' ),
566- protected = bool (parsed_args .is_protected ),
546+ image = image_client .create_image (** kwargs )
547+ return _format_image (image )
548+
549+ def _take_action_volume (self , parsed_args ):
550+ volume_client = self .app .client_manager .volume
551+
552+ unsupported_opts = {
553+ # 'name', # 'name' is a positional argument and will always exist
554+ 'id' ,
555+ 'min_disk' ,
556+ 'min_ram' ,
557+ 'file' ,
558+ 'force' ,
559+ 'progress' ,
560+ 'sign_key_path' ,
561+ 'sign_cert_id' ,
562+ 'properties' ,
563+ 'tags' ,
564+ 'project' ,
565+ 'use_import' ,
566+ }
567+ for unsupported_opt in unsupported_opts :
568+ if getattr (parsed_args , unsupported_opt , None ):
569+ opt_name = unsupported_opt .replace ('-' , '_' )
570+ if unsupported_opt == 'use_import' :
571+ opt_name = 'import'
572+ msg = _ (
573+ "'--%s' was given, which is not supported when "
574+ "creating an image from a volume. "
575+ "This will be an error in a future version."
567576 )
568- else :
569- if (
570- parsed_args .visibility or
571- parsed_args .is_protected is not None
572- ):
573- msg = _ (
574- '--os-volume-api-version 3.1 or greater is required '
575- 'to support the --public, --private, --community, '
576- '--shared or --protected option.'
577- )
578- raise exceptions .CommandError (msg )
579-
580- response , body = volume_client .volumes .upload_to_image (
581- source_volume .id ,
582- parsed_args .force ,
583- parsed_args .name ,
584- parsed_args .container_format ,
585- parsed_args .disk_format ,
586- ** mv_kwargs
587- )
588- info = body ['os-volume_upload_image' ]
589- try :
590- info ['volume_type' ] = info ['volume_type' ]['name' ]
591- except TypeError :
592- info ['volume_type' ] = None
577+ # TODO(stephenfin): These should be an error in a future
578+ # version
579+ LOG .warning (msg % opt_name )
580+
581+ source_volume = utils .find_resource (
582+ volume_client .volumes ,
583+ parsed_args .volume ,
584+ )
585+ kwargs = {}
586+ if volume_client .api_version < api_versions .APIVersion ('3.1' ):
587+ if (
588+ parsed_args .visibility or
589+ parsed_args .is_protected is not None
590+ ):
591+ msg = _ (
592+ '--os-volume-api-version 3.1 or greater is required '
593+ 'to support the --public, --private, --community, '
594+ '--shared or --protected option.'
595+ )
596+ raise exceptions .CommandError (msg )
593597 else :
594- image = image_client .create_image (** kwargs )
598+ kwargs .update (
599+ visibility = parsed_args .visibility or 'private' ,
600+ protected = parsed_args .is_protected or False ,
601+ )
602+
603+ response , body = volume_client .volumes .upload_to_image (
604+ source_volume .id ,
605+ parsed_args .force ,
606+ parsed_args .name ,
607+ parsed_args .container_format ,
608+ parsed_args .disk_format ,
609+ ** kwargs
610+ )
611+ info = body ['os-volume_upload_image' ]
612+ try :
613+ info ['volume_type' ] = info ['volume_type' ]['name' ]
614+ except TypeError :
615+ info ['volume_type' ] = None
616+
617+ return info
595618
596- if not info :
597- info = _format_image (image )
619+ def take_action (self , parsed_args ):
620+ for deadopt in self .deadopts :
621+ if getattr (parsed_args , deadopt .replace ('-' , '_' ), None ):
622+ msg = _ (
623+ "ERROR: --%s was given, which is an Image v1 option "
624+ "that is no longer supported in Image v2"
625+ )
626+ raise exceptions .CommandError (msg % deadopt )
627+
628+ if parsed_args .volume :
629+ info = self ._take_action_volume (parsed_args )
630+ else :
631+ info = self ._take_action_image (parsed_args )
598632
599633 return zip (* sorted (info .items ()))
600634
0 commit comments