source: trunk/packages/sipb-xen-database/sipb-xen-database/sipb_xen_database/models.py @ 238

Last change on this file since 238 was 238, checked in by quentin, 16 years ago

Add a machine_access table where access information will be cached

File size: 4.2 KB
Line 
1from sqlalchemy import *
2
3from sqlalchemy.ext.sessioncontext import SessionContext
4from sqlalchemy.ext.assignmapper import assign_mapper
5
6__all__ = ['meta',
7           'ctx',
8           'machine_table',
9           'nic_table',
10           'disk_table',
11           'types_table',
12           'cdroms_table',
13           'Machine',
14           'NIC',
15           'Disk',
16           'Type',
17           'CDROM']
18
19meta = DynamicMetaData()
20ctx = SessionContext(create_session)
21
22machine_table = Table('machines', meta,
23       Column('machine_id', Integer, primary_key=True, nullable=False),
24       Column('name', String, nullable=False),
25       Column('memory', Integer, nullable=False),
26       Column('owner', String, nullable=False),
27       Column('contact', String, nullable=False),
28       Column('uuid', String, nullable=False),
29       Column('administrator', String, nullable=False, default=False),
30       Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
31       Column('autorestart', Boolean, nullable=False, default=False),
32       Column('cpus', Integer, nullable=False, default=1))
33
34nic_table = Table('nics', meta,
35       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
36       Column('mac_addr', String, nullable=False, primary_key=True),
37       Column('ip', String, nullable=False, unique=True),
38       Column('hostname', String, nullable=True))
39
40disk_table = Table('disks', meta,
41       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False),
42       Column('guest_device_name', String, nullable=False),
43       Column('size', Integer, nullable=False),
44       PrimaryKeyConstraint('machine_id', 'guest_device_name'))
45
46types_table = Table('types', meta,
47       Column('type_id', String, primary_key=True, nullable=False),
48       Column('description', String, nullable=False),
49       Column('hvm', Boolean, nullable=False),
50       Column('apic', Boolean, nullable=False),
51       Column('acpi', Boolean, nullable=False),
52       Column('pae', Boolean, nullable=False))
53
54cdroms_table = Table('cdroms', meta,
55       Column('cdrom_id', String, primary_key=True, nullable=False),
56       Column('description', String, nullable=False))
57
58machine_access_table = Table('machine_access', meta,
59       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False, index=True),
60       Column('user', String, nullable=False, index=True),
61       PrimaryKeyConstraint('machine_id', 'user'))
62
63class Machine(object):
64    def __repr__(self):
65        return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner)
66
67class MachineAccess(object):
68    def __repr__(self):
69        return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user)
70
71class NIC(object):
72    def __init__(self, machine_id, mac_addr, ip, hostname):
73        self.machine_id = machine_id
74        self.mac_addr = mac_addr
75        self.ip = ip
76        self.hostname = hostname
77    def __repr__(self):
78        return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname)
79
80class Disk(object):
81    def __init__(self, machine_id, guest, size):
82        self.machine_id = machine_id
83        self.guest_device_name = guest
84        self.size = size
85    def __repr__(self):
86        return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size)
87
88class Type(object):
89    def __repr__(self):
90        return "<Type %s: %s>" % (self.type_id, self.description)
91
92class CDROM(object):
93    def __init__(self, cdrom_id, description):
94        self.cdrom_id = cdrom_id
95        self.description = description
96    def __repr__(self):
97        return "<CDROM %s: %s>" % (self.cdrom_id, self.description)
98
99assign_mapper(ctx, Machine, machine_table,
100              properties={'nics': relation(NIC, backref="machine"),
101                          'disks': relation(Disk, backref="machine"),
102                          'type': relation(Type),
103                          'users': relation(MachineAccess, backref="machine")});
104assign_mapper(ctx, MachineAccess, machine_access_table)
105assign_mapper(ctx, NIC, nic_table)
106assign_mapper(ctx, Disk, disk_table)
107assign_mapper(ctx, Type, types_table)
108assign_mapper(ctx, CDROM, cdroms_table)
Note: See TracBrowser for help on using the repository browser.