# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2006 The Trustees of Princeton University
#
-# $Id: NodeGroups.py,v 1.11 2006/10/03 19:25:30 mlhuang Exp $
+# $Id: NodeGroups.py,v 1.18 2006/11/09 03:07:42 mlhuang Exp $
#
from types import StringTypes
from PLC.Faults import *
from PLC.Parameter import Parameter
+from PLC.Filter import Filter
from PLC.Debug import profile
from PLC.Table import Row, Table
from PLC.Nodes import Node, Nodes
table_name = 'nodegroups'
primary_key = 'nodegroup_id'
+ join_tables = ['nodegroup_node', 'conf_file_nodegroup']
fields = {
'nodegroup_id': Parameter(int, "Node group identifier"),
'name': Parameter(str, "Node group name", max = 50),
- 'description': Parameter(str, "Node group description", max = 200),
+ 'description': Parameter(str, "Node group description", max = 200, nullok = True),
'node_ids': Parameter([int], "List of nodes in this node group"),
+ 'conf_file_ids': Parameter([int], "List of configuration files specific to this node group"),
}
- def __init__(self, api, fields = {}):
- Row.__init__(self, fields)
- self.api = api
-
def validate_name(self, name):
- # Remove leading and trailing spaces
- name = name.strip()
-
- # Make sure name is not blank after we removed the spaces
- if not len(name) > 0:
+ # Make sure name is not blank
+ if not len(name):
raise PLCInvalidArgument, "Invalid node group name"
# Make sure node group does not alredy exist
self['node_ids'].remove(node_id)
node['nodegroup_ids'].remove(nodegroup_id)
- def delete(self, commit = True):
- """
- Delete existing nodegroup from the database.
- """
-
- assert 'nodegroup_id' in self
-
- # Clean up miscellaneous join tables
- for table in ['nodegroup_node', 'nodegroups']:
- self.api.db.do("DELETE FROM %s" \
- " WHERE nodegroup_id = %d" % \
- (table, self['nodegroup_id']), self)
-
- if commit:
- self.api.db.commit()
-
class NodeGroups(Table):
"""
Representation of row(s) from the nodegroups table in the
database.
"""
- def __init__(self, api, nodegroup_id_or_name_list = None):
- self.api = api
-
- sql = "SELECT %s FROM view_nodegroups" % \
- ", ".join(NodeGroup.fields)
-
- if nodegroup_id_or_name_list:
- # Separate the list into integers and strings
- nodegroup_ids = filter(lambda nodegroup_id: isinstance(nodegroup_id, (int, long)),
- nodegroup_id_or_name_list)
- names = filter(lambda name: isinstance(name, StringTypes),
- nodegroup_id_or_name_list)
- sql += " WHERE (False"
- if nodegroup_ids:
- sql += " OR nodegroup_id IN (%s)" % ", ".join(map(str, nodegroup_ids))
- if names:
- sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
- sql += ")"
-
- rows = self.api.db.selectall(sql)
-
- for row in rows:
- self[row['nodegroup_id']] = nodegroup = NodeGroup(api, row)
- for aggregate in ['node_ids']:
- if not nodegroup.has_key(aggregate) or nodegroup[aggregate] is None:
- nodegroup[aggregate] = []
- else:
- nodegroup[aggregate] = map(int, nodegroup[aggregate].split(','))
+ def __init__(self, api, nodegroup_filter = None, columns = None):
+ Table.__init__(self, api, NodeGroup, columns)
+
+ sql = "SELECT %s FROM view_nodegroups WHERE True" % \
+ ", ".join(self.columns)
+
+ if nodegroup_filter is not None:
+ 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)
+ nodegroup_filter = Filter(NodeGroup.fields, {'nodegroup_id': ints, 'name': strs})
+ sql += " AND (%s)" % nodegroup_filter.sql(api, "OR")
+ elif isinstance(nodegroup_filter, dict):
+ nodegroup_filter = Filter(NodeGroup.fields, nodegroup_filter)
+ sql += " AND (%s)" % nodegroup_filter.sql(api, "AND")
+
+ self.selectall(sql)