f832faab3c0c7731c5d6799b54a1c7557473ded5
[plcapi.git] / PLC / Methods / DeleteConfFileFromNodeGroup.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.ConfFiles import ConfFile, ConfFiles
6 from PLC.NodeGroups import NodeGroup, NodeGroups
7 from PLC.Auth import Auth
8
9 class DeleteConfFileFromNodeGroup(Method):
10     """
11     Deletes a configuration file from the specified nodegroup. If the nodegroup
12     is not linked to the configuration file, no errors are returned.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     accepts = [
20         Auth(),
21         ConfFile.fields['conf_file_id'],
22         Mixed(NodeGroup.fields['nodegroup_id'],
23               NodeGroup.fields['groupname'])
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28
29     def call(self, auth, conf_file_id, nodegroup_id_or_name):
30         # Get configuration file
31         conf_files = ConfFiles(self.api, [conf_file_id])
32         if not conf_files:
33             raise PLCInvalidArgument, "No such configuration file"
34         conf_file = conf_files[0]
35
36         # Get nodegroup
37         nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
38         if not nodegroups:
39                 raise PLCInvalidArgument, "No such nodegroup"
40         nodegroup = nodegroups[0]
41         
42         # Link configuration file to nodegroup
43         if nodegroup['nodegroup_id'] in conf_file['nodegroup_ids']:
44             conf_file.remove_nodegroup(nodegroup)
45
46         # Log affected objects
47         self.event_objects = {'ConfFile': [conf_file_id], 
48                               'NodeGroup': [nodegroup['nodegroup_id']]}
49
50         return 1