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