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