From b9f1b7fd99da758478e38cecc9b441baaa156078 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Sat, 14 Feb 2015 14:26:37 -0600 Subject: [PATCH] Move metric capabilities into a single Resource Previously, one call to /capabilities would result in potentially several Capability objects being returned (through `list`), and you had to check the value to know what type of capability it was. This change brings that all together in one object and sets the type of capabiltiy returned as a property on the object. It now works like this: >>> from openstack.metric.v1 import capabilities >>> caps = capabilities.Capabilities().get(session) >>> caps.aggregation_methods ['mean', 'max', 'avg'] Change-Id: Icee03330d42df8aeea11863eef7e4721d8311156 --- .../v1/{capability.py => capabilities.py} | 21 ++------ .../tests/metric/v1/test_capabilities.py | 36 +++++++++++++ openstack/tests/metric/v1/test_capability.py | 54 ------------------- 3 files changed, 40 insertions(+), 71 deletions(-) rename openstack/metric/v1/{capability.py => capabilities.py} (59%) create mode 100644 openstack/tests/metric/v1/test_capabilities.py delete mode 100644 openstack/tests/metric/v1/test_capability.py diff --git a/openstack/metric/v1/capability.py b/openstack/metric/v1/capabilities.py similarity index 59% rename from openstack/metric/v1/capability.py rename to openstack/metric/v1/capabilities.py index e7133496..e085492d 100644 --- a/openstack/metric/v1/capability.py +++ b/openstack/metric/v1/capabilities.py @@ -10,29 +10,16 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from openstack.metric import metric_service from openstack import resource -class Capability(resource.Resource): +class Capabilities(resource.Resource): base_path = '/capabilities' service = metric_service.MetricService() # Supported Operations - allow_list = True + allow_retrieve = True - # Properties - value = resource.prop('value') - - @classmethod - def page(cls, session, limit, marker=None, path_args=None, **params): - if marker: - return [] - - resp = super(Capability, cls).page(session, limit, - marker, path_args, **params) - - return [{"id": key, "value": value} - for key, value in six.iteritems(resp)] + #: The supported methods of aggregation. + aggregation_methods = resource.prop('aggregation_methods', type=list) diff --git a/openstack/tests/metric/v1/test_capabilities.py b/openstack/tests/metric/v1/test_capabilities.py new file mode 100644 index 00000000..0f2ee38c --- /dev/null +++ b/openstack/tests/metric/v1/test_capabilities.py @@ -0,0 +1,36 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import testtools + +from openstack.metric.v1 import capabilities + +BODY = { + 'aggregation_methods': ['mean', 'max', 'avg'], +} + + +class TestCapabilites(testtools.TestCase): + def test_basic(self): + sot = capabilities.Capabilities() + self.assertEqual('/capabilities', sot.base_path) + self.assertEqual('metric', sot.service.service_type) + self.assertFalse(sot.allow_create) + self.assertTrue(sot.allow_retrieve) + self.assertFalse(sot.allow_update) + self.assertFalse(sot.allow_delete) + self.assertFalse(sot.allow_list) + + def test_make_it(self): + sot = capabilities.Capabilities(BODY) + self.assertEqual(BODY['aggregation_methods'], + sot.aggregation_methods) diff --git a/openstack/tests/metric/v1/test_capability.py b/openstack/tests/metric/v1/test_capability.py deleted file mode 100644 index a5ef0ddf..00000000 --- a/openstack/tests/metric/v1/test_capability.py +++ /dev/null @@ -1,54 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import mock -import testtools - -from openstack.metric.v1 import capability - -EXAMPLE = { - "id": "123", - "value": False, -} -BODY = { - "aggregation_methods": ['mean', 'max', 'avg'], -} - - -class TestCapability(testtools.TestCase): - def test_basic(self): - sot = capability.Capability() - self.assertEqual('/capabilities', sot.base_path) - self.assertEqual('metric', sot.service.service_type) - self.assertFalse(sot.allow_create) - self.assertFalse(sot.allow_retrieve) - self.assertFalse(sot.allow_update) - self.assertFalse(sot.allow_delete) - self.assertTrue(sot.allow_list) - - def test_make_it(self): - sot = capability.Capability(EXAMPLE) - self.assertEqual(EXAMPLE['id'], sot.id) - self.assertEqual(EXAMPLE['value'], sot.value) - - def test_list(self): - sess = mock.Mock() - resp = mock.Mock() - resp.body = BODY - sess.get = mock.Mock(return_value=resp) - - caps = capability.Capability.list(sess) - - caps = sorted(caps, key=lambda cap: cap.id) - self.assertEqual(1, len(caps)) - self.assertEqual('aggregation_methods', caps[0].id) - self.assertEqual(['mean', 'max', 'avg'], caps[0].value)