initial checkin
[plcapi.git] / PLC / NodeGroups.py
1 #
2 # Functions for interacting with the nodegroups table in the database
3 #
4 #
5
6 from types import StringTypes
7
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Debug import profile
11 from PLC.Storage.AlchemyObject import AlchemyObj
12 from PLC.Nodes import Node, Nodes
13
14 class NodeGroup(Row):
15     """
16     Representation of a row in the nodegroups table. To use, optionally
17     instantiate with a dict of values. Update as you would a
18     dict. Commit to the database with sync().
19     """
20
21     tablename = 'nodegroups'
22     join_tables = ['conf_file_nodegroup']
23     fields = {
24         'nodegroup_id': Parameter(int, "Node group identifier", primary_key),
25         'groupname': Parameter(str, "Node group name", max = 50),
26         'tag_type_id': Parameter (int, "Node tag type id"),
27         'value' : Parameter(str, "value that the nodegroup definition is based upon"),
28         'tagname' : Parameter(str, "Tag name that the nodegroup definition is based upon"),
29         'conf_file_ids': Parameter([int], "List of configuration files specific to this node group", joined=True),
30         'node_ids' : Parameter([int], "List of node_ids that belong to this nodegroup", joined=True),
31         }
32     related_fields = {
33         }
34
35     def validate_name(self, name):
36         # Make sure name is not blank
37         if not len(name):
38             raise PLCInvalidArgument, "Invalid node group name"
39
40         # Make sure node group does not alredy exist
41         conflicts = NodeGroups(self.api, name)
42         for nodegroup in conflicts:
43             if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup['nodegroup_id']:
44                 raise PLCInvalidArgument, "Node group name already in use"
45
46         return name
47
48     def sync(self, commit=True, validate=True):
49         AlchemyObj.sync(self, commit, validate)
50         if 'nodegroup_id' not in self:
51             AlchemyObj.insert(self, dict(self))
52         else:
53             AlchemyObj.update(self, {'nodegroup_id': self['nodegroup_id']}, dict(self))
54
55     def delete(self, commit=True):
56         assert 'nodegroup_id' in self
57         AlchemyObj.delete(self, dict(self))
58
59 class NodeGroups(list):
60     """
61     Representation of row(s) from the nodegroups table in the
62     database.
63     """
64
65     def __init__(self, api, nodegroup_filter = None, columns = None):
66
67         if not nodegroup_filter is not None:
68             nodegroups = NodeGroup().select()
69         if isinstance(nodegroup_filter, (list, tuple, set)):
70             # Separate the list into integers and strings
71             ints = filter(lambda x: isinstance(x, (int, long)), nodegroup_filter)
72             strs = filter(lambda x: isinstance(x, StringTypes), nodegroup_filter)
73             nodegroups = NodeGroup().select(filter={'nodegroup_id': ints, 'groupname': strs})
74         elif isinstance(nodegroup_filter, dict):
75             nodegroups = NodeGroup().select(filter=nodegroup_filter)
76         elif isinstance(nodegroup_filter, (int, long)):
77             nodegroups = NodeGroup().select(filter={'nodegroup_id': nodegroup_filter})
78         elif isinstance(nodegroup_filter, StringTypes):
79             nodegroups = NodeGroup().select(filter={'groupname': nodegroup_filter})
80         else:
81             raise PLCInvalidArgument, "Wrong node group filter %r"%nodegroup_filter
82
83         for nodegroup in nodegroups:
84             self.append(nodegroup)