From 9765fce9ee4b154e25dbb1450377849401df2668 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Fri, 5 Dec 2008 08:10:30 +0000 Subject: [PATCH] node_type object and methods --- PLC/Methods/AddNodeType.py | 30 +++++++++++++++++++++ PLC/Methods/DeleteNodeType.py | 36 +++++++++++++++++++++++++ PLC/Methods/GetNodeTypes.py | 23 ++++++++++++++++ PLC/Methods/__init__.py | 3 +++ PLC/NodeTypes.py | 50 +++++++++++++++++++++++++++++++++++ PLC/__init__.py | 1 + 6 files changed, 143 insertions(+) create mode 100644 PLC/Methods/AddNodeType.py create mode 100644 PLC/Methods/DeleteNodeType.py create mode 100644 PLC/Methods/GetNodeTypes.py create mode 100644 PLC/NodeTypes.py diff --git a/PLC/Methods/AddNodeType.py b/PLC/Methods/AddNodeType.py new file mode 100644 index 0000000..b49135f --- /dev/null +++ b/PLC/Methods/AddNodeType.py @@ -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 index 0000000..c684502 --- /dev/null +++ b/PLC/Methods/DeleteNodeType.py @@ -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 index 0000000..243fbe1 --- /dev/null +++ b/PLC/Methods/GetNodeTypes.py @@ -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)] diff --git a/PLC/Methods/__init__.py b/PLC/Methods/__init__.py index 3e06b7d..a6fc8bc 100644 --- a/PLC/Methods/__init__.py +++ b/PLC/Methods/__init__.py @@ -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 index 0000000..e3db781 --- /dev/null +++ b/PLC/NodeTypes.py @@ -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) diff --git a/PLC/__init__.py b/PLC/__init__.py index 21b3b5f..0c58a0f 100644 --- a/PLC/__init__.py +++ b/PLC/__init__.py @@ -25,6 +25,7 @@ NetworkMethods NetworkTypes NodeGroups NodeTags +NodeTypes Nodes PCUProtocolTypes PCUTypes -- 2.43.0