now accepts nodegroup_id or name (previously only accepted nodegroup_id)
[plcapi.git] / PLC / Methods / AdmDeleteNodeGroup.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import PasswordAuth
5 from PLC.Nodes import Node, Nodes
6 from PLC.NodeGroups import NodeGroup, NodeGroups
7
8 class AdmDeleteNodeGroup(Method):
9     """
10     Delete an existing Node Group.
11
12     Admins my delete any node group
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     accepts = [
20         PasswordAuth(),
21         Mixed(NodeGroup.fields['nodegroup_id'],
22               NodeGroup.fields['name'])
23         ]
24
25     returns = Parameter(int, '1 if successful')
26
27     def call(self, auth, node_group_id_or_name):
28         # Get account information
29         nodegroups = NodeGroups(self.api, [node_group_id_or_name])
30         if not nodegroups:
31             raise PLCInvalidArgument, "No such node group"
32
33         nodegroup = nodegroups.values()[0]
34
35         # If we are not an admin, make sure that the caller is a
36         # member of the site at which the node is located.
37         if 'admin' not in self.caller['roles']:
38             # Authenticated function
39             raise PLCPermissionDenied, "Not allowed to delete nodes groups"
40
41         nodegroup.delete()
42
43         return 1