#NEWLINE# def on_post( self , req , resp , tenant_id ) : #NEWLINE# #INDENT# body = json . loads ( req . stream . read ( ) . decode ( ) ) #NEWLINE# try : #NEWLINE# #INDENT# name = body [ 'keypair' ] [ 'name' ] #NEWLINE# key = body [ 'keypair' ] . get ( 'public_key' , generate_random_key ( ) ) #NEWLINE# #UNINDENT# except ( KeyError , TypeError ) : #NEWLINE# #INDENT# return error_handling . bad_request ( resp , 'Not all fields exist to create keypair.' ) #NEWLINE# #UNINDENT# validate_result = validate_keypair_name ( resp , name ) #NEWLINE# if ( not validate_result ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# client = req . env [ 'sl_client' ] #NEWLINE# mgr = SoftLayer . SshKeyManager ( client ) #NEWLINE# existing_keys = mgr . list_keys ( label = name ) #NEWLINE# if existing_keys : #NEWLINE# #INDENT# return error_handling . duplicate ( resp , 'Duplicate key by that name' ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# keypair = mgr . add_key ( key , name ) #NEWLINE# resp . body = { #NEWLINE# #INDENT# 'keypair' : format_keypair ( keypair ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# except SoftLayer . SoftLayerAPIError as e : #NEWLINE# #INDENT# if ( 'Unable to generate a fingerprint' in e . faultString ) : #NEWLINE# #INDENT# return error_handling . bad_request ( resp , e . faultString ) #NEWLINE# #UNINDENT# if ( 'SSH key already exists' in e . faultString ) : #NEWLINE# #INDENT# return error_handling . duplicate ( resp , e . faultString ) #NEWLINE# #UNINDENT# raise