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:
@@ -10,29 +10,16 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import six
|
|
||||||
|
|
||||||
from openstack.metric import metric_service
|
from openstack.metric import metric_service
|
||||||
from openstack import resource
|
from openstack import resource
|
||||||
|
|
||||||
|
|
||||||
class Capability(resource.Resource):
|
class Capabilities(resource.Resource):
|
||||||
base_path = '/capabilities'
|
base_path = '/capabilities'
|
||||||
service = metric_service.MetricService()
|
service = metric_service.MetricService()
|
||||||
|
|
||||||
# Supported Operations
|
# Supported Operations
|
||||||
allow_list = True
|
allow_retrieve = True
|
||||||
|
|
||||||
# Properties
|
#: The supported methods of aggregation.
|
||||||
value = resource.prop('value')
|
aggregation_methods = resource.prop('aggregation_methods', type=list)
|
||||||
|
|
||||||
@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)]
|
|
||||||
36
openstack/tests/metric/v1/test_capabilities.py
Normal file
36
openstack/tests/metric/v1/test_capabilities.py
Normal 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)
|
||||||
@@ -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)
|
|
||||||
Reference in New Issue
Block a user