2 # Functions for interacting with the nodegroups table in the database
6 from types import StringTypes
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
13 class NodeGroup(AlchemyObj):
15 Representation of a row in the nodegroups table. To use, optionally
16 instantiate with a dict of values. Update as you would a
17 dict. Commit to the database with sync().
20 tablename = 'nodegroups'
21 join_tables = ['conf_file_nodegroup']
23 'nodegroup_id': Parameter(int, "Node group identifier", primary_key=True),
24 'groupname': Parameter(str, "Node group name", max = 50),
25 'tag_type_id': Parameter (int, "Node tag type id"),
26 'value' : Parameter(str, "value that the nodegroup definition is based upon"),
27 'tagname' : Parameter(str, "Tag name that the nodegroup definition is based upon"),
28 'conf_file_ids': Parameter([int], "List of configuration files specific to this node group", joined=True),
29 'node_ids' : Parameter([int], "List of node_ids that belong to this nodegroup", joined=True),
32 def validate_name(self, name):
33 # Make sure name is not blank
35 raise PLCInvalidArgument, "Invalid node group name"
37 # Make sure node group does not alredy exist
38 conflicts = NodeGroups(self.api, name)
39 for nodegroup in conflicts:
40 if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup['nodegroup_id']:
41 raise PLCInvalidArgument, "Node group name already in use"
45 def sync(self, commit=True, validate=True):
46 AlchemyObj.sync(self, commit, validate)
47 if 'nodegroup_id' not in self:
48 AlchemyObj.insert(self, dict(self))
50 AlchemyObj.update(self, {'nodegroup_id': self['nodegroup_id']}, dict(self))
52 def delete(self, commit=True):
53 assert 'nodegroup_id' in self
54 AlchemyObj.delete(self, dict(self))
56 class NodeGroups(list):
58 Representation of row(s) from the nodegroups table in the
62 def __init__(self, api, nodegroup_filter = None, columns = None):
64 if not nodegroup_filter is not None:
65 nodegroups = NodeGroup().select()
66 if isinstance(nodegroup_filter, (list, tuple, set)):
67 # Separate the list into integers and strings
68 ints = filter(lambda x: isinstance(x, (int, long)), nodegroup_filter)
69 strs = filter(lambda x: isinstance(x, StringTypes), nodegroup_filter)
70 nodegroups = NodeGroup().select(filter={'nodegroup_id': ints, 'groupname': strs})
71 elif isinstance(nodegroup_filter, dict):
72 nodegroups = NodeGroup().select(filter=nodegroup_filter)
73 elif isinstance(nodegroup_filter, (int, long)):
74 nodegroups = NodeGroup().select(filter={'nodegroup_id': nodegroup_filter})
75 elif isinstance(nodegroup_filter, StringTypes):
76 nodegroups = NodeGroup().select(filter={'groupname': nodegroup_filter})
78 raise PLCInvalidArgument, "Wrong node group filter %r"%nodegroup_filter
80 for nodegroup in nodegroups:
81 self.append(nodegroup)