StringTypes has gone
[plcapi.git] / PLC / NodeGroups.py
1 #
2 # Functions for interacting with the nodegroups table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
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
14
15 class NodeGroup(Row):
16     """
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().
20     """
21
22     table_name = 'nodegroups'
23     primary_key = 'nodegroup_id'
24     join_tables = ['conf_file_nodegroup']
25     primary_field = 'nodegroup_id'
26     fields = {
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"),
34         }
35     related_fields = {
36         }
37
38     def validate_name(self, name):
39         # Make sure name is not blank
40         if not len(name):
41             raise PLCInvalidArgument("Invalid node group name")
42
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")
48
49         return name
50
51     def associate_conf_files(self, auth, field, value):
52         """
53         Add conf_files found in value list (AddConfFileToNodeGroup)
54         Delets conf_files not found in value list (DeleteConfFileFromNodeGroup)
55         """
56
57         assert 'conf_file_ids' in self
58         assert 'nodegroup_id' in self
59         assert isinstance(value, list)
60
61         conf_file_ids = self.separate_types(value)[0]
62
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)
68
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'])
75
76
77 class NodeGroups(Table):
78     """
79     Representation of row(s) from the nodegroups table in the
80     database.
81     """
82
83     def __init__(self, api, nodegroup_filter = None, columns = None):
84         Table.__init__(self, api, NodeGroup, columns)
85
86         sql = "SELECT %s FROM view_nodegroups WHERE True" % \
87               ", ".join(self.columns)
88
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")
105             else:
106                 raise PLCInvalidArgument("Wrong node group filter %r"%nodegroup_filter)
107
108         self.selectall(sql)