7ddb6b081d341f281a2a8b5079c5c5107cf97420
[plcapi.git] / PLC / Methods / AddConfFileToNode.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 AddConfFileToNode(Method):
9     """
10     Adds a configuration file to the specified node. If the node is
11     already 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     event_type = 'AddTo'
28     object_type = 'ConfFile'
29     object_ids = []
30
31     def call(self, auth, conf_file_id, node_id_or_hostname):
32         # Get configuration file
33         conf_files = ConfFiles(self.api, [conf_file_id])
34         if not conf_files:
35             raise PLCInvalidArgument, "No such configuration file"
36         conf_file = conf_files[0]
37
38         # Get node
39         nodes = Nodes(self.api, [node_id_or_hostname])
40         if not nodes:
41                 raise PLCInvalidArgument, "No such node"
42         node = nodes[0]
43         
44         # Link configuration file to node
45         if node['node_id'] not in conf_file['node_ids']:
46             conf_file.add_node(node)
47
48         # Log affected objects
49         self.object_ids = [conf_file_id, node['node_id']]
50
51         return 1