#NEWLINE# @ rest_api . post ( '/imagefactory/<image_collection>' ) #NEWLINE# @ rest_api . post ( '/imagefactory/base_images/<base_image_id>/<image_collection>' ) #NEWLINE# @ rest_api . post ( '/imagefactory/base_images/<base_image_id>/target_images/<target_image_id>/<image_collection>' ) #NEWLINE# @ rest_api . post ( '/imagefactory/target_images/<target_image_id>/<image_collection>' ) #NEWLINE# @ log_request #NEWLINE# @ oauth_protect #NEWLINE# @ check_accept_header #NEWLINE# def create_image( image_collection , base_image_id = None , target_image_id = None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# image_type = image_collection [ 0 : ( - 1 ) ] #NEWLINE# content_type = request . headers . get ( 'Content-Type' ) #NEWLINE# form_data = form_data_for_content_type ( content_type ) #NEWLINE# if ( ( 'application/x-www-form-urlencoded' in content_type ) or ( 'multipart/form-data' in content_type ) ) : #NEWLINE# #INDENT# request_data = form_data #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# request_data = form_data . get ( image_type ) #NEWLINE# #UNINDENT# if ( not request_data ) : #NEWLINE# #INDENT# raise HTTPResponse ( status = 400 , output = ( '%s not found in request.' % image_type ) ) #NEWLINE# #UNINDENT# req_base_img_id = request_data . get ( 'base_image_id' ) #NEWLINE# req_target_img_id = request_data . get ( 'target_image_id' ) #NEWLINE# base_img_id = ( req_base_img_id if req_base_img_id else base_image_id ) #NEWLINE# target_img_id = ( req_target_img_id if req_target_img_id else target_image_id ) #NEWLINE# if ( image_collection == 'base_images' ) : #NEWLINE# #INDENT# builder = BuildDispatcher ( ) . builder_for_base_image ( template = request_data . get ( 'template' ) , parameters = request_data . get ( 'parameters' ) ) #NEWLINE# image = builder . base_image #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( image_collection == 'target_images' ) : #NEWLINE# #INDENT# builder = BuildDispatcher ( ) . builder_for_target_image ( target = request_data . get ( 'target' ) , image_id = base_img_id , template = request_data . get ( 'template' ) , parameters = request_data . get ( 'parameters' ) ) #NEWLINE# image = builder . target_image #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( image_collection == 'provider_images' ) : #NEWLINE# #INDENT# _provider = request_data . get ( 'provider' ) #NEWLINE# _credentials = request_data . get ( 'credentials' ) #NEWLINE# _target = request_data . get ( 'target' ) #NEWLINE# if ( _provider and _credentials and _target ) : #NEWLINE# #INDENT# builder = BuildDispatcher ( ) . builder_for_provider_image ( provider = _provider , credentials = _credentials , target = _target , image_id = target_img_id , template = request_data . get ( 'template' ) , parameters = request_data . get ( 'parameters' ) ) #NEWLINE# image = builder . provider_image #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# _credentials = ( 'REDACTED' if _credentials else None ) #NEWLINE# raise HTTPResponse ( status = 400 , output = ( 'Missing key/value pair: provider(%s), credentials(%s), target(%s)' % ( _provider , _credentials , _target ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise HTTPResponse ( status = 404 , output = ( '%s not found' % image_collection ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# _response = { #NEWLINE# #INDENT# '_type' : type ( image ) . __name__ , #NEWLINE# 'id' : image . identifier , #NEWLINE# 'href' : ( '%s/%s' % ( request . url , image . identifier ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# for key in image . metadata ( ) : #NEWLINE# #INDENT# if ( key not in ( 'identifier' , 'data' ) ) : #NEWLINE# #INDENT# _response [ key ] = getattr ( image , key , None ) #NEWLINE# #UNINDENT# #UNINDENT# response . status = 202 #NEWLINE# return converted_response ( { #NEWLINE# #INDENT# image_collection [ 0 : ( - 1 ) ] : _response , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# except KeyError as e : #NEWLINE# #INDENT# log . exception ( e ) #NEWLINE# raise HTTPResponse ( status = 400 , output = ( 'Missing value for key: %s' % e ) ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# log . exception ( e ) #NEWLINE# raise HTTPResponse ( status = 500 , output = e )