svn keywords
[plcapi.git] / PLC / Methods / DeleteConfFileFromNode.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.ConfFiles import ConfFile, ConfFiles
7 from PLC.Nodes import Node, Nodes
8 from PLC.Auth import Auth
9
10 class DeleteConfFileFromNode(Method):
11     """
12     Deletes a configuration file from the specified node. If the node
13     is not linked to the configuration file, no errors are returned.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     accepts = [
21         Auth(),
22         ConfFile.fields['conf_file_id'],
23         Mixed(Node.fields['node_id'],
24               Node.fields['hostname'])
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29     def call(self, auth, conf_file_id, node_id_or_hostname):
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 node
37         nodes = Nodes(self.api, [node_id_or_hostname])
38         if not nodes:
39                 raise PLCInvalidArgument, "No such node"
40         node = nodes[0]
41         
42         # Link configuration file to node
43         if node['node_id'] in conf_file['node_ids']:
44             conf_file.remove_node(node)
45
46         # Log affected objects
47         self.event_objects = {'ConfFile': [conf_file_id], 
48                               'Node': [node['node_id']]}
49
50         return 1