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
This commit is contained in:
Brian Curtin
2015-02-14 14:26:37 -06:00
committed by TerryHowe
parent 8b9c9062fc
commit b9f1b7fd99
3 changed files with 40 additions and 71 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)