Setting tag plcapi-5.4-2
[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 types import StringTypes
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter, Mixed
12 from PLC.Filter import Filter
13 from PLC.Debug import profile
14 from PLC.Table import Row, Table
15 from PLC.Nodes import Node, Nodes
16
17 class NodeGroup(Row):
18     """
19     Representation of a row in the nodegroups table. To use, optionally
20     instantiate with a dict of values. Update as you would a
21     dict. Commit to the database with sync().
22     """
23
24     table_name = 'nodegroups'
25     primary_key = 'nodegroup_id'
26     join_tables = ['conf_file_nodegroup']
27     primary_field = 'nodegroup_id'
28     fields = {
29         'nodegroup_id': Parameter(int, "Node group identifier"),
30         'groupname': Parameter(str, "Node group name", max = 50),
31         'tag_type_id': Parameter (int, "Node tag type id"),
32         'value' : Parameter(str, "value that the nodegroup definition is based upon"),
33         'tagname' : Parameter(str, "Tag name that the nodegroup definition is based upon"),
34         'conf_file_ids': Parameter([int], "List of configuration files specific to this node group"),
35         'node_ids' : Parameter([int], "List of node_ids that belong to this nodegroup"),
36         }
37     related_fields = {
38         }
39
40     def validate_name(self, name):
41         # Make sure name is not blank
42         if not len(name):
43             raise PLCInvalidArgument, "Invalid node group name"
44
45         # Make sure node group does not alredy exist
46         conflicts = NodeGroups(self.api, [name])
47         for nodegroup in conflicts:
48             if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup['nodegroup_id']:
49                 raise PLCInvalidArgument, "Node group name already in use"
50
51         return name
52
53     def associate_conf_files(self, auth, field, value):
54         """
55         Add conf_files found in value list (AddConfFileToNodeGroup)
56         Delets conf_files not found in value list (DeleteConfFileFromNodeGroup)
57         """
58
59         assert 'conf_file_ids' in self
60         assert 'nodegroup_id' in self
61         assert isinstance(value, list)
62
63         conf_file_ids = self.separate_types(value)[0]
64
65         if self['conf_file_ids'] != conf_file_ids:
66             from PLC.Methods.AddConfFileToNodeGroup import AddConfFileToNodeGroup
67             from PLC.Methods.DeleteConfFileFromNodeGroup import DeleteConfFileFromNodeGroup
68             new_conf_files = set(conf_file_ids).difference(self['conf_file_ids'])
69             stale_conf_files = set(self['conf_file_ids']).difference(conf_file_ids)
70
71             for new_conf_file in new_conf_files:
72                 AddConfFileToNodeGroup.__call__(AddConfFileToNodeGroup(self.api),
73                                                 auth, new_conf_file, self['nodegroup_id'])
74             for stale_conf_file in stale_conf_files:
75                 DeleteConfFileFromNodeGroup.__call__(DeleteConfFileFromNodeGroup(self.api),
76                                                      auth, stale_conf_file, self['nodegroup_id'])
77
78
79 class NodeGroups(Table):
80     """
81     Representation of row(s) from the nodegroups table in the
82     database.
83     """
84
85     def __init__(self, api, nodegroup_filter = None, columns = None):
86         Table.__init__(self, api, NodeGroup, columns)
87
88         sql = "SELECT %s FROM view_nodegroups WHERE True" % \
89               ", ".join(self.columns)
90
91         if nodegroup_filter is not None:
92             if isinstance(nodegroup_filter, (list, tuple, set)):
93                 # Separate the list into integers and strings
94                 ints = filter(lambda x: isinstance(x, (int, long)), nodegroup_filter)
95                 strs = filter(lambda x: isinstance(x, StringTypes), nodegroup_filter)
96                 nodegroup_filter = Filter(NodeGroup.fields, {'nodegroup_id': ints, 'groupname': strs})
97                 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "OR")
98             elif isinstance(nodegroup_filter, dict):
99                 nodegroup_filter = Filter(NodeGroup.fields, nodegroup_filter)
100                 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND")
101             elif isinstance(nodegroup_filter, (int, long)):
102                 nodegroup_filter = Filter(NodeGroup.fields, {'nodegroup_id': nodegroup_filter})
103                 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND")
104             elif isinstance(nodegroup_filter, StringTypes):
105                 nodegroup_filter = Filter(NodeGroup.fields, {'groupname': nodegroup_filter})
106                 sql += " AND (%s) %s" % nodegroup_filter.sql(api, "AND")
107             else:
108                 raise PLCInvalidArgument, "Wrong node group filter %r"%nodegroup_filter
109
110         self.selectall(sql)