<s> import uuid <EOL> import webob <EOL> from rack . api . v1 . views import securitygroups as views_securitygroups <EOL> from rack . api import wsgi <EOL> from rack import db <EOL> from rack import exception <EOL> from rack . openstack . common . gettextutils import _ <EOL> from rack . openstack . common import log as logging <EOL> from rack . openstack . common import strutils <EOL> from rack . openstack . common import uuidutils <EOL> from rack . resourceoperator import manager <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Controller ( wsgi . Controller ) : <EOL> """<STR_LIT>""" <EOL> _view_builder_class = views_securitygroups . ViewBuilder <EOL> def __init__ ( self ) : <EOL> super ( Controller , self ) . __init__ ( ) <EOL> self . manager = manager . ResourceOperator ( ) <EOL> def _uuid_check ( self , gid = None , securitygroup_id = None ) : <EOL> if gid : <EOL> if not uuidutils . is_uuid_like ( gid ) : <EOL> raise exception . GroupNotFound ( gid = gid ) <EOL> if securitygroup_id : <EOL> if not uuidutils . is_uuid_like ( securitygroup_id ) : <EOL> raise exception . SecuritygroupNotFound ( <EOL> securitygroup_id = securitygroup_id ) <EOL> @ wsgi . response ( <NUM_LIT:200> ) <EOL> def index ( self , req , gid ) : <EOL> try : <EOL> self . _uuid_check ( gid ) <EOL> except exception . SecuritygroupNotFound : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPNotFound ( explanation = msg ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> securitygroup_list = db . securitygroup_get_all ( context , gid ) <EOL> securitygroup_list = self . manager . securitygroup_list ( <EOL> context , securitygroup_list ) <EOL> return self . _view_builder . index ( securitygroup_list ) <EOL> @ wsgi . response ( <NUM_LIT:200> ) <EOL> def show ( self , req , gid , securitygroup_id ) : <EOL> try : <EOL> self . _uuid_check ( gid , securitygroup_id ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> securitygroup = db . securitygroup_get_by_securitygroup_id ( <EOL> context , gid , securitygroup_id ) <EOL> securitygroup = self . manager . securitygroup_show ( <EOL> context , securitygroup ) <EOL> except exception . NotFound as exc : <EOL> raise webob . exc . HTTPNotFound ( explanation = exc . format_message ( ) ) <EOL> return self . _view_builder . show ( securitygroup ) <EOL> @ wsgi . response ( <NUM_LIT> ) <EOL> def create ( self , req , body , gid ) : <EOL> def _validate ( context , body , gid ) : <EOL> if not self . is_valid_body ( body , '<STR_LIT>' ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> self . _uuid_check ( gid ) <EOL> db . group_get_by_gid ( context , gid ) <EOL> values = body [ "<STR_LIT>" ] <EOL> name = values . get ( "<STR_LIT:name>" ) <EOL> is_default = values . get ( "<STR_LIT>" ) <EOL> if is_default : <EOL> try : <EOL> is_default = strutils . bool_from_string ( <EOL> is_default , strict = True ) <EOL> except ValueError : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> else : <EOL> is_default = False <EOL> valid_values = { } <EOL> valid_values [ "<STR_LIT>" ] = gid <EOL> valid_values [ "<STR_LIT>" ] = name <EOL> valid_values [ "<STR_LIT>" ] = is_default <EOL> rules = values . get ( "<STR_LIT>" ) <EOL> valid_rules = [ ] <EOL> if rules is not None : <EOL> if not isinstance ( rules , list ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> for rule in rules : <EOL> valid_rule = { } <EOL> valid_rule [ "<STR_LIT>" ] = rule . get ( "<STR_LIT>" ) <EOL> valid_rule [ "<STR_LIT>" ] = rule . get ( "<STR_LIT>" ) <EOL> valid_rule [ "<STR_LIT>" ] = rule . get ( "<STR_LIT>" ) <EOL> valid_rule [ "<STR_LIT>" ] = rule . get ( <EOL> "<STR_LIT>" ) <EOL> remote_securitygroup_id = rule . get ( <EOL> "<STR_LIT>" ) <EOL> if remote_securitygroup_id : <EOL> ref = db . securitygroup_get_by_securitygroup_id ( <EOL> context , gid , <EOL> remote_securitygroup_id ) <EOL> valid_rule [ '<STR_LIT>' ] = ref [ '<STR_LIT>' ] <EOL> valid_rules . append ( valid_rule ) <EOL> return valid_values , valid_rules <EOL> try : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> values , rules = _validate ( context , body , gid ) <EOL> values [ "<STR_LIT>" ] = unicode ( uuid . uuid4 ( ) ) <EOL> if not values [ "<STR_LIT>" ] : <EOL> values [ "<STR_LIT>" ] = "<STR_LIT>" + values [ "<STR_LIT>" ] <EOL> result_value = self . manager . securitygroup_create ( <EOL> context , values [ "<STR_LIT>" ] , rules ) <EOL> values . update ( result_value ) <EOL> values [ "<STR_LIT>" ] = context . user_id <EOL> values [ "<STR_LIT>" ] = context . project_id <EOL> securitygroup = db . securitygroup_create ( context , values ) <EOL> except exception . InvalidInput as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> except exception . GroupNotFound as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> return self . _view_builder . create ( securitygroup ) <EOL> @ wsgi . response ( <NUM_LIT:200> ) <EOL> def update ( self , req , body , gid , securitygroup_id ) : <EOL> def _validate ( body , gid , securitygroup_id ) : <EOL> if not self . is_valid_body ( body , '<STR_LIT>' ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> self . _uuid_check ( gid , securitygroup_id ) <EOL> values = body [ "<STR_LIT>" ] <EOL> is_default = values . get ( "<STR_LIT>" ) <EOL> if is_default is not None : <EOL> try : <EOL> is_default = strutils . bool_from_string ( <EOL> is_default , strict = True ) <EOL> except ValueError : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> valid_values = { } <EOL> valid_values [ "<STR_LIT>" ] = is_default <EOL> return valid_values <EOL> try : <EOL> values = _validate ( body , gid , securitygroup_id ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> securitygroup = db . securitygroup_update ( <EOL> context , gid , securitygroup_id , values ) <EOL> except exception . InvalidInput as exc : <EOL> raise webob . exc . HTTPBadRequest ( explanation = exc . format_message ( ) ) <EOL> except exception . NotFound as exc : <EOL> raise webob . exc . HTTPNotFound ( explanation = exc . format_message ( ) ) <EOL> return self . _view_builder . update ( securitygroup ) <EOL> @ wsgi . response ( <NUM_LIT> ) <EOL> def delete ( self , req , gid , securitygroup_id ) : <EOL> try : <EOL> self . _uuid_check ( gid , securitygroup_id ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> securitygroup = db . securitygroup_get_by_securitygroup_id ( <EOL> context , gid , securitygroup_id ) <EOL> if securitygroup [ "<STR_LIT>" ] : <EOL> raise exception . SecuritygroupInUse ( <EOL> securitygroup_id = securitygroup_id ) <EOL> self . manager . securitygroup_delete ( <EOL> context , securitygroup [ '<STR_LIT>' ] ) <EOL> db . securitygroup_delete ( context , gid , securitygroup_id ) <EOL> except exception . SecuritygroupInUse as exc : <EOL> raise webob . exc . HTTPConflict ( explanation = exc . format_message ( ) ) <EOL> except exception . NotFound as exc : <EOL> raise webob . exc . HTTPNotFound ( explanation = exc . format_message ( ) ) <EOL> def create_resource ( ) : <EOL> return wsgi . Resource ( Controller ( ) ) </s>
