Exemple #1
0
async def change_bucket_name(ctx, entity_bucket, new_bucket_name):
    # check if there's any bucket with the new name
    # if found, return None/failure
    # else update name and bucket entity

    try:
        async with ctx.entity_client_for_user(
                papiea_test.bucket_kind_dict) as bucket_entity_client:
            matched_bucket = await bucket_entity_client.filter(
                AttributeDict(spec=AttributeDict(name=new_bucket_name)))
            if len(matched_bucket.results) == 0:
                papiea_test.logger.debug(
                    "Bucket found. Changing the bucket name...")

                entity_bucket.spec.name = new_bucket_name
                await bucket_entity_client.update(
                    metadata=entity_bucket.metadata, spec=entity_bucket.spec)

                ret_entity = await bucket_entity_client.get(
                    entity_bucket.metadata)
                return EntityReference(uuid=ret_entity.metadata.uuid,
                                       kind=ret_entity.metadata.kind)
            else:
                raise Exception("Bucket with new name already exists")
    except:
        raise

    raise Exception("Unable to change name for the bucket entity")
Exemple #2
0
async def link_object(ctx, entity_bucket, input_object):
    # assuming input_object to be the object name and the uuid
    # check if the name already exist in the objects list
    # if exists, return None/failure
    # else add object name and uuid in bucket' objects list
    # and return the bucket reference

    try:
        objects_list = entity_bucket.spec.objects
        if not any(obj.name == input_object.object_name
                   for obj in objects_list):
            papiea_test.logger.debug(
                "Object does not exist. Linking the object...")

            async with ctx.entity_client_for_user(
                    papiea_test.bucket_kind_dict) as bucket_entity_client:
                entity_bucket.spec.objects.append(
                    AttributeDict(name=input_object.object_name,
                                  reference=EntityReference(
                                      uuid=input_object.object_uuid,
                                      kind=papiea_test.OBJECT_KIND)))
                await bucket_entity_client.update(
                    metadata=entity_bucket.metadata, spec=entity_bucket.spec)

            async with ctx.entity_client_for_user(
                    papiea_test.object_kind_dict) as object_entity_client:
                ret_entity = await object_entity_client.get(
                    AttributeDict(uuid=input_object.object_uuid))
                return EntityReference(uuid=ret_entity.metadata.uuid,
                                       kind=ret_entity.metadata.kind)
        else:
            raise Exception("Object already exists in the bucket")
    except:
        raise
    raise Exception("Unable to link object entity")
Exemple #3
0
async def ensure_bucket_exists(ctx, input_bucket_name):
    # Run get query to obtain the list of buckets
    # Check if bucket_name exists in the bucket list
    # If true, simply return the bucket
    # Else, create a new bucket with input_bucket_name and return

    try:
        async with ctx.entity_client_for_user(
                papiea_test.bucket_kind_dict) as bucket_entity_client:
            desired_bucket = await bucket_entity_client.filter(
                AttributeDict(spec=AttributeDict(name=input_bucket_name)))
            if len(desired_bucket.results) != 0:
                papiea_test.logger.debug(
                    "Bucket already exists. Returning it...")

                return EntityReference(
                    uuid=desired_bucket.results[0].metadata.uuid,
                    kind=desired_bucket.results[0].metadata.kind)

            papiea_test.logger.debug(
                "Bucket not found. Creating new bucket...")

            bucket_ref = await bucket_entity_client.create({
                "name": input_bucket_name,
                "objects": list(),
                "owner": "nutanix"
            })

            ret_entity = await bucket_entity_client.get(bucket_ref.metadata)

            return EntityReference(uuid=ret_entity.metadata.uuid,
                                   kind=ret_entity.metadata.kind)
    except:
        raise
    raise Exception("Unable to create bucket entity")
Exemple #4
0
def ref_type(kind: str, description: str = '') -> AttributeDict:
    return AttributeDict(
        type='object',
        description=description,
        required=['uuid', 'kind'],
        properties=AttributeDict(
            uuid=AttributeDict(
                type='string'
            ),
            kind=AttributeDict(
                type='string',
                example=kind or 'kind_name'
            )
        )
    )
