From acaad55b0a6cf0ad4aba5162d15c59ed407aca47 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Fri, 13 Mar 2015 15:50:52 +0100 Subject: [PATCH] alarm: Use new gnocchi aggregation API Since https://review.openstack.org/#/c/163462/ and https://review.openstack.org/#/c/162649/4 in gnocchi, all kind of aggregations have been moved in the same URI namespace (/v1/aggregation/...) and we can use complex query to make aggregation across multiples metrics. This change sync the ceilometerclient alarm shell to match these changes. Change-Id: I5717a4604af5505dfb8c56463e2bdb86c59e41ce (cherry picked from commit db48aa28b169fa8df01020d54a20e644c9a0b7df) --- ceilometerclient/v2/shell.py | 97 ++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 15 deletions(-) diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index 67a76466..f1ecf02a 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -477,10 +477,11 @@ def common_alarm_gnocchi_arguments(rule_namespace, create=False): return _wrapper -def common_alarm_gnocchi_metrics_arguments(create=False): +def common_alarm_gnocchi_aggregation_by_metrics_arguments(create=False): def _wrapper(func): @utils.arg('-m', '--metrics', metavar='', - dest='gnocchi_metrics_threshold_rule/meter_name', + dest=('gnocchi_aggregation_by_metrics_threshold_rule/' + 'meter_name'), action='append', required=create, help='Metric to evaluate against.') @functools.wraps(func) @@ -490,6 +491,30 @@ def common_alarm_gnocchi_metrics_arguments(create=False): return _wrapper +def common_alarm_gnocchi_aggregation_by_resources_arguments(create=False): + def _wrapper(func): + @utils.arg('-m', '--metric', metavar='', + dest=('gnocchi_aggregation_by_resources_threshold_rule/' + 'metric'), + required=create, + help='Metric to evaluate against.') + @utils.arg('--resource-type', metavar='', + dest=('gnocchi_aggregation_by_resources_threshold_rule/' + 'resource_type'), + required=create, + help='Resource_type to evaluate against.') + @utils.arg('--query', metavar='', + dest=('gnocchi_aggregation_by_resources_threshold_rule/' + 'query'), + required=create, + help=('Gnocchi resources search query filter')) + @functools.wraps(func) + def _wrapped(*args, **kwargs): + return func(*args, **kwargs) + return _wrapped + return _wrapper + + def common_alarm_gnocchi_resources_arguments(create=False): def _wrapper(func): @utils.arg('-m', '--metric', metavar='', @@ -500,11 +525,10 @@ def common_alarm_gnocchi_resources_arguments(create=False): dest='gnocchi_resources_threshold_rule/resource_type', required=create, help='Resource_type to evaluate against.') - @utils.arg('--resource-constraint', metavar='', - dest='gnocchi_resources_threshold_rule/resource_constraint', + @utils.arg('--resource-id', metavar='', + dest='gnocchi_resources_threshold_rule/resource_id', required=create, - help=('Resources to evaluate against or a expression ' - 'to select multiple resources.')) + help=('Resource id to evaluate against')) @functools.wraps(func) def _wrapped(*args, **kwargs): return func(*args, **kwargs) @@ -559,14 +583,29 @@ def do_alarm_gnocchi_resources_threshold_create(cc, args={}): @common_alarm_arguments(create=True) -@common_alarm_gnocchi_arguments('gnocchi_metrics_threshold_rule', create=True) -@common_alarm_gnocchi_metrics_arguments(create=True) -def do_alarm_gnocchi_metrics_threshold_create(cc, args={}): +@common_alarm_gnocchi_arguments( + 'gnocchi_aggregation_by_metrics_threshold_rule', create=True) +@common_alarm_gnocchi_aggregation_by_metrics_arguments(create=True) +def do_alarm_gnocchi_aggregation_by_metrics_threshold_create(cc, args={}): """Create a new alarm based on computed statistics.""" fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) fields = utils.args_array_to_list_of_dicts(fields, 'time_constraints') fields = utils.key_with_slash_to_nested_dict(fields) - fields['type'] = 'gnocchi_metrics_threshold' + fields['type'] = 'gnocchi_aggregation_by_metrics_threshold' + alarm = cc.alarms.create(**fields) + _display_alarm(alarm) + + +@common_alarm_arguments(create=True) +@common_alarm_gnocchi_arguments( + 'gnocchi_aggregation_by_resources_threshold_rule', create=True) +@common_alarm_gnocchi_aggregation_by_resources_arguments(create=True) +def do_alarm_gnocchi_aggregation_by_resources_threshold_create(cc, args={}): + """Create a new alarm based on computed statistics.""" + fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) + fields = utils.args_array_to_list_of_dicts(fields, 'time_constraints') + fields = utils.key_with_slash_to_nested_dict(fields) + fields['type'] = 'gnocchi_aggregation_by_resources_threshold' alarm = cc.alarms.create(**fields) _display_alarm(alarm) @@ -741,7 +780,7 @@ def do_alarm_threshold_update(cc, args={}): @utils.arg('alarm_id', metavar='', nargs='?', action=NotEmptyAction, help='ID of the alarm to update.') @common_alarm_arguments() -@common_alarm_gnocchi_arguments('gnocchi_resources_threshold') +@common_alarm_gnocchi_arguments('gnocchi_resources_threshold_rule') @common_alarm_gnocchi_resources_arguments() @utils.arg('--remove-time-constraint', action='append', metavar='', @@ -767,19 +806,47 @@ def do_alarm_gnocchi_resources_threshold_update(cc, args={}): @utils.arg('alarm_id', metavar='', nargs='?', action=NotEmptyAction, help='ID of the alarm to update.') @common_alarm_arguments() -@common_alarm_gnocchi_arguments('gnocchi_metrics_threshold') -@common_alarm_gnocchi_metrics_arguments() +@common_alarm_gnocchi_arguments( + 'gnocchi_aggregation_by_metrics_threshold_rule') +@common_alarm_gnocchi_aggregation_by_metrics_arguments() @utils.arg('--remove-time-constraint', action='append', metavar='', dest='remove_time_constraints', help='Name or list of names of the time constraints to remove.') -def do_alarm_gnocchi_metrics_threshold_update(cc, args={}): +def do_alarm_gnocchi_aggregation_by_metrics_threshold_update(cc, args={}): """Update an existing alarm based on computed statistics.""" fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) fields = utils.args_array_to_list_of_dicts(fields, 'time_constraints') fields = utils.key_with_slash_to_nested_dict(fields) fields.pop('alarm_id') - fields['type'] = 'gnocchi_metrics_threshold' + fields['type'] = 'gnocchi_aggregation_by_metrics_threshold' + try: + alarm = cc.alarms.update(args.alarm_id, **fields) + except exc.HTTPNotFound: + raise exc.CommandError('Alarm not found: %s' % args.alarm_id) + _display_alarm(alarm) + + +@utils.arg('-a', '--alarm_id', metavar='', + action=obsoleted_by('alarm_id'), help=argparse.SUPPRESS, + dest='alarm_id_deprecated') +@utils.arg('alarm_id', metavar='', nargs='?', + action=NotEmptyAction, help='ID of the alarm to update.') +@common_alarm_arguments() +@common_alarm_gnocchi_arguments( + 'gnocchi_aggregation_by_resources_threshold_rule') +@common_alarm_gnocchi_aggregation_by_resources_arguments() +@utils.arg('--remove-time-constraint', action='append', + metavar='', + dest='remove_time_constraints', + help='Name or list of names of the time constraints to remove.') +def do_alarm_gnocchi_aggregation_by_resources_threshold_update(cc, args={}): + """Update an existing alarm based on computed statistics.""" + fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) + fields = utils.args_array_to_list_of_dicts(fields, 'time_constraints') + fields = utils.key_with_slash_to_nested_dict(fields) + fields.pop('alarm_id') + fields['type'] = 'gnocchi_aggregation_by_resources_threshold' try: alarm = cc.alarms.update(args.alarm_id, **fields) except exc.HTTPNotFound: