node_type object and methods
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 5 Dec 2008 08:10:30 +0000 (08:10 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 5 Dec 2008 08:10:30 +0000 (08:10 +0000)
PLC/Methods/AddNodeType.py [new file with mode: 0644]
PLC/Methods/DeleteNodeType.py [new file with mode: 0644]
PLC/Methods/GetNodeTypes.py [new file with mode: 0644]
PLC/Methods/__init__.py
PLC/NodeTypes.py [new file with mode: 0644]
PLC/__init__.py

diff --git a/PLC/Methods/AddNodeType.py b/PLC/Methods/AddNodeType.py
new file mode 100644 (file)
index 0000000..b49135f
--- /dev/null
@@ -0,0 +1,30 @@
+# $Id$
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.NodeTypes import NodeType, NodeTypes
+from PLC.Auth import Auth
+
+class AddNodeType(Method):
+    """
+    Adds a new node node type.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        Auth(),
+        NodeType.fields['node_type']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    
+    def call(self, auth, name):
+        node_type = NodeType(self.api)
+        node_type['node_type'] = name
+        node_type.sync(insert = True)
+
+        return 1
diff --git a/PLC/Methods/DeleteNodeType.py b/PLC/Methods/DeleteNodeType.py
new file mode 100644 (file)
index 0000000..c684502
--- /dev/null
@@ -0,0 +1,36 @@
+# $Id$
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.NodeTypes import NodeType, NodeTypes
+from PLC.Auth import Auth
+
+class DeleteNodeType(Method):
+    """
+    Deletes a node node type.
+
+    WARNING: This will cause the deletion of all nodes in this boot
+    state.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        Auth(),
+        NodeType.fields['node_type']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+
+    def call(self, auth, name):
+        node_types = NodeTypes(self.api, [name])
+        if not node_types:
+            raise PLCInvalidArgument, "No such node type"
+        node_type = node_types[0]
+
+        node_type.delete()
+       
+        return 1
diff --git a/PLC/Methods/GetNodeTypes.py b/PLC/Methods/GetNodeTypes.py
new file mode 100644 (file)
index 0000000..243fbe1
--- /dev/null
@@ -0,0 +1,23 @@
+# $Id$
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.NodeTypes import NodeType, NodeTypes
+from PLC.Auth import Auth
+
+class GetNodeTypes(Method):
+    """
+    Returns an array of all valid node node types.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech', 'node']
+
+    accepts = [
+        Auth()
+        ]
+
+    returns = [NodeType.fields['node_type']]
+    
+
+    def call(self, auth):
+        return [node_type['node_type'] for node_type in NodeTypes(self.api)]
index 3e06b7d..a6fc8bc 100644 (file)
@@ -17,6 +17,7 @@ AddNode
 AddNodeGroup
 AddNodeTag
 AddNodeToPCU
+AddNodeType
 AddPCU
 AddPCUProtocolType
 AddPCUType
@@ -62,6 +63,7 @@ DeleteNode
 DeleteNodeFromPCU
 DeleteNodeGroup
 DeleteNodeTag
+DeleteNodeType
 DeletePCU
 DeletePCUProtocolType
 DeletePCUType
@@ -98,6 +100,7 @@ GetNetworkMethods
 GetNetworkTypes
 GetNodeGroups
 GetNodeTags
+GetNodeTypes
 GetNodes
 GetPCUProtocolTypes
 GetPCUTypes
diff --git a/PLC/NodeTypes.py b/PLC/NodeTypes.py
new file mode 100644 (file)
index 0000000..e3db781
--- /dev/null
@@ -0,0 +1,50 @@
+#
+# Functions for interacting with the node_types table in the database
+#
+# $Id$
+#
+
+from PLC.Faults import *
+from PLC.Parameter import Parameter
+from PLC.Table import Row, Table
+
+class NodeType(Row):
+    """
+    Representation of a row in the node_types table. To use,
+    instantiate with a dict of values.
+    """
+
+    table_name = 'node_types'
+    primary_key = 'node_type'
+    join_tables = ['nodes']
+    fields = {
+        'node_type': Parameter(str, "Node type", max = 20),
+        }
+
+    def validate_node_type(self, name):
+       # Make sure name is not blank
+        if not len(name):
+            raise PLCInvalidArgument, "Node type must be specified"
+       
+       # Make sure node type does not alredy exist
+       conflicts = NodeTypes(self.api, [name])
+        if conflicts:
+            raise PLCInvalidArgument, "Node type name already in use"
+
+       return name
+
+class NodeTypes(Table):
+    """
+    Representation of the node_types table in the database.
+    """
+
+    def __init__(self, api, node_types = None):
+        Table.__init__(self, api, NodeType)
+
+        sql = "SELECT %s FROM node_types" % \
+              ", ".join(NodeType.fields)
+        
+        if node_types:
+            sql += " WHERE node_type IN (%s)" % ", ".join(map(api.db.quote, node_types))
+
+        self.selectall(sql)
index 21b3b5f..0c58a0f 100644 (file)
@@ -25,6 +25,7 @@ NetworkMethods
 NetworkTypes
 NodeGroups
 NodeTags
+NodeTypes
 Nodes
 PCUProtocolTypes
 PCUTypes