<s> from stacktach import utils <EOL> from stacktach import stacklog <EOL> from stacktach import image_type <EOL> from stacktach import db <EOL> class Notification ( object ) : <EOL> def __init__ ( self , body , deployment , routing_key , json ) : <EOL> self . body = body <EOL> self . request_id = body . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> self . deployment = deployment <EOL> self . routing_key = routing_key <EOL> self . json = json <EOL> self . payload = body . get ( '<STR_LIT>' , { } ) <EOL> self . publisher = self . body [ '<STR_LIT>' ] <EOL> self . event = self . body [ '<STR_LIT>' ] <EOL> @ property <EOL> def when ( self ) : <EOL> when = self . body . get ( '<STR_LIT>' , None ) <EOL> if not when : <EOL> when = self . body [ '<STR_LIT>' ] <EOL> when = utils . str_time_to_unix ( when ) <EOL> return when <EOL> @ property <EOL> def service ( self ) : <EOL> parts = self . publisher . split ( '<STR_LIT:.>' ) <EOL> return parts [ <NUM_LIT:0> ] <EOL> @ property <EOL> def host ( self ) : <EOL> host = None <EOL> parts = self . publisher . split ( '<STR_LIT:.>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> host = "<STR_LIT:.>" . join ( parts [ <NUM_LIT:1> : ] ) <EOL> return host <EOL> @ property <EOL> def tenant ( self ) : <EOL> tenant = self . body . get ( '<STR_LIT>' , None ) <EOL> tenant = self . payload . get ( '<STR_LIT>' , tenant ) <EOL> return tenant <EOL> @ property <EOL> def instance ( self ) : <EOL> instance = self . payload . get ( '<STR_LIT>' , None ) <EOL> instance = self . payload . get ( '<STR_LIT>' , instance ) <EOL> if not instance : <EOL> instance = self . payload . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) <EOL> if not instance : <EOL> instance = self . payload . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) <EOL> return instance <EOL> @ property <EOL> def message_id ( self ) : <EOL> return self . body . get ( '<STR_LIT>' , None ) <EOL> def save ( self ) : <EOL> return db . create_generic_rawdata ( deployment = self . deployment , <EOL> routing_key = self . routing_key , <EOL> tenant = self . tenant , <EOL> json = self . json , <EOL> when = self . when , <EOL> publisher = self . publisher , <EOL> event = self . event , <EOL> service = self . service , <EOL> host = self . host , <EOL> instance = self . instance , <EOL> request_id = self . request_id , <EOL> message_id = self . message_id ) <EOL> class GlanceNotification ( Notification ) : <EOL> def __init__ ( self , body , deployment , routing_key , json ) : <EOL> super ( GlanceNotification , self ) . __init__ ( body , deployment , <EOL> routing_key , json ) <EOL> if isinstance ( self . payload , dict ) : <EOL> self . properties = self . payload . get ( '<STR_LIT>' , { } ) <EOL> self . image_type = image_type . get_numeric_code ( self . payload ) <EOL> self . status = self . payload . get ( '<STR_LIT:status>' , None ) <EOL> self . uuid = self . payload . get ( '<STR_LIT:id>' , None ) <EOL> self . size = self . payload . get ( '<STR_LIT:size>' , None ) <EOL> created_at = self . payload . get ( '<STR_LIT>' , None ) <EOL> self . created_at = created_at and utils . str_time_to_unix ( created_at ) <EOL> else : <EOL> self . properties = { } <EOL> self . image_type = None <EOL> self . status = None <EOL> self . uuid = None <EOL> self . size = None <EOL> self . created_at = None <EOL> @ property <EOL> def owner ( self ) : <EOL> if isinstance ( self . payload , dict ) : <EOL> return self . payload . get ( '<STR_LIT>' , None ) <EOL> else : <EOL> return None <EOL> @ property <EOL> def instance ( self ) : <EOL> return self . properties . get ( '<STR_LIT>' , None ) <EOL> @ property <EOL> def deleted_at ( self ) : <EOL> deleted_at = self . body . get ( '<STR_LIT>' , None ) <EOL> if isinstance ( self . payload , dict ) : <EOL> deleted_at = deleted_at or self . payload . get ( '<STR_LIT>' , None ) <EOL> return deleted_at and utils . str_time_to_unix ( deleted_at ) <EOL> def save ( self ) : <EOL> return db . create_glance_rawdata ( deployment = self . deployment , <EOL> routing_key = self . routing_key , <EOL> owner = self . owner , <EOL> json = self . json , <EOL> when = self . when , <EOL> publisher = self . publisher , <EOL> event = self . event , <EOL> service = self . service , <EOL> host = self . host , <EOL> instance = self . instance , <EOL> request_id = self . request_id , <EOL> image_type = self . image_type , <EOL> status = self . status , <EOL> uuid = self . uuid ) <EOL> def save_exists ( self , raw ) : <EOL> if isinstance ( self . payload , dict ) : <EOL> audit_period_beginning = self . payload . get ( <EOL> '<STR_LIT>' , None ) <EOL> audit_period_beginning = audit_period_beginning and utils . str_time_to_unix ( audit_period_beginning ) <EOL> audit_period_ending = self . payload . get ( <EOL> '<STR_LIT>' , None ) <EOL> audit_period_ending = audit_period_ending and utils . str_time_to_unix ( audit_period_ending ) <EOL> message_id = self . message_id <EOL> images = self . payload . get ( '<STR_LIT>' , [ ] ) <EOL> else : <EOL> stacklog . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % raw . id ) <EOL> audit_period_beginning = None <EOL> audit_period_ending = None <EOL> images = [ ] <EOL> for image in images : <EOL> created_at = image [ '<STR_LIT>' ] <EOL> created_at = created_at and utils . str_time_to_unix ( created_at ) <EOL> uuid = image [ '<STR_LIT:id>' ] <EOL> deleted_at = image [ '<STR_LIT>' ] <EOL> deleted_at = deleted_at and utils . str_time_to_unix ( deleted_at ) <EOL> if created_at : <EOL> values = { <EOL> '<STR_LIT>' : uuid , <EOL> '<STR_LIT>' : audit_period_beginning , <EOL> '<STR_LIT>' : audit_period_ending , <EOL> '<STR_LIT>' : self . owner , <EOL> '<STR_LIT:size>' : image [ '<STR_LIT:size>' ] , <EOL> '<STR_LIT>' : raw , <EOL> '<STR_LIT>' : message_id <EOL> } <EOL> usage = db . get_image_usage ( uuid = uuid ) <EOL> values [ '<STR_LIT>' ] = usage <EOL> values [ '<STR_LIT>' ] = created_at <EOL> if deleted_at : <EOL> delete = db . get_image_delete ( uuid = uuid ) <EOL> values [ '<STR_LIT>' ] = delete <EOL> values [ '<STR_LIT>' ] = deleted_at <EOL> db . create_image_exists ( ** values ) <EOL> else : <EOL> stacklog . warn ( "<STR_LIT>" <EOL> % raw . id ) <EOL> def save_usage ( self , raw ) : <EOL> values = { <EOL> '<STR_LIT>' : self . uuid , <EOL> '<STR_LIT>' : self . created_at , <EOL> '<STR_LIT>' : self . owner , <EOL> '<STR_LIT:size>' : self . size , <EOL> '<STR_LIT>' : raw <EOL> } <EOL> db . create_image_usage ( ** values ) <EOL> def save_delete ( self , raw ) : <EOL> values = { <EOL> '<STR_LIT>' : self . uuid , <EOL> '<STR_LIT>' : raw , <EOL> '<STR_LIT>' : self . deleted_at <EOL> } <EOL> db . create_image_delete ( ** values ) <EOL> class NovaNotification ( Notification ) : <EOL> def __init__ ( self , body , deployment , routing_key , json ) : <EOL> super ( NovaNotification , self ) . __init__ ( body , deployment , routing_key , <EOL> json ) <EOL> self . state = self . payload . get ( '<STR_LIT:state>' , '<STR_LIT>' ) <EOL> self . old_state = self . payload . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . old_task = self . payload . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . task = self . payload . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . image_type = image_type . get_numeric_code ( self . payload ) <EOL> image_meta = self . payload . get ( '<STR_LIT>' , { } ) <EOL> self . os_architecture = image_meta . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . os_distro = image_meta . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . os_version = image_meta . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . rax_options = image_meta . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . instance_type_id = self . payload . get ( '<STR_LIT>' , None ) <EOL> self . instance_flavor_id = self . payload . get ( '<STR_LIT>' , None ) <EOL> self . new_instance_type_id = self . payload . get ( '<STR_LIT>' , None ) <EOL> self . launched_at = self . payload . get ( '<STR_LIT>' , None ) <EOL> self . deleted_at = self . payload . get ( '<STR_LIT>' , None ) <EOL> self . terminated_at = self . payload . get ( '<STR_LIT>' , None ) <EOL> self . audit_period_beginning = self . payload . get ( <EOL> '<STR_LIT>' , None ) <EOL> self . audit_period_ending = self . payload . get ( <EOL> '<STR_LIT>' , None ) <EOL> self . message = self . payload . get ( '<STR_LIT:message>' , None ) <EOL> bandwidth = self . payload . get ( '<STR_LIT>' , { } ) or { } <EOL> bandwidth_public = bandwidth . get ( '<STR_LIT>' , { } ) <EOL> self . bandwidth_public_out = bandwidth_public . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> @ property <EOL> def host ( self ) : <EOL> host = None <EOL> parts = self . publisher . split ( '<STR_LIT:.>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> host = "<STR_LIT:.>" . join ( parts [ <NUM_LIT:1> : ] ) <EOL> return host <EOL> @ property <EOL> def service ( self ) : <EOL> parts = self . publisher . split ( '<STR_LIT:.>' ) <EOL> return parts [ <NUM_LIT:0> ] <EOL> def save ( self ) : <EOL> return db . create_nova_rawdata ( deployment = self . deployment , <EOL> routing_key = self . routing_key , <EOL> tenant = self . tenant , <EOL> json = self . json , <EOL> when = self . when , <EOL> publisher = self . publisher , <EOL> event = self . event , <EOL> service = self . service , <EOL> host = self . host , <EOL> instance = self . instance , <EOL> request_id = self . request_id , <EOL> image_type = self . image_type , <EOL> state = self . state , <EOL> old_state = self . old_state , <EOL> task = self . task , <EOL> old_task = self . old_task , <EOL> os_architecture = self . os_architecture , <EOL> os_distro = self . os_distro , <EOL> os_version = self . os_version , <EOL> rax_options = self . rax_options ) <EOL> def notification_factory ( body , deployment , routing_key , json , exchange ) : <EOL> if exchange == '<STR_LIT>' : <EOL> return NovaNotification ( body , deployment , routing_key , json ) <EOL> if exchange == "<STR_LIT>" : <EOL> return GlanceNotification ( body , deployment , routing_key , json ) <EOL> return Notification ( body , deployment , routing_key , json ) </s>
