ac3bbd59bfea3c68841e5f483e15141f1cc4ec2e
[plcapi.git] / PLC / Methods / DeleteConfFileFromNode.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.Nodes import Node, Nodes
7 from PLC.Auth import Auth
8
9 class DeleteConfFileFromNode(Method):
10     """
11     Deletes a configuration file from the specified node. If the node
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(Node.fields['node_id'],
23               Node.fields['hostname'])
24         ]
25
26     returns = Parameter(int, '1 if successful')
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.event_objects = {'ConfFile': [conf_file_id], 
47                               'Node': [node['node_id']]}
48
49         return 1