[13] | 1 | from sqlalchemy import * |
---|
| 2 | |
---|
[18] | 3 | from sqlalchemy.ext.sessioncontext import SessionContext |
---|
| 4 | from sqlalchemy.ext.assignmapper import assign_mapper |
---|
| 5 | |
---|
[77] | 6 | __all__ = ['meta', |
---|
| 7 | 'ctx', |
---|
| 8 | 'machine_table', |
---|
| 9 | 'nic_table', |
---|
| 10 | 'disk_table', |
---|
| 11 | 'types_table', |
---|
| 12 | 'Machine', |
---|
| 13 | 'NIC', |
---|
| 14 | 'Disk', |
---|
| 15 | 'Type'] |
---|
| 16 | |
---|
[13] | 17 | meta = DynamicMetaData() |
---|
[18] | 18 | ctx = SessionContext(create_session) |
---|
[13] | 19 | |
---|
| 20 | machine_table = Table('machines', meta, |
---|
| 21 | Column('machine_id', Integer, primary_key=True, nullable=False), |
---|
| 22 | Column('name', String, nullable=False), |
---|
| 23 | Column('memory', Integer, nullable=False), |
---|
| 24 | Column('owner', String, nullable=False), |
---|
| 25 | Column('contact', String, nullable=False), |
---|
| 26 | Column('uuid', String, nullable=False), |
---|
| 27 | Column('boot_off_cd', Boolean, nullable=False, default=False), |
---|
| 28 | Column('type_id', String, ForeignKey('types.type_id'), nullable=False), |
---|
| 29 | Column('autorestart', Boolean, nullable=False, default=False), |
---|
| 30 | Column('cpus', Integer, nullable=False, default=1)) |
---|
| 31 | |
---|
| 32 | nic_table = Table('nics', meta, |
---|
| 33 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True), |
---|
| 34 | Column('mac_addr', String, nullable=False), |
---|
[18] | 35 | Column('ip', String, nullable=False), |
---|
| 36 | Column('hostname', String, primary_key=True, nullable=False)) |
---|
[13] | 37 | |
---|
| 38 | disk_table = Table('disks', meta, |
---|
| 39 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False), |
---|
| 40 | Column('guest_device_name', String, nullable=False), |
---|
| 41 | Column('size', Integer, nullable=False), |
---|
| 42 | PrimaryKeyConstraint('machine_id', 'guest_device_name')) |
---|
| 43 | |
---|
| 44 | types_table = Table('types', meta, |
---|
| 45 | Column('type_id', String, primary_key=True, nullable=False), |
---|
| 46 | Column('description', String, nullable=False), |
---|
[64] | 47 | Column('hvm', Boolean, nullable=False), |
---|
[13] | 48 | Column('apic', Boolean, nullable=False), |
---|
| 49 | Column('acpi', Boolean, nullable=False), |
---|
| 50 | Column('pae', Boolean, nullable=False)) |
---|
| 51 | |
---|
[18] | 52 | |
---|
| 53 | class Machine(object): |
---|
| 54 | pass |
---|
| 55 | |
---|
| 56 | class NIC(object): |
---|
| 57 | pass |
---|
| 58 | |
---|
| 59 | class Disk(object): |
---|
| 60 | pass |
---|
| 61 | |
---|
| 62 | class Type(object): |
---|
| 63 | pass |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | assign_mapper(ctx, Machine, machine_table, \ |
---|
| 67 | properties={'nics': relation(NIC), |
---|
| 68 | 'disks': relation(Disk)}); |
---|
| 69 | assign_mapper(ctx, NIC, nic_table) |
---|
| 70 | assign_mapper(ctx, Disk, disk_table) |
---|
| 71 | assign_mapper(ctx, Type, types_table) |
---|
| 72 | |
---|