import etcd
import json

from tendrl.commons.event import Event
from tendrl.commons.message import Message, ExceptionMessage

from tendrl.node_agent.discovery.sds import manager as sds_manager


def load_and_execute_sds_discovery_plugins():
    Event(
        Message(
            priority="debug",
            publisher=NS.publisher_id,
            payload={"message": "load_and_execute_sds_discovery_plugins"}
        )
    )
    try:
        sds_discovery_manager = sds_manager.SDSDiscoveryManager()
    except ValueError as ex:
        Event(
            ExceptionMessage(
                priority="error",
                publisher=NS.publisher_id,
                payload={"message": "Failed to init SDSDiscoveryManager.",
                         "exception": ex
                }
            )
        )
        return

    # Execute the SDS discovery plugins and tag the nodes with data
    for plugin in sds_discovery_manager.get_available_plugins():
        sds_details = plugin.discover_storage_system()
        if sds_details:
            try:
                NS.tendrl.objects.DetectedCluster(
                    detected_cluster_id=sds_details.get(
                        'detected_cluster_id'),
                    detected_cluster_name=sds_details.get(
                        'detected_cluster_name'),
                    sds_pkg_name=sds_details.get('pkg_name'),
                    sds_pkg_version=sds_details.get('pkg_version'),
                ).save()
                NS.node_context = NS.node_context.load()
                current_tags = json.loads(NS.node_context.tags)
                detected_cluster_tag = "detected_cluster/%s" % sds_details['detected_cluster_id']
                current_tags += [detected_cluster_tag]
                NS.node_context.tags = list(set(current_tags))
                NS.node_context.save()

            except (etcd.EtcdException, KeyError) as ex:
                Event(
                    ExceptionMessage(
                        priority="error",
                        publisher=NS.publisher_id,
                        payload={"message": "Failed to update sds_discovery",
                                 "exception": ex
                                 }
                    )
                )
            break

@@ -1024,21 +1024,78 @@
  if 
-sds_details:%0A
+'detected_cluster_id' in sds_details:%0A            if sds_details:%0A    
     
@@ -1099,32 +1099,36 @@
            try:%0A
+    
                 
@@ -1162,16 +1162,20 @@
 luster(%0A
+    
         
@@ -1239,32 +1239,36 @@
                 
+    
 'detected_cluste
@@ -1271,24 +1271,28 @@
 uster_id'),%0A
+    
             
@@ -1354,32 +1354,36 @@
                 
+   
+ 
 'detected_cluste
@@ -1384,32 +1384,36 @@
 cluster_name'),%0A
+    
                 
@@ -1470,32 +1470,36 @@
                 
+    
 sds_pkg_version=
@@ -1546,16 +1546,20 @@
         
+ 
+   
 ).save()
@@ -1559,16 +1559,20 @@
 .save()%0A
+    
         
@@ -1620,16 +1620,20 @@
 .load()%0A
+    
         
@@ -1696,32 +1696,36 @@
                 
+    
 detected_cluster
@@ -1798,32 +1798,36 @@
                 
+    
 current_tags += 
@@ -1857,32 +1857,36 @@
                 
+   
+ 
 NS.node_context.
@@ -1924,32 +1924,36 @@
                 
+    
 NS.node_context.
@@ -1968,24 +1968,28 @@
             
+ 
+   
 except (etcd
@@ -2029,39 +2029,47 @@
                 
+  
+  
 Event(%0A
+    
                 
@@ -2082,32 +2082,36 @@
 ceptionMessage(%0A
+    
                 
@@ -2152,32 +2152,36 @@
                 
+    
 publisher=NS.pub
@@ -2183,32 +2183,36 @@
 S.publisher_id,%0A
+    
                 
@@ -2298,32 +2298,36 @@
                 
+    
 %22exception%22: ex%0A
@@ -2351,33 +2351,63 @@
                 
-%7D
+    %7D%0A                        )
 %0A               
@@ -2413,38 +2413,24 @@
       )%0A    
-            )%0A
             

Update sds_detect.py