Completed
Push — master ( 4e35fb...c22065 )
by Manas
03:42
created

BaseRestControllerMixin._parse_query_params()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
from st2common.rbac.utils import request_user_is_admin
23
24
__all__ = [
25
    'BaseRestControllerMixin',
26
    'SHOW_SECRETS_QUERY_PARAM'
27
]
28
29
30
SHOW_SECRETS_QUERY_PARAM = 'show_secrets'
31
32
33
class BaseRestControllerMixin(RestController):
34
    """
35
    Base REST controller class which contains various utility functions.
36
    """
37
38
    def _parse_query_params(self, request):
39
        """
40
        Parse query string for the provided request.
41
42
        :rtype: ``dict``
43
        """
44
        query_string = request.query_string
45
        query_params = dict(urlparse.parse_qsl(query_string))
46
47
        return query_params
48
49
    def _get_query_param_value(self, request, param_name, param_type, default_value=None):
50
        """
51
        Return a value for the provided query param and optionally cast it for boolean types.
52
53
        If the requested query parameter is not provided, default value is returned instead.
54
55
        :param request: Request object.
56
57
        :param param_name: Name of the param to retrieve the value for.
58
        :type param_name: ``str``
59
60
        :param param_type: Type of the query param (e.g. "bool").
61
        :type param_type: ``str``
62
63
        :param default_value: Value to return if query param is not provided.
64
        :type default_value: ``object``
65
        """
66
        query_params = self._parse_query_params(request=request)
67
        value = query_params.get(param_name, default_value)
68
69
        if param_type == 'bool' and isinstance(value, six.string_types):
70
            value = transform_to_bool(value)
71
72
        return value
73
74
    def _get_mask_secrets(self, request):
75
        """
76
        Return a value for mask_secrets which can be used in masking secret properties
77
        to be retruned by any API. The default value is as per the config however admin
78
        users have the ability to override by passing in a special query parameter
79
        ?show_secrets=True.
80
81
        :param request: Request object.
82
83
        :rtype: ``bool``
84
        """
85
        mask_secrets = cfg.CONF.api.mask_secrets
86
        show_secrets = self._get_query_param_value(request=request,
87
                                                   param_name=SHOW_SECRETS_QUERY_PARAM,
88
                                                   param_type='bool',
89
                                                   default_value=False)
90
91
        if show_secrets and request_user_is_admin(request=request):
92
            mask_secrets = False
93
94
        return mask_secrets
95