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-Id: I6feaf639e198a5af7ce970552ce7633a26629024
Closes-Bug: #1472891
This commit is contained in:
Miguel Grinberg 2015-07-08 23:15:24 -07:00 committed by Mehdi Abaakouk
parent 6dd4a93905
commit c40e790548
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') auth_plugin = pecan.request.environ.get('keystone.token_auth')
for actions in (self.ok_actions, self.alarm_actions, for actions in (self.ok_actions, self.alarm_actions,
self.insufficient_data_actions): self.insufficient_data_actions):
for index, action in enumerate(actions[:]): if actions is not None:
url = netutils.urlsplit(action) for index, action in enumerate(actions[:]):
if self._is_trust_url(url): url = netutils.urlsplit(action)
if '@' not in url.netloc: if self._is_trust_url(url):
# We have a trust action without a trust ID, create it if '@' not in url.netloc:
trust_id = keystone_client.create_trust_id( # We have a trust action without a trust ID,
trustor_user_id, trustor_project_id, roles, # create it
auth_plugin) trust_id = keystone_client.create_trust_id(
netloc = '%s:delete@%s' % (trust_id, url.netloc) trustor_user_id, trustor_project_id, roles,
url = list(url) auth_plugin)
url[1] = netloc netloc = '%s:delete@%s' % (trust_id, url.netloc)
actions[index] = urlparse.urlunsplit(url) url = list(url)
url[1] = netloc
actions[index] = urlparse.urlunsplit(url)
if old_alarm: if old_alarm:
for key in ('ok_actions', 'alarm_actions', for key in ('ok_actions', 'alarm_actions',
'insufficient_data_actions'): 'insufficient_data_actions'):

View File

@ -1782,6 +1782,30 @@ class TestAlarms(v2.FunctionalTest,
self.assertEqual(['test://', 'log://'], self.assertEqual(['test://', 'log://'],
alarms[0].alarm_actions) 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): def test_post_alarm_trust(self):
json = { json = {
'name': 'added_alarm_defaults', 'name': 'added_alarm_defaults',