<s> """<STR_LIT>""" <EOL> import datetime <EOL> from webob import exc <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack . compute . schemas import aggregates <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . api import validation <EOL> from nova . compute import api as compute_api <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> ALIAS = "<STR_LIT>" <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> def _get_context ( req ) : <EOL> return req . environ [ '<STR_LIT>' ] <EOL> class AggregateController ( wsgi . Controller ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . api = compute_api . AggregateAPI ( ) <EOL> @ extensions . expected_errors ( ( ) ) <EOL> def index ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT:index>' ) <EOL> aggregates = self . api . get_aggregate_list ( context ) <EOL> return { '<STR_LIT>' : [ self . _marshall_aggregate ( a ) [ '<STR_LIT>' ] <EOL> for a in aggregates ] } <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( aggregates . create_v20 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ validation . schema ( aggregates . create , '<STR_LIT>' ) <EOL> def create ( self , req , body ) : <EOL> """<STR_LIT>""" <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> host_aggregate = body [ "<STR_LIT>" ] <EOL> name = common . normalize_name ( host_aggregate [ "<STR_LIT:name>" ] ) <EOL> avail_zone = host_aggregate . get ( "<STR_LIT>" ) <EOL> if avail_zone : <EOL> avail_zone = common . normalize_name ( avail_zone ) <EOL> try : <EOL> aggregate = self . api . create_aggregate ( context , name , avail_zone ) <EOL> except exception . AggregateNameExists as e : <EOL> raise exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except exception . InvalidAggregateAction as e : <EOL> raise exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> agg = self . _marshall_aggregate ( aggregate ) <EOL> del agg [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> del agg [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return agg <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def show ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> try : <EOL> aggregate = self . api . get_aggregate ( context , id ) <EOL> except exception . AggregateNotFound as e : <EOL> raise exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> return self . _marshall_aggregate ( aggregate ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( aggregates . update_v20 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ validation . schema ( aggregates . update , '<STR_LIT>' ) <EOL> def update ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> updates = body [ "<STR_LIT>" ] <EOL> if '<STR_LIT:name>' in updates : <EOL> updates [ '<STR_LIT:name>' ] = common . normalize_name ( updates [ '<STR_LIT:name>' ] ) <EOL> try : <EOL> aggregate = self . api . update_aggregate ( context , id , updates ) <EOL> except exception . AggregateNameExists as e : <EOL> raise exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except exception . AggregateNotFound as e : <EOL> raise exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InvalidAggregateAction as e : <EOL> raise exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> return self . _marshall_aggregate ( aggregate ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> def delete ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> try : <EOL> self . api . delete_aggregate ( context , id ) <EOL> except exception . AggregateNotFound as e : <EOL> raise exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InvalidAggregateAction as e : <EOL> raise exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( aggregates . add_host ) <EOL> def _add_host ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> host = body [ '<STR_LIT>' ] [ '<STR_LIT:host>' ] <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> try : <EOL> aggregate = self . api . add_host_to_aggregate ( context , id , host ) <EOL> except ( exception . AggregateNotFound , <EOL> exception . ComputeHostNotFound ) as e : <EOL> raise exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except ( exception . AggregateHostExists , <EOL> exception . InvalidAggregateAction ) as e : <EOL> raise exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> return self . _marshall_aggregate ( aggregate ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( aggregates . remove_host ) <EOL> def _remove_host ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> host = body [ '<STR_LIT>' ] [ '<STR_LIT:host>' ] <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> try : <EOL> aggregate = self . api . remove_host_from_aggregate ( context , id , host ) <EOL> except ( exception . AggregateNotFound , exception . AggregateHostNotFound , <EOL> exception . ComputeHostNotFound ) : <EOL> msg = _ ( '<STR_LIT>' ) % { <EOL> '<STR_LIT:host>' : host , '<STR_LIT:id>' : id } <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> except exception . InvalidAggregateAction : <EOL> msg = _ ( '<STR_LIT>' ) % { <EOL> '<STR_LIT:host>' : host , '<STR_LIT:id>' : id } <EOL> raise exc . HTTPConflict ( explanation = msg ) <EOL> return self . _marshall_aggregate ( aggregate ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( aggregates . set_metadata ) <EOL> def _set_metadata ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = _get_context ( req ) <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> metadata = body [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> try : <EOL> aggregate = self . api . update_aggregate_metadata ( context , <EOL> id , metadata ) <EOL> except exception . AggregateNotFound as e : <EOL> raise exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InvalidAggregateAction as e : <EOL> raise exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> return self . _marshall_aggregate ( aggregate ) <EOL> def _marshall_aggregate ( self , aggregate ) : <EOL> _aggregate = { } <EOL> for key , value in self . _build_aggregate_items ( aggregate ) : <EOL> if isinstance ( value , datetime . datetime ) : <EOL> value = value . replace ( tzinfo = None ) <EOL> _aggregate [ key ] = value <EOL> return { "<STR_LIT>" : _aggregate } <EOL> def _build_aggregate_items ( self , aggregate ) : <EOL> keys = aggregate . obj_fields <EOL> for key in keys : <EOL> if ( ( aggregate . obj_attr_is_set ( key ) <EOL> or key in aggregate . obj_extra_fields ) and <EOL> key != '<STR_LIT>' ) : <EOL> yield key , getattr ( aggregate , key ) <EOL> class Aggregates ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> resources = [ extensions . ResourceExtension ( <EOL> ALIAS , <EOL> AggregateController ( ) , <EOL> member_actions = { '<STR_LIT:action>' : '<STR_LIT:POST>' } ) ] <EOL> return resources <EOL> def get_controller_extensions ( self ) : <EOL> return [ ] </s>
