Debian Codegraph @

ansible_collections/f5networks/f5_modules/plugins/module_utils/urls.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2017, F5 Networks Inc.
4 # GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9
10 import re
11
12 from .common import F5ModuleError
13
14 _CLEAN_HEADER_REGEX_BYTE = re.compile(b'^\\S[^\\r\\n]*$|^$')
15 _CLEAN_HEADER_REGEX_STR = re.compile(r'^\S[^\r\n]*$|^$')
16
17
18 def check_header_validity(header):
19 """Verifies that header value is a string which doesn't contain
20 leading whitespace or return characters.
21
22 NOTE: This is a slightly modified version of the original function
23 taken from the requests library:
24 http://docs.python-requests.org/en/master/_modules/requests/utils/
25
26 :param header: string containing ':'.
27 """
28 try:
29 name, value = header.split(':')
30 except ValueError:
31 raise F5ModuleError('Invalid header format: {0}'.format(header))
32 if name == '':
33 raise F5ModuleError('Invalid header format: {0}'.format(header))
34
35 if isinstance(value, bytes):
36 pat = _CLEAN_HEADER_REGEX_BYTE
37 else:
38 pat = _CLEAN_HEADER_REGEX_STR
39 try:
40 if not pat.match(value):
41 raise F5ModuleError("Invalid return character or leading space in header: %s" % name)
42 except TypeError:
43 raise F5ModuleError("Value for header {%s: %s} must be of type str or "
44 "bytes, not %s" % (name, value, type(value)))
45
46
47 def build_service_uri(base_uri, partition, name):
48 """Build the proper uri for a service resource.
49 This follows the scheme:
50 <base_uri>/~<partition>~<<name>.app>~<name>
51 :param base_uri: str -- base uri of the REST endpoint
52 :param partition: str -- partition for the service
53 :param name: str -- name of the service
54 :returns: str -- uri to access the service
55 """
56 name = name.replace('/', '~')
57 return '%s~%s~%s.app~%s' % (base_uri, partition, name, name)
58
59
60 def parseStats(entry):
61 if 'description' in entry:
62 return entry['description']
63 elif 'value' in entry:
64 return entry['value']
65 elif 'entries' in entry or 'nestedStats' in entry and 'entries' in entry['nestedStats']:
66 if 'entries' in entry:
67 entries = entry['entries']
68 else:
69 entries = entry['nestedStats']['entries']
70 result = None
71
72 for name in entries:
73 entry = entries[name]
74 if 'https://localhost' in name:
75 name = name.split('/')
76 name = name[-1]
77 if result and isinstance(result, list):
78 result.append(parseStats(entry))
79 elif result and isinstance(result, dict):
80 result[name] = parseStats(entry)
81 else:
82 try:
83 int(name)
84 result = list()
85 result.append(parseStats(entry))
86 except ValueError:
87 result = dict()
88 result[name] = parseStats(entry)
89 else:
90 if '.' in name:
91 names = name.split('.')
92 key = names[0]
93 value = names[1]
94 if result is None:
95 # result can be None if this branch is reached first
96 #
97 # For example, the mgmt/tm/net/trunk/NAME/stats API
98 # returns counters.bitsIn before anything else.
99 result = dict()
100 result[key] = dict()
101 elif key not in result:
102 result[key] = dict()
103 elif result[key] is None:
104 result[key] = dict()
105 result[key][value] = parseStats(entry)
106 else:
107 if result and isinstance(result, list):
108 result.append(parseStats(entry))
109 elif result and isinstance(result, dict):
110 result[name] = parseStats(entry)
111 else:
112 try:
113 int(name)
114 result = list()
115 result.append(parseStats(entry))
116 except ValueError:
117 result = dict()
118 result[name] = parseStats(entry)
119 return result