dd3bcd5482941b2ad847b88b8a2bc2d64d8fd299
[plcapi.git] / PLC / Methods / AddConfFileToNodeGroup.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.NodeGroups import NodeGroup, NodeGroups
7 from PLC.Auth import Auth
8
9 class AddConfFileToNodeGroup(Method):
10     """
11     Adds a configuration file to the specified node group. If the node
12     group is already linked to the configuration file, no errors are
13     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(NodeGroup.fields['nodegroup_id'],
24               NodeGroup.fields['groupname'])
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29
30     def call(self, auth, conf_file_id, nodegroup_id_or_name):
31         # Get configuration file
32         conf_files = ConfFiles(self.api, [conf_file_id])
33         if not conf_files:
34             raise PLCInvalidArgument, "No such configuration file"
35         conf_file = conf_files[0]
36
37         # Get node
38         nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
39         if not nodegroups:
40             raise PLCInvalidArgument, "No such node group"
41         nodegroup = nodegroups[0]
42         
43         # Link configuration file to node
44         if nodegroup['nodegroup_id'] not in conf_file['nodegroup_ids']:
45             conf_file.add_nodegroup(nodegroup)
46
47         # Log affected objects
48         self.event_objects = {'ConfFile': [conf_file_id], 
49                               'NodeGroup': [nodegroup['nodegroup_id']] }
50
51         return 1