- added class variables: event_type, object_type, object_ids (used by event logger)
[plcapi.git] / PLC / Methods / AddNodeToNodeGroup.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.NodeGroups import NodeGroup, NodeGroups
5 from PLC.Nodes import Node, Nodes
6 from PLC.Auth import PasswordAuth
7
8 class AddNodeToNodeGroup(Method):
9     """
10     Add a node to the specified node group. If the node is
11     already a member of the nodegroup, no errors are returned.
12
13     Returns 1 if successful, faults otherwise.
14     """
15
16     roles = ['admin']
17
18     accepts = [
19         PasswordAuth(),
20         Mixed(NodeGroup.fields['nodegroup_id'],
21               NodeGroup.fields['name']),
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 = 'NodeGroup'
30     object_ids = []
31
32     def call(self, auth, nodegroup_id_or_name, node_id_or_hostname):
33         # Get node info
34         nodes = Nodes(self.api, [node_id_or_hostname])
35         if not nodes:
36                 raise PLCInvalidArgument, "No such node"
37         node = nodes.values()[0]
38
39         # Get nodegroup info
40         nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
41         if not nodegroups:
42             raise PLCInvalidArgument, "No such nodegroup"
43
44         nodegroup = nodegroups.values()[0]
45         
46         # add node to nodegroup
47         if node['node_id'] not in nodegroup['node_ids']:
48             nodegroup.add_node(node)
49         self.object_ids = [nodegroup['nodegroup_id']]
50
51         return 1