- - removed anything having to do with event_type/event_object
[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 Auth
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         Auth(),
20         Mixed(Node.fields['node_id'],
21               Node.fields['hostname']),
22         Mixed(NodeGroup.fields['nodegroup_id'],
23               NodeGroup.fields['name']),
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28
29     def call(self, auth, node_id_or_hostname, nodegroup_id_or_name):
30         # Get node info
31         nodes = Nodes(self.api, [node_id_or_hostname])
32         if not nodes:
33                 raise PLCInvalidArgument, "No such node"
34         node = nodes[0]
35
36         # Get nodegroup info
37         nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
38         if not nodegroups:
39             raise PLCInvalidArgument, "No such nodegroup"
40
41         nodegroup = nodegroups[0]
42         
43         # add node to nodegroup
44         if node['node_id'] not in nodegroup['node_ids']:
45             nodegroup.add_node(node)
46         self.object_ids = [nodegroup['nodegroup_id']]
47
48         return 1