<s> from six . moves import range <EOL> from webob import exc <EOL> from nova import context <EOL> from nova . i18n import _ <EOL> from nova import objects <EOL> from nova import utils <EOL> CHUNKS = <NUM_LIT:4> <EOL> CHUNK_LENGTH = <NUM_LIT:255> <EOL> MAX_SIZE = CHUNKS * CHUNK_LENGTH <EOL> def extract_password ( instance ) : <EOL> result = '<STR_LIT>' <EOL> sys_meta = utils . instance_sys_meta ( instance ) <EOL> for key in sorted ( sys_meta . keys ( ) ) : <EOL> if key . startswith ( '<STR_LIT>' ) : <EOL> result += sys_meta [ key ] <EOL> return result or None <EOL> def convert_password ( context , password ) : <EOL> """<STR_LIT>""" <EOL> password = password or '<STR_LIT>' <EOL> meta = { } <EOL> for i in range ( CHUNKS ) : <EOL> meta [ '<STR_LIT>' % i ] = password [ : CHUNK_LENGTH ] <EOL> password = password [ CHUNK_LENGTH : ] <EOL> return meta <EOL> def handle_password ( req , meta_data ) : <EOL> ctxt = context . get_admin_context ( ) <EOL> if req . method == '<STR_LIT:GET>' : <EOL> return meta_data . password <EOL> elif req . method == '<STR_LIT:POST>' : <EOL> if meta_data . password : <EOL> raise exc . HTTPConflict ( ) <EOL> if ( req . content_length > MAX_SIZE or len ( req . body ) > MAX_SIZE ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> instance = objects . Instance . get_by_uuid ( ctxt , meta_data . uuid ) <EOL> instance . system_metadata . update ( convert_password ( ctxt , req . body ) ) <EOL> instance . save ( ) <EOL> else : <EOL> raise exc . HTTPBadRequest ( ) </s>
