diff options
-rw-r--r-- | ceilometerclient/tests/unit/v2/test_samples.py | 18 | ||||
-rw-r--r-- | ceilometerclient/tests/unit/v2/test_shell.py | 54 | ||||
-rw-r--r-- | ceilometerclient/v2/samples.py | 20 | ||||
-rw-r--r-- | ceilometerclient/v2/shell.py | 13 |
4 files changed, 105 insertions, 0 deletions
diff --git a/ceilometerclient/tests/unit/v2/test_samples.py b/ceilometerclient/tests/unit/v2/test_samples.py index 1d42fb4..9c79cd5 100644 --- a/ceilometerclient/tests/unit/v2/test_samples.py +++ b/ceilometerclient/tests/unit/v2/test_samples.py @@ -35,6 +35,8 @@ GET_OLD_SAMPLE = {u'counter_name': u'instance', CREATE_SAMPLE = copy.deepcopy(GET_OLD_SAMPLE) del CREATE_SAMPLE['message_id'] del CREATE_SAMPLE['source'] +CREATE_LIST_SAMPLE = copy.deepcopy(CREATE_SAMPLE) +CREATE_LIST_SAMPLE['counter_name'] = 'image' GET_SAMPLE = { "user_id": None, @@ -52,6 +54,7 @@ GET_SAMPLE = { } METER_URL = '/v2/meters/instance' +SECOND_METER_URL = '/v2/meters/image' SAMPLE_URL = '/v2/samples' QUERIES = ('q.field=resource_id&q.field=source&q.op=&q.op=' '&q.type=&q.type=&q.value=foo&q.value=bar') @@ -68,6 +71,12 @@ OLD_SAMPLE_FIXTURES = { [CREATE_SAMPLE], ), }, + SECOND_METER_URL: { + 'POST': ( + {}, + [CREATE_LIST_SAMPLE] * 10, + ), + }, '%s?%s' % (METER_URL, QUERIES): { 'GET': ( {}, @@ -147,6 +156,15 @@ class OldSampleManagerTest(utils.BaseTestCase): self.http_client.assert_called(*expect, body=[CREATE_SAMPLE]) self.assertIsNotNone(sample) + def test_create_list(self): + test_samples = [CREATE_LIST_SAMPLE] * 10 + samples = self.mgr.create_list(test_samples) + expect = [ + 'POST', '/v2/meters/image' + ] + self.http_client.assert_called(*expect, body=test_samples) + self.assertEqual(10, len(samples)) + def test_limit(self): samples = list(self.mgr.list(meter_name='instance', limit=1)) expect = ['GET', '/v2/meters/instance?limit=1'] diff --git a/ceilometerclient/tests/unit/v2/test_shell.py b/ceilometerclient/tests/unit/v2/test_shell.py index f568ebd..663cf24 100644 --- a/ceilometerclient/tests/unit/v2/test_shell.py +++ b/ceilometerclient/tests/unit/v2/test_shell.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import json import re import sys @@ -576,6 +577,59 @@ class ShellSampleCreateCommandTest(utils.BaseTestCase): ''', sys.stdout.getvalue()) +class ShellSampleCreateListCommandTest(utils.BaseTestCase): + + SAMPLE = { + u'counter_name': u'image', + u'user_id': u'21b442b8101d407d8242b6610e0ed0eb', + u'resource_id': u'0564c64c-3545-4e34-abfb-9d18e5f2f2f9', + u'timestamp': u'2015-05-19T12:00:08.368574', + u'source': u'384260c6987b451d8290e66e1f108082: openstack', + u'counter_unit': u'image', + u'counter_volume': 1.0, + u'project_id': u'384260c6987b451d8290e66e1f108082', + u'resource_metadata': {}, + u'counter_type': u'cumulative' + } + + def setUp(self): + super(ShellSampleCreateListCommandTest, self).setUp() + self.cc = mock.Mock() + self.cc.samples = mock.Mock() + self.cc.samples.create_list = mock.Mock() + self.args = mock.Mock() + self.samples = [self.SAMPLE] * 5 + self.args.samples_list = json.dumps(self.samples) + + @mock.patch('sys.stdout', new=six.StringIO()) + def test_sample_create_list(self): + ret_samples = [samples.OldSample(mock.Mock(), + sample) for sample in self.samples] + self.cc.samples.create_list.return_value = ret_samples + ceilometer_shell.do_sample_create_list(self.cc, self.args) + self.cc.samples.create_list.assert_called_with(self.samples) + self.assertEqual('''\ ++--------------------------------------+-------+------------+--------+-------\ ++----------------------------+ +| Resource ID | Name | Type | Volume | Unit \ +| Timestamp | ++--------------------------------------+-------+------------+--------+-------\ ++----------------------------+ +| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \ +| 2015-05-19T12:00:08.368574 | +| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \ +| 2015-05-19T12:00:08.368574 | +| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \ +| 2015-05-19T12:00:08.368574 | +| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \ +| 2015-05-19T12:00:08.368574 | +| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \ +| 2015-05-19T12:00:08.368574 | ++--------------------------------------+-------+------------+--------+-------\ ++----------------------------+ +''', sys.stdout.getvalue()) + + class ShellQuerySamplesCommandTest(utils.BaseTestCase): SAMPLE = [{u'id': u'b55d1526-9929-11e3-a3f6-02163e5df1e6', diff --git a/ceilometerclient/v2/samples.py b/ceilometerclient/v2/samples.py index c046051..17aa10b 100644 --- a/ceilometerclient/v2/samples.py +++ b/ceilometerclient/v2/samples.py @@ -56,6 +56,26 @@ class OldSampleManager(base.Manager): if body: return [OldSample(self, b) for b in body] + def create_list(self, sample_list=None, **kwargs): + sample_dict = {} + + for sample_body in sample_list: + sample = dict((key, value) for (key, value) in sample_body.items() + if key in CREATION_ATTRIBUTES) + sample_dict.setdefault( + sample_body["counter_name"], [] + ).append(sample) + + sample_return_list = [] + + for (counter_name, sample_body) in sample_dict.items(): + url = self._path(counter_name=counter_name) + body = self.api.post(url, json=sample_body).json() + + if body: + sample_return_list.extend([OldSample(self, b) for b in body]) + return sample_return_list + class Sample(base.Resource): """Represents API v2 Sample object. diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index 224e189..b67d765 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -258,6 +258,19 @@ def do_meter_list(cc, args={}): sortby=0) +@utils.arg('samples_list', metavar='<SAMPLES_LIST>', action=NotEmptyAction, + help='Json array with samples to create.') +def do_sample_create_list(cc, args={}): + """Create a sample list.""" + sample_list_array = json.loads(args.samples_list) + samples = cc.samples.create_list(sample_list_array) + 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 _display_alarm_list(alarms, sortby=None): # omit action initially to keep output width sane # (can switch over to vertical formatting when available from CLIFF) |