From e30cbe513486f88cbf2c6bc13fa84edc0125b551 Mon Sep 17 00:00:00 2001 From: GowthamShanmugasundaram Date: Thu, 29 Mar 2018 13:43:05 +0530 Subject: [PATCH] Modified code as per review comment tendrl-bug-id: Tendrl/gluster-integration#598 Signed-off-by: GowthamShanmugasundaram --- .../gluster_integration/sds_sync/__init__.py | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/tendrl/gluster_integration/sds_sync/__init__.py b/tendrl/gluster_integration/sds_sync/__init__.py index 2016065..c6caaac 100644 --- a/tendrl/gluster_integration/sds_sync/__init__.py +++ b/tendrl/gluster_integration/sds_sync/__init__.py @@ -826,7 +826,7 @@ def update_cluster_alert_count(): cluster_alert_count = 0 severity = ["WARNING", "CRITICAL"] try: - alert_counts = find_volume_id() + alert_counts = get_volume_alert_counts() alerts_arr = NS.tendrl.objects.ClusterAlert( tags={'integration_id': NS.tendrl_context.integration_id} ).load_all() @@ -837,22 +837,21 @@ def update_cluster_alert_count(): if alert.resource in NS.gluster.objects.VolumeAlertCounters( )._defs['relationship'][alert.alert_type.lower()]: vol_name = alert.tags.get('volume_name', None) - if vol_name: - if vol_name in alert_counts.keys(): - alert_counts[vol_name]['alert_count'] += 1 + if vol_name and vol_name in alert_counts.keys(): + alert_counts[vol_name]['alert_count'] += 1 # Update cluster alert count NS.tendrl.objects.ClusterAlertCounters( integration_id=NS.tendrl_context.integration_id, alert_count=cluster_alert_count ).save() # Update volume alert count - for volume in alert_counts: + for volume, vol_dict in alert_counts.iteritems(): NS.gluster.objects.VolumeAlertCounters( integration_id=NS.tendrl_context.integration_id, - alert_count=alert_counts[volume]['alert_count'], - volume_id=alert_counts[volume]['vol_id'] + alert_count=vol_dict['alert_count'], + volume_id=vol_dict['vol_id'] ).save() - except etcd.EtcdException as ex: + except (etcd.EtcdException, AttributeError) as ex: logger.log( "debug", NS.publisher_id, @@ -860,19 +859,11 @@ def update_cluster_alert_count(): ) -def find_volume_id(): +def get_volume_alert_counts(): alert_counts = {} - volumes = etcd_utils.read( - "clusters/%s/Volumes" % NS.tendrl_context.integration_id - ) - for volume in volumes.leaves: - try: - volume_id = volume.key.split("/")[-1] - key = volume.key + "/name" - vol_name = etcd_utils.read(key).value - alert_counts[vol_name] = {} - alert_counts[vol_name]['vol_id'] = volume_id - alert_counts[vol_name]['alert_count'] = 0 - except etcd.EtcdKeyNotFound: - continue + volumes = NS.gluster.objects.Volume().load_all() + for volume in volumes: + alert_counts[volume.name] = {'vol_id': volume.vol_id, + 'alert_count': 0 + } return alert_counts