~soren/nova/lp658257

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/models.py

  • Committer: Tarmac
  • Author(s): Soren Hansen
  • Date: 2010-10-04 19:23:38 UTC
  • mfrom: (306.3.5 redisectomy)
  • Revision ID: hudson@openstack.org-20101004192338-ga19zarvlnbmvk2g
A shiny, new Auth driver backed by SQLAlchemy. Read it and weep. I did.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from sqlalchemy.orm import relationship, backref, exc, object_mapper
28
28
from sqlalchemy import Column, Integer, String
29
29
from sqlalchemy import ForeignKey, DateTime, Boolean, Text
 
30
from sqlalchemy.exc import IntegrityError
30
31
from sqlalchemy.ext.declarative import declarative_base
 
32
from sqlalchemy.schema import ForeignKeyConstraint
31
33
 
32
34
from nova.db.sqlalchemy.session import get_session
33
35
 
60
62
        if not session:
61
63
            session = get_session()
62
64
        session.add(self)
63
 
        session.flush()
 
65
        try:
 
66
            session.flush()
 
67
        except IntegrityError, e:
 
68
            if str(e).endswith('is not unique'):
 
69
                raise exception.Duplicate(str(e))
 
70
            else:
 
71
                raise
64
72
 
65
73
    def delete(self, session=None):
66
74
        """Delete this object"""
374
382
        return self.address
375
383
 
376
384
 
 
385
class User(BASE, NovaBase):
 
386
    """Represents a user"""
 
387
    __tablename__ = 'users'
 
388
    id = Column(String(255), primary_key=True)
 
389
 
 
390
    name = Column(String(255))
 
391
    access_key = Column(String(255))
 
392
    secret_key = Column(String(255))
 
393
 
 
394
    is_admin = Column(Boolean)
 
395
 
 
396
 
 
397
class Project(BASE, NovaBase):
 
398
    """Represents a project"""
 
399
    __tablename__ = 'projects'
 
400
    id = Column(String(255), primary_key=True)
 
401
    name = Column(String(255))
 
402
    description = Column(String(255))
 
403
 
 
404
    project_manager = Column(String(255), ForeignKey(User.id))
 
405
 
 
406
    members = relationship(User,
 
407
                           secondary='user_project_association',
 
408
                           backref='projects')
 
409
 
 
410
 
 
411
class UserProjectRoleAssociation(BASE, NovaBase):
 
412
    __tablename__ = 'user_project_role_association'
 
413
    user_id = Column(String(255), primary_key=True)
 
414
    user = relationship(User,
 
415
                        primaryjoin=user_id==User.id,
 
416
                        foreign_keys=[User.id],
 
417
                        uselist=False)
 
418
 
 
419
    project_id = Column(String(255), primary_key=True)
 
420
    project = relationship(Project,
 
421
                           primaryjoin=project_id==Project.id,
 
422
                           foreign_keys=[Project.id],
 
423
                           uselist=False)
 
424
 
 
425
    role = Column(String(255), primary_key=True)
 
426
    ForeignKeyConstraint(['user_id',
 
427
                          'project_id'],
 
428
                         ['user_project_association.user_id',
 
429
                          'user_project_association.project_id'])
 
430
 
 
431
 
 
432
class UserRoleAssociation(BASE, NovaBase):
 
433
    __tablename__ = 'user_role_association'
 
434
    user_id = Column(String(255), ForeignKey('users.id'), primary_key=True)
 
435
    user = relationship(User, backref='roles')
 
436
    role = Column(String(255), primary_key=True)
 
437
 
 
438
 
 
439
class UserProjectAssociation(BASE, NovaBase):
 
440
    __tablename__ = 'user_project_association'
 
441
    user_id = Column(String(255), ForeignKey(User.id), primary_key=True)
 
442
    project_id = Column(String(255), ForeignKey(Project.id), primary_key=True)
 
443
 
 
444
 
 
445
 
377
446
class FloatingIp(BASE, NovaBase):
378
447
    """Represents a floating ip that dynamically forwards to a fixed ip"""
379
448
    __tablename__ = 'floating_ips'
394
463
    from sqlalchemy import create_engine
395
464
    models = (Service, Instance, Volume, ExportDevice,
396
465
              FixedIp, FloatingIp, Network, NetworkIndex,
397
 
              AuthToken)  # , Image, Host)
 
466
              AuthToken, UserProjectAssociation, User, Project)  # , Image, Host)
398
467
    engine = create_engine(FLAGS.sql_connection, echo=False)
399
468
    for model in models:
400
469
        model.metadata.create_all(engine)