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