reviewing the tags permission system
[plcapi.git] / PLC / TagTypes.py
1 # $Id$
2 # $Id$
3 # $URL$
4 #
5 # Thierry Parmentelat - INRIA
6 #
7 from types import StringTypes
8
9 from PLC.Faults import *
10 from PLC.Parameter import Parameter
11 from PLC.Filter import Filter
12 from PLC.Table import Row, Table
13 from PLC.Roles import Role, Roles
14
15 # xxx todo : deleting a tag type should delete the related nodegroup(s)
16
17 class TagType (Row):
18
19     """
20     Representation of a row in the tag_types table.
21     """
22
23     table_name = 'tag_types'
24     primary_key = 'tag_type_id'
25     join_tables = ['tag_type_role', 'node_tag', 'interface_tag', 'slice_tag', 'site_tag', 'person_tag' ]
26     fields = {
27         'tag_type_id': Parameter(int, "Node tag type identifier"),
28         'tagname': Parameter(str, "Node tag type name", max = 100),
29         'description': Parameter(str, "Node tag type description", max = 254),
30         'category' : Parameter (str, "Node tag category", max=64, optional=True),
31         'role_ids': Parameter([int], "List of role identifiers"),
32         'roles': Parameter([str], "List of roles"),
33         }
34
35     def validate_name(self, name):
36         if not len(name):
37             raise PLCInvalidArgument, "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, "tag type name already in use"
44
45         return name
46
47     add_role = Row.add_object(Role, 'tag_type_role')
48     remove_role = Row.remove_object(Role, 'tag_type_role')
49
50
51 class TagTypes(Table):
52     """
53     Representation of row(s) from the tag_types table
54     in the database.
55     """
56
57     def __init__(self, api, tag_type_filter = None, columns = None):
58         Table.__init__(self, api, TagType, columns)
59
60         sql = "SELECT %s FROM view_tag_types WHERE True" % \
61               ", ".join(self.columns)
62
63         if tag_type_filter is not None:
64             if isinstance(tag_type_filter, (list, tuple, set)):
65                 # Separate the list into integers and strings
66                 ints = filter(lambda x: isinstance(x, (int, long)), tag_type_filter)
67                 strs = filter(lambda x: isinstance(x, StringTypes), tag_type_filter)
68                 tag_type_filter = Filter(TagType.fields, {'tag_type_id': ints, 'tagname': strs})
69                 sql += " AND (%s) %s" % tag_type_filter.sql(api, "OR")
70             elif isinstance(tag_type_filter, dict):
71                 tag_type_filter = Filter(TagType.fields, tag_type_filter)
72                 sql += " AND (%s) %s" % tag_type_filter.sql(api, "AND")
73             elif isinstance(tag_type_filter, (int, long)):
74                 tag_type_filter = Filter(TagType.fields, {'tag_type_id':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 tag type filter %r"%tag_type_filter
81
82         self.selectall(sql)