2 # Thierry Parmentelat - INRIA
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
10 # xxx todo : deleting a tag type should delete the related nodegroup(s)
15 Representation of a row in the tag_types table.
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' ]
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"),
30 def validate_name(self, name):
32 raise PLCInvalidArgument("tag type name must be set")
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")
42 add_role = Row.add_object(Role, 'tag_type_role')
43 remove_role = Row.remove_object(Role, 'tag_type_role')
46 class TagTypes(Table):
48 Representation of row(s) from the tag_types table
52 def __init__(self, api, tag_type_filter = None, columns = None):
53 Table.__init__(self, api, TagType, columns)
55 sql = "SELECT %s FROM view_tag_types WHERE True" % \
56 ", ".join(self.columns)
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")
75 raise PLCInvalidArgument("Wrong tag type filter %r"%tag_type_filter)