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