Ejemplo n.º 1
0
            def transfer():
                session = Session()
                with session.begin():
                    # print("executing")
                    accounts = Query(BankAccount, session=session).all()

                    bob_account = accounts[0] if accounts[0].owner is "bob" else accounts[1]
                    alice_account = accounts[0] if accounts[0].owner is "alice" else accounts[1]

                    # bob_account.money -= 100
                    # alice_account.money += 100

                    bob_account.update({"money": bob_account.money - 100})
                    alice_account.update({"money": alice_account.money + 100})

                    session.add(bob_account)

                    session.flush()
                    session.add(alice_account)
Ejemplo n.º 2
0
def _security_group_ensure_default(session=None):
    from lib.rome.core.session.session import Session as Session

    session = Session()

    with session.begin(subtransactions=True):
        try:
            default_group = _security_group_get_by_names(session, context.project_id, ["default"])[0]
        except exception.NotFound:
            values = {
                "name": "default",
                "description": "default",
                "user_id": context.user_id,
                "project_id": context.project_id,
            }
            default_group = _security_group_create(context, values, session=session)
            usage = (
                model_query(context, models.QuotaUsage, read_deleted="no", session=session)
                .filter_by(project_id=context.project_id)
                .filter_by(user_id=context.user_id)
                .filter_by(resource="security_groups")
            )
            # Create quota usage for auto created default security group
            if not usage.first():
                _quota_usage_create(context.project_id, context.user_id, "security_groups", 1, 0, None, session=session)
            else:
                usage.update({"in_use": int(usage.first().in_use) + 1})
                # TODO (Jonathan): add a "session.add" to ease the session management :)
                session.add(usage)

            default_rules = _security_group_rule_get_default_query(context, session=session).all()
            for default_rule in default_rules:
                # This is suboptimal, it should be programmatic to know
                # the values of the default_rule
                rule_values = {
                    "protocol": default_rule.protocol,
                    "from_port": default_rule.from_port,
                    "to_port": default_rule.to_port,
                    "cidr": default_rule.cidr,
                    "parent_group_id": default_group.id,
                }
                _security_group_rule_create(context, rule_values, session=session)
        return default_group
Ejemplo n.º 3
0
def test_relationships_single_str(save_instance=True,
                                  save_info_cache=True,
                                  use_update=False,
                                  use_session=False):
    print(
        "Ensure that foreign keys are working test_relationships_single_str(save_instance=%s, save_info_cache=%s, use_update=%s, use_session=%s)"
        % (save_instance, save_info_cache, use_update, use_session))

    session = None
    if use_session:
        session = Session()

    instance_count = Query(models.Instance).count()

    instance = models.Instance()
    instance.uuid = "uuid_%s" % (instance_count)
    if save_instance:
        if use_session:
            session.add(instance)
        else:
            instance.save()

    instance_info_cache = models.InstanceInfoCache()

    if not use_update:
        instance_info_cache.instance_uuid = instance.uuid
    else:
        instance_info_cache.update({"instance_uuid": instance.uuid})

    if not save_info_cache:
        if use_session:
            session.add(instance)
        else:
            instance.save()
    else:
        if use_session:
            session.add(instance_info_cache)
        else:
            instance_info_cache.save()

    if use_session:
        session.flush()

    instance_from_db = Query(models.Instance,
                             models.Instance.id == instance.id).first()
    instance_info_cache_from_db = Query(
        models.InstanceInfoCache,
        models.InstanceInfoCache.id == instance_info_cache.id).first()

    assert instance_from_db.id == instance.id
    assert instance_info_cache_from_db.id == instance_info_cache.id

    assert instance_from_db.info_cache is not None
    assert instance_from_db.info_cache.id == instance_info_cache.id

    assert instance_info_cache_from_db.instance is not None
    assert instance_info_cache_from_db.instance.id == instance.id
    assert instance_info_cache_from_db.instance_uuid == instance.uuid
Ejemplo n.º 4
0
def test_relationships_single_str(save_instance=True, save_info_cache=True, use_update=False, use_session=False):
    print("Ensure that foreign keys are working test_relationships_single_str(save_instance=%s, save_info_cache=%s, use_update=%s, use_session=%s)" % (save_instance, save_info_cache, use_update, use_session))

    session = None
    if use_session:
        session = Session()

    instance_count = Query(models.Instance).count()

    instance = models.Instance()
    instance.uuid = "uuid_%s" % (instance_count)
    if save_instance:
        if use_session:
            session.add(instance)
        else:
            instance.save()

    instance_info_cache = models.InstanceInfoCache()

    if not use_update:
        instance_info_cache.instance_uuid = instance.uuid
    else:
        instance_info_cache.update({"instance_uuid": instance.uuid})

    if not save_info_cache:
        if use_session:
            session.add(instance)
        else:
            instance.save()
    else:
        if use_session:
            session.add(instance_info_cache)
        else:
            instance_info_cache.save()

    if use_session:
        session.flush()

    instance_from_db = Query(models.Instance, models.Instance.id==instance.id).first()
    instance_info_cache_from_db = Query(models.InstanceInfoCache, models.InstanceInfoCache.id==instance_info_cache.id).first()

    assert instance_from_db.id == instance.id
    assert instance_info_cache_from_db.id == instance_info_cache.id

    assert instance_from_db.info_cache is not None
    assert instance_from_db.info_cache.id == instance_info_cache.id

    assert instance_info_cache_from_db.instance is not None
    assert instance_info_cache_from_db.instance.id == instance.id
    assert instance_info_cache_from_db.instance_uuid == instance.uuid
