From 5ac399f349470a15dc57d7261c97a69a17059f9f Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Wed, 13 Sep 2006 15:46:29 +0000 Subject: [PATCH] Initial checkin of new API implementation --- PLC/Methods/AdmAddNodeGroup.py | 53 +++++++++++++++++++++++++++++ PLC/Methods/AdmDeleteNodeGroup.py | 42 +++++++++++++++++++++++ PLC/Methods/AdmGetNodeGroupNodes.py | 47 +++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 PLC/Methods/AdmAddNodeGroup.py create mode 100644 PLC/Methods/AdmDeleteNodeGroup.py create mode 100644 PLC/Methods/AdmGetNodeGroupNodes.py diff --git a/PLC/Methods/AdmAddNodeGroup.py b/PLC/Methods/AdmAddNodeGroup.py new file mode 100644 index 0000000..38f7df6 --- /dev/null +++ b/PLC/Methods/AdmAddNodeGroup.py @@ -0,0 +1,53 @@ +# NodeGroup.validate_name() changes name to null. This causes database error + + +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 AdmAddNodeGroup(Method): + """ + Adds a new node group. Any values specified in optional_vals are used, + otherwise defaults are used. + + Returns the new node_id (> 0) if successful, faults otherwise. + """ + + roles = ['admin'] + + can_update = lambda (field, value): field in \ + ['model', 'version'] + update_fields = dict(filter(can_update, NodeGroup.fields.items())) + + accepts = [ + PasswordAuth(), + NodeGroup.fields['name'], + NodeGroup.fields['description'], + NodeGroup.fields['is_custom'], + update_fields + ] + + returns = Parameter(int, '1 if successful') + + def call(self, auth, name, description, optional_vals = {}): + 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 add node groups" + + #creat node group + node_group = NodeGroup(self.api, optional_vals) + node_group['name'] = name + node_group['description'] = description + node_group.flush() + + return node_group['nodegroup_id'] diff --git a/PLC/Methods/AdmDeleteNodeGroup.py b/PLC/Methods/AdmDeleteNodeGroup.py new file mode 100644 index 0000000..d780e59 --- /dev/null +++ b/PLC/Methods/AdmDeleteNodeGroup.py @@ -0,0 +1,42 @@ +from PLC.Faults import * +from PLC.Method import Method +from PLC.Parameter import Parameter, Mixed +from PLC.Auth import PasswordAuth +from PLC.Nodes import Node, Nodes +from PLC.NodeGroups import NodeGroup, NodeGroups + +class AdmDeleteNodeGroup(Method): + """ + Delete an existing Node Group. + + Admins my delete any node group + + Returns 1 if successful, faults otherwise. + """ + + roles = ['admin'] + + accepts = [ + PasswordAuth(), + NodeGroup.fields['nodegroup_id'], + ] + + returns = Parameter(int, '1 if successful') + + def call(self, auth, node_group_id): + # Get account information + nodegroups = NodeGroups(self.api, [node_group_id]) + if not nodegroups: + raise PLCInvalidArgument, "No such node group" + + nodegroup = nodegroups.values()[0] + + # If we are not an admin, make sure that the caller is a + # member of the site at which the node is located. + if 'admin' not in self.caller['roles']: + # Authenticated function + raise PLCPermissionDenied, "Not allowed to delete nodes groups" + + nodegroup.delete() + + return 1 diff --git a/PLC/Methods/AdmGetNodeGroupNodes.py b/PLC/Methods/AdmGetNodeGroupNodes.py new file mode 100644 index 0000000..8e845a6 --- /dev/null +++ b/PLC/Methods/AdmGetNodeGroupNodes.py @@ -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 AdmGetNodeGroupNodes(Method): + """ + Return a list of node_ids for the node group specified. + + """ + + roles = ['admin', 'pi', 'user', 'tech'] + + accepts = [ + PasswordAuth(), + NodeGroup.fields['nodegroup_id'] + ] + + returns = [NodeGroup.join_fields['node_ids']] + + 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): + # Authenticated function + assert self.caller is not None + + # Get nodes in this nodegroup + nodegroup = NodeGroups(self.api, [nodegroup_id]) + + # make sure sites are found + if not nodegroup: + raise PLCInvalidArgument, "No such site" + + #get the info for the node group specified + nodegroup_values = nodegroup.values()[0] + + #grab the list of node ides fromt the disctioary + node_ids = nodegroup_values['node_ids'] + + return node_ids -- 2.45.2