Exemple #5
0
async def bucket_name_handler(ctx, entity_bucket, diff):
    # fetch unique uuids for the objects in the bucket
    # for each uuid, get the object references list
    # iterate and update bucket name if bucket ref match is found
    # update all such object entities

    try:
        papiea_test.logger.debug(
            "Executing bucket name change intent handler...")

        object_uuid_set = set()
        for obj in entity_bucket.spec.objects:
            object_uuid_set.add(obj.reference.uuid)

        async with papiea_test.get_client(
                papiea_test.OBJECT_KIND) as object_entity_client:
            for object_uuid in object_uuid_set:
                entity_object = await object_entity_client.get(
                    AttributeDict(uuid=object_uuid))

                for i in range(len(entity_object.status.references)):
                    if entity_object.status.references[
                            i].bucket_reference.uuid == entity_bucket.metadata.uuid:
                        entity_object.status.references[
                            i].bucket_name = entity_bucket.spec.name

                await ctx.update_status(entity_object.metadata,
                                        entity_object.status)

        entity_bucket.status.name = entity_bucket.spec.name
        await ctx.update_status(entity_bucket.metadata, entity_bucket.status)
    except:
        raise
Exemple #6
0
    async def test_different_bucket_different_name_link(self):
        papiea_test.logger.debug("Running test to link to different object in a different bucket")

        try:
            sdk = await provider.setup_and_register_sdk()
        except Exception as ex:
            papiea_test.logger.debug("Failed to setup/register sdk : " + str(ex))
            return

        try:
            await test_utils.cleanup()
        except Exception as ex:
            papiea_test.logger.debug("Failed cleanup : " + str(ex))
            raise Exception("Cleanup operation failed : " + str(ex))

        try:
            async with papiea_test.get_client(papiea_test.BUCKET_KIND) as bucket_entity_client:
                bucket1_name = "test-bucket1"
                bucket2_name = "test-bucket2"

                bucket1_ref = await bucket_entity_client.invoke_kind_procedure("ensure_bucket_exists", bucket1_name)
                bucket1_entity = await bucket_entity_client.get(bucket1_ref)

                bucket2_ref = await bucket_entity_client.invoke_kind_procedure("ensure_bucket_exists", bucket2_name)
                bucket2_entity = await bucket_entity_client.get(bucket2_ref)

                assert bucket1_entity.spec.name == bucket1_name
                assert len(bucket1_entity.spec.objects) == 0
                assert bucket2_entity.spec.name == bucket2_name
                assert len(bucket2_entity.spec.objects) == 0

                object1_name = "test-object1"
                object2_name = "test-object2"

                object_ref = await bucket_entity_client.invoke_procedure("create_object", bucket1_entity.metadata, object1_name)
                async with papiea_test.get_client(papiea_test.OBJECT_KIND) as object_entity_client:
                    b1_object1_entity = await object_entity_client.get(object_ref)

                bucket1_entity = await bucket_entity_client.get(bucket1_ref)

                assert len(bucket1_entity.spec.objects) == 1
                assert bucket1_entity.spec.objects[0].name == object1_name
                assert bucket1_entity.spec.objects[0].reference.uuid == b1_object1_entity.metadata.uuid
                assert b1_object1_entity.spec.content == ""

                object_input = AttributeDict(
                    object_name=object2_name,
                    object_uuid=b1_object1_entity.metadata.uuid
                )
                await bucket_entity_client.invoke_procedure("link_object", bucket2_entity.metadata, object_input)
                async with papiea_test.get_client(papiea_test.OBJECT_KIND) as object_entity_client:
                    b1_object1_entity = await object_entity_client.get(object_ref)

                bucket2_entity = await bucket_entity_client.get(bucket2_ref)

                assert len(bucket2_entity.spec.objects) == 1
                assert bucket2_entity.spec.objects[0].name == object2_name
                assert bucket2_entity.spec.objects[0].reference.uuid == b1_object1_entity.metadata.uuid
        finally:
            await sdk.server.close()
Exemple #7
0
async def on_object_removed_handler(ctx, entity_bucket, diff):
    try:
        papiea_test.logger.debug(
            "Executing object removed from bucket intent handler...")

        async with papiea_test.get_client(
                papiea_test.OBJECT_KIND) as object_entity_client:
            for entity in diff:
                entity_object = await object_entity_client.get(
                    AttributeDict(uuid=entity.status[0].reference.uuid))

                entity_object.status.references[:] = [
                    d for d in entity_object.status.references
                    if d.get("object_name") != entity.status[0].name
                    or d.get("bucket_name") != entity_bucket.spec.name
                ]

                if not entity_object.status.references:
                    papiea_test.logger.debug(
                        "Object refcount is zero. Deleting the object...")
                    await object_entity_client.delete(entity_object.metadata)
                else:
                    await ctx.update_status(entity_object.metadata,
                                            entity_object.status)

        entity_bucket.status.objects = entity_bucket.spec.objects
        await ctx.update_status(entity_bucket.metadata, entity_bucket.status)
    except:
        raise
