Merge "Verify alarm found before modifying"
This commit is contained in:
@@ -19,6 +19,7 @@ Base utilities to build API operation managers and objects on top of.
|
|||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
|
from ceilometerclient import exc
|
||||||
from ceilometerclient.openstack.common.apiclient import base
|
from ceilometerclient.openstack.common.apiclient import base
|
||||||
|
|
||||||
# Python 2.4 compat
|
# Python 2.4 compat
|
||||||
@@ -55,7 +56,10 @@ class Manager(object):
|
|||||||
|
|
||||||
def _list(self, url, response_key=None, obj_class=None, body=None,
|
def _list(self, url, response_key=None, obj_class=None, body=None,
|
||||||
expect_single=False):
|
expect_single=False):
|
||||||
body = self.api.get(url).json()
|
resp = self.api.get(url)
|
||||||
|
if not resp.content:
|
||||||
|
raise exc.HTTPNotFound
|
||||||
|
body = resp.json()
|
||||||
|
|
||||||
if obj_class is None:
|
if obj_class is None:
|
||||||
obj_class = self.resource_class
|
obj_class = self.resource_class
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import six
|
|||||||
from six.moves import xrange # noqa
|
from six.moves import xrange # noqa
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
|
from ceilometerclient import exc
|
||||||
from ceilometerclient.openstack.common.apiclient import client
|
from ceilometerclient.openstack.common.apiclient import client
|
||||||
from ceilometerclient.openstack.common.apiclient import fake_client
|
from ceilometerclient.openstack.common.apiclient import fake_client
|
||||||
from ceilometerclient.v2 import alarms
|
from ceilometerclient.v2 import alarms
|
||||||
@@ -207,6 +208,17 @@ fixtures = {
|
|||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
'/v2/alarms/unk-alarm-id':
|
||||||
|
{
|
||||||
|
'GET': (
|
||||||
|
{},
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
'PUT': (
|
||||||
|
{},
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
},
|
||||||
'/v2/alarms/alarm-id/state':
|
'/v2/alarms/alarm-id/state':
|
||||||
{
|
{
|
||||||
'PUT': (
|
'PUT': (
|
||||||
@@ -380,6 +392,14 @@ class AlarmManagerTest(testtools.TestCase):
|
|||||||
self.http_client.assert_called(*expect_get_2, pos=1)
|
self.http_client.assert_called(*expect_get_2, pos=1)
|
||||||
self.assertEqual('alarm', state)
|
self.assertEqual('alarm', state)
|
||||||
|
|
||||||
|
def test_update_missing(self):
|
||||||
|
alarm = None
|
||||||
|
try:
|
||||||
|
alarm = self.mgr.update(alarm_id='unk-alarm-id', **UPDATE_ALARM)
|
||||||
|
except exc.CommandError:
|
||||||
|
pass
|
||||||
|
self.assertEqual(alarm, None)
|
||||||
|
|
||||||
def test_delete_from_alarm_class(self):
|
def test_delete_from_alarm_class(self):
|
||||||
alarm = self.mgr.get(alarm_id='alarm-id')
|
alarm = self.mgr.get(alarm_id='alarm-id')
|
||||||
self.assertIsNotNone(alarm)
|
self.assertIsNotNone(alarm)
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ class AlarmManager(base.Manager):
|
|||||||
return self._list(self._path(alarm_id), expect_single=True)[0]
|
return self._list(self._path(alarm_id), expect_single=True)[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except exc.HTTPNotFound:
|
except exc.HTTPNotFound:
|
||||||
# When we try to get deleted alarm HTTPNotFound occurs
|
# When we try to get deleted alarm HTTPNotFound occurs
|
||||||
# or when alarm doesn't exists this exception don't must
|
# or when alarm doesn't exists this exception don't must
|
||||||
@@ -156,7 +157,10 @@ class AlarmManager(base.Manager):
|
|||||||
|
|
||||||
def update(self, alarm_id, **kwargs):
|
def update(self, alarm_id, **kwargs):
|
||||||
self._compat_legacy_alarm_kwargs(kwargs)
|
self._compat_legacy_alarm_kwargs(kwargs)
|
||||||
updated = self.get(alarm_id).to_dict()
|
alarm = self.get(alarm_id)
|
||||||
|
if alarm is None:
|
||||||
|
raise exc.CommandError('Alarm not found: %s' % alarm_id)
|
||||||
|
updated = alarm.to_dict()
|
||||||
updated['time_constraints'] = self._merge_time_constraints(
|
updated['time_constraints'] = self._merge_time_constraints(
|
||||||
updated.get('time_constraints', []), kwargs)
|
updated.get('time_constraints', []), kwargs)
|
||||||
kwargs = dict((k, v) for k, v in kwargs.items()
|
kwargs = dict((k, v) for k, v in kwargs.items()
|
||||||
|
|||||||
Reference in New Issue
Block a user