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
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Filter import Filter
11 from PLC.Debug import profile
12 from PLC.Table import Row, Table
13 from PLC.Nodes import Node, Nodes
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 sync().
22 table_name = 'nodegroups'
23 primary_key = 'nodegroup_id'
24 join_tables = ['conf_file_nodegroup']
25 primary_field = 'nodegroup_id'
27 'nodegroup_id': Parameter(int, "Node group identifier"),
28 'groupname': Parameter(str, "Node group name", max = 50),
29 'tag_type_id': Parameter (int, "Node tag type id"),
30 'value' : Parameter(str, "value that the nodegroup definition is based upon"),
31 'tagname' : Parameter(str, "Tag name that the nodegroup definition is based upon"),
32 'conf_file_ids': Parameter([int], "List of configuration files specific to this node group"),
33 'node_ids' : Parameter([int], "List of node_ids that belong to this nodegroup"),
38 def validate_name(self, name):
39 # Make sure name is not blank
41 raise PLCInvalidArgument("Invalid node group name")
43 # Make sure node group does not alredy exist
44 conflicts = NodeGroups(self.api, [name])
45 for nodegroup in conflicts:
46 if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup['nodegroup_id']:
47 raise PLCInvalidArgument("Node group name already in use")
51 def associate_conf_files(self, auth, field, value):
53 Add conf_files found in value list (AddConfFileToNodeGroup)
54 Delets conf_files not found in value list (DeleteConfFileFromNodeGroup)
57 assert 'conf_file_ids' in self
58 assert 'nodegroup_id' in self
59 assert isinstance(value, list)
61 conf_file_ids = self.separate_types(value)[0]
63 if self['conf_file_ids'] != conf_file_ids:
64 from PLC.Methods.AddConfFileToNodeGroup import AddConfFileToNodeGroup
65 from PLC.Methods.DeleteConfFileFromNodeGroup import DeleteConfFileFromNodeGroup
66 new_conf_files = set(conf_file_ids).difference(self['conf_file_ids'])
67 stale_conf_files = set(self['conf_file_ids']).difference(conf_file_ids)
69 for new_conf_file in new_conf_files:
70 AddConfFileToNodeGroup.__call__(AddConfFileToNodeGroup(self.api),
71 auth, new_conf_file, self['nodegroup_id'])
72 for stale_conf_file in stale_conf_files:
73 DeleteConfFileFromNodeGroup.__call__(DeleteConfFileFromNodeGroup(self.api),
74 auth, stale_conf_file, self['nodegroup_id'])
77 class NodeGroups(Table):
79 Representation of row(s) from the nodegroups table in the
83 def __init__(self, api, nodegroup_filter = None, columns = None):
84 Table.__init__(self, api, NodeGroup, columns)
86 sql = "SELECT %s FROM view_nodegroups WHERE True" % \
87 ", ".join(self.columns)
89 if nodegroup_filter is not None:
90 if isinstance(nodegroup_filter, (list, tuple, set)):
91 # Separate the list into integers and strings
92 ints = [x for x in nodegroup_filter if isinstance(x, int)]
93 strs = [x for x in nodegroup_filter if isinstance(x, str)]
94 nodegroup_filter = Filter(NodeGroup.fields, {'nodegroup_id': ints, 'groupname': strs})
95 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "OR")
96 elif isinstance(nodegroup_filter, dict):
97 nodegroup_filter = Filter(NodeGroup.fields, nodegroup_filter)
98 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND")
99 elif isinstance(nodegroup_filter, int):
100 nodegroup_filter = Filter(NodeGroup.fields, {'nodegroup_id': nodegroup_filter})
101 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND")
102 elif isinstance(nodegroup_filter, str):
103 nodegroup_filter = Filter(NodeGroup.fields, {'groupname': nodegroup_filter})
104 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND")
106 raise PLCInvalidArgument("Wrong node group filter %r"%nodegroup_filter)