Exemple #8
0
async def object_create_handler(ctx, entity_object):
    try:
        papiea_test.logger.debug("Executing object create intent handler...")

        spec = AttributeDict(content=entity_object.content)
        status = AttributeDict(content=entity_object.content,
                               size=len(entity_object.content),
                               last_modified=str(datetime.now(timezone.utc)),
                               references=list())
        return ConstructorResult(
            spec=spec,
            status=status,
            metadata={"extension": {
                "owner": entity_object.owner
            }})
    except:
        raise
async def bucket_create_handler(ctx, entity_bucket):
    try:
        papiea_test.logger.debug("Executing bucket create intent handler...")

        status = AttributeDict(
            name=entity_bucket.spec.name,
            objects=list()
        )
        await ctx.update_status(entity_bucket.metadata, status)
    except Exception as ex:
        raise Exception("Unable to execute bucket create intent handler: " + str(ex))
Exemple #10
0
async def object_content_handler(ctx, entity_object, diff):
    try:
        papiea_test.logger.debug(
            "Executing object content change intent handler...")

        status = AttributeDict(content=entity_object.spec.content,
                               size=len(entity_object.spec.content),
                               last_modified=str(datetime.now(timezone.utc)),
                               references=entity_object.status.references)
        await ctx.update_status(entity_object.metadata, status)
    except:
        raise
Exemple #11
0
async def on_object_added_handler(ctx, entity_bucket, diff):
    try:
        papiea_test.logger.debug(
            "Executing object added to bucket intent handler...")

        async with papiea_test.get_client(
                papiea_test.OBJECT_KIND) as object_entity_client:
            for entity in diff:
                entity_object = await object_entity_client.get(
                    AttributeDict(uuid=entity.spec[0].reference.uuid))

                entity_object.status.references.append(
                    AttributeDict(bucket_name=entity_bucket.spec.name,
                                  object_name=entity.spec[0].name,
                                  bucket_reference=AttributeDict(
                                      uuid=entity_bucket.metadata.uuid,
                                      kind=papiea_test.BUCKET_KIND)))
                await ctx.update_status(entity_object.metadata,
                                        entity_object.status)

        entity_bucket.status.objects = entity_bucket.spec.objects
        await ctx.update_status(entity_bucket.metadata, entity_bucket.status)
    except:
        raise
Exemple #12
0
async def bucket_create_handler(ctx, entity_bucket):
    try:
        papiea_test.logger.debug("Executing bucket create intent handler...")

        status = AttributeDict(name=entity_bucket.name, objects=list())

        return ConstructorResult(
            spec=status,
            status=status,
            metadata={"extension": {
                "owner": entity_bucket.owner
            }})
    except Exception as ex:
        raise Exception("Unable to execute bucket create intent handler: " +
                        str(ex))
async def create_object(ctx, entity_bucket, input_object_name):
    # check if object name already exists in entity.objects
    # if found, return None/failure
    # else create a new object entity and add the object name
    # reference in the objects list and return the bucket reference

    try:
        objects_list = entity_bucket.spec.objects
        if not any(obj.name == input_object_name for obj in objects_list):
            papiea_test.logger.debug("Object does not exist. Creating new object...")

            async with ctx.entity_client_for_user(papiea_test.object_kind_dict) as object_entity_client:
                entity_object = await object_entity_client.create(
                    Spec(content=""),
                    metadata_extension={
                        "owner": "nutanix"
                    }
                )

                async with ctx.entity_client_for_user(papiea_test.bucket_kind_dict) as bucket_entity_client:
                    entity_bucket.spec.objects.append(
                        AttributeDict(name=input_object_name,
                            reference=EntityReference(
                                uuid=entity_object.metadata.uuid,
                                kind=papiea_test.OBJECT_KIND
                            )
                        )
                    )
                    await bucket_entity_client.update(
                        metadata=entity_bucket.metadata,
                        spec=entity_bucket.spec
                    )

                ret_entity = await object_entity_client.get(entity_object.metadata)
                return EntityReference(
                    uuid=ret_entity.metadata.uuid,
                    kind=ret_entity.metadata.kind
                )
        else:
            raise Exception("Object already exists in the bucket")
    except:
        raise

    return EntityReference(uuid="", kind="", message="Unable to create object entity")
