diff --git a/ceilometerclient/shell.py b/ceilometerclient/shell.py index 57cb604..474c59a 100644 --- a/ceilometerclient/shell.py +++ b/ceilometerclient/shell.py @@ -227,7 +227,7 @@ class CeilometerShell(object): else: logging.basicConfig(format=format, level=logging.WARN) - def main(self, argv): + def parse_args(self, argv): # Parse args once to find version parser = self.get_base_parser() (options, args) = parser.parse_known_args(argv) @@ -244,8 +244,14 @@ class CeilometerShell(object): self.do_help(options) return 0 - # Parse args again and call whatever callback was selected - args = subcommand_parser.parse_args(argv) + # Return parsed args + return api_version, subcommand_parser.parse_args(argv) + + def main(self, argv): + parsed = self.parse_args(argv) + if parsed == 0: + return 0 + api_version, args = parsed # Short-circuit and deal with help command right away. if args.func == self.do_help: @@ -278,6 +284,7 @@ class CeilometerShell(object): client = ceiloclient.get_client(api_version, **(args.__dict__)) + # call whatever callback was selected try: args.func(client, args) except exc.Unauthorized: diff --git a/ceilometerclient/tests/v2/test_shell.py b/ceilometerclient/tests/v2/test_shell.py index 55f7270..71b26e5 100644 --- a/ceilometerclient/tests/v2/test_shell.py +++ b/ceilometerclient/tests/v2/test_shell.py @@ -17,6 +17,7 @@ import sys from testtools import matchers +from ceilometerclient import shell as base_shell from ceilometerclient.tests import utils from ceilometerclient.v2 import alarms from ceilometerclient.v2 import samples @@ -232,6 +233,49 @@ class ShellAlarmCommandTest(utils.BaseTestCase): method = ceilometer_shell.do_alarm_threshold_update self._do_test_alarm_update_repeat_actions(method, False) + def test_alarm_threshold_create_args(self): + shell = base_shell.CeilometerShell() + argv = ['alarm-threshold-create', + '--name', 'cpu_high', + '--description', 'instance running hot', + '--meter-name', 'cpu_util', + '--threshold', '70.0', + '--comparison-operator', 'gt', + '--statistic', 'avg', + '--period', '600', + '--evaluation-periods', '3', + '--alarm-action', 'log://', + '--alarm-action', 'http://example.com/alarm/state', + '--query', 'resource_id=INSTANCE_ID'] + _, args = shell.parse_args(argv) + + orig = sys.stdout + sys.stdout = six.StringIO() + alarm = alarms.Alarm(mock.Mock(), self.ALARM) + self.cc.alarms.create.return_value = alarm + + try: + ceilometer_shell.do_alarm_threshold_create(self.cc, args) + _, kwargs = self.cc.alarms.create.call_args + self.assertEqual('cpu_high', kwargs.get('name')) + self.assertEqual('instance running hot', kwargs.get('description')) + actions = ['log://', 'http://example.com/alarm/state'] + self.assertEqual(actions, kwargs.get('alarm_actions')) + self.assertTrue('threshold_rule' in kwargs) + rule = kwargs['threshold_rule'] + self.assertEqual('cpu_util', rule.get('meter_name')) + self.assertEqual(70.0, rule.get('threshold')) + self.assertEqual('gt', rule.get('comparison_operator')) + self.assertEqual('avg', rule.get('statistic')) + self.assertEqual(600, rule.get('period')) + self.assertEqual(3, rule.get('evaluation_periods')) + query = dict(field='resource_id', type='', + value='INSTANCE_ID', op='eq') + self.assertEqual([query], rule['query']) + finally: + sys.stdout.close() + sys.stdout = orig + class ShellSampleListCommandTest(utils.BaseTestCase): diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index 6d861aa..8486135 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -330,6 +330,7 @@ def do_alarm_create(cc, args={}): dest='threshold_rule/threshold', help='Threshold to evaluate against') @utils.arg('-q', '--query', metavar='', + dest='threshold_rule/query', help='key[op]data_type::value; list. data_type is optional, ' 'but if supplied must be string, integer, float, or boolean') @utils.arg('--repeat-actions', dest='repeat_actions',