01a28d35a60cf8e380d1d39c1c2f2fbaeca2464f
[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(AlchemyObj):
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=True),
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
33     def validate_name(self, name):
34         # Make sure name is not blank
35         if not len(name):
36             raise PLCInvalidArgument, "Invalid node group name"
37
38         # Make sure node group does not alredy exist
39         conflicts = NodeGroups(self.api, name)
40         for nodegroup in conflicts:
41             if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup['nodegroup_id']:
42                 raise PLCInvalidArgument, "Node group name already in use"
43
44         return name
45
46     def sync(self, commit=True, validate=True):
47         AlchemyObj.sync(self, commit, validate)
48         if 'nodegroup_id' not in self:
49             AlchemyObj.insert(self, dict(self))
50         else:
51             AlchemyObj.update(self, {'nodegroup_id': self['nodegroup_id']}, dict(self))
52
53     def delete(self, commit=True):
54         assert 'nodegroup_id' in self
55         AlchemyObj.delete(self, dict(self))
56
57 class NodeGroups(list):
58     """
59     Representation of row(s) from the nodegroups table in the
60     database.
61     """
62
63     def __init__(self, api, nodegroup_filter = None, columns = None):
64
65         if not nodegroup_filter is not None:
66             nodegroups = NodeGroup().select()
67         if isinstance(nodegroup_filter, (list, tuple, set)):
68             # Separate the list into integers and strings
69             ints = filter(lambda x: isinstance(x, (int, long)), nodegroup_filter)
70             strs = filter(lambda x: isinstance(x, StringTypes), nodegroup_filter)
71             nodegroups = NodeGroup().select(filter={'nodegroup_id': ints, 'groupname': strs})
72         elif isinstance(nodegroup_filter, dict):
73             nodegroups = NodeGroup().select(filter=nodegroup_filter)
74         elif isinstance(nodegroup_filter, (int, long)):
75             nodegroups = NodeGroup().select(filter={'nodegroup_id': nodegroup_filter})
76         elif isinstance(nodegroup_filter, StringTypes):
77             nodegroups = NodeGroup().select(filter={'groupname': nodegroup_filter})
78         else:
79             raise PLCInvalidArgument, "Wrong node group filter %r"%nodegroup_filter
80
81         for nodegroup in nodegroups:
82             self.append(nodegroup)