~soren/nova/lp658257

« back to all changes in this revision

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

  • Committer: Tarmac
  • Author(s): Michael Gundlach
  • Date: 2010-10-05 18:58:37 UTC
  • mfrom: (295.1.22 servers_api)
  • Revision ID: hudson@openstack.org-20101005185837-wb2sq7zy5swxa9q7
Replace model.Instance.ec2_id with an integer internal_id so that both APIs can represent the ID to external users.

Show diffs side-by-side

added added

removed removed

Lines of Context:
525
525
###################
526
526
 
527
527
 
 
528
#TODO(gundlach): instance_create and volume_create are nearly identical
 
529
#and should be refactored.  I expect there are other copy-and-paste
 
530
#functions between the two of them as well.
528
531
@require_context
529
532
def instance_create(context, values):
530
533
    instance_ref = models.Instance()
533
536
 
534
537
    session = get_session()
535
538
    with session.begin():
536
 
        while instance_ref.ec2_id == None:
537
 
            ec2_id = utils.generate_uid(instance_ref.__prefix__)
538
 
            if not instance_ec2_id_exists(context, ec2_id, session=session):
539
 
                instance_ref.ec2_id = ec2_id
 
539
        while instance_ref.internal_id == None:
 
540
            internal_id = utils.generate_uid(instance_ref.__prefix__)
 
541
            if not instance_internal_id_exists(context, internal_id, 
 
542
                                               session=session):
 
543
                instance_ref.internal_id = internal_id
540
544
        instance_ref.save(session=session)
541
545
    return instance_ref
542
546
 
635
639
 
636
640
 
637
641
@require_context
638
 
def instance_get_by_ec2_id(context, ec2_id):
 
642
def instance_get_by_internal_id(context, internal_id):
639
643
    session = get_session()
640
644
 
641
645
    if is_admin_context(context):
642
646
        result = session.query(models.Instance
643
 
                       ).filter_by(ec2_id=ec2_id
 
647
                       ).filter_by(internal_id=internal_id
644
648
                       ).filter_by(deleted=can_read_deleted(context)
645
649
                       ).first()
646
650
    elif is_user_context(context):
647
651
        result = session.query(models.Instance
648
652
                       ).filter_by(project_id=context.project.id
649
 
                       ).filter_by(ec2_id=ec2_id
 
653
                       ).filter_by(internal_id=internal_id
650
654
                       ).filter_by(deleted=False
651
655
                       ).first()
652
656
    if not result:
653
 
        raise exception.NotFound('Instance %s not found' % (ec2_id))
 
657
        raise exception.NotFound('Instance %s not found' % (internal_id))
654
658
 
655
659
    return result
656
660
 
657
661
 
658
662
@require_context
659
 
def instance_ec2_id_exists(context, ec2_id, session=None):
 
663
def instance_internal_id_exists(context, internal_id, session=None):
660
664
    if not session:
661
665
        session = get_session()
662
 
    return session.query(exists().where(models.Instance.id==ec2_id)).one()[0]
 
666
    return session.query(
 
667
            exists().where(models.Instance.internal_id==internal_id)
 
668
           ).one()[0]
663
669
 
664
670
 
665
671
@require_context