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