c8eba2550bd1245ede58d9c9e30fcd193f18baef
[plcapi.git] / PLC / Methods / DeleteConfFileFromNode.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.Nodes import Node, Nodes
6 from PLC.Auth import Auth
7
8 class DeleteConfFileFromNode(Method):
9     """
10     Deletes a configuration file from the specified node. If the node
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(Node.fields['node_id'],
22               Node.fields['hostname'])
23         ]
24
25     returns = Parameter(int, '1 if successful')
26
27
28     def call(self, auth, conf_file_id, node_id_or_hostname):
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 node
36         nodes = Nodes(self.api, [node_id_or_hostname])
37         if not nodes:
38                 raise PLCInvalidArgument, "No such node"
39         node = nodes[0]
40         
41         # Link configuration file to node
42         if node['node_id'] in conf_file['node_ids']:
43             conf_file.remove_node(node)
44
45         # Log affected objects
46         self.object_ids = [conf_file_id, node['node_id']]
47
48         return 1