<s> from oslo_log import log as logging <EOL> from oslo_utils import uuidutils <EOL> from nova . compute import utils as compute_utils <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova . objects import base <EOL> from nova . objects import fields <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ base . NovaObjectRegistry . register <EOL> class Aggregate ( base . NovaPersistentObject , base . NovaObject ) : <EOL> VERSION = '<STR_LIT>' <EOL> fields = { <EOL> '<STR_LIT:id>' : fields . IntegerField ( ) , <EOL> '<STR_LIT>' : fields . UUIDField ( nullable = False ) , <EOL> '<STR_LIT:name>' : fields . StringField ( ) , <EOL> '<STR_LIT>' : fields . ListOfStringsField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . DictOfStringsField ( nullable = True ) , <EOL> } <EOL> obj_extra_fields = [ '<STR_LIT>' ] <EOL> @ staticmethod <EOL> def _from_db_object ( context , aggregate , db_aggregate ) : <EOL> for key in aggregate . fields : <EOL> if key == '<STR_LIT>' : <EOL> db_key = '<STR_LIT>' <EOL> elif key == '<STR_LIT>' : <EOL> continue <EOL> else : <EOL> db_key = key <EOL> setattr ( aggregate , key , db_aggregate [ db_key ] ) <EOL> if db_aggregate . get ( '<STR_LIT>' ) : <EOL> aggregate . uuid = db_aggregate [ '<STR_LIT>' ] <EOL> aggregate . _context = context <EOL> aggregate . obj_reset_changes ( ) <EOL> if '<STR_LIT>' not in aggregate : <EOL> aggregate . uuid = uuidutils . generate_uuid ( ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> dict ( uuid = aggregate . uuid , agg = aggregate . id ) ) <EOL> aggregate . save ( ) <EOL> return aggregate <EOL> def _assert_no_hosts ( self , action ) : <EOL> if '<STR_LIT>' in self . obj_what_changed ( ) : <EOL> raise exception . ObjectActionError ( <EOL> action = action , <EOL> reason = '<STR_LIT>' ) <EOL> @ base . remotable_classmethod <EOL> def get_by_id ( cls , context , aggregate_id ) : <EOL> db_aggregate = db . aggregate_get ( context , aggregate_id ) <EOL> return cls . _from_db_object ( context , cls ( ) , db_aggregate ) <EOL> @ base . remotable <EOL> def create ( self ) : <EOL> if self . obj_attr_is_set ( '<STR_LIT:id>' ) : <EOL> raise exception . ObjectActionError ( action = '<STR_LIT>' , <EOL> reason = '<STR_LIT>' ) <EOL> self . _assert_no_hosts ( '<STR_LIT>' ) <EOL> updates = self . obj_get_changes ( ) <EOL> payload = dict ( updates ) <EOL> if '<STR_LIT>' in updates : <EOL> payload [ '<STR_LIT>' ] = payload . pop ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in updates : <EOL> updates [ '<STR_LIT>' ] = uuidutils . generate_uuid ( ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> dict ( uuid = updates [ '<STR_LIT>' ] ) ) <EOL> compute_utils . notify_about_aggregate_update ( self . _context , <EOL> "<STR_LIT>" , <EOL> payload ) <EOL> metadata = updates . pop ( '<STR_LIT>' , None ) <EOL> db_aggregate = db . aggregate_create ( self . _context , updates , <EOL> metadata = metadata ) <EOL> self . _from_db_object ( self . _context , self , db_aggregate ) <EOL> payload [ '<STR_LIT>' ] = self . id <EOL> compute_utils . notify_about_aggregate_update ( self . _context , <EOL> "<STR_LIT>" , <EOL> payload ) <EOL> @ base . remotable <EOL> def save ( self ) : <EOL> self . _assert_no_hosts ( '<STR_LIT>' ) <EOL> updates = self . obj_get_changes ( ) <EOL> payload = { '<STR_LIT>' : self . id } <EOL> if '<STR_LIT>' in updates : <EOL> payload [ '<STR_LIT>' ] = updates [ '<STR_LIT>' ] <EOL> compute_utils . notify_about_aggregate_update ( self . _context , <EOL> "<STR_LIT>" , <EOL> payload ) <EOL> updates . pop ( '<STR_LIT:id>' , None ) <EOL> db_aggregate = db . aggregate_update ( self . _context , self . id , updates ) <EOL> compute_utils . notify_about_aggregate_update ( self . _context , <EOL> "<STR_LIT>" , <EOL> payload ) <EOL> self . _from_db_object ( self . _context , self , db_aggregate ) <EOL> @ base . remotable <EOL> def update_metadata ( self , updates ) : <EOL> payload = { '<STR_LIT>' : self . id , <EOL> '<STR_LIT>' : updates } <EOL> compute_utils . notify_about_aggregate_update ( self . _context , <EOL> "<STR_LIT>" , <EOL> payload ) <EOL> to_add = { } <EOL> for key , value in updates . items ( ) : <EOL> if value is None : <EOL> try : <EOL> db . aggregate_metadata_delete ( self . _context , self . id , key ) <EOL> except exception . AggregateMetadataNotFound : <EOL> pass <EOL> try : <EOL> self . metadata . pop ( key ) <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> to_add [ key ] = value <EOL> self . metadata [ key ] = value <EOL> db . aggregate_metadata_add ( self . _context , self . id , to_add ) <EOL> compute_utils . notify_about_aggregate_update ( self . _context , <EOL> "<STR_LIT>" , <EOL> payload ) <EOL> self . obj_reset_changes ( fields = [ '<STR_LIT>' ] ) <EOL> @ base . remotable <EOL> def destroy ( self ) : <EOL> db . aggregate_delete ( self . _context , self . id ) <EOL> @ base . remotable <EOL> def add_host ( self , host ) : <EOL> db . aggregate_host_add ( self . _context , self . id , host ) <EOL> if self . hosts is None : <EOL> self . hosts = [ ] <EOL> self . hosts . append ( host ) <EOL> self . obj_reset_changes ( fields = [ '<STR_LIT>' ] ) <EOL> @ base . remotable <EOL> def delete_host ( self , host ) : <EOL> db . aggregate_host_delete ( self . _context , self . id , host ) <EOL> self . hosts . remove ( host ) <EOL> self . obj_reset_changes ( fields = [ '<STR_LIT>' ] ) <EOL> @ property <EOL> def availability_zone ( self ) : <EOL> return self . metadata . get ( '<STR_LIT>' , None ) <EOL> @ base . NovaObjectRegistry . register <EOL> class AggregateList ( base . ObjectListBase , base . NovaObject ) : <EOL> VERSION = '<STR_LIT>' <EOL> fields = { <EOL> '<STR_LIT>' : fields . ListOfObjectsField ( '<STR_LIT>' ) , <EOL> } <EOL> @ classmethod <EOL> def _filter_db_aggregates ( cls , db_aggregates , hosts ) : <EOL> if not isinstance ( hosts , set ) : <EOL> hosts = set ( hosts ) <EOL> filtered_aggregates = [ ] <EOL> for db_aggregate in db_aggregates : <EOL> for host in db_aggregate [ '<STR_LIT>' ] : <EOL> if host in hosts : <EOL> filtered_aggregates . append ( db_aggregate ) <EOL> break <EOL> return filtered_aggregates <EOL> @ base . remotable_classmethod <EOL> def get_all ( cls , context ) : <EOL> db_aggregates = db . aggregate_get_all ( context ) <EOL> return base . obj_make_list ( context , cls ( context ) , objects . Aggregate , <EOL> db_aggregates ) <EOL> @ base . remotable_classmethod <EOL> def get_by_host ( cls , context , host , key = None ) : <EOL> db_aggregates = db . aggregate_get_by_host ( context , host , key = key ) <EOL> return base . obj_make_list ( context , cls ( context ) , objects . Aggregate , <EOL> db_aggregates ) <EOL> @ base . remotable_classmethod <EOL> def get_by_metadata_key ( cls , context , key , hosts = None ) : <EOL> db_aggregates = db . aggregate_get_by_metadata_key ( context , key = key ) <EOL> if hosts is not None : <EOL> db_aggregates = cls . _filter_db_aggregates ( db_aggregates , hosts ) <EOL> return base . obj_make_list ( context , cls ( context ) , objects . Aggregate , <EOL> db_aggregates ) </s>
