Beispiel #1
0
def create_lambda(
    name: str,
    runtime: str,
    role: str = "foobar",
    handler: str = "main.handler",
    path: str = "dist/build.zip",
    environment: dict = {},
    **kwargs,
):
    def clean_env(env):
        for k, v in env.items():
            if isinstance(v, bool):
                if v:
                    env[k] = str(v)
                else:
                    continue
            elif isinstance(v, type(None)):
                env[k] = ""
        return env

    with open(str(Path().absolute() / path), "rb") as f:
        zipped_code = f.read()
        utils.call(
            boto3_fixtures.contrib.boto3.client("lambda").create_function,
            FunctionName=name,
            Runtime=runtime,
            Role=role,
            Handler=handler,
            Code=dict(ZipFile=zipped_code),
            Timeout=300,
            Environment={"Variables": clean_env(environment)},
        )
Beispiel #2
0
def destroy_bucket(b: str):
    client = boto3_fixtures.contrib.boto3.client("s3")
    objects = boto3_fixtures.contrib.boto3.resource("s3").Bucket(
        b).objects.all()
    [utils.call(client.delete_object, Bucket=b, Key=o.key) for o in objects]
    resp = utils.call(client.delete_bucket, Bucket=b)
    client.get_waiter("bucket_not_exists").wait(Bucket=b)
    return resp
Beispiel #3
0
def check_sqs_fixtures(sqs_queues):
    client = boto3.client("sqs")
    for q in sqs_queues:
        queue_url = sqs.get_queue_url(q)
        utils.call(
            client.send_message,
            QueueUrl=queue_url,
            MessageBody=json.dumps({"foo": "bar"}),
        )
Beispiel #4
0
def create_queue(QueueName, **kwargs):
    client = boto3_fixtures.contrib.boto3.client("sqs")
    resp = utils.call(client.create_queue, QueueName=QueueName, **kwargs)
    utils.call(
        backoff_check,
        func=lambda: boto3_fixtures.contrib.boto3.client("sqs").get_queue_url(
            QueueName=QueueName
        ),
    )
    return SQSQueue(
        name=QueueName,
        url=resp["QueueUrl"],
        arn=get_queue_arn(resp["QueueUrl"]),
        response=resp,
    )
Beispiel #5
0
def check_sns_fixtures(sns_topics):
    client = boto3.client("sns")
    resp = utils.call(utils.backoff_check, func=lambda: client.list_topics())
    topic_names = [
        tpc["TopicArn"].rsplit(":", 1)[-1] for tpc in resp["Topics"]
    ]
    assert set(topic_names) == set(sns_topics)
Beispiel #6
0
def check_lambda_fixtures(lambda_functions):
    client = boto3.client("lambda")
    resp = utils.call(utils.backoff_check,
                      func=lambda: client.list_functions())
    lambda_names = {lam["FunctionName"]: lam for lam in resp["Functions"]}
    for lam in lambda_functions:
        assert lam["FunctionName"] in lambda_names
Beispiel #7
0
def create_topic(topic_config: dict) -> SNSTopic:
    resp = utils.call(
        boto3_fixtures.contrib.boto3.client("sns").create_topic,
        **topic_config)

    def _check_topic(arn):
        return boto3_fixtures.contrib.boto3.client("sns").get_topic_attributes(
            TopicArn=arn)

    utils.call(utils.backoff_check,
               func=lambda: _check_topic(resp["TopicArn"]))
    return SNSTopic(
        name=topic_config["Name"],
        arn=resp["TopicArn"],
        response=resp,
    )
def create_table(TableName: str, **kwargs):
    kwargs.update({"BillingMode": "PAY_PER_REQUEST"})
    resp = utils.call(boto3.client("dynamodb").create_table,
                      TableName=TableName,
                      **kwargs)
    name = mdict.get_nested(resp, ["TableDescription", "TableName"])
    arn = mdict.get_nested(resp, ["TableDescription", "TableArn"])
    return DynamoDBTable(name=name, arn=arn, response=resp)
Beispiel #9
0
def create_queue(q, dlq_url=None):
    client = boto3_fixtures.contrib.boto3.client("sqs")
    attrs = {}
    if dlq_url:
        attrs["RedrivePolicy"] = json.dumps({
            "deadLetterTargetArn":
            get_queue_arn(dlq_url),
            "maxReceiveCount":
            1
        })
    resp = utils.call(client.create_queue, QueueName=q, Attributes=attrs)
    utils.call(
        backoff_check,
        func=lambda: boto3_fixtures.contrib.boto3.client("sqs").get_queue_url(
            QueueName=q),
    )
    return resp["QueueUrl"]
Beispiel #10
0
def get_queue_arn(queue_url):
    return mindictive.get_nested(
        utils.call(
            boto3.client("sqs").get_queue_attributes,
            QueueUrl=queue_url,
            AttributeNames=["QueueArn"],
        ),
        ["Attributes", "QueueArn"],
    )
