setup nova connection in __init__
[plcapi.git] / PLC / Ilinks.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Parameter import Parameter
6 from PLC.Interfaces import Interface, Interfaces
7 from PLC.TagTypes import TagType, TagTypes
8 from PLC.Storage.AlchemyObject import AlchemyObj
9
10 class Ilink(AlchemyObj):
11     """
12     Representation of a row in the ilink table.
13     To use, instantiate with a dict of values.
14     """
15
16     tablename = 'ilink'
17     fields = {
18         'ilink_id': Parameter(int, "ilink identifier", primary_key=True),
19         'tag_type_id': TagType.fields['tag_type_id'],
20         'src_interface_id': Parameter(int, "source interface identifier"),
21         'dst_interface_id': Parameter(int, "destination interface identifier"),
22         'value': Parameter( str, "optional ilink value"),
23         }
24
25     def sync(self, insert=False, validate=True):
26         AlchemyObj.sync(self, commit, validate)
27         if 'ilink_id' in self:
28             AlchemyObj.insert(self, dict(self))
29         else:
30             AlchemyObj.update(self, {'ilink_id': self['ilink_id']}, dict(self))
31
32     def delete(self, commit=True):
33         assert 'ilink_id' in self
34         AlchemyObj.delete(self, dict(self))
35
36 class Ilinks(list):
37     """
38     Representation of row(s) from the ilink table in the
39     database.
40     """
41
42     def __init__(self, api, ilink_filter = None, columns = None):
43         if not ilink_filter:
44             ilinks = Ilink().select()
45         elif isinstance(ilink_filter, (list, tuple, set, int, long)):
46             ilinks = Ilink().select(filter={'ilink_id': ilink_filter})
47         elif isinstance(ilink_filter, dict):
48             ilink_filter = Ilink().select(filter=ilink_filter)
49         else:
50             raise PLCInvalidArgument, "Wrong ilink filter %r"%ilink_filter
51
52         for ilink in ilinks:
53             self.append(ilink)