2 # Functions for interacting with the nodegroups table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Debug import profile
13 from PLC.Table import Row, Table
17 Representation of a row in the nodegroups table. To use, optionally
18 instantiate with a dict of values. Update as you would a
19 dict. Commit to the database with flush().
23 'nodegroup_id': Parameter(int, "Node group identifier"),
24 'name': Parameter(str, "Node group name"),
25 'description': Parameter(str, "Node group description"),
26 'is_custom': Parameter(bool, "Is a custom node group (i.e., is not a site node group)")
29 # These fields are derived from join tables and are not
30 # actually in the nodegroups table.
32 'node_ids': Parameter([int], "List of nodes in this node group"),
35 def __init__(self, api, fields):
36 Row.__init__(self, fields)
39 def validate_name(self, name):
40 conflicts = NodeGroups(self.api, [name])
41 for nodegroup_id in conflicts:
42 if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup_id:
43 raise PLCInvalidArgument, "Node group name already in use"
45 def flush(self, commit = True):
47 Flush changes back to the database.
52 # Fetch a new nodegroup_id if necessary
53 if 'nodegroup_id' not in self:
54 rows = self.api.db.selectall("SELECT NEXTVAL('nodegroups_nodegroup_id_seq') AS nodegroup_id")
56 raise PLCDBError, "Unable to fetch new nodegroup_id"
57 self['nodegroup_id'] = rows[0]['nodegroup_id']
62 # Filter out fields that cannot be set or updated directly
63 fields = dict(filter(lambda (key, value): key in self.fields,
66 # Parameterize for safety
68 values = [self.api.db.param(key, value) for (key, value) in fields.items()]
71 # Insert new row in nodegroups table
72 sql = "INSERT INTO nodegroups (%s) VALUES (%s)" % \
73 (", ".join(keys), ", ".join(values))
75 # Update existing row in sites table
76 columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
77 sql = "UPDATE nodegroups SET " + \
78 ", ".join(columns) + \
79 " WHERE nodegroup_id = %(nodegroup_id)d"
81 self.api.db.do(sql, fields)
86 def delete(self, commit = True):
88 Delete existing nodegroup from the database.
91 assert 'nodegroup_id' in self
94 tables = ['nodegroup_nodes', 'override_bootscripts',
95 'conf_assoc', 'node_root_access']
98 tables.append('nodegroups')
100 # XXX Cannot delete site node groups yet
104 self.api.db.do("DELETE FROM %s" \
105 " WHERE nodegroup_id = %(nodegroup_id)" % \
111 class NodeGroups(Table):
113 Representation of row(s) from the nodegroups table in the
117 def __init__(self, api, nodegroup_id_or_name_list = None):
120 # N.B.: Node IDs returned may be deleted.
121 sql = "SELECT nodegroups.*, nodegroup_nodes.node_id" \
123 " LEFT JOIN nodegroup_nodes USING (nodegroup_id)"
125 if nodegroup_id_or_name_list:
126 # Separate the list into integers and strings
127 nodegroup_ids = filter(lambda nodegroup_id: isinstance(nodegroup_id, (int, long)),
128 nodegroup_id_or_name_list)
129 names = filter(lambda name: isinstance(name, StringTypes),
130 nodegroup_id_or_name_list)
133 sql += " OR nodegroup_id IN (%s)" % ", ".join(map(str, nodegroup_ids))
135 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names)).lower()
138 rows = self.api.db.selectall(sql)
140 if self.has_key(row['nodegroup_id']):
141 nodegroup = self[row['nodegroup_id']]
142 nodegroup.update(row)
144 self[row['nodegroup_id']] = NodeGroup(api, row)