#NEWLINE# @ sensitive_variables ( 'context' ) #NEWLINE# def handle( self , request , context ) : #NEWLINE# #INDENT# custom_script = context . get ( 'customization_script' , '' ) #NEWLINE# dev_mapping_1 = None #NEWLINE# dev_mapping_2 = None #NEWLINE# image_id = '' #NEWLINE# source_type = context . get ( 'source_type' , None ) #NEWLINE# if ( source_type in [ 'image_id' , 'instance_snapshot_id' ] ) : #NEWLINE# #INDENT# image_id = context [ 'source_id' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( source_type in [ 'volume_id' , 'volume_snapshot_id' ] ) : #NEWLINE# #INDENT# dev_mapping_1 = { #NEWLINE# #INDENT# context [ 'device_name' ] : ( '%s::%s' % ( context [ 'source_id' ] , int ( bool ( context [ 'delete_on_terminate' ] ) ) ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( source_type == 'volume_image_id' ) : #NEWLINE# #INDENT# dev_mapping_2 = [ { #NEWLINE# #INDENT# 'device_name' : str ( context [ 'device_name' ] ) , #NEWLINE# 'source_type' : 'image' , #NEWLINE# 'destination_type' : 'volume' , #NEWLINE# 'delete_on_termination' : int ( bool ( context [ 'delete_on_terminate' ] ) ) , #NEWLINE# 'uuid' : context [ 'source_id' ] , #NEWLINE# 'boot_index' : '0' , #NEWLINE# 'volume_size' : context [ 'volume_size' ] , #NEWLINE# #UNINDENT# } ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# netids = context . get ( 'network_id' , None ) #NEWLINE# if netids : #NEWLINE# #INDENT# nics = [ { #NEWLINE# #INDENT# 'net-id' : netid , #NEWLINE# 'v4-fixed-ip' : '' , #NEWLINE# #UNINDENT# } for netid in netids ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# nics = None #NEWLINE# #UNINDENT# avail_zone = context . get ( 'availability_zone' , None ) #NEWLINE# if api . neutron . is_port_profiles_supported ( ) : #NEWLINE# #INDENT# net_id = context [ 'network_id' ] [ 0 ] #NEWLINE# LOG . debug ( 'Horizon->Create Port with %(netid)s %(profile_id)s' , { #NEWLINE# #INDENT# 'netid' : net_id , #NEWLINE# 'profile_id' : context [ 'profile_id' ] , #NEWLINE# #UNINDENT# } ) #NEWLINE# try : #NEWLINE# #INDENT# port = api . neutron . port_create ( request , net_id , policy_profile_id = context [ 'profile_id' ] ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# msg = ( _ ( 'Port not created for profile-id (%s).' ) % context [ 'profile_id' ] ) #NEWLINE# exceptions . handle ( request , msg ) #NEWLINE# #UNINDENT# if ( port and port . id ) : #NEWLINE# #INDENT# nics = [ { #NEWLINE# #INDENT# 'port-id' : port . id , #NEWLINE# #UNINDENT# } ] #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# api . nova . server_create ( request , context [ 'name' ] , image_id , context [ 'flavor' ] , context [ 'keypair_id' ] , normalize_newlines ( custom_script ) , context [ 'security_group_ids' ] , block_device_mapping = dev_mapping_1 , block_device_mapping_v2 = dev_mapping_2 , nics = nics , availability_zone = avail_zone , instance_count = int ( context [ 'count' ] ) , admin_pass = context [ 'admin_pass' ] ) #NEWLINE# return True #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# exceptions . handle ( netids ) #NEWLINE# return False