Completed
Pull Request — master (#2754)
by Manas
11:04 queued 05:47
created

BaseRestControllerMixin   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 62
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
B _get_query_param_value() 0 24 3
A _parse_query_params() 0 10 1
A _get_mask_secrets() 0 21 3
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
from pecan.rest import RestController
18
from oslo_config import cfg
19
from six.moves.urllib import parse as urlparse  # pylint: disable=import-error
20
21
from st2api.controllers.controller_transforms import transform_to_bool
22
23
__all__ = [
24
    'BaseRestControllerMixin'
25
]
26
27
28
SHOW_SECRETS_QUERY_PARAM = 'show_secrets'
29
30
31
class BaseRestControllerMixin(RestController):
32
    """
33
    Base REST controller class which contains various utility functions.
34
    """
35
36
    def _parse_query_params(self, request):
37
        """
38
        Parse query string for the provided request.
39
40
        :rtype: ``dict``
41
        """
42
        query_string = request.query_string
43
        query_params = dict(urlparse.parse_qsl(query_string))
44
45
        return query_params
46
47
    def _get_query_param_value(self, request, param_name, param_type, default_value=None):
48
        """
49
        Return a value for the provided query param and optionally cast it for boolean types.
50
51
        If the requested query parameter is not provided, default value is returned instead.
52
53
        :param request: Request object.
54
55
        :param param_name: Name of the param to retrieve the value for.
56
        :type param_name: ``str``
57
58
        :param param_type: Type of the query param (e.g. "bool").
59
        :type param_type: ``str``
60
61
        :param default_value: Value to return if query param is not provided.
62
        :type default_value: ``object``
63
        """
64
        query_params = self._parse_query_params(request=request)
65
        value = query_params.get(param_name, default_value)
66
67
        if param_type == 'bool' and isinstance(value, six.string_types):
68
            value = transform_to_bool(value)
69
70
        return value
71
72
    def _get_mask_secrets(self, request):
73
        """
74
        Return a value for mask_secrets which can be used in masking secret properties
75
        to be retruned by any API. The default value is as per the config however admin
76
        users have the ability to override by passing in a special query parameter
77
        show_secrets.
78
79
        :param request: Request object.
80
81
        :rtype: ``bool``
82
        """
83
        mask_secrets = cfg.CONF.api.mask_secrets
84
        show_secrets_param = self._get_query_param_value(request=request,
85
                                                   param_name=SHOW_SECRETS_QUERY_PARAM,
86
                                                   param_type='bool',
87
                                                   default_value=False)
88
89
        if show_secrets and request_user_is_admin(request=request):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'show_secrets'
Loading history...
Comprehensibility Best Practice introduced by
Undefined variable 'request_user_is_admin'
Loading history...
90
            mask_secrets = False
91
92
        return mask_secrets
93