diff options
author | ZhiQiang Fan <zhiqiang.fan@huawei.com> | 2015-04-01 22:22:07 +0800 |
---|---|---|
committer | ZhiQiang Fan <zhiqiang.fan@huawei.com> | 2015-04-02 18:43:25 +0800 |
commit | 16880fcaebb9a950c7ad7ca17ff87b000645f2ac (patch) | |
tree | b5d4cae1e62a8be5b12c30fb7b8c785c2be94b4d | |
parent | c40d67cf97d9e4bfdde4e6d3ee28a31d61c8cafe (diff) | |
download | python-ceilometerclient-16880fcaebb9a950c7ad7ca17ff87b000645f2ac.tar.gz |
print user friendly error message for alarm update time constraints
Currently, if we update an alarm with wrong time constraint which
doesn't get name defined, then the shell only prints a very simple
string 'name'.
This is because our code assume name field has always been specified,
however it is not true, then KeyError exception will be raised but not
handled well, finally user only gets an implicit message.
This patch uses dict.get() for name field, and sends request (may be broken)
to ceilometer api, then extracts error message from response.
Change-Id: I086c4ec790acc22767ba7f5e43dbcf73f3af5dff
Closes-Bug: #1439207
-rw-r--r-- | ceilometerclient/tests/unit/v2/test_alarms.py | 17 | ||||
-rw-r--r-- | ceilometerclient/v2/alarms.py | 5 |
2 files changed, 20 insertions, 2 deletions
diff --git a/ceilometerclient/tests/unit/v2/test_alarms.py b/ceilometerclient/tests/unit/v2/test_alarms.py index 4239d2f..9d3b75e 100644 --- a/ceilometerclient/tests/unit/v2/test_alarms.py +++ b/ceilometerclient/tests/unit/v2/test_alarms.py @@ -529,6 +529,23 @@ class AlarmTimeConstraintTest(testtools.TestCase): ] self.http_client.assert_called(*expect) + def test_update_time_constraint_no_name(self): + updated_constraint = { + 'start': '0 23 * * *', + 'duration': 500 + } + kwargs = dict(time_constraints=[updated_constraint]) + self.mgr.update(alarm_id='alarm-id', **kwargs) + body = copy.deepcopy(AN_ALARM) + body[u'time_constraints'].append({ + 'start': '0 23 * * *', + 'duration': 500, + }) + expect = [ + 'PUT', '/v2/alarms/alarm-id', body + ] + self.http_client.assert_called(*expect) + def test_remove(self): kwargs = dict(remove_time_constraints=['cons2']) self.mgr.update(alarm_id='alarm-id', **kwargs) diff --git a/ceilometerclient/v2/alarms.py b/ceilometerclient/v2/alarms.py index 18cdbb0..a54183a 100644 --- a/ceilometerclient/v2/alarms.py +++ b/ceilometerclient/v2/alarms.py @@ -135,14 +135,15 @@ class AlarmManager(base.Manager): updated_tcs = [dict(tc) for tc in existing_tcs] for tc in new_tcs: for i, old_tc in enumerate(updated_tcs): - if old_tc['name'] == tc['name']: # if names match, merge + # if names match, merge + if old_tc['name'] == tc.get('name'): utils.merge_nested_dict(updated_tcs[i], tc) break else: updated_tcs.append(tc) tcs_to_remove = kwargs.get('remove_time_constraints', []) for tc in updated_tcs: - if tc['name'] in tcs_to_remove: + if tc.get('name') in tcs_to_remove: updated_tcs.remove(tc) return updated_tcs |