@@ -111,6 +111,19 @@ def get_parser(self, prog_name):
111111 help = "Similar to --location, but this indicates that the image"
112112 " should immediately be copied from the data store" ,
113113 )
114+ parser .add_argument (
115+ "--volume" ,
116+ metavar = "<volume>" ,
117+ help = "Create the image from the specified volume" ,
118+ )
119+ parser .add_argument (
120+ "--force" ,
121+ dest = 'force' ,
122+ action = 'store_true' ,
123+ default = False ,
124+ help = "If the image is created from a volume, force creation of the"
125+ " image even if volume is in use." ,
126+ )
114127 parser .add_argument (
115128 "--property" ,
116129 dest = "properties" ,
@@ -162,7 +175,9 @@ def take_action(self, parsed_args):
162175 args .pop ("variables" )
163176
164177 if "location" not in args and "copy_from" not in args :
165- if "file" in args :
178+ if "volume" in args :
179+ pass
180+ elif "file" in args :
166181 args ["data" ] = open (args .pop ("file" ), "rb" )
167182 else :
168183 args ["data" ] = None
@@ -171,23 +186,35 @@ def take_action(self, parsed_args):
171186 msvcrt .setmode (sys .stdin .fileno (), os .O_BINARY )
172187 args ["data" ] = sys .stdin
173188
174- image_client = self .app .client_manager .image
175- try :
176- image = utils .find_resource (
177- image_client .images ,
178- parsed_args .name ,
179- )
180- except exceptions .CommandError :
181- # This is normal for a create or reserve (create w/o an image)
182- image = image_client .images .create (** args )
189+ if "volume" in args :
190+ volume_client = self .app .client_manager .volume
191+ source_volume = utils .find_resource (volume_client .volumes ,
192+ parsed_args .volume )
193+ response , body = volume_client .volumes .upload_to_image (
194+ source_volume ,
195+ parsed_args .force ,
196+ parsed_args .name ,
197+ parsed_args .container_format ,
198+ parsed_args .disk_format )
199+ info = body ['os-volume_upload_image' ]
183200 else :
184- # It must be an update
185- # If an image is specified via --file, --location or --copy-from
186- # let the API handle it
187- image = image_client .images .update (image , ** args )
201+ image_client = self .app .client_manager .image
202+ try :
203+ image = utils .find_resource (
204+ image_client .images ,
205+ parsed_args .name ,
206+ )
207+ except exceptions .CommandError :
208+ # This is normal for a create or reserve (create w/o an image)
209+ image = image_client .images .create (** args )
210+ else :
211+ # It must be an update
212+ # If an image is specified via --file, --location or
213+ # --copy-from let the API handle it
214+ image = image_client .images .update (image , ** args )
188215
189- info = {}
190- info .update (image ._info )
216+ info = {}
217+ info .update (image ._info )
191218 return zip (* sorted (six .iteritems (info )))
192219
193220
0 commit comments