Exemple #14
0
    async def test_non_existent_object_link(self):
        papiea_test.logger.debug("Running test to link to a non-existent object")

        try:
            sdk = await provider.setup_and_register_sdk()
        except Exception as ex:
            papiea_test.logger.debug("Failed to setup/register sdk : " + str(ex))
            return

        try:
            await test_utils.cleanup()
        except Exception as ex:
            papiea_test.logger.debug("Failed cleanup : " + str(ex))
            raise Exception("Cleanup operation failed : " + str(ex))

        try:
            async with papiea_test.get_client(papiea_test.BUCKET_KIND) as bucket_entity_client:
                bucket1_name = "test-bucket1"

                bucket_ref = await bucket_entity_client.invoke_kind_procedure("ensure_bucket_exists", bucket1_name)
                bucket1_entity = await bucket_entity_client.get(bucket_ref)

                assert bucket1_entity.spec.name == bucket1_name
                assert len(bucket1_entity.spec.objects) == 0

                object1_name = "test-object1"

                object_input = AttributeDict(
                    object_name=object1_name,
                    object_uuid="shouldfailuuid"
                )
                await bucket_entity_client.invoke_procedure("link_object", bucket1_entity.metadata, object_input)
        except Exception as ex:
            papiea_test.logger.debug("Failed to perform entity operation : " + str(ex))
            assert str(ex) == "Entity shouldfailuuid not found"
        finally:
            await sdk.server.close()
Exemple #15
0
 async def on_create_handler(ctx, handler_input) -> ConstructorResult:
     spec = AttributeDict(x=handler_input["x"], y=handler_input["y"])
     return ConstructorResult(spec=spec, status=spec)
Exemple #16
0
import e2e_tests as papiea_test

from datetime import datetime, timezone

from papiea.core import AttributeDict, EntityReference, Spec, ConstructorResult

ensure_bucket_exists_takes = AttributeDict(
    EnsureBucketExistsInput=AttributeDict(
        type="string",
        title="Input value of ensure_bucket_exists function",
        description="Name of the bucket to be created/checked for existence"))
ensure_bucket_exists_returns = AttributeDict(
    EnsureBucketExistsOutput=papiea_test.ref_type(
        papiea_test.BUCKET_KIND, "Reference of the bucket created/found"))


async def ensure_bucket_exists(ctx, input_bucket_name):
    # Run get query to obtain the list of buckets
    # Check if bucket_name exists in the bucket list
    # If true, simply return the bucket
    # Else, create a new bucket with input_bucket_name and return

    try:
        async with ctx.entity_client_for_user(
                papiea_test.bucket_kind_dict) as bucket_entity_client:
            desired_bucket = await bucket_entity_client.filter(
                AttributeDict(spec=AttributeDict(name=input_bucket_name)))
            if len(desired_bucket.results) != 0:
                papiea_test.logger.debug(
                    "Bucket already exists. Returning it...")
Exemple #17
0
SERVER_PORT = int(os.environ.get("SERVER_PORT", "3000"))
PAPIEA_ADMIN_S2S_KEY = os.environ.get("PAPIEA_ADMIN_S2S_KEY", "")
PAPIEA_URL = os.getenv("PAPIEA_URL", "http://127.0.0.1:3000")

SERVER_CONFIG_HOST = "127.0.0.1"
SERVER_CONFIG_PORT = 9005
PROVIDER_PREFIX = "test_provider"
PROVIDER_VERSION = "0.1.0"
PROVIDER_ADMIN_S2S_KEY = "Sa8xaic9"
USER_S2S_KEY = ""

BUCKET_KIND = "bucket"
OBJECT_KIND = "object"

bucket_kind_dict = AttributeDict(kind=BUCKET_KIND)
object_kind_dict = AttributeDict(kind=OBJECT_KIND)

logger = logging.getLogger(__name__)
logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)


def get_client(kind: str, tracer: Tracer = init_default_tracer()):
    return EntityCRUD(
        PAPIEA_URL, PROVIDER_PREFIX, PROVIDER_VERSION, kind, USER_S2S_KEY, tracer=tracer
    )