4 # Thierry Parmentelat - INRIA
6 from types import StringTypes
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
14 # xxx todo : deleting a tag type should delete the related nodegroup(s)
19 Representation of a row in the tag_types table.
22 table_name = 'tag_types'
23 primary_key = 'tag_type_id'
24 join_tables = ['node_tag', 'interface_tag', 'slice_tag', 'site_tag', 'person_tag' ]
26 'tag_type_id': Parameter(int, "Node tag type identifier"),
27 'tagname': Parameter(str, "Node tag type name", max = 100),
28 'description': Parameter(str, "Node tag type description", max = 254),
29 'category' : Parameter (str, "Node tag category", max=64, optional=True),
30 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
33 def validate_name(self, name):
35 raise PLCInvalidArgument, "node tag type name must be set"
37 conflicts = TagTypes(self.api, [name])
38 for tag_type in conflicts:
39 if 'tag_type_id' not in self or \
40 self['tag_type_id'] != tag_type['tag_type_id']:
41 raise PLCInvalidArgument, "node tag type name already in use"
45 def validate_min_role_id(self, role_id):
46 roles = [row['role_id'] for row in Roles(self.api)]
47 if role_id not in roles:
48 raise PLCInvalidArgument, "Invalid role"
52 class TagTypes(Table):
54 Representation of row(s) from the tag_types table
58 def __init__(self, api, tag_type_filter = None, columns = None):
59 Table.__init__(self, api, TagType, columns)
61 sql = "SELECT %s FROM tag_types WHERE True" % \
62 ", ".join(self.columns)
64 if tag_type_filter is not None:
65 if isinstance(tag_type_filter, (list, tuple, set)):
66 # Separate the list into integers and strings
67 ints = filter(lambda x: isinstance(x, (int, long)), tag_type_filter)
68 strs = filter(lambda x: isinstance(x, StringTypes), tag_type_filter)
69 tag_type_filter = Filter(TagType.fields, {'tag_type_id': ints, 'tagname': strs})
70 sql += " AND (%s) %s" % tag_type_filter.sql(api, "OR")
71 elif isinstance(tag_type_filter, dict):
72 tag_type_filter = Filter(TagType.fields, tag_type_filter)
73 sql += " AND (%s) %s" % tag_type_filter.sql(api, "AND")
74 elif isinstance (tag_type_filter, StringTypes):
75 tag_type_filter = Filter(TagType.fields, {'tagname':[tag_type_filter]})
76 sql += " AND (%s) %s" % tag_type_filter.sql(api, "AND")
78 raise PLCInvalidArgument, "Wrong node tag type filter %r"%tag_type_filter