step2 : basic functions for handling nodetags and nodegroups - still highly volatile
[plcapi.git] / PLC / NodeTagTypes.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 # $Revision: 9423 $
5 #
6 from types import StringTypes
7
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Filter import Filter
11 from PLC.Table import Row, Table
12 from PLC.Roles import Role, Roles
13
14 class NodeTagType (Row):
15
16     """
17     Representation of a row in the node_tag_types table.
18     """
19
20     table_name = 'node_tag_types'
21     primary_key = 'node_tag_type_id'
22     join_tables = ['node_tag']
23     fields = {
24         'node_tag_type_id': Parameter(int, "Node tag type identifier"),
25         'name': Parameter(str, "Node tag type name", max = 100),
26         'description': Parameter(str, "Node tag type description", max = 254),
27         'category' : Parameter (str, "Node tag category", max=64),
28         'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
29         }
30
31     # for Cache
32     class_key = 'name'
33     foreign_fields = ['category','description','min_role_id']
34     foreign_xrefs = []
35
36     def validate_name(self, name):
37         if not len(name):
38             raise PLCInvalidArgument, "node tag type name must be set"
39
40         conflicts = NodeTagTypes(self.api, [name])
41         for tag_type in conflicts:
42             if 'node_tag_type_id' not in self or \
43                    self['node_tag_type_id'] != tag_type['node_tag_type_id']:
44                 raise PLCInvalidArgument, "node tag type name already in use"
45
46         return name
47
48     def validate_min_role_id(self, role_id):
49         roles = [row['role_id'] for row in Roles(self.api)]
50         if role_id not in roles:
51             raise PLCInvalidArgument, "Invalid role"
52
53         return role_id
54
55 class NodeTagTypes(Table):
56     """
57     Representation of row(s) from the node_tag_types table
58     in the database.
59     """
60
61     def __init__(self, api, node_tag_type_filter = None, columns = None):
62         Table.__init__(self, api, NodeTagType, columns)
63
64         sql = "SELECT %s FROM node_tag_types WHERE True" % \
65               ", ".join(self.columns)
66
67         if node_tag_type_filter is not None:
68             if isinstance(node_tag_type_filter, (list, tuple, set)):
69                 # Separate the list into integers and strings
70                 ints = filter(lambda x: isinstance(x, (int, long)), node_tag_type_filter)
71                 strs = filter(lambda x: isinstance(x, StringTypes), node_tag_type_filter)
72                 node_tag_type_filter = Filter(NodeTagType.fields, {'node_tag_type_id': ints, 'name': strs})
73                 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "OR")
74             elif isinstance(node_tag_type_filter, dict):
75                 node_tag_type_filter = Filter(NodeTagType.fields, node_tag_type_filter)
76                 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "AND")
77             elif isinstance (node_tag_type_filter, StringTypes):
78                 node_tag_type_filter = Filter(NodeTagType.fields, {'name':[node_tag_type_filter]})
79                 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "AND")
80             else:
81                 raise PLCInvalidArgument, "Wrong node tag type filter %r"%node_tag_type_filter
82
83         self.selectall(sql)