Beispiel #11
0
def destroy_stream(StreamName: str, **kwargs):
    client = boto3_fixtures.contrib.boto3.client("kinesis")
    resp = utils.call(client.delete_stream, StreamName=StreamName)
    client.get_waiter("stream_not_exists").wait(StreamName=StreamName,
                                                WaiterConfig={
                                                    "Delay": 2,
                                                    "MaxAttempts": 2
                                                })
    return resp
def create_stream(name: str,
                  waiter_config: dict = {
                      "Delay": 2,
                      "MaxAttempts": 2
                  }):
    client = boto3_fixtures.contrib.boto3.client("kinesis")
    resp = utils.call(client.create_stream, StreamName=name, ShardCount=1)
    client.get_waiter("stream_exists").wait(StreamName=name,
                                            WaiterConfig=waiter_config)
    return resp
Beispiel #13
0
def create_tables(dynamodb_tables):
    client = boto3_fixtures.contrib.boto3.client("dynamodb")
    for table in dynamodb_tables:
        assert create_table(table)
    for table in dynamodb_tables:
        name = table["TableName"]
        client.get_waiter("table_exists").wait(
            TableName=name, WaiterConfig={"Delay": 1, "MaxAttempts": 30}
        )
        assert utils.call(client.describe_table, TableName=name)
    return [t["TableName"] for t in dynamodb_tables]
def create_tables(tables: list):
    client = boto3_fixtures.contrib.boto3.client("dynamodb")
    _tables = [create_table(**table) for table in tables]
    for table in _tables:
        client.get_waiter("table_exists").wait(TableName=table.name,
                                               WaiterConfig={
                                                   "Delay": 1,
                                                   "MaxAttempts": 30
                                               })
        assert utils.call(client.describe_table, TableName=table.name)
    return {t.name: t for t in _tables}
Beispiel #15
0
def create_stream(StreamName: str, ShardCount: int, **kwargs):
    client = boto3_fixtures.contrib.boto3.client("kinesis")
    resp = utils.call(client.create_stream,
                      StreamName=StreamName,
                      ShardCount=ShardCount,
                      **kwargs)
    client.get_waiter("stream_exists").wait(StreamName=StreamName,
                                            WaiterConfig={
                                                "Delay": 2,
                                                "MaxAttempts": 2
                                            })
    return KinesisStream(name=StreamName, response=resp)
def destroy_table(TableName: str, **kwargs):
    client = boto3_fixtures.contrib.boto3.client("dynamodb")
    return utils.call(client.delete_table, TableName=TableName)
Beispiel #17
0
def get_queue_url(queue_name):
    return utils.call(boto3.client("sqs").get_queue_url,
                      QueueName=queue_name)["QueueUrl"]
def destroy_lambda(FunctionName: str, **kwargs):
    return utils.call(
        boto3_fixtures.contrib.boto3.client("lambda").delete_function,
        FunctionName=FunctionName,
    )
Beispiel #19
0
def create_bucket(b: str):
    client = boto3_fixtures.contrib.boto3.client("s3")
    resp = utils.call(client.create_bucket, Bucket=b)
    client.get_waiter("bucket_exists").wait(Bucket=b)
    return resp
Beispiel #20
0
def destroy_queue(QueueUrl, **kwargs):
    return utils.call(
        boto3_fixtures.contrib.boto3.client("sqs").delete_queue, QueueUrl=QueueUrl
    )
Beispiel #21
0
def create_bucket(Bucket: str, **kwargs):
    client = boto3_fixtures.contrib.boto3.client("s3")
    resp = utils.call(client.create_bucket, Bucket=Bucket, **kwargs)
    client.get_waiter("bucket_exists").wait(Bucket=Bucket)
    return S3Bucket(name=Bucket, response=resp)
Beispiel #22
0
def destroy_table(config):
    client = boto3_fixtures.contrib.boto3.client("dynamodb")
    return utils.call(client.delete_table, TableName=config["TableName"])
Beispiel #23
0
def create_table(config):
    config.update({"BillingMode": "PAY_PER_REQUEST"})
    return utils.call(boto3.client("dynamodb").create_table, **config)
Beispiel #24
0
def destroy_topic(topic_arn: str):
    return utils.call(boto3_fixtures.contrib.boto3.client("sns").delete_topic,
                      TopicArn=topic_arn)
Beispiel #25
0
def check_dynamodb_fixtures(dynamodb_tables):
    client = boto3.client("dynamodb")
    resp = utils.call(utils.backoff_check, func=lambda: client.list_tables())
    for table in dynamodb_tables:
        assert table in resp["TableNames"]
Beispiel #26
0
def check_s3_fixtures(s3_buckets):
    client = boto3.client("s3")
    resp = utils.call(utils.backoff_check, func=lambda: client.list_buckets())
    bucket_names = {b["Name"]: b for b in resp["Buckets"]}
    for b in s3_buckets:
        assert b in bucket_names