Ejemplo n.º 5
0
def test_relationships_single_object(save_instance=True,
                                     save_info_cache=True,
                                     use_update=False,
                                     update_instance=False,
                                     use_session=False):
    print(
        "Ensure that foreign keys are working test_relationships_single_object(save_instance=%s, save_info_cache=%s, use_update=%s, update_instance=%s, use_session=%s)"
        % (save_instance, save_info_cache, use_update, update_instance,
           use_session))

    session = None
    if use_session:
        session = Session()

    instance_count = Query(models.Instance).count()

    instance = models.Instance()
    instance_uuid = "uuid_%s" % (instance_count)

    if save_instance:
        if use_session:
            session.add(instance)
        else:
            instance.save()

    instance_info_cache = models.InstanceInfoCache()

    if update_instance:
        if not use_update:
            instance.info_cache = instance_info_cache
            instance.uuid = instance_uuid
        else:
            # CLASSIC
            # instance.update({"info_cache": instance_info_cache})
            # DEBUG
            values = {}
            values['uuid'] = instance_uuid
            # instance['info_cache'] = models.InstanceInfoCache()
            instance['info_cache'] = instance_info_cache
            info_cache = values.pop('info_cache', None)
            if info_cache is not None:
                instance['info_cache'].update(info_cache)
            instance.update(values, do_save=False)
        if not save_info_cache:
            if use_session:
                session.add(instance)
            else:
                instance.save()
        else:
            if use_session:
                session.add(instance_info_cache)
            else:
                instance_info_cache.save()
    else:
        instance.uuid = instance_uuid
        if not use_update:
            instance_info_cache.instance = instance
        else:
            instance_info_cache.update({"instance": instance})
        if not save_info_cache:
            instance.save()
        else:
            if use_session:
                session.add(instance_info_cache)
            else:
                instance_info_cache.save()

    if use_session:
        session.flush()

    instance_from_db = Query(models.Instance,
                             models.Instance.id == instance.id).first()
    instance_info_cache_from_db = Query(
        models.InstanceInfoCache,
        models.InstanceInfoCache.id == instance_info_cache.id).first()

    assert instance_from_db.id == instance.id
    assert instance_info_cache_from_db.id == instance_info_cache.id

    assert instance_from_db.info_cache is not None
    assert instance_from_db.info_cache.id == instance_info_cache.id

    assert instance_info_cache_from_db.instance is not None
    assert instance_info_cache_from_db.instance.id == instance.id
    assert instance_info_cache_from_db.instance_uuid == instance.uuid
Ejemplo n.º 6
0
def test_relationships_single_object(save_instance=True, save_info_cache=True, use_update=False, update_instance=False, use_session=False):
    print("Ensure that foreign keys are working test_relationships_single_object(save_instance=%s, save_info_cache=%s, use_update=%s, update_instance=%s, use_session=%s)" % (save_instance, save_info_cache, use_update, update_instance, use_session))

    session = None
    if use_session:
        session = Session()

    instance_count = Query(models.Instance).count()

    instance = models.Instance()
    instance_uuid = "uuid_%s" % (instance_count)

    if save_instance:
        if use_session:
            session.add(instance)
        else:
            instance.save()

    instance_info_cache = models.InstanceInfoCache()

    if update_instance:
        if not use_update:
            instance.info_cache = instance_info_cache
            instance.uuid = instance_uuid
        else:
            # CLASSIC
            # instance.update({"info_cache": instance_info_cache})
            # DEBUG
            values = {}
            values['uuid'] = instance_uuid
            # instance['info_cache'] = models.InstanceInfoCache()
            instance['info_cache'] = instance_info_cache
            info_cache = values.pop('info_cache', None)
            if info_cache is not None:
                instance['info_cache'].update(info_cache)
            instance.update(values, do_save=False)
        if not save_info_cache:
            if use_session:
                session.add(instance)
            else:
                instance.save()
        else:
            if use_session:
                session.add(instance_info_cache)
            else:
                instance_info_cache.save()
    else:
        instance.uuid = instance_uuid
        if not use_update:
            instance_info_cache.instance = instance
        else:
            instance_info_cache.update({"instance": instance})
        if not save_info_cache:
            instance.save()
        else:
            if use_session:
                session.add(instance_info_cache)
            else:
                instance_info_cache.save()

    if use_session:
        session.flush()

    instance_from_db = Query(models.Instance, models.Instance.id==instance.id).first()
    instance_info_cache_from_db = Query(models.InstanceInfoCache, models.InstanceInfoCache.id==instance_info_cache.id).first()

    assert instance_from_db.id == instance.id
    assert instance_info_cache_from_db.id == instance_info_cache.id

    assert instance_from_db.info_cache is not None
    assert instance_from_db.info_cache.id == instance_info_cache.id

    assert instance_info_cache_from_db.instance is not None
    assert instance_info_cache_from_db.instance.id == instance.id
    assert instance_info_cache_from_db.instance_uuid == instance.uuid