-fixed validate_name function. It now returns the name if it is valid.
[plcapi.git] / PLC / NodeGroups.py
index aff2c8f..b2d4024 100644 (file)
@@ -4,13 +4,16 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id$
+# $Id: NodeGroups.py,v 1.3 2006/09/07 23:44:49 mlhuang Exp $
 #
 
+from types import StringTypes
+
 from PLC.Faults import *
 from PLC.Parameter import Parameter
 from PLC.Debug import profile
 from PLC.Table import Row, Table
+from PLC.Nodes import Node, Nodes
 
 class NodeGroup(Row):
     """
@@ -37,10 +40,60 @@ class NodeGroup(Row):
         self.api = api
 
     def validate_name(self, name):
-        conflicts = NodeGroups(self.api, [name])
-        for nodegroup_id in conflicts:
+       conflicts = NodeGroups(self.api, [name])
+       for nodegroup_id in conflicts:
             if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup_id:
-                raise PLCInvalidArgument, "Node group name already in use"
+               raise PLCInvalidArgument, "Node group name already in use"
+       return name     
+       
+
+    def add_node(self, node, commit = True):
+        """
+        Add node to existing nodegroup.
+        """
+
+        assert 'nodegroup_id' in self
+        assert isinstance(node, Node)
+        assert 'node_id' in node
+
+        node_id = node['node_id']
+        nodegroup_id = self['nodegroup_id']
+        self.api.db.do("INSERT INTO nodegroup_nodes (nodegroup_id, node_id)" \
+                       " VALUES(%(nodegroup_id)d, %(node_id)d)",
+                       locals())
+
+        if commit:
+            self.api.db.commit()
+
+        if 'node_ids' in self and node_id not in self['node_ids']:
+            self['node_ids'].append(node_id)
+
+        if 'nodegroup_ids' in node and nodegroup_id not in node['nodegroup_ids']:
+            node['nodegroup_ids'].append(nodegroup_id)
+
+    def remove_node(self, node, commit = True):
+        """
+        Remove node from existing nodegroup.
+        """
+
+        assert 'nodegroup_id' in self
+        assert isinstance(node, Node)
+        assert 'node_id' in node
+
+        node_id = node['node_id']
+        nodegroup_id = self['nodegroup_id']
+        self.api.db.do("INSERT INTO nodegroup_nodes (nodegroup_id, node_id)" \
+                       " VALUES(%(nodegroup_id)d, %(node_id)d)",
+                       locals())
+
+        if commit:
+            self.api.db.commit()
+
+        if 'node_ids' in self and node_id not in self['node_ids']:
+            self['node_ids'].append(node_id)
+
+        if 'nodegroup_ids' in node and nodegroup_id not in node['nodegroup_ids']:
+            node['nodegroup_ids'].append(nodegroup_id)
 
     def flush(self, commit = True):
         """
@@ -82,6 +135,7 @@ class NodeGroup(Row):
 
         if commit:
             self.api.db.commit()
+           
 
     def delete(self, commit = True):
         """
@@ -115,7 +169,7 @@ class NodeGroups(Table):
     """
 
     def __init__(self, api, nodegroup_id_or_name_list = None):
-        self.api = api
+       self.api = api
 
         # N.B.: Node IDs returned may be deleted.
         sql = "SELECT nodegroups.*, nodegroup_nodes.node_id" \
@@ -128,7 +182,7 @@ class NodeGroups(Table):
                                    nodegroup_id_or_name_list)
             names = filter(lambda name: isinstance(name, StringTypes),
                            nodegroup_id_or_name_list)
-            sql += " AND (False"
+            sql += " WHERE (False"
             if nodegroup_ids:
                 sql += " OR nodegroup_id IN (%s)" % ", ".join(map(str, nodegroup_ids))
             if names: