Browse Source

add /find?mac=... functionality

Helge Jung 9 years ago
parent
commit
a8be15b5f4
1 changed files with 30 additions and 2 deletions
  1. 30 2
      ffstatus/server.py

+ 30 - 2
ffstatus/server.py

@@ -336,9 +336,21 @@ angesprochen und sollte aus einer Mehrzahl von Gründen nicht
         self.__send_headers('application/json',
                             extra={'Content-Disposition': 'inline'})
 
-        name = query.get('name').lower()
-        fuzzy = query.get('fuzzy', '0') == '1'
+        name = query.get('name')
+        mac = query.get('mac')
 
+        if name is not None:
+            fuzzy = query.get('fuzzy', '0') == '1'
+            return self.__respond_findnode_name(name, fuzzy)
+
+        if mac is not None:
+            return self.__respond_findnode_mac(mac)
+
+        self.logger.error('/find called without name or mac parameter')
+        self.wfile.write('null')
+
+    def __respond_findnode_name(self, name, fuzzy):
+        name = name.lower()
         names = {}
         for node in self.server.storage.get_nodes():
             nodename = node.get('hostname').lower()
@@ -380,6 +392,22 @@ angesprochen und sollte aus einer Mehrzahl von Gründen nicht
 
         self.wfile.write(json.dumps(result))
 
+    def __respond_findnode_mac(self, mac):
+        mac = mac.lower()
+        result = []
+
+        for node in self.server.storage.get_nodes():
+            if node.get('mac', '').lower() == mac or \
+               mac in [x.lower() for x in node.get('macs', [])]:
+                node_id = node.get('node_id')
+                result.append({
+                    'id': node_id,
+                    'name': node.get('hostname'),
+                    'status': self.server.storage.get_nodestatus(node_id),
+                })
+
+        self.wfile.write(json.dumps(result))
+
     def __respond_nodeidmac2name(self, ids):
         """Return a mapping of the given IDs (or MACs) into their hostname."""