# # Functions for interacting with the nodegroups table in the database # # from types import StringTypes from PLC.Faults import * from PLC.Parameter import Parameter, Mixed from PLC.Debug import profile from PLC.Storage.AlchemyObject import AlchemyObj class NodeGroup(AlchemyObj): """ Representation of a row in the nodegroups table. To use, optionally instantiate with a dict of values. Update as you would a dict. Commit to the database with sync(). """ tablename = 'nodegroups' join_tables = ['conf_file_nodegroup'] fields = { 'nodegroup_id': Parameter(int, "Node group identifier", primary_key=True), 'groupname': Parameter(str, "Node group name", max = 50), 'tag_type_id': Parameter (int, "Node tag type id"), 'value' : Parameter(str, "value that the nodegroup definition is based upon"), 'tagname' : Parameter(str, "Tag name that the nodegroup definition is based upon"), 'conf_file_ids': Parameter([int], "List of configuration files specific to this node group", joined=True), 'node_ids' : Parameter([int], "List of node_ids that belong to this nodegroup", joined=True), } def validate_name(self, name): # Make sure name is not blank if not len(name): raise PLCInvalidArgument, "Invalid node group name" # Make sure node group does not alredy exist conflicts = NodeGroups(self.api, name) for nodegroup in conflicts: if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup['nodegroup_id']: raise PLCInvalidArgument, "Node group name already in use" return name def sync(self, commit=True, validate=True): AlchemyObj.sync(self, commit, validate) if 'nodegroup_id' not in self: AlchemyObj.insert(self, dict(self)) else: AlchemyObj.update(self, {'nodegroup_id': self['nodegroup_id']}, dict(self)) def delete(self, commit=True): assert 'nodegroup_id' in self AlchemyObj.delete(self, dict(self)) class NodeGroups(list): """ Representation of row(s) from the nodegroups table in the database. """ def __init__(self, api, nodegroup_filter = None, columns = None): if not nodegroup_filter is not None: nodegroups = NodeGroup().select() if isinstance(nodegroup_filter, (list, tuple, set)): # Separate the list into integers and strings ints = filter(lambda x: isinstance(x, (int, long)), nodegroup_filter) strs = filter(lambda x: isinstance(x, StringTypes), nodegroup_filter) nodegroups = NodeGroup().select(filter={'nodegroup_id': ints, 'groupname': strs}) elif isinstance(nodegroup_filter, dict): nodegroups = NodeGroup().select(filter=nodegroup_filter) elif isinstance(nodegroup_filter, (int, long)): nodegroups = NodeGroup().select(filter={'nodegroup_id': nodegroup_filter}) elif isinstance(nodegroup_filter, StringTypes): nodegroups = NodeGroup().select(filter={'groupname': nodegroup_filter}) else: raise PLCInvalidArgument, "Wrong node group filter %r"%nodegroup_filter for nodegroup in nodegroups: self.append(nodegroup)