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