651c7b1b014b419f488a92a456fa352a4ae3bdfc
[plcapi.git] / PLC / Methods / AddNodeGroup.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Auth import Auth
6
7 from PLC.NodeGroups import NodeGroup, NodeGroups
8 from PLC.TagTypes import TagType, TagTypes
9 from PLC.NodeTags import NodeTag, NodeTags
10
11 can_update = lambda (field, value): field in NodeGroup.fields.keys() and field != NodeGroup.primary_field
12
13 class AddNodeGroup(Method):
14     """
15     Adds a new node group. Any values specified in nodegroup_fields
16     are used, otherwise defaults are used.
17
18     Returns the new nodegroup_id (> 0) if successful, faults otherwise.
19     """
20
21     roles = ['admin']
22
23     nodegroup_fields = dict(filter(can_update, NodeGroup.fields.items()))
24
25     accepts = [
26         Auth(),
27         NodeGroup.fields['groupname'],
28         Mixed(TagType.fields['tag_type_id'],
29               TagType.fields['tagname']),
30         NodeTag.fields['value'],
31         ]
32
33     returns = Parameter(int, 'New nodegroup_id (> 0) if successful')
34
35
36     def call(self, auth, groupname, tag_type_id_or_tagname, value):
37         # locate tag type
38         tag_types = TagTypes (self.api,[tag_type_id_or_tagname])
39         if not(tag_types):
40             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_tagname
41         tag_type=tag_types[0]
42
43         nodegroup_fields = { 'groupname' : groupname,
44                              'tag_type_id' : tag_type['tag_type_id'],
45                              'value' : value }
46         nodegroup = NodeGroup(self.api, nodegroup_fields)
47         nodegroup.sync()
48
49         # Logging variables
50         self.event_objects = {'NodeGroup': [nodegroup['nodegroup_id']]}
51         self.message = 'Node group %d created' % nodegroup['nodegroup_id']
52  
53         return nodegroup['nodegroup_id']