updated parameters
[plcapi.git] / PLC / NodeGroups.py
1 #
2 # Functions for interacting with the nodegroups table in the database
3 #
4 #
5
6 from types import StringTypes
7
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Debug import profile
11 from PLC.Storage.AlchemyObject import AlchemyObj
12 from PLC.Nodes import Node, Nodes
13
14 class NodeGroup(Row):
15     """
16     Representation of a row in the nodegroups table. To use, optionally
17     instantiate with a dict of values. Update as you would a
18     dict. Commit to the database with sync().
19     """
20
21     tablename = 'nodegroups'
22     join_tables = ['conf_file_nodegroup']
23     fields = {
24         'nodegroup_id': Parameter(int, "Node group identifier", primary_key),
25         'groupname': Parameter(str, "Node group name", max = 50),
26         'tag_type_id': Parameter (int, "Node tag type id"),
27         'value' : Parameter(str, "value that the nodegroup definition is based upon"),
28         'tagname' : Parameter(str, "Tag name that the nodegroup definition is based upon"),
29         'conf_file_ids': Parameter([int], "List of configuration files specific to this node group", joined=True),
30         'node_ids' : Parameter([int], "List of node_ids that belong to this nodegroup", joined=True),
31         }
32     related_fields = {
33         }
34
35     def validate_name(self, name):
36         # Make sure name is not blank
37         if not len(name):
38             raise PLCInvalidArgument, "Invalid node group name"
39
40         # Make sure node group does not alredy exist
41         conflicts = NodeGroups(self.api, name)
42         for nodegroup in conflicts:
43             if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup['nodegroup_id']:
44                 raise PLCInvalidArgument, "Node group name already in use"
45
46         return name
47
48     def sync(self, commit=True, validate=True):
49         AlchemyObj.sync(self, commit, validate)
50         if 'nodegroup_id' not in self:
51             AlchemyObj.insert(self, dict(self))
52         else:
53             AlchemyObj.update(self, {'nodegroup_id': self['nodegroup_id']}, dict(self))
54
55 class NodeGroups(list):
56     """
57     Representation of row(s) from the nodegroups table in the
58     database.
59     """
60
61     def __init__(self, api, nodegroup_filter = None, columns = None):
62
63         if not nodegroup_filter is not None:
64             nodegroups = NodeGroup().select()
65         if isinstance(nodegroup_filter, (list, tuple, set)):
66             # Separate the list into integers and strings
67             ints = filter(lambda x: isinstance(x, (int, long)), nodegroup_filter)
68             strs = filter(lambda x: isinstance(x, StringTypes), nodegroup_filter)
69             nodegroups = NodeGroup().select(filter={'nodegroup_id': ints, 'groupname': strs})
70         elif isinstance(nodegroup_filter, dict):
71             nodegroups = NodeGroup().select(filter=nodegroup_filter)
72         elif isinstance(nodegroup_filter, (int, long)):
73             nodegroups = NodeGroup().select(filter={'nodegroup_id': nodegroup_filter})
74         elif isinstance(nodegroup_filter, StringTypes):
75             nodegroups = NodeGroup().select(filter={'groupname': nodegroup_filter})
76         else:
77             raise PLCInvalidArgument, "Wrong node group filter %r"%nodegroup_filter
78
79         for nodegroup in nodegroups:
80             self.append(nodegroup)