Tolerate alarm actions set to None

This change avoids a crash when an alarm action list is set to None by
the client, as Heat does in some of its tests.

Change come from: https://review.openstack.org/#/c/199909/

Change-Id: I267c9da14546cd9650fc24aaedd35fefa6b92a7c
Closes-Bug: #1472891
This commit is contained in:
Mehdi Abaakouk 2015-07-09 11:02:14 +02:00
parent 126dbd021d
commit 1d122e6396
2 changed files with 38 additions and 12 deletions

View File

@ -396,18 +396,20 @@ class Alarm(base.Base):
auth_plugin = pecan.request.environ.get('keystone.token_auth')
for actions in (self.ok_actions, self.alarm_actions,
self.insufficient_data_actions):
for index, action in enumerate(actions[:]):
url = netutils.urlsplit(action)
if self._is_trust_url(url):
if '@' not in url.netloc:
# We have a trust action without a trust ID, create it
trust_id = keystone_client.create_trust_id(
trustor_user_id, trustor_project_id, roles,
auth_plugin)
netloc = '%s:delete@%s' % (trust_id, url.netloc)
url = list(url)
url[1] = netloc
actions[index] = urlparse.urlunsplit(url)
if actions is not None:
for index, action in enumerate(actions[:]):
url = netutils.urlsplit(action)
if self._is_trust_url(url):
if '@' not in url.netloc:
# We have a trust action without a trust ID,
# create it
trust_id = keystone_client.create_trust_id(
trustor_user_id, trustor_project_id, roles,
auth_plugin)
netloc = '%s:delete@%s' % (trust_id, url.netloc)
url = list(url)
url[1] = netloc
actions[index] = urlparse.urlunsplit(url)
if old_alarm:
for key in ('ok_actions', 'alarm_actions',
'insufficient_data_actions'):

View File

@ -1782,6 +1782,30 @@ class TestAlarms(v2.FunctionalTest,
self.assertEqual(['test://', 'log://'],
alarms[0].alarm_actions)
def test_post_alarm_without_actions(self):
body = {
'name': 'alarm_actions_none',
'type': 'combination',
'combination_rule': {
'alarm_ids': ['a', 'b'],
},
'alarm_actions': None
}
headers = self.auth_headers
headers['X-Roles'] = 'admin'
self.post_json('/alarms', params=body, status=201,
headers=headers)
alarms = list(self.alarm_conn.get_alarms(name='alarm_actions_none'))
self.assertEqual(1, len(alarms))
# FIXME(sileht): This should really returns [] not None
# but the mongodb and sql just store the json dict as is...
# migration script for sql will be a mess because we have
# to parse all JSON :(
# I guess we assume that wsme convert the None input to []
# because of the array type, but it won't...
self.assertIsNone(alarms[0].alarm_actions)
def test_post_alarm_trust(self):
json = {
'name': 'added_alarm_defaults',