示例#1
0
def bond_no_subs():
    """Returns a Bond with no subscribers"""
    return Bond(bond_id='HostAcctA-SubAcctB',
                host_account_id='HostAcctA',
                sub_account_id='SubAcctB',
                host_cost_center='HostCostCenterA',
                sub_cost_center='SubCostCenterB')
示例#2
0
def update_bond(bond_id: str, bond: Bond) -> Bond:
    """Update a bond. Only updates those fields that have changed.

    Args:
        bond_id (str): The bond id of the bond to update.
        bond (Bond): The bond object with updates.

    Raises:
        HTTPException(400) if the bond does not exist.

    Returns:
        The updated bond."""
    logger.info(f"Update bond: bond_id={bond_id}, bond={bond}")

    # find the existing bond and overwrite only the changed values
    try:
        stored_bond: Bond = crud.get_bond(bond_id)
        if stored_bond is None:
            raise HTTPException(status_code=400,
                                detail=f"Bond {bond_id} not found.")
        update_data = bond.dict(exclude_unset=True)
        updated_bond = stored_bond.copy(update=update_data)

        new_bond = crud.update_bond(updated_bond)
    except ConditionalCheckError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except RegistryClientError as err:
        raise HTTPException(status_code=500, detail=str(err))
    return new_bond
示例#3
0
def bond_from_item(item) -> Bond:
    """A helper function to convert a DynamoDB item to a Bond object."""
    bond = Bond(bond_id=item["bond_id"],
                host_account_id=item["host_account_id"],
                sub_account_id=item["sub_account_id"],
                host_cost_center=item["host_cost_center"],
                sub_cost_center=item["sub_cost_center"],
                subscribers=item["subscribers"])
    return bond
示例#4
0
def test_update_bond(table, bond_with_subs):
    """Update a bond with changed values and adding a new subscriber."""
    table.put_item(
        Item={
            'bond_id': bond_with_subs.bond_id,
            'host_account_id': bond_with_subs.host_account_id,
            'sub_account_id': bond_with_subs.sub_account_id,
            'host_cost_center': bond_with_subs.host_cost_center,
            'sub_cost_center': bond_with_subs.sub_cost_center,
            'subscribers': jsonable_encoder(bond_with_subs.subscribers)
        })

    # create the same bond with updated values and with a new subscriber
    upd_bond = Bond(bond_id=bond_with_subs.bond_id,
                    host_account_id=bond_with_subs.host_account_id,
                    sub_account_id=bond_with_subs.sub_account_id,
                    host_cost_center='Fuz',
                    sub_cost_center='Boo',
                    subscribers=bond_with_subs.subscribers)
    upd_bond.add_subscriber(
        Subscriber(sid='jbojo', name='Joe Bojo', email='*****@*****.**'))

    # update the bond
    bond = crud.update_bond(upd_bond)
    response = table.query(
        ProjectionExpression="bond_id, host_account_id, sub_account_id, "
        "host_cost_center, sub_cost_center, subscribers",
        KeyConditionExpression=Key('bond_id').eq('HostAcctA-SubAcctB'))
    # print(response)
    assert bond.subscribers.__len__() == 4
    assert response.get("Count") == 1
    items = response.get("Items")
    assert items[0]["bond_id"] == 'HostAcctA-SubAcctB'
    assert items[0]["host_account_id"] == 'HostAcctA'
    assert items[0]["sub_account_id"] == 'SubAcctB'
    assert items[0]["host_cost_center"] == 'Fuz'
    assert items[0]["sub_cost_center"] == 'Boo'
    assert len(items[0]["subscribers"]) == 4
示例#5
0
def bond_with_subs():
    """Return a Bond with an existing set of subscribers"""
    subs = {
        'eniesc200':
        Subscriber(sid='eniesc200', name='Ed', email='*****@*****.**'),
        'tfomoo100':
        Subscriber(sid='tfomoo100', name='Tom', email='*****@*****.**'),
        'bfoere300':
        Subscriber(sid='bfoere300', name='Bill', email='*****@*****.**')
    }
    return Bond(bond_id='HostAcctA-SubAcctB',
                host_account_id='HostAcctA',
                sub_account_id='SubAcctB',
                host_cost_center='HostCostCenterA',
                sub_cost_center='SubCostCenterB',
                subscribers=subs)
示例#6
0
def test_add_bond_already_exists(table, bond_no_subs):
    """Add a bond that already exists in DynamoDB. It will raise an error."""
    table.put_item(
        Item={
            'bond_id': bond_no_subs.bond_id,
            'host_account_id': bond_no_subs.host_account_id,
            'sub_account_id': bond_no_subs.sub_account_id,
            'host_cost_center': bond_no_subs.host_cost_center,
            'sub_cost_center': bond_no_subs.sub_cost_center,
            'subscribers': {}
        })

    # create a new bond that shares the same partition key as the one
    # we just inserted.
    new_bond = Bond(bond_id=bond_no_subs.bond_id,
                    host_account_id='Foo',
                    sub_account_id='Bar',
                    host_cost_center='Fuz',
                    sub_cost_center='Boo')

    # will throw a KeyError on insert as that partition key already exists
    with pytest.raises(ConditionalCheckError):
        crud.create_bond(new_bond)

    # make sure the original bond is untouched.
    response = table.query(
        ProjectionExpression="bond_id, host_account_id, sub_account_id, "
        "host_cost_center, sub_cost_center, subscribers",
        KeyConditionExpression=Key('bond_id').eq('HostAcctA-SubAcctB'))
    # print(response)

    # assert the original bond is still there and untouched.
    assert response.get("Count") == 1
    items = response.get("Items")
    assert items[0]["bond_id"] == bond_no_subs.bond_id
    assert items[0]["host_account_id"] == bond_no_subs.host_account_id
    assert items[0]["sub_account_id"] == bond_no_subs.sub_account_id
    assert items[0]["host_cost_center"] == bond_no_subs.host_cost_center
    assert items[0]["sub_cost_center"] == bond_no_subs.sub_cost_center
    assert len(items[0]["subscribers"]) == 0