<s> """<STR_LIT>""" <EOL> from oslo_log import log as logging <EOL> import webob <EOL> from webob import exc <EOL> from nova . api . openstack import api_version_request <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack . compute . schemas import server_groups as schema <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . api import validation <EOL> import nova . exception <EOL> from nova . i18n import _ <EOL> from nova . i18n import _LE <EOL> from nova import objects <EOL> LOG = logging . getLogger ( __name__ ) <EOL> ALIAS = "<STR_LIT>" <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> def _authorize_context ( req ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> return context <EOL> class ServerGroupController ( wsgi . Controller ) : <EOL> """<STR_LIT>""" <EOL> def _format_server_group ( self , context , group , req ) : <EOL> server_group = { } <EOL> server_group [ '<STR_LIT:id>' ] = group . uuid <EOL> server_group [ '<STR_LIT:name>' ] = group . name <EOL> server_group [ '<STR_LIT>' ] = group . policies or [ ] <EOL> server_group [ '<STR_LIT>' ] = { } <EOL> members = [ ] <EOL> if group . members : <EOL> filters = { '<STR_LIT>' : group . members , '<STR_LIT>' : False } <EOL> instances = objects . InstanceList . get_by_filters ( <EOL> context , filters = filters ) <EOL> members = [ instance . uuid for instance in instances ] <EOL> server_group [ '<STR_LIT>' ] = members <EOL> if api_version_request . is_supported ( req , min_version = "<STR_LIT>" ) : <EOL> server_group [ '<STR_LIT>' ] = group . project_id <EOL> server_group [ '<STR_LIT>' ] = group . user_id <EOL> return server_group <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def show ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = _authorize_context ( req ) <EOL> try : <EOL> sg = objects . InstanceGroup . get_by_uuid ( context , id ) <EOL> except nova . exception . InstanceGroupNotFound as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> return { '<STR_LIT>' : self . _format_server_group ( context , sg , req ) } <EOL> @ wsgi . response ( <NUM_LIT> ) <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def delete ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = _authorize_context ( req ) <EOL> try : <EOL> sg = objects . InstanceGroup . get_by_uuid ( context , id ) <EOL> except nova . exception . InstanceGroupNotFound as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> quotas = objects . Quotas ( context = context ) <EOL> project_id , user_id = objects . quotas . ids_from_server_group ( context , sg ) <EOL> try : <EOL> quotas . reserve ( project_id = project_id , <EOL> user_id = user_id , server_groups = - <NUM_LIT:1> ) <EOL> except Exception : <EOL> quotas = None <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> try : <EOL> sg . destroy ( ) <EOL> except nova . exception . InstanceGroupNotFound as e : <EOL> if quotas : <EOL> quotas . rollback ( ) <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> if quotas : <EOL> quotas . commit ( ) <EOL> @ extensions . expected_errors ( ( ) ) <EOL> def index ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = _authorize_context ( req ) <EOL> project_id = context . project_id <EOL> if '<STR_LIT>' in req . GET and context . is_admin : <EOL> sgs = objects . InstanceGroupList . get_all ( context ) <EOL> else : <EOL> sgs = objects . InstanceGroupList . get_by_project_id ( <EOL> context , project_id ) <EOL> limited_list = common . limited ( sgs . objects , req ) <EOL> result = [ self . _format_server_group ( context , group , req ) <EOL> for group in limited_list ] <EOL> return { '<STR_LIT>' : result } <EOL> @ wsgi . Controller . api_version ( "<STR_LIT>" ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( schema . create , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ validation . schema ( schema . create_v215 , "<STR_LIT>" ) <EOL> def create ( self , req , body ) : <EOL> """<STR_LIT>""" <EOL> context = _authorize_context ( req ) <EOL> quotas = objects . Quotas ( context = context ) <EOL> try : <EOL> quotas . reserve ( project_id = context . project_id , <EOL> user_id = context . user_id , server_groups = <NUM_LIT:1> ) <EOL> except nova . exception . OverQuota : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPForbidden ( explanation = msg ) <EOL> vals = body [ '<STR_LIT>' ] <EOL> sg = objects . InstanceGroup ( context ) <EOL> sg . project_id = context . project_id <EOL> sg . user_id = context . user_id <EOL> try : <EOL> sg . name = vals . get ( '<STR_LIT:name>' ) <EOL> sg . policies = vals . get ( '<STR_LIT>' ) <EOL> sg . create ( ) <EOL> except ValueError as e : <EOL> quotas . rollback ( ) <EOL> raise exc . HTTPBadRequest ( explanation = e ) <EOL> quotas . commit ( ) <EOL> return { '<STR_LIT>' : self . _format_server_group ( context , sg , req ) } <EOL> class ServerGroups ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> res = extensions . ResourceExtension ( <EOL> ALIAS , controller = ServerGroupController ( ) , <EOL> member_actions = { "<STR_LIT:action>" : "<STR_LIT:POST>" , } ) <EOL> return [ res ] <EOL> def get_controller_extensions ( self ) : <EOL> return [ ] </s>
