Browse Source

merging alias nodes: move alias' mac into 'macs' field

Helge Jung 9 years ago
parent
commit
e304a1af03
1 changed files with 32 additions and 6 deletions
  1. 32 6
      ffstatus/basestorage.py

+ 32 - 6
ffstatus/basestorage.py

@@ -98,6 +98,37 @@ class BaseStorage(object):
             'now': int(time.time()),
         }
 
+    def __merge_alias_node(self, item, alias):
+        # start by using standard dict_merge()
+        update = ffstatus.dict_merge(item, alias, overwrite_lists=False)
+
+        # extract some fields for further inspection
+        update_macs = update.get('macs', []) or []
+
+        # field 'node_id': keep original value
+        update['node_id'] = item['node_id']
+
+        # field 'mac': keep original value
+        if 'mac' in item:
+            update['mac'] = item['mac']
+            if 'mac' in alias:
+                update_macs.append(alias['mac'])
+
+        # field 'macs': get rid of duplicates and primary mac
+        primary_mac = update.get('mac')
+        macs = []
+        for x in update_macs:
+            if x != primary_mac and x not in macs:
+                macs.append(x)
+        update['macs'] = update_macs = macs
+
+        # field 'type': keep special node type
+        item_type = item.get('type', 'node')
+        if item_type != 'node' and update.get('type', 'node') == 'node':
+            update['type'] = item_type
+
+        return update
+
     def merge_new_data(self, newdata):
         """Updates data in the storage by merging the new data."""
 
@@ -177,12 +208,7 @@ class BaseStorage(object):
                 continue
 
             # merge data
-            item_type = item.get('type', 'node')
-            update = ffstatus.dict_merge(item, alias, overwrite_lists=False)
-            update['node_id'] = item_id  # keep original item's id
-            if item_type != 'node' and update.get('type', 'node') == 'node':
-                # revert overwriting of special node type
-                update['type'] = item_type
+            update = self.__merge_alias_node(item, alias)
             updated[item_id] = update
             logging.debug("Merged alias '%s' into '%s'.", alias_id, item_id)