X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FNodeGroups.py;h=bbbb6b3027582a248cbc1408cc515ddcc36f179c;hb=19d4a01ccf66af9e00914351b3eacd5fc880f988;hp=aff2c8fe18f2af75c9104f048f06fb95279e800f;hpb=24d16d18acab3da7bccc3e09df4927e9cf2d3246;p=plcapi.git diff --git a/PLC/NodeGroups.py b/PLC/NodeGroups.py index aff2c8f..bbbb6b3 100644 --- a/PLC/NodeGroups.py +++ b/PLC/NodeGroups.py @@ -4,109 +4,77 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id$ -# + +from types import StringTypes from PLC.Faults import * -from PLC.Parameter import Parameter +from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Debug import profile from PLC.Table import Row, Table +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(). """ + table_name = 'nodegroups' + primary_key = 'nodegroup_id' + join_tables = ['conf_file_nodegroup'] + primary_field = 'nodegroup_id' 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)") + '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"), + 'node_ids' : Parameter([int], "List of node_ids that belong to this nodegroup"), } - - # 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): + # 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_id in conflicts: - if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup_id: + 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 - 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)) - 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): + def associate_conf_files(self, auth, field, value): """ - Delete existing nodegroup from the database. + Add conf_files found in value list (AddConfFileToNodeGroup) + Delets conf_files not found in value list (DeleteConfFileFromNodeGroup) """ + assert 'conf_file_ids' in self assert 'nodegroup_id' in self + assert isinstance(value, list) - # Delete ourself - tables = ['nodegroup_nodes', 'override_bootscripts', - 'conf_assoc', 'node_root_access'] + conf_file_ids = self.separate_types(value)[0] - if self['is_custom']: - tables.append('nodegroups') - else: - # XXX Cannot delete site node groups yet - pass + if self['conf_file_ids'] != conf_file_ids: + from PLC.Methods.AddConfFileToNodeGroup import AddConfFileToNodeGroup + from PLC.Methods.DeleteConfFileFromNodeGroup import DeleteConfFileFromNodeGroup + new_conf_files = set(conf_file_ids).difference(self['conf_file_ids']) + stale_conf_files = set(self['conf_file_ids']).difference(conf_file_ids) - for table in tables: - self.api.db.do("DELETE FROM %s" \ - " WHERE nodegroup_id = %(nodegroup_id)" % \ - table, self) + for new_conf_file in new_conf_files: + AddConfFileToNodeGroup.__call__(AddConfFileToNodeGroup(self.api), + auth, new_conf_file, self['nodegroup_id']) + for stale_conf_file in stale_conf_files: + DeleteConfFileFromNodeGroup.__call__(DeleteConfFileFromNodeGroup(self.api), + auth, stale_conf_file, self['nodegroup_id']) - if commit: - self.api.db.commit() class NodeGroups(Table): """ @@ -114,31 +82,29 @@ class NodeGroups(Table): 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)" - - 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 += " AND (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 += ")" - - 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) + 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, 'groupname': strs}) + sql += " AND (%s) %s" % nodegroup_filter.sql(api, "OR") + elif isinstance(nodegroup_filter, dict): + nodegroup_filter = Filter(NodeGroup.fields, nodegroup_filter) + sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND") + elif isinstance(nodegroup_filter, (int, long)): + nodegroup_filter = Filter(NodeGroup.fields, {'nodegroup_id': nodegroup_filter}) + sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND") + elif isinstance(nodegroup_filter, StringTypes): + nodegroup_filter = Filter(NodeGroup.fields, {'groupname': nodegroup_filter}) + sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND") else: - self[row['nodegroup_id']] = NodeGroup(api, row) + raise PLCInvalidArgument, "Wrong node group filter %r"%nodegroup_filter + + self.selectall(sql)