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
This commit is contained in:
ZhiQiang Fan
2015-04-01 22:22:07 +08:00
parent c40d67cf97
commit 16880fcaeb
2 changed files with 20 additions and 2 deletions

View File

@@ -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)

View File

@@ -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