From 284894bd35e5c9939e8569a912336e79f2809d5a Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 10 Feb 2015 12:22:24 +0100 Subject: [PATCH] telemetry: add support for Gnocchi capabilities Change-Id: Id16bd2fbe1b6d71f0a6a746b6e40429daaf4c15d --- openstack/metric/__init__.py | 0 openstack/metric/metric_service.py | 24 +++++++++ openstack/metric/v1/__init__.py | 0 openstack/metric/v1/capability.py | 38 +++++++++++++ openstack/tests/metric/__init__.py | 0 openstack/tests/metric/test_metric_service.py | 28 ++++++++++ openstack/tests/metric/v1/__init__.py | 0 openstack/tests/metric/v1/test_capability.py | 54 +++++++++++++++++++ 8 files changed, 144 insertions(+) create mode 100644 openstack/metric/__init__.py create mode 100644 openstack/metric/metric_service.py create mode 100644 openstack/metric/v1/__init__.py create mode 100644 openstack/metric/v1/capability.py create mode 100644 openstack/tests/metric/__init__.py create mode 100644 openstack/tests/metric/test_metric_service.py create mode 100644 openstack/tests/metric/v1/__init__.py create mode 100644 openstack/tests/metric/v1/test_capability.py diff --git a/openstack/metric/__init__.py b/openstack/metric/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/openstack/metric/metric_service.py b/openstack/metric/metric_service.py new file mode 100644 index 00000000..9bcaf0f5 --- /dev/null +++ b/openstack/metric/metric_service.py @@ -0,0 +1,24 @@ +# 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. + +from openstack.auth import service_filter + + +class MetricService(service_filter.ServiceFilter): + """The metric service.""" + + valid_versions = [service_filter.ValidVersion('v1')] + + def __init__(self, version=None): + """Create a metric service.""" + super(MetricService, self).__init__(service_type='metric', + version=version) diff --git a/openstack/metric/v1/__init__.py b/openstack/metric/v1/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/openstack/metric/v1/capability.py b/openstack/metric/v1/capability.py new file mode 100644 index 00000000..e7133496 --- /dev/null +++ b/openstack/metric/v1/capability.py @@ -0,0 +1,38 @@ +# 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 six + +from openstack.metric import metric_service +from openstack import resource + + +class Capability(resource.Resource): + base_path = '/capabilities' + service = metric_service.MetricService() + + # Supported Operations + allow_list = 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)] diff --git a/openstack/tests/metric/__init__.py b/openstack/tests/metric/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/openstack/tests/metric/test_metric_service.py b/openstack/tests/metric/test_metric_service.py new file mode 100644 index 00000000..e55f3961 --- /dev/null +++ b/openstack/tests/metric/test_metric_service.py @@ -0,0 +1,28 @@ +# 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 import metric_service + + +class TestMetricService(testtools.TestCase): + + def test_service(self): + sot = metric_service.MetricService() + self.assertEqual('metric', sot.service_type) + self.assertEqual('public', sot.visibility) + self.assertIsNone(sot.region) + self.assertIsNone(sot.service_name) + self.assertEqual(1, len(sot.valid_versions)) + self.assertEqual('v1', sot.valid_versions[0].module) + self.assertEqual('v1', sot.valid_versions[0].path) diff --git a/openstack/tests/metric/v1/__init__.py b/openstack/tests/metric/v1/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/openstack/tests/metric/v1/test_capability.py b/openstack/tests/metric/v1/test_capability.py new file mode 100644 index 00000000..a5ef0ddf --- /dev/null +++ b/openstack/tests/metric/v1/test_capability.py @@ -0,0 +1,54 @@ +# 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)