<s> import six <EOL> import jsonschema <EOL> import pecan <EOL> from pecan import abort <EOL> from mongoengine import ValidationError <EOL> from st2common import log as logging <EOL> from st2common . exceptions . apivalidation import ValueValidationException <EOL> from st2common . exceptions . triggers import TriggerDoesNotExistException <EOL> from st2api . controllers import resource <EOL> from st2api . controllers . v1 . ruleviews import RuleViewController <EOL> from st2common . models . api . rule import RuleAPI <EOL> from st2common . models . api . base import jsexpose <EOL> from st2common . persistence . rule import Rule <EOL> from st2common . rbac . types import PermissionType <EOL> from st2common . rbac . decorators import request_user_has_permission <EOL> from st2common . rbac . decorators import request_user_has_resource_api_permission <EOL> from st2common . rbac . decorators import request_user_has_resource_db_permission <EOL> from st2common . rbac . utils import assert_request_user_has_rule_trigger_and_action_permission <EOL> from st2common . services . triggers import cleanup_trigger_db_for_rule , increment_trigger_ref_count <EOL> http_client = six . moves . http_client <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class RuleController ( resource . ContentPackResourceController ) : <EOL> """<STR_LIT>""" <EOL> views = RuleViewController ( ) <EOL> model = RuleAPI <EOL> access = Rule <EOL> supported_filters = { <EOL> '<STR_LIT:name>' : '<STR_LIT:name>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> query_options = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:name>' ] <EOL> } <EOL> include_reference = True <EOL> @ request_user_has_permission ( permission_type = PermissionType . RULE_LIST ) <EOL> @ jsexpose ( ) <EOL> def get_all ( self , ** kwargs ) : <EOL> return super ( RuleController , self ) . _get_all ( ** kwargs ) <EOL> @ request_user_has_resource_db_permission ( permission_type = PermissionType . RULE_VIEW ) <EOL> @ jsexpose ( arg_types = [ str ] ) <EOL> def get_one ( self , ref_or_id ) : <EOL> return super ( RuleController , self ) . _get_one ( ref_or_id ) <EOL> @ jsexpose ( body_cls = RuleAPI , status_code = http_client . CREATED ) <EOL> @ request_user_has_resource_api_permission ( permission_type = PermissionType . RULE_CREATE ) <EOL> def post ( self , rule ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> rule_db = RuleAPI . to_model ( rule ) <EOL> LOG . debug ( '<STR_LIT>' , rule_db ) <EOL> assert_request_user_has_rule_trigger_and_action_permission ( request = pecan . request , <EOL> rule_api = rule ) <EOL> rule_db = Rule . add_or_update ( rule_db ) <EOL> increment_trigger_ref_count ( rule_api = rule ) <EOL> except ( ValidationError , ValueError ) as e : <EOL> LOG . exception ( '<STR_LIT>' , rule ) <EOL> abort ( http_client . BAD_REQUEST , str ( e ) ) <EOL> return <EOL> except ( ValueValidationException , jsonschema . ValidationError ) as e : <EOL> LOG . exception ( '<STR_LIT>' , rule ) <EOL> abort ( http_client . BAD_REQUEST , str ( e ) ) <EOL> return <EOL> except TriggerDoesNotExistException as e : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( rule . trigger [ '<STR_LIT:type>' ] ) ) <EOL> LOG . exception ( msg ) <EOL> abort ( http_client . BAD_REQUEST , msg ) <EOL> return <EOL> extra = { '<STR_LIT>' : rule_db } <EOL> LOG . audit ( '<STR_LIT>' % ( rule_db . id ) , extra = extra ) <EOL> rule_api = RuleAPI . from_model ( rule_db ) <EOL> return rule_api <EOL> @ request_user_has_resource_db_permission ( permission_type = PermissionType . RULE_MODIFY ) <EOL> @ jsexpose ( arg_types = [ str ] , body_cls = RuleAPI ) <EOL> def put ( self , rule_ref_or_id , rule ) : <EOL> rule_db = self . _get_by_ref_or_id ( rule_ref_or_id ) <EOL> LOG . debug ( '<STR_LIT>' , rule_ref_or_id , rule_db ) <EOL> try : <EOL> if rule . id is not None and rule . id is not '<STR_LIT>' and rule . id != rule_ref_or_id : <EOL> LOG . warning ( '<STR_LIT>' , <EOL> rule . id , rule_ref_or_id ) <EOL> old_rule_db = rule_db <EOL> rule_db = RuleAPI . to_model ( rule ) <EOL> assert_request_user_has_rule_trigger_and_action_permission ( request = pecan . request , <EOL> rule_api = rule ) <EOL> rule_db . id = rule_ref_or_id <EOL> rule_db = Rule . add_or_update ( rule_db ) <EOL> increment_trigger_ref_count ( rule_api = rule ) <EOL> except ( ValueValidationException , jsonschema . ValidationError , ValueError ) as e : <EOL> LOG . exception ( '<STR_LIT>' , rule ) <EOL> abort ( http_client . BAD_REQUEST , str ( e ) ) <EOL> return <EOL> cleanup_trigger_db_for_rule ( old_rule_db ) <EOL> extra = { '<STR_LIT>' : old_rule_db , '<STR_LIT>' : rule_db } <EOL> LOG . audit ( '<STR_LIT>' % ( rule_db . id ) , extra = extra ) <EOL> rule_api = RuleAPI . from_model ( rule_db ) <EOL> return rule_api <EOL> @ request_user_has_resource_db_permission ( permission_type = PermissionType . RULE_DELETE ) <EOL> @ jsexpose ( arg_types = [ str ] , status_code = http_client . NO_CONTENT ) <EOL> def delete ( self , rule_ref_or_id ) : <EOL> """<STR_LIT>""" <EOL> rule_db = self . _get_by_ref_or_id ( ref_or_id = rule_ref_or_id ) <EOL> LOG . debug ( '<STR_LIT>' , rule_ref_or_id , rule_db ) <EOL> try : <EOL> Rule . delete ( rule_db ) <EOL> except Exception as e : <EOL> LOG . exception ( '<STR_LIT>' , <EOL> rule_ref_or_id ) <EOL> abort ( http_client . INTERNAL_SERVER_ERROR , str ( e ) ) <EOL> return <EOL> cleanup_trigger_db_for_rule ( rule_db ) <EOL> extra = { '<STR_LIT>' : rule_db } <EOL> LOG . audit ( '<STR_LIT>' % ( rule_db . id ) , extra = extra ) </s>
