Add client support for creating new alarms.
Change-Id: I4e3be2e480803eeaf4ec11079e69e7e6afd5e0d1
This commit is contained in:
@@ -47,6 +47,11 @@ class Manager(object):
|
||||
def __init__(self, api):
|
||||
self.api = api
|
||||
|
||||
def _create(self, url, body):
|
||||
resp, body = self.api.json_request('POST', url, body=body)
|
||||
if body:
|
||||
return self.resource_class(self, body)
|
||||
|
||||
def _list(self, url, response_key=None, obj_class=None, body=None,
|
||||
expect_single=False):
|
||||
resp, body = self.api.json_request('GET', url)
|
||||
@@ -65,15 +70,15 @@ class Manager(object):
|
||||
data = [data]
|
||||
return [obj_class(self, res, loaded=True) for res in data if res]
|
||||
|
||||
def _delete(self, url):
|
||||
self.api.raw_request('DELETE', url)
|
||||
|
||||
def _update(self, url, body, response_key=None):
|
||||
resp, body = self.api.json_request('PUT', url, body=body)
|
||||
# PUT requests may not return a body
|
||||
if body:
|
||||
return self.resource_class(self, body)
|
||||
|
||||
def _delete(self, url):
|
||||
self.api.raw_request('DELETE', url)
|
||||
|
||||
|
||||
class Resource(object):
|
||||
"""A resource represents a particular instance of an object (tenant, user,
|
||||
|
||||
@@ -34,6 +34,7 @@ UPDATABLE_ATTRIBUTES = [
|
||||
'ok_actions',
|
||||
'insufficient_data_actions',
|
||||
]
|
||||
CREATION_ATTRIBUTES = UPDATABLE_ATTRIBUTES + ['name', 'project_id', 'user_id']
|
||||
|
||||
|
||||
class Alarm(base.Resource):
|
||||
@@ -57,6 +58,11 @@ class AlarmManager(base.Manager):
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
def create(self, **kwargs):
|
||||
new = dict((key, value) for (key, value) in kwargs.items()
|
||||
if key in CREATION_ATTRIBUTES)
|
||||
return self._create(self._path(), new)
|
||||
|
||||
def update(self, alarm_id, **kwargs):
|
||||
existing = self.get(alarm_id)
|
||||
updated = existing.to_dict()
|
||||
|
||||
@@ -126,6 +126,51 @@ def do_alarm_show(cc, args={}):
|
||||
_display_alarm(alarm)
|
||||
|
||||
|
||||
@utils.arg('--name', metavar='<NAME>',
|
||||
help='Name of the alarm (must be unique per tenant)')
|
||||
@utils.arg('--project-id', metavar='<PROJECT_ID>',
|
||||
help='Tenant to associate with alarm '
|
||||
'(only settable by admin users)')
|
||||
@utils.arg('--user-id', metavar='<USER_ID>',
|
||||
help='User to associate with alarm '
|
||||
'(only settable by admin users)')
|
||||
@utils.arg('--description', metavar='<DESCRIPTION>',
|
||||
help='Free text description of the alarm')
|
||||
@utils.arg('--period', type=int, metavar='<PERIOD>',
|
||||
help='Length of each period (seconds) to evaluate over')
|
||||
@utils.arg('--evaluation-periods', type=int, metavar='<COUNT>',
|
||||
help='Number of periods to evaluate over')
|
||||
@utils.arg('--state', metavar='<STATE>',
|
||||
help='State of the alarm, one of: ' + str(ALARM_STATES))
|
||||
@utils.arg('--enabled', type=utils.string_to_bool, metavar='{True|False}',
|
||||
help='True if alarm evaluation/actioning is enabled')
|
||||
@utils.arg('--counter-name', metavar='<METRIC>',
|
||||
help='Metric to evaluate against')
|
||||
@utils.arg('--statistic', metavar='<STATISTIC>',
|
||||
help='Statistic to evaluate, one of: ' + str(STATISTICS))
|
||||
@utils.arg('--comparison-operator', metavar='<OPERATOR>',
|
||||
help='Operator to compare with, one of: ' + str(ALARM_OPERATORS))
|
||||
@utils.arg('--threshold', type=float, metavar='<THRESHOLD>',
|
||||
help='Threshold to evaluate against')
|
||||
@utils.arg('--alarm-action', dest='alarm_actions',
|
||||
metavar='<Webhook URL>', action='append', default=None,
|
||||
help=('URL to invoke when state transitions to alarm. '
|
||||
'May be used multiple times.'))
|
||||
@utils.arg('--ok-action', dest='ok_actions',
|
||||
metavar='<Webhook URL>', action='append', default=None,
|
||||
help=('URL to invoke when state transitions to OK. '
|
||||
'May be used multiple times.'))
|
||||
@utils.arg('--insufficient-data-action', dest='insufficient_data_actions',
|
||||
metavar='<Webhook URL>', action='append', default=None,
|
||||
help=('URL to invoke when state transitions to unkown. '
|
||||
'May be used multiple times.'))
|
||||
def do_alarm_create(cc, args={}):
|
||||
'''Create a new alarm.'''
|
||||
fields = dict(filter(lambda x: not (x[1] is None), vars(args).items()))
|
||||
alarm = cc.alarms.create(**fields)
|
||||
_display_alarm(alarm)
|
||||
|
||||
|
||||
@utils.arg('-a', '--alarm_id', metavar='<ALARM_ID>',
|
||||
help='ID of the alarm to update.')
|
||||
@utils.arg('--description', metavar='<DESCRIPTION>',
|
||||
|
||||
@@ -42,6 +42,11 @@ AN_ALARM = {u'alarm_actions': [u'http://site:8000/alarm'],
|
||||
u'state_timestamp': u'2013-05-09T13:41:23.085000',
|
||||
u'comparison_operator': 'gt',
|
||||
u'name': 'SwiftObjectAlarm'}
|
||||
CREATE_ALARM = copy.deepcopy(AN_ALARM)
|
||||
del CREATE_ALARM['timestamp']
|
||||
del CREATE_ALARM['state_timestamp']
|
||||
del CREATE_ALARM['matching_metadata']
|
||||
del CREATE_ALARM['alarm_id']
|
||||
DELTA_ALARM = {u'alarm_actions': ['url1', 'url2'],
|
||||
u'comparison_operator': u'lt',
|
||||
u'threshold': 42.1}
|
||||
@@ -55,6 +60,10 @@ fixtures = {
|
||||
{},
|
||||
[AN_ALARM],
|
||||
),
|
||||
'POST': (
|
||||
{},
|
||||
CREATE_ALARM,
|
||||
),
|
||||
},
|
||||
'/v2/alarms/alarm-id':
|
||||
{
|
||||
@@ -126,6 +135,14 @@ class AlarmManagerTest(unittest.TestCase):
|
||||
self.assertTrue(alarm)
|
||||
self.assertEqual(alarm.alarm_id, 'alarm-id')
|
||||
|
||||
def test_create(self):
|
||||
alarm = self.mgr.create(**CREATE_ALARM)
|
||||
expect = [
|
||||
('POST', '/v2/alarms', {}, CREATE_ALARM),
|
||||
]
|
||||
self.assertEqual(self.api.calls, expect)
|
||||
self.assertTrue(alarm)
|
||||
|
||||
def test_update(self):
|
||||
alarm = self.mgr.update(alarm_id='alarm-id', **DELTA_ALARM)
|
||||
expect = [
|
||||
|
||||
Reference in New Issue
Block a user