def do_resource_list(cc, args={}): '''List the resources.''' resources = cc.resources.list(q=options.cli_to_array(args.query)) field_labels = ['Resource ID', 'Source', 'User ID', 'Project ID'] fields = ['resource_id', 'source', 'user_id', 'project_id'] utils.print_list(resources, fields, field_labels, sortby=1)
def test_typed_string_whitespace(self): ar = options.cli_to_array('state=string::insufficient data') self.assertEqual([{ 'field': 'state', 'op': 'eq', 'type': 'string', 'value': 'insufficient data' }], ar)
def test_one(self): ar = options.cli_to_array('this<=34') self.assertEqual(ar, [{ 'field': 'this', 'op': 'le', 'value': '34', 'type': '' }])
def test_with_dot(self): ar = options.cli_to_array('metadata.this<=34') self.assertEqual(ar, [{ 'field': 'metadata.this', 'op': 'le', 'value': '34', 'type': '' }])
def test_with_bool_data_type(self): ar = options.cli_to_array('port=boolean::true') self.assertEqual(ar, [{ 'field': 'port', 'op': 'eq', 'type': 'boolean', 'value': 'true' }])
def test_with_string_data_type(self): ar = options.cli_to_array('hostname=string::localhost') self.assertEqual(ar, [{ 'field': 'hostname', 'op': 'eq', 'type': 'string', 'value': 'localhost' }])
def test_with_float_data_type(self): ar = options.cli_to_array('average=float::1234.5678') self.assertEqual(ar, [{ 'field': 'average', 'op': 'eq', 'type': 'float', 'value': '1234.5678' }])
def test_with_int_data_type(self): ar = options.cli_to_array('port=integer::1234') self.assertEqual(ar, [{ 'field': 'port', 'op': 'eq', 'type': 'integer', 'value': '1234' }])
def test_with_single_colon(self): ar = options.cli_to_array('timestamp=datetime:sometimestamp') self.assertEqual(ar, [{ 'field': 'timestamp', 'op': 'eq', 'type': '', 'value': 'datetime:sometimestamp' }])
def test_with_incorrect_type(self): ar = options.cli_to_array('timestamp=invalid::sometimestamp') self.assertEqual(ar, [{ 'field': 'timestamp', 'op': 'eq', 'type': '', 'value': 'invalid::sometimestamp' }])
def test_negative(self): ar = options.cli_to_array('this>=-783') self.assertEqual(ar, [{ 'field': 'this', 'op': 'ge', 'value': '-783', 'type': '' }])
def do_meter_list(cc, args={}): '''List the user's meters.''' meters = cc.meters.list(q=options.cli_to_array(args.query)) field_labels = [ 'Name', 'Type', 'Unit', 'Resource ID', 'User ID', 'Project ID' ] fields = ['name', 'type', 'unit', 'resource_id', 'user_id', 'project_id'] utils.print_list(meters, fields, field_labels, sortby=0)
def test_special_character(self): ar = options.cli_to_array('key~123=value!123') self.assertEqual([{ 'field': 'key~123', 'op': 'eq', 'value': 'value!123', 'type': '' }], ar)
def _do_test_typed_float_op(self, op, op_str): ar = options.cli_to_array('that%sfloat::283.347' % op) self.assertEqual([{ 'field': 'that', 'type': 'float', 'value': '283.347', 'op': op_str }], ar)
def test_comma(self): ar = options.cli_to_array('this=2.4,fooo=doof') self.assertEqual([{ 'field': 'this', 'op': 'eq', 'value': '2.4,fooo=doof', 'type': '' }], ar)
def do_resource_list(cc, args={}): '''List the resources.''' resources = cc.resources.list(q=options.cli_to_array(args.query)) field_labels = ['Resource ID', 'Source', 'User ID', 'Project ID'] fields = ['resource_id', 'source', 'user_id', 'project_id'] utils.print_list(resources, fields, field_labels, sortby=1)
def test_with_datetime_data_type(self): ar = options.cli_to_array('timestamp=datetime::sometimestamp') self.assertEqual(ar, [{ 'field': 'timestamp', 'op': 'eq', 'type': 'datetime', 'value': 'sometimestamp' }])
def test_float(self): ar = options.cli_to_array('this<=283.347') self.assertEqual(ar, [{ 'field': 'this', 'op': 'le', 'value': '283.347', 'type': '' }])
def do_meter_list(cc, args={}): '''List the user's meters.''' meters = cc.meters.list(q=options.cli_to_array(args.query)) field_labels = ['Name', 'Type', 'Unit', 'Resource ID', 'User ID', 'Project ID'] fields = ['name', 'type', 'unit', 'resource_id', 'user_id', 'project_id'] utils.print_list(meters, fields, field_labels, sortby=0)
def do_event_list(cc, args={}): '''List events.''' events = cc.events.list(q=options.cli_to_array(args.query)) field_labels = ['Message ID', 'Event Type', 'Generated', 'Traits'] fields = ['message_id', 'event_type', 'generated', 'traits'] utils.print_list(events, fields, field_labels, formatters={ 'traits': utils.nested_list_of_dict_formatter( 'traits', ['name', 'type', 'value'] )})
def do_alarm_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'] = 'threshold' if 'query' in fields['threshold_rule']: fields['threshold_rule']['query'] = options.cli_to_array( fields['threshold_rule']['query']) alarm = cc.alarms.create(**fields) _display_alarm(alarm)
def do_alarm_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'] = 'threshold' if 'query' in fields['threshold_rule']: fields['threshold_rule']['query'] = options.cli_to_array( fields['threshold_rule']['query']) alarm = cc.alarms.create(**fields) _display_alarm(alarm)
def do_alarm_list(cc, args={}): '''List the user's alarms.''' alarms = cc.alarms.list(q=options.cli_to_array(args.query)) # omit action initially to keep output width sane # (can switch over to vertical formatting when available from CLIFF) field_labels = ['Alarm ID', 'Name', 'State', 'Enabled', 'Continuous', 'Alarm condition', 'Time constraints'] fields = ['alarm_id', 'name', 'state', 'enabled', 'repeat_actions', 'rule', 'time_constraints'] utils.print_list( alarms, fields, field_labels, formatters={'rule': alarm_rule_formatter, 'time_constraints': time_constraints_formatter}, sortby=0)
def test_two(self): ar = options.cli_to_array('this<=34;that!=foo') self.assertEqual(ar, [{ 'field': 'this', 'op': 'le', 'value': '34', 'type': '' }, { 'field': 'that', 'op': 'ne', 'value': 'foo', 'type': '' }])
def do_alarm_history(cc, args={}): '''Display the change history of an alarm.''' kwargs = dict(alarm_id=args.alarm_id, q=options.cli_to_array(args.query)) try: history = cc.alarms.get_history(**kwargs) except exc.HTTPNotFound: raise exc.CommandError('Alarm not found: %s' % args.alarm_id) field_labels = ['Type', 'Timestamp', 'Detail'] fields = ['type', 'timestamp', 'detail'] utils.print_list(history, fields, field_labels, formatters={'detail': alarm_change_detail_formatter}, sortby=1)
def do_event_list(cc, args={}): '''List events.''' events = cc.events.list(q=options.cli_to_array(args.query)) field_labels = ['Message ID', 'Event Type', 'Generated', 'Traits'] fields = ['message_id', 'event_type', 'generated', 'traits'] utils.print_list(events, fields, field_labels, formatters={ 'traits': utils.nested_list_of_dict_formatter( 'traits', ['name', 'type', 'value']) })
def test_typed_string_whitespace_complex(self): ar = options.cli_to_array( 'that>=float::99.9999;state=string::insufficient data') self.assertEqual([{ 'field': 'that', 'op': 'ge', 'type': 'float', 'value': '99.9999' }, { 'field': 'state', 'op': 'eq', 'type': 'string', 'value': 'insufficient data' }], ar)
def do_alarm_history(cc, args={}): '''Display the change history of an alarm.''' kwargs = dict(alarm_id=args.alarm_id, q=options.cli_to_array(args.query)) try: history = cc.alarms.get_history(**kwargs) except exc.HTTPNotFound: raise exc.CommandError('Alarm not found: %s' % args.alarm_id) field_labels = ['Type', 'Timestamp', 'Detail'] fields = ['type', 'timestamp', 'detail'] utils.print_list(history, fields, field_labels, formatters={'detail': alarm_change_detail_formatter}, sortby=1)
def test_timestamp_value(self): ar = options.cli_to_array( 'project=cow;timestamp>=datetime::2014-03-11T16:02:58') self.assertEqual([{ 'field': 'project', 'op': 'eq', 'type': '', 'value': 'cow' }, { 'field': 'timestamp', 'op': 'ge', 'type': 'datetime', 'value': '2014-03-11T16:02:58' }], ar)
def do_alarm_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'] = 'threshold' if 'threshold_rule' in fields and 'query' in fields['threshold_rule']: fields['threshold_rule']['query'] = options.cli_to_array( fields['threshold_rule']['query']) 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)
def do_alarm_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'] = 'threshold' if 'threshold_rule' in fields and 'query' in fields['threshold_rule']: fields['threshold_rule']['query'] = options.cli_to_array( fields['threshold_rule']['query']) 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)
def do_sample_list(cc, args): '''List the samples for a meter.''' fields = {'meter_name': args.meter, 'q': options.cli_to_array(args.query), 'limit': args.limit} try: samples = cc.samples.list(**fields) except exc.HTTPNotFound: raise exc.CommandError('Samples not found: %s' % args.meter) else: field_labels = ['Resource ID', 'Name', 'Type', 'Volume', 'Unit', 'Timestamp'] fields = ['resource_id', 'counter_name', 'counter_type', 'counter_volume', 'counter_unit', 'timestamp'] utils.print_list(samples, fields, field_labels, sortby=None)
def test_single_char_field_or_value(self): ar = options.cli_to_array('m<=34;large.thing>s;x!=y') self.assertEqual([{ 'field': 'm', 'op': 'le', 'value': '34', 'type': '' }, { 'field': 'large.thing', 'op': 'gt', 'value': 's', 'type': '' }, { 'field': 'x', 'op': 'ne', 'value': 'y', 'type': '' }], ar)
def do_statistics(cc, args): '''List the statistics for a meter.''' aggregates = [] for a in args.aggregate: aggregates.append(dict(zip(('func', 'param'), a.split("<-")))) api_args = { 'meter_name': args.meter, 'q': options.cli_to_array(args.query), 'period': args.period, 'groupby': args.groupby, 'aggregates': aggregates } try: statistics = cc.statistics.list(**api_args) except exc.HTTPNotFound: raise exc.CommandError('Samples not found: %s' % args.meter) else: fields_display = { 'duration': 'Duration', 'duration_end': 'Duration End', 'duration_start': 'Duration Start', 'period': 'Period', 'period_end': 'Period End', 'period_start': 'Period Start', 'groupby': 'Group By' } fields_display.update(AGGREGATES) fields = ['period', 'period_start', 'period_end'] if args.groupby: fields.append('groupby') if args.aggregate: for a in aggregates: if 'param' in a: fields.append("%(func)s/%(param)s" % a) else: fields.append(a['func']) for stat in statistics: stat.__dict__.update(stat.aggregate) else: fields.extend(['max', 'min', 'avg', 'sum', 'count']) fields.extend(['duration', 'duration_start', 'duration_end']) cols = [fields_display.get(f, f) for f in fields] utils.print_list(statistics, fields, cols)
def do_sample_list(cc, args): '''List the samples for a meter.''' fields = { 'meter_name': args.meter, 'q': options.cli_to_array(args.query), 'limit': args.limit } try: samples = cc.samples.list(**fields) except exc.HTTPNotFound: raise exc.CommandError('Samples not found: %s' % args.meter) else: field_labels = [ 'Resource ID', 'Name', 'Type', 'Volume', 'Unit', 'Timestamp' ] fields = [ 'resource_id', 'counter_name', 'counter_type', 'counter_volume', 'counter_unit', 'timestamp' ] utils.print_list(samples, fields, field_labels, sortby=None)
def do_alarm_list(cc, args={}): '''List the user's alarms.''' alarms = cc.alarms.list(q=options.cli_to_array(args.query)) # omit action initially to keep output width sane # (can switch over to vertical formatting when available from CLIFF) field_labels = [ 'Alarm ID', 'Name', 'State', 'Enabled', 'Continuous', 'Alarm condition', 'Time constraints' ] fields = [ 'alarm_id', 'name', 'state', 'enabled', 'repeat_actions', 'rule', 'time_constraints' ] utils.print_list(alarms, fields, field_labels, formatters={ 'rule': alarm_rule_formatter, 'time_constraints': time_constraints_formatter }, sortby=0)
def do_statistics(cc, args): '''List the statistics for a meter.''' aggregates = [] for a in args.aggregate: aggregates.append(dict(zip(('func', 'param'), a.split("<-")))) api_args = {'meter_name': args.meter, 'q': options.cli_to_array(args.query), 'period': args.period, 'groupby': args.groupby, 'aggregates': aggregates} try: statistics = cc.statistics.list(**api_args) except exc.HTTPNotFound: raise exc.CommandError('Samples not found: %s' % args.meter) else: fields_display = {'duration': 'Duration', 'duration_end': 'Duration End', 'duration_start': 'Duration Start', 'period': 'Period', 'period_end': 'Period End', 'period_start': 'Period Start', 'groupby': 'Group By'} fields_display.update(AGGREGATES) fields = ['period', 'period_start', 'period_end'] if args.groupby: fields.append('groupby') if args.aggregate: for a in aggregates: if 'param' in a: fields.append("%(func)s/%(param)s" % a) else: fields.append(a['func']) for stat in statistics: stat.__dict__.update(stat.aggregate) else: fields.extend(['max', 'min', 'avg', 'sum', 'count']) fields.extend(['duration', 'duration_start', 'duration_end']) cols = [fields_display.get(f, f) for f in fields] utils.print_list(statistics, fields, cols)