d7292a881a1eb07c49a127ffbf54495a9db92922
[plcapi.git] / PLC / Methods / AddConfFileToNode.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 AddConfFileToNode(Method):
11     """
12     Adds a configuration file to the specified node. If the node is
13     already 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         if node['peer_id'] is not None:
43             raise PLCInvalidArgument, "Not a local node"
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.event_objects = {'ConfFile': [conf_file_id],
51                               'Node': [node['node_id']] }
52
53         return 1