Allow to set matching_metadata with the cli
This change allows to set the matching_metadata of a alarm like this:
ceilometer alarm-create --matching-metadata 'key=value' \
--matching-metadata 'key2=value2' --name 'alarm' ...
Fixes bug #1201877
Change-Id: I22bf261b0a9580a06ae107ed45d082171f21fcc4
This commit is contained in:
committed by
Mehdi Abaakouk
parent
3010ebcc75
commit
04cc271da2
@@ -127,6 +127,19 @@ def import_versioned_module(version, submodule=None):
|
||||
return importutils.import_module(module)
|
||||
|
||||
|
||||
def args_array_to_dict(kwargs, key_to_convert):
|
||||
values_to_convert = kwargs.get(key_to_convert)
|
||||
if values_to_convert:
|
||||
try:
|
||||
kwargs[key_to_convert] = dict(v.split("=", 1)
|
||||
for v in values_to_convert)
|
||||
except ValueError:
|
||||
raise exc.CommandError(
|
||||
'%s must be a list of key=value not "%s"' % (
|
||||
key_to_convert, values_to_convert))
|
||||
return kwargs
|
||||
|
||||
|
||||
def exit(msg=''):
|
||||
if msg:
|
||||
print >> sys.stderr, msg
|
||||
|
||||
@@ -45,3 +45,15 @@ class UtilsTest(test_utils.BaseTestCase):
|
||||
| Key | Value |
|
||||
+----------+-------+
|
||||
''')
|
||||
|
||||
def test_args_array_to_dict(self):
|
||||
my_args = {
|
||||
'matching_metadata': ['metadata.key=metadata_value'],
|
||||
'other': 'value'
|
||||
}
|
||||
cleaned_dict = utils.args_array_to_dict(my_args,
|
||||
"matching_metadata")
|
||||
self.assertEqual(cleaned_dict, {
|
||||
'matching_metadata': {'metadata.key': 'metadata_value'},
|
||||
'other': 'value'
|
||||
})
|
||||
|
||||
@@ -147,7 +147,8 @@ def _display_alarm(alarm):
|
||||
fields = ['name', 'description', 'counter_name', 'period',
|
||||
'evaluation_periods', 'threshold', 'comparison_operator',
|
||||
'state', 'enabled', 'alarm_id', 'user_id', 'project_id',
|
||||
'alarm_actions', 'ok_actions', 'insufficient_data_actions']
|
||||
'alarm_actions', 'ok_actions', 'insufficient_data_actions',
|
||||
'matching_metadata']
|
||||
data = dict([(f, getattr(alarm, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
@@ -204,9 +205,14 @@ def do_alarm_show(cc, args={}):
|
||||
metavar='<Webhook URL>', action='append', default=None,
|
||||
help=('URL to invoke when state transitions to unkown. '
|
||||
'May be used multiple times.'))
|
||||
@utils.arg('--matching-metadata', dest='matching_metadata',
|
||||
metavar='<Matching Metadata>', action='append', default=None,
|
||||
help=('A meter should match this resource metadata (key=value)'
|
||||
'additionnal to the counter_name'))
|
||||
def do_alarm_create(cc, args={}):
|
||||
'''Create a new alarm.'''
|
||||
fields = dict(filter(lambda x: not (x[1] is None), vars(args).items()))
|
||||
fields = utils.args_array_to_dict(fields, "matching_metadata")
|
||||
alarm = cc.alarms.create(**fields)
|
||||
_display_alarm(alarm)
|
||||
|
||||
@@ -243,9 +249,14 @@ def do_alarm_create(cc, args={}):
|
||||
metavar='<Webhook URL>', action='append', default=None,
|
||||
help=('URL to invoke when state transitions to unkown. '
|
||||
'May be used multiple times.'))
|
||||
@utils.arg('--matching-metadata', dest='matching_metadata',
|
||||
metavar='<Matching Metadata>', action='append', default=None,
|
||||
help=('A meter should match this resource metadata (key=value)'
|
||||
'additionnal to the counter_name'))
|
||||
def do_alarm_update(cc, args={}):
|
||||
'''Update an existing alarm.'''
|
||||
fields = dict(filter(lambda x: not (x[1] is None), vars(args).items()))
|
||||
fields = utils.args_array_to_dict(fields, "matching_metadata")
|
||||
fields.pop('alarm_id')
|
||||
alarm = cc.alarms.update(args.alarm_id, **fields)
|
||||
_display_alarm(alarm)
|
||||
|
||||
Reference in New Issue
Block a user