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