diff options
-rw-r--r-- | ceilometer/polling/dynamic_pollster.py | 8 | ||||
-rw-r--r-- | ceilometer/tests/unit/polling/test_dynamic_pollster.py | 21 | ||||
-rw-r--r-- | doc/source/admin/telemetry-dynamic-pollster.rst | 9 |
3 files changed, 34 insertions, 4 deletions
diff --git a/ceilometer/polling/dynamic_pollster.py b/ceilometer/polling/dynamic_pollster.py index 6cb5442a..db183308 100644 --- a/ceilometer/polling/dynamic_pollster.py +++ b/ceilometer/polling/dynamic_pollster.py @@ -138,8 +138,12 @@ class PollsterSampleExtractor(object): attribute_key, json_object) keys_and_operations = attribute_key.split("|") attribute_key = keys_and_operations[0].strip() - nested_keys = attribute_key.split(".") - value = reduce(operator.getitem, nested_keys, json_object) + + if attribute_key == ".": + value = json_object + else: + nested_keys = attribute_key.split(".") + value = reduce(operator.getitem, nested_keys, json_object) return self.operate_value(keys_and_operations, value) diff --git a/ceilometer/tests/unit/polling/test_dynamic_pollster.py b/ceilometer/tests/unit/polling/test_dynamic_pollster.py index 7276cbe8..aa73ca0f 100644 --- a/ceilometer/tests/unit/polling/test_dynamic_pollster.py +++ b/ceilometer/tests/unit/polling/test_dynamic_pollster.py @@ -759,3 +759,24 @@ class TestDynamicPollster(base.BaseTestCase): finally: dynamic_pollster.PollsterSampleGatherer. \ internal_execute_request_get_samples = original_method + + def test_retrieve_attribute_self_reference_sample(self): + key = " . | value['key1']['subKey1'][0]['d'] if 'key1' in value else 0" + + sub_value1 = [{"d": 2}, {"g": {"h": "val"}}] + sub_value2 = [{"r": 245}, {"h": {"yu": "yu"}}] + + json_object = {"key1": {"subKey1": sub_value1}, + "key2": {"subkey2": sub_value2}} + + pollster = dynamic_pollster.DynamicPollster( + self.pollster_definition_only_required_fields) + + returned_value = pollster.definitions.sample_extractor.\ + retrieve_attribute_nested_value(json_object, key) + self.assertEqual(2, returned_value) + + del json_object['key1'] + returned_value = pollster.definitions.sample_extractor.\ + retrieve_attribute_nested_value(json_object, key) + self.assertEqual(0, returned_value) diff --git a/doc/source/admin/telemetry-dynamic-pollster.rst b/doc/source/admin/telemetry-dynamic-pollster.rst index c56725bc..6ed1faa2 100644 --- a/doc/source/admin/telemetry-dynamic-pollster.rst +++ b/doc/source/admin/telemetry-dynamic-pollster.rst @@ -40,8 +40,13 @@ attributes to define a dynamic pollster: * ``value_attribute``: mandatory attribute; defines the attribute in the JSON response from the URL of the component being polled. We also accept nested values dictionaries. To use a nested value one can simply use - ``attribute1.attribute2.<asMuchAsNeeded>.lastattribute``. In our magnum - example, we can use ``status`` as the value attribute; + ``attribute1.attribute2.<asMuchAsNeeded>.lastattribute``. It is also + possible to reference the sample itself using ``.``; the self reference + of the sample is interesting in cases when the attribute might not exist. + Therefore, together with the operations one can first check if it exist + before retrieving it (e.g. + `` . | value['some_field'] if 'some_field' in value else ''``). + In our magnum example, we can use ``status`` as the value attribute; * ``endpoint_type``: mandatory field; defines the endpoint type that is used to discover the base URL of the component to be monitored; for |