Skip to content

Commit 84ffed9

Browse files
committed
logging to debug. manila parse json body fix
1 parent 5ebc491 commit 84ffed9

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

watcher/cadf_strategy.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
import json
1615
import logging
1716
import re
1817
import six
@@ -122,12 +121,13 @@ def determine_target_type_uri(self, req):
122121
target_type_uri = self._determine_target_type_uri_by_parts(path.split('/'))
123122

124123
except Exception as e:
125-
self.logger.warning("exception while determining the target type URI of '{0} {1}': {2}"
126-
.format(req.method, req.path, str(e)))
124+
self.logger.debug(
125+
"exception while determining the target type URI of '{0} {1}': {2}".format(req.method, req.path, str(e))
126+
)
127127

128128
finally:
129129
if common.is_none_or_unknown(target_type_uri):
130-
self.logger.warning("failed to determine target type URI of '{0} {1}'".format(req.method, req.path))
130+
self.logger.debug("failed to determine target type URI of '{0} {1}'".format(req.method, req.path))
131131
return
132132

133133
return self._add_prefix_target_type_uri(target_type_uri)
@@ -211,16 +211,18 @@ def _cadf_action_from_body(self, json_body):
211211
"""
212212
cadf_action = taxonomy.UNKNOWN
213213
try:
214-
if json_body:
215-
d = json.loads(json_body)
216-
# the 1st key specifies the action type
217-
os_action = next(iter(d))
218-
if os_action:
219-
# add prefix to os_action
220-
cadf_action = self.cadf_os_action_prefix + os_action
221-
return
214+
if isinstance(json_body, str) or isinstance(json_body, unicode):
215+
json_body = common.load_json_dict(json_body)
216+
# the 1st key specifies the action type
217+
os_action = next(iter(json_body))
218+
# avoid empty string '""'
219+
if os_action and len(os_action) > 2:
220+
# add prefix to os_action
221+
cadf_action = self.cadf_os_action_prefix + str(os_action)
222+
return
222223
except Exception as e:
223-
self.logger.error("error while determining action from json body: {0}".format(str(e)))
224+
self.logger.debug("error while determining action from json body: {0}".format(str(e)))
225+
224226
finally:
225227
return cadf_action
226228

@@ -303,7 +305,7 @@ def _determine_target_type_uri_by_regex(self, path):
303305
return new_path
304306

305307
except Exception as e:
306-
self.logger.warning('failed to apply regex {0} to path: {1}: {2}'.format(regex, path, e))
308+
self.logger.debug('failed to apply regex {0} to path: {1}: {2}'.format(regex, path, e))
307309
continue
308310

309311
# return 'None' if path is unchanged or new path
@@ -335,7 +337,7 @@ def _determine_target_type_uri_by_parts(self, path_parts):
335337
target_type_uri.append(part)
336338

337339
except Exception as e:
338-
self.logger.warning("failed to get target_type_uri from request path: %s" % str(e))
340+
self.logger.debug("failed to get target_type_uri from request path: %s" % str(e))
339341
target_type_uri = []
340342
finally:
341343
# we need at least one part
@@ -485,7 +487,7 @@ def determine_target_type_uri(self, req):
485487
target_type_uri.append('object')
486488

487489
except Exception as e:
488-
self.logger.error("error while determining target type URI from request '{0} {1}': {2}"
490+
self.logger.debug("error while determining target type URI from request '{0} {1}': {2}"
489491
.format(req.method, req.path, str(e)))
490492

491493
finally:

watcher/common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
import json
1516
import re
1617
import six
1718

@@ -207,3 +208,17 @@ def endswith_version(string):
207208

208209
def string_to_bool(bool_string):
209210
return bool_string.lower() == 'true'
211+
212+
213+
def load_json_dict(json_body):
214+
"""
215+
converts a json body to a dictionary
216+
217+
:param json_body: the json body of a request might be string, unicode or dict
218+
:return: the json body as a dictionary
219+
"""
220+
if isinstance(json_body, str) or isinstance(json_body, unicode):
221+
json_body = json.loads(json_body)
222+
if isinstance(json_body, dict):
223+
return json_body
224+
return load_json_dict(json_body)

watcher/tests/test_manila.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ def test_cadf_action(self):
8888
),
8989
'expected': 'update/unmanage'
9090
},
91+
{
92+
'request': fake.create_request(
93+
path='/v2/b206a1900310484f8a9504754c84b067/shares/b206a1900310484f8a9504754c84b067/action',
94+
body_dict='{ "access_list": null }'
95+
),
96+
'expected': 'update/access_list'
97+
},
98+
{
99+
'request': fake.create_request(
100+
path='/v2/b206a1900310484f8a9504754c84b067/shares/b206a1900310484f8a9504754c84b067/action',
101+
body_dict=u'{ "access_list": null }'
102+
),
103+
'expected': 'update/access_list'
104+
},
91105
]
92106

93107
for stim in stimuli:

watcher/watcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, app, config, logger=logging.getLogger(__name__)):
5959
try:
6060
self.watcher_config = load_config(config_file_path)
6161
except errors.ConfigError as e:
62-
self.logger.warning("custom actions not available: %s", str(e))
62+
self.logger.debug("custom actions not available: %s", str(e))
6363

6464
custom_action_config = self.watcher_config.get('custom_actions', {})
6565
path_keywords = self.watcher_config.get('path_keywords', {})
@@ -204,7 +204,7 @@ def _start_response_wrapper(status, headers, exc_info=None):
204204
self.metric_client.timing('api_requests_duration_seconds', time.time() - start, tags=labels)
205205
self.metric_client.increment('api_requests_total', tags=labels)
206206
except Exception as e:
207-
self.logger.info("failed to submit metrics for %s: %s" % (str(labels), str(e)))
207+
self.logger.debug("failed to submit metrics for %s: %s" % (str(labels), str(e)))
208208
finally:
209209
self.metric_client.close_buffer()
210210

@@ -340,7 +340,7 @@ def get_project_domain_and_user_id_from_keystone_authentication_request(self, re
340340
domain_id = common.find_domain_id_in_auth_dict(json_body_dict)
341341
user_id = common.find_user_id_in_auth_dict(json_body_dict)
342342
except ValueError as e:
343-
self.logger.warning('unable to read request body: ', str(e))
343+
self.logger.debug('unable to read request body: ', str(e))
344344
finally:
345345
return project_id, domain_id, user_id
346346

0 commit comments

Comments
 (0)