Completed
Pull Request — master (#2333)
by Arma
08:08
created

st2api.controllers.v1.MistralValidationController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 0 %
Metric Value
wmc 2
dl 0
loc 11
rs 10
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import six
17
18
from st2common import log as logging
19
from st2api.controllers import resource
20
from st2common.models.api.base import jsexpose
21
from st2common.models.api.rule_enforcement import RuleEnforcementAPI
22
from st2common.persistence.rule_enforcement import RuleEnforcement
23
from st2common.rbac.decorators import request_user_has_permission
24
from st2common.rbac.decorators import request_user_has_resource_db_permission
25
from st2common.rbac.types import PermissionType
26
from st2common.util import isotime
27
28
29
http_client = six.moves.http_client
30
31
LOG = logging.getLogger(__name__)
32
33
34
SUPPORTED_FILTERS = {
35
    'rule_ref': 'rule.ref',
36
    'rule_id': 'rule.id',
37
    'execution': 'execution_id',
38
    'trigger_instance': 'trigger_instance_id',
39
    'enforced_at': 'enforced_at',
40
    'enforced_at_gt': 'enforced_at.gt',
41
    'enforced_at_lt': 'enforced_at.lt'
42
}
43
44
45
class RuleEnforcementController(resource.ResourceController):
46
47
    model = RuleEnforcementAPI
48
    access = RuleEnforcement
49
50
    # ResourceController attributes
51
    query_options = {
52
        'sort': ['-enforced_at', 'rule.ref']
53
    }
54
55
    supported_filters = SUPPORTED_FILTERS
56
    filter_transform_functions = {
57
        'enforced_at': lambda value: isotime.parse(value=value),
58
        'enforced_at_gt': lambda value: isotime.parse(value=value),
59
        'enforced_at_lt': lambda value: isotime.parse(value=value)
60
    }
61
62
    @request_user_has_permission(permission_type=PermissionType.RULE_ENFORCEMENT_LIST)
63
    @jsexpose()
64
    def get_all(self, **kwargs):
65
        return super(RuleEnforcementController, self)._get_all(**kwargs)
66
67
    @request_user_has_resource_db_permission(permission_type=PermissionType.RULE_ENFORCEMENT_VIEW)
68
    @jsexpose(arg_types=[str])
69
    def get_one(self, ref_or_id):
70
        return super(RuleEnforcementController, self)._get_one(ref_or_id)
71