#
# Functions for interacting with the nodegroups table in the database
#
-# Mark Huang <mlhuang@cs.princeton.edu>
-# Copyright (C) 2006 The Trustees of Princeton University
-#
-# $Id: NodeGroups.py,v 1.3 2006/09/07 23:44:49 mlhuang Exp $
#
from types import StringTypes
from PLC.Faults import *
-from PLC.Parameter import Parameter
+from PLC.Parameter import Parameter, Mixed
from PLC.Debug import profile
-from PLC.Table import Row, Table
+from PLC.Storage.AlchemyObject import AlchemyObj
from PLC.Nodes import Node, Nodes
class NodeGroup(Row):
"""
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 flush().
+ dict. Commit to the database with sync().
"""
+ tablename = 'nodegroups'
+ join_tables = ['conf_file_nodegroup']
fields = {
- 'nodegroup_id': Parameter(int, "Node group identifier"),
- 'name': Parameter(str, "Node group name"),
- 'description': Parameter(str, "Node group description"),
- 'is_custom': Parameter(bool, "Is a custom node group (i.e., is not a site node group)")
+ 'nodegroup_id': Parameter(int, "Node group identifier", primary_key),
+ '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),
}
-
- # These fields are derived from join tables and are not
- # actually in the nodegroups table.
- join_fields = {
- 'node_ids': Parameter([int], "List of nodes in this node group"),
+ related_fields = {
}
- def __init__(self, api, fields):
- Row.__init__(self, fields)
- self.api = api
-
def validate_name(self, name):
- conflicts = NodeGroups(self.api, [name])
- for nodegroup_id in conflicts:
- if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup_id:
- raise PLCInvalidArgument, "Node group name already in use"
- return name
-
-
- def add_node(self, node, commit = True):
- """
- Add node to existing nodegroup.
- """
-
- assert 'nodegroup_id' in self
- assert isinstance(node, Node)
- assert 'node_id' in node
-
- node_id = node['node_id']
- nodegroup_id = self['nodegroup_id']
- self.api.db.do("INSERT INTO nodegroup_nodes (nodegroup_id, node_id)" \
- " VALUES(%(nodegroup_id)d, %(node_id)d)",
- locals())
-
- if commit:
- self.api.db.commit()
-
- if 'node_ids' in self and node_id not in self['node_ids']:
- self['node_ids'].append(node_id)
-
- if 'nodegroup_ids' in node and nodegroup_id not in node['nodegroup_ids']:
- node['nodegroup_ids'].append(nodegroup_id)
-
- def remove_node(self, node, commit = True):
- """
- Remove node from existing nodegroup.
- """
-
- assert 'nodegroup_id' in self
- assert isinstance(node, Node)
- assert 'node_id' in node
-
- node_id = node['node_id']
- nodegroup_id = self['nodegroup_id']
- self.api.db.do("INSERT INTO nodegroup_nodes (nodegroup_id, node_id)" \
- " VALUES(%(nodegroup_id)d, %(node_id)d)",
- locals())
-
- if commit:
- self.api.db.commit()
-
- if 'node_ids' in self and node_id not in self['node_ids']:
- self['node_ids'].append(node_id)
+ # Make sure name is not blank
+ if not len(name):
+ raise PLCInvalidArgument, "Invalid node group name"
- if 'nodegroup_ids' in node and nodegroup_id not in node['nodegroup_ids']:
- node['nodegroup_ids'].append(nodegroup_id)
+ # 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"
- def flush(self, commit = True):
- """
- Flush changes back to the database.
- """
+ return name
- self.validate()
-
- # Fetch a new nodegroup_id if necessary
+ def sync(self, commit=True, validate=True):
+ AlchemyObj.sync(self, commit, validate)
if 'nodegroup_id' not in self:
- rows = self.api.db.selectall("SELECT NEXTVAL('nodegroups_nodegroup_id_seq') AS nodegroup_id")
- if not rows:
- raise PLCDBError, "Unable to fetch new nodegroup_id"
- self['nodegroup_id'] = rows[0]['nodegroup_id']
- insert = True
- else:
- insert = False
-
- # Filter out fields that cannot be set or updated directly
- fields = dict(filter(lambda (key, value): key in self.fields,
- self.items()))
-
- # Parameterize for safety
- keys = fields.keys()
- values = [self.api.db.param(key, value) for (key, value) in fields.items()]
-
- if insert:
- # Insert new row in nodegroups table
- sql = "INSERT INTO nodegroups (%s) VALUES (%s)" % \
- (", ".join(keys), ", ".join(values))
+ AlchemyObj.insert(self, dict(self))
else:
- # Update existing row in sites table
- columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
- sql = "UPDATE nodegroups SET " + \
- ", ".join(columns) + \
- " WHERE nodegroup_id = %(nodegroup_id)d"
-
- self.api.db.do(sql, fields)
-
- if commit:
- self.api.db.commit()
-
-
- def delete(self, commit = True):
- """
- Delete existing nodegroup from the database.
- """
+ 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))
- # Delete ourself
- tables = ['nodegroup_nodes', 'override_bootscripts',
- 'conf_assoc', 'node_root_access']
-
- if self['is_custom']:
- tables.append('nodegroups')
- else:
- # XXX Cannot delete site node groups yet
- pass
-
- for table in tables:
- self.api.db.do("DELETE FROM %s" \
- " WHERE nodegroup_id = %(nodegroup_id)" % \
- table, self)
-
- if commit:
- self.api.db.commit()
-
-class NodeGroups(Table):
+class NodeGroups(list):
"""
Representation of row(s) from the nodegroups table in the
database.
"""
- def __init__(self, api, nodegroup_id_or_name_list = None):
- self.api = api
-
- # N.B.: Node IDs returned may be deleted.
- sql = "SELECT nodegroups.*, nodegroup_nodes.node_id" \
- " FROM nodegroups" \
- " LEFT JOIN nodegroup_nodes USING (nodegroup_id)"
+ def __init__(self, api, nodegroup_filter = None, columns = None):
- if nodegroup_id_or_name_list:
+ if not nodegroup_filter is not None:
+ nodegroups = NodeGroup().select()
+ if isinstance(nodegroup_filter, (list, tuple, set)):
# 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)).lower()
- sql += ")"
+ 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
- rows = self.api.db.selectall(sql)
- for row in rows:
- if self.has_key(row['nodegroup_id']):
- nodegroup = self[row['nodegroup_id']]
- nodegroup.update(row)
- else:
- self[row['nodegroup_id']] = NodeGroup(api, row)
+ for nodegroup in nodegroups:
+ self.append(nodegroup)