Optimization of "check_for_metric" function
Signed-off-by: gcalvino <guillermo.calvinosanchez@altran.com>
Change-Id: I1beb698c564b21f3340d9e67947fe1031f57dfe7
diff --git a/osm_mon/plugins/OpenStack/Aodh/alarming.py b/osm_mon/plugins/OpenStack/Aodh/alarming.py
index 13b6541..e52b12f 100644
--- a/osm_mon/plugins/OpenStack/Aodh/alarming.py
+++ b/osm_mon/plugins/OpenStack/Aodh/alarming.py
@@ -432,31 +432,17 @@
def check_for_metric(self, auth_token, metric_endpoint, m_name, r_id):
"""Check for the alarm metric."""
try:
- url = "{}/v1/metric?sort=name:asc".format(metric_endpoint)
+ url = "{}/v1/resource/generic/{}".format(metric_endpoint, r_id)
result = Common.perform_request(
url, auth_token, req_type="get")
- metric_list = []
- metrics_partial = json.loads(result.text)
- for metric in metrics_partial:
- metric_list.append(metric)
-
- while len(json.loads(result.text)) > 0:
- last_metric_id = metrics_partial[-1]['id']
- url = "{}/v1/metric?sort=name:asc&marker={}".format(metric_endpoint, last_metric_id)
- result = Common.perform_request(
- url, auth_token, req_type="get")
- if len(json.loads(result.text)) > 0:
- metrics_partial = json.loads(result.text)
- for metric in metrics_partial:
- metric_list.append(metric)
- metric_id = None
- for metric in metric_list:
- name = metric['name']
- resource = metric['resource_id']
- if name == METRIC_MAPPINGS[m_name] and resource == r_id:
- metric_id = metric['id']
- log.info("The required metric exists, an alarm will be created.")
+ resource = json.loads(result.text)
+ metric_list = resource['metrics']
+ if metric_list.get(METRIC_MAPPINGS[m_name]):
+ metric_id = metric_list[METRIC_MAPPINGS[m_name]]
+ else:
+ metric_id = None
+ log.info("Desired Gnocchi metric not found")
return metric_id
except Exception as exc:
log.info("Desired Gnocchi metric not found:%s", exc)
- return None
+ return None
\ No newline at end of file
diff --git a/osm_mon/test/OpenStack/unit/test_alarming.py b/osm_mon/test/OpenStack/unit/test_alarming.py
index c45c052..a46e1f3 100644
--- a/osm_mon/test/OpenStack/unit/test_alarming.py
+++ b/osm_mon/test/OpenStack/unit/test_alarming.py
@@ -287,4 +287,4 @@
self.alarming.check_for_metric(auth_token, metric_endpoint, "metric_name", "r_id")
perf_req.assert_called_with(
- "metric_endpoint/v1/metric?sort=name:asc", auth_token, req_type="get")
+ "metric_endpoint/v1/resource/generic/r_id", auth_token, req_type="get")