Initial checkin of new API implementation
authorTony Mack <tmack@cs.princeton.edu>
Thu, 14 Sep 2006 15:44:03 +0000 (15:44 +0000)
committerTony Mack <tmack@cs.princeton.edu>
Thu, 14 Sep 2006 15:44:03 +0000 (15:44 +0000)
PLC/Methods/AdmAddNodeToNodeGroup.py [new file with mode: 0644]
PLC/Methods/AdmGetNodeGroups.py [new file with mode: 0644]
PLC/Methods/AdmRemoveNodeFromNodeGroup.py [new file with mode: 0644]
PLC/Methods/AdmUpdateNodeGroup.py [new file with mode: 0644]

diff --git a/PLC/Methods/AdmAddNodeToNodeGroup.py b/PLC/Methods/AdmAddNodeToNodeGroup.py
new file mode 100644 (file)
index 0000000..419ef63
--- /dev/null
@@ -0,0 +1,46 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.NodeGroups import NodeGroup, NodeGroups
+from PLC.Nodes import Node, Nodes
+from PLC.Auth import PasswordAuth
+
+class AdmAddNodeToNodeGroup(Method):
+    """
+    Add a node to the specified node group. If the node is
+    already a member of the nodegroup, no errors are returned.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(NodeGroup.fields['nodegroup_id'],
+             NodeGroup.fields['name']),
+       Mixed(Node.fields['node_id'],
+             Node.fields['hostname'])
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, nodegroup_id_or_name, node_id_or_hostname):
+        # Get node info
+       nodes = Nodes(self.api, [node_id_or_hostname])
+       if not nodes:
+               raise PLCInvalidArgument, "No such node"
+       node = nodes.values()[0]
+
+       # Get nodegroup info
+        nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
+        if not nodegroups:
+            raise PLCInvalidArgument, "No such nodegroup"
+
+        nodegroup = nodegroups.values()[0]
+
+       # add node to nodegroup
+        if node['node_id'] not in nodegroup['node_ids']:
+            nodegroup.add_node(node)
+
+        return 1
diff --git a/PLC/Methods/AdmGetNodeGroups.py b/PLC/Methods/AdmGetNodeGroups.py
new file mode 100644 (file)
index 0000000..6ceaba8
--- /dev/null
@@ -0,0 +1,47 @@
+import os
+
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Sites import Site, Sites
+from PLC.Nodes import Node, Nodes
+from PLC.Auth import PasswordAuth
+from PLC.NodeGroups import NodeGroup, NodeGroups
+class AdmGetNodeGroups(Method):
+    """
+    Returns a list of structs containing the details about the node groups 
+    specified. 
+
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech']
+
+    accepts = [
+        PasswordAuth(),
+        [Mixed(NodeGroup.fields['nodegroup_id'],
+              NodeGroup.fields['name'])]
+        ]
+
+    returns = [NodeGroup.all_fields]
+  
+    def __init__(self, *args, **kwds):
+        Method.__init__(self, *args, **kwds)
+        # Update documentation with list of default fields returned
+        self.__doc__ += os.linesep.join(Site.default_fields.keys())
+
+    def call(self, auth, nodegroup_id_or_name_list=None):
+        # Authenticated function
+        assert self.caller is not None
+
+        # Get nodes in this nodegroup
+       nodegroups = NodeGroups(self.api, nodegroup_id_or_name_list)    
+
+       # make sure sites are found
+       if not nodegroups:
+               raise PLCInvalidArgument, "No such site"
+       elif nodegroup_id_or_name_list is None:
+               pass
+       elif not len(nodegroups) == len(nodegroup_id_or_name_list):
+               raise PLCInvalidArgument, "at least one node group id is invalid"
+       
+        return nodegroups.values()
diff --git a/PLC/Methods/AdmRemoveNodeFromNodeGroup.py b/PLC/Methods/AdmRemoveNodeFromNodeGroup.py
new file mode 100644 (file)
index 0000000..03e54a9
--- /dev/null
@@ -0,0 +1,45 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.NodeGroups import NodeGroup, NodeGroups
+from PLC.Nodes import Node, Nodes
+from PLC.Auth import PasswordAuth
+
+class AdmRemoveNodeFromNodeGroup(Method):
+    """
+    Removes a node from the specified node group. 
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(NodeGroup.fields['nodegroup_id'],
+             NodeGroup.fields['name']),
+       Mixed(Node.fields['node_id'],
+             Node.fields['hostname'])
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, nodegroup_id_or_name, node_id_or_hostname):
+        # Get node info
+       nodes = Nodes(self.api, [node_id_or_hostname])
+       if not nodes:
+               raise PLCInvalidArgument, "No such node"
+       node = nodes.values()[0]
+
+       # Get nodegroup info
+        nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
+        if not nodegroups:
+            raise PLCInvalidArgument, "No such nodegroup"
+
+        nodegroup = nodegroups.values()[0]
+
+       # add node to nodegroup
+        if node['node_id'] in nodegroup['node_ids']:
+            nodegroup.remove_node(node)
+
+        return 1
diff --git a/PLC/Methods/AdmUpdateNodeGroup.py b/PLC/Methods/AdmUpdateNodeGroup.py
new file mode 100644 (file)
index 0000000..1ea77b3
--- /dev/null
@@ -0,0 +1,59 @@
+
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Nodes import Node, Nodes
+from PLC.NodeGroups import NodeGroup, NodeGroups
+#from PLC.Sites import Site, Sites
+from PLC.Auth import PasswordAuth
+
+class AdmUpdateNodeGroup(Method):
+    """
+    Updates a custom node group.
+     
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    can_update = lambda (field, value): field in \
+                 ['name', 'description']
+    update_fields = dict(filter(can_update, NodeGroup.fields.items()))
+
+    accepts = [
+        PasswordAuth(),
+       NodeGroup.fields['nodegroup_id'],
+        NodeGroup.fields['name'],
+       NodeGroup.fields['description']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, nodegroup_id, name, description):
+        #if filter(lambda field: field not in self.update_fields, optional_vals):
+        #    raise PLCInvalidArgument, "Invalid fields specified"
+
+        # Authenticated function
+        assert self.caller is not None
+
+        # make sure we are 'admin'
+        if 'admin' not in self.caller['roles']:
+               raise PLCPermissionDenied, "Not allowed to update node groups"
+
+       # Get nodegroup information
+       nodegroups = NodeGroups(self.api, [nodegroup_id])
+       if not nodegroups:
+               raise PLCInvalidArgument, "No such nodegroup"
+
+       nodegroup = nodegroups.values()[0]
+       
+       # Modify node group
+        update_fields = {
+               'name': name,
+               'description': description
+               }
+
+       nodegroup.update(update_fields)
+        nodegroup.flush()
+
+        return 1