<s> from django . db import models <EOL> from libcloud . providers import DRIVERS , get_driver <EOL> import opus . lib . log <EOL> log = opus . lib . log . get_logger ( ) <EOL> class Provider ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> DRIVER_CHOICES = ( <EOL> ( <NUM_LIT:0> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:10> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:2> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:5> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:15> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:7> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:16> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:3> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:9> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:4> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:12> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:8> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:11> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:6> , "<STR_LIT>" ) , <EOL> ) <EOL> name = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> driver = models . IntegerField ( choices = DRIVER_CHOICES ) <EOL> uri = models . URLField ( blank = True , help_text = "<STR_LIT>" "<STR_LIT>" ) <EOL> username = models . CharField ( max_length = <NUM_LIT> ) <EOL> password = models . CharField ( max_length = <NUM_LIT> ) <EOL> def get_client ( self ) : <EOL> """<STR_LIT>""" <EOL> Driver = get_driver ( self . driver ) <EOL> d = Driver ( <EOL> str ( self . username ) , <EOL> str ( self . password ) <EOL> ) <EOL> return d <EOL> class Meta : <EOL> unique_together = ( "<STR_LIT>" , "<STR_LIT:username>" , "<STR_LIT:password>" ) <EOL> app_label = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> driver = "<STR_LIT>" <EOL> for item in Provider . DRIVER_CHOICES : <EOL> if item [ <NUM_LIT:0> ] is self . driver : <EOL> driver = item [ <NUM_LIT:1> ] <EOL> break <EOL> return '<STR_LIT>' % ( self . name , driver ) <EOL> class RealImage ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> provider = models . ForeignKey ( "<STR_LIT>" ) <EOL> image_id = models . CharField ( max_length = <NUM_LIT> ) <EOL> aggregate_image = models . ForeignKey ( "<STR_LIT>" , help_text = "<STR_LIT>" ) <EOL> class Meta : <EOL> unique_together = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> app_label = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . image_id , self . provider ) <EOL> class AggregateImage ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> description = models . TextField ( blank = True ) <EOL> architecture = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> class Meta : <EOL> app_label = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> class Instance ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> image = models . ForeignKey ( "<STR_LIT>" ) <EOL> owner_id = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> hardware_profile = "<STR_LIT>" <EOL> provider = models . ForeignKey ( "<STR_LIT>" ) <EOL> instance_id = models . CharField ( max_length = <NUM_LIT> ) <EOL> policy = models . ForeignKey ( "<STR_LIT>" ) <EOL> class Meta : <EOL> app_label = "<STR_LIT>" <EOL> unique_together = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . instance_id , self . provider ) <EOL> @ property <EOL> def driver_instance_object ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> driver = self . provider . get_client ( ) <EOL> for node in driver . list_nodes ( ) : <EOL> if node . id == self . instance_id : <EOL> self . _cached_driver_instance_object = node <EOL> return node <EOL> raise ValueError ( "<STR_LIT>" '<STR_LIT>' % ( self . instance_id , self . provider ) <EOL> ) <EOL> else : <EOL> return self . _cached_driver_instance_object <EOL> @ property <EOL> def state ( self ) : <EOL> STATE_MAPPING = { <EOL> <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:4> : "<STR_LIT>" , <EOL> } <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return self . _state_override <EOL> return STATE_MAPPING [ self . driver_instance_object . state ] <EOL> @ state . setter <EOL> def state ( self , value ) : <EOL> self . _state_override = value <EOL> @ property <EOL> def public_addresses ( self ) : <EOL> return self . driver_instance_object . public_ip <EOL> @ property <EOL> def private_addresses ( self ) : <EOL> return self . driver_instance_object . private_ip <EOL> @ property <EOL> def actions ( self ) : <EOL> if self . state == "<STR_LIT>" : <EOL> actions = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> actions = [ "<STR_LIT>" ] <EOL> return actions <EOL> class Policy ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> STATE_CHOICES = ( <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> ) <EOL> name = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> state = models . CharField ( max_length = <NUM_LIT:12> , choices = STATE_CHOICES ) <EOL> description = models . TextField ( blank = True ) <EOL> type = models . CharField ( max_length = <NUM_LIT> , editable = False , blank = True ) <EOL> def get_next_provider ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> PolicyClass = getattr ( self , self . type . lower ( ) ) <EOL> return PolicyClass . get_next_provider ( image_id ) <EOL> class Meta : <EOL> app_label = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return self . name </s>
