Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / PLC / SliceAttributeTypes.py
1 from types import StringTypes
2
3 from PLC.Faults import *
4 from PLC.Parameter import Parameter
5 from PLC.Filter import Filter
6 from PLC.Table import Row, Table
7 from PLC.Roles import Role, Roles
8
9 class SliceAttributeType(Row):
10     """
11     Representation of a row in the slice_attribute_types table. To
12     use, instantiate with a dict of values.
13     """
14
15     table_name = 'slice_attribute_types'
16     primary_key = 'attribute_type_id'
17     join_tables = ['slice_attribute']
18     fields = {
19         'attribute_type_id': Parameter(int, "Slice attribute type identifier"),
20         'name': Parameter(str, "Slice attribute type name", max = 100),
21         'description': Parameter(str, "Slice attribute type description", max = 254),
22         'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
23         }
24
25     # for Cache
26     class_key = 'name'
27     foreign_fields = ['description','min_role_id']
28     foreign_xrefs = []
29
30     def validate_name(self, name):
31         if not len(name):
32             raise PLCInvalidArgument, "Slice attribute type name must be set"
33
34         conflicts = SliceAttributeTypes(self.api, [name])
35         for attribute in conflicts:
36             if 'attribute_type_id' not in self or \
37                self['attribute_type_id'] != attribute['attribute_type_id']:
38                 raise PLCInvalidArgument, "Slice attribute type name already in use"
39
40         return name
41
42     def validate_min_role_id(self, role_id):
43         roles = [row['role_id'] for row in Roles(self.api)]
44         if role_id not in roles:
45             raise PLCInvalidArgument, "Invalid role"
46
47         return role_id
48
49 class SliceAttributeTypes(Table):
50     """
51     Representation of row(s) from the slice_attribute_types table in the
52     database.
53     """
54
55     def __init__(self, api, attribute_type_filter = None, columns = None):
56         Table.__init__(self, api, SliceAttributeType, columns)
57
58         sql = "SELECT %s FROM slice_attribute_types WHERE True" % \
59               ", ".join(self.columns)
60
61         if attribute_type_filter is not None:
62             if isinstance(attribute_type_filter, (list, tuple, set)):
63                 # Separate the list into integers and strings
64                 ints = filter(lambda x: isinstance(x, (int, long)), attribute_type_filter)
65                 strs = filter(lambda x: isinstance(x, StringTypes), attribute_type_filter)
66                 attribute_type_filter = Filter(SliceAttributeType.fields, {'attribute_type_id': ints, 'name': strs})
67                 sql += " AND (%s) %s" % attribute_type_filter.sql(api, "OR")
68             elif isinstance(attribute_type_filter, dict):
69                 attribute_type_filter = Filter(SliceAttributeType.fields, attribute_type_filter)
70                 sql += " AND (%s) %s" % attribute_type_filter.sql(api, "AND")
71
72         self.selectall(sql)