Ejemplo n.º 1
0
    async def test_provider_on_create_should_change_request_format_when_not_set(self):
        async with ProviderSdk.create_provider(PAPIEA_URL, ADMIN_KEY, SERVER_CONFIG_HOST, SERVER_CONFIG_PORT) as sdk:
            try:
                sdk.version(PROVIDER_VERSION)
                sdk.prefix("location_provider_on_create_2")

                location = sdk.new_kind(self.location_yaml)

                await sdk.register()

                user_s2s_key = await create_user_s2s_key(sdk)

                async with EntityCRUD(
                        PAPIEA_URL, "location_provider_on_create_2", "0.1.0", "Location", user_s2s_key
                ) as entity_client:

                    entity = await entity_client.create(
                        {"spec": Spec(x=10, y=11)}
                    )
                    updated_entity = await entity_client.get(entity.metadata)

                    assert updated_entity.metadata.spec_version == 1
                    assert updated_entity.status.x == 10

            except Exception as ex:
                print(str(ex))
            finally:
                await sdk.server.close()
Ejemplo n.º 2
0
async def create_provider_admin_s2s_key(sdk: ProviderSdk, new_key: Key):
    admin_security_api = sdk.provider_security_api

    the_key = CreateS2SKeyRequest(
        name="Test provider admin S2S key",
        owner="*****@*****.**",
        key=new_key,
        user_info={"is_provider_admin": True},
    )

    try:
        keys = await admin_security_api.list_keys()
        for key in keys:
            if key.name == the_key.name:
                papiea_test.logger.debug(f"Key {the_key.name} already exists")
                return
    except SecurityApiError as err:
        raise SecurityApiError.from_error(err, str(err))

    try:
        await admin_security_api.create_key(the_key)
        provider_admin_security_api = sdk.new_security_api(new_key)
        await provider_admin_security_api.user_info()
        # papiea_test.logger.debug(f"User info {user_info}")
    except SecurityApiError as err:
        raise SecurityApiError.from_error(err, str(err))
Ejemplo n.º 3
0
 async def test_provider_missing_version(self):
     async with ProviderSdk.create_provider(PAPIEA_URL, ADMIN_KEY, SERVER_CONFIG_HOST, SERVER_CONFIG_PORT) as sdk:
         with pytest.raises(Exception) as excinfo:
             sdk.prefix("test_provider")
             sdk.new_kind(self.location_yaml)
             await sdk.register()
             await sdk.server.close()
         assert str(excinfo.value) == "Malformed provider description. Missing: version"
Ejemplo n.º 4
0
async def create_user_s2s_key(sdk: ProviderSdk):
    admin_security_api = sdk.provider_security_api

    the_key = CreateS2SKeyRequest(
        name="location provider some.user s2s key",
        user_info={"owner": "nitesh", "tenant": "ada14b27-c147-4aca-9b9f-7762f1f48426"},
    )

    new_s2s_key = await admin_security_api.create_key(the_key)
    user_security_api = sdk.new_security_api(new_s2s_key.key)
    await user_security_api.user_info()
    return new_s2s_key.key
Ejemplo n.º 5
0
async def create_user_s2s_key(sdk: ProviderSdk):
    admin_security_api = sdk.provider_security_api

    the_key = CreateS2SKeyRequest(
        name="test provider some.user s2s key",
        user_info={"owner": "nutanix"},
    )

    try:
        new_s2s_key = await admin_security_api.create_key(the_key)
        user_security_api = sdk.new_security_api(new_s2s_key.key)
        await user_security_api.user_info()
        return new_s2s_key.key
    except SecurityApiError as err:
        raise SecurityApiError.from_error(err, str(err))
Ejemplo n.º 6
0
    async def test_provider_entity_modify(self):
        async with ProviderSdk.create_provider(PAPIEA_URL, ADMIN_KEY, SERVER_CONFIG_HOST, SERVER_CONFIG_PORT) as sdk:
            try:
                sdk.version(PROVIDER_VERSION)
                sdk.prefix("location_provider")

                location = sdk.new_kind(self.location_yaml)
                procedure_description = ProcedureDescription(
                    input_schema=load_yaml_from_file('./test_data/procedure_move_input.yml'),
                    output_schema=load_yaml_from_file('./test_data/procedure_move_output.yml')
                )

                async def movex_handler(ctx, entity, input):
                    entity.spec.x += input
                    allowed = await ctx.check_permission([(Action.Update, entity.metadata)])
                    if not allowed:
                        raise Exception("Permission denied")
                    async with ctx.entity_client_for_user(entity.metadata) as entity_client:
                        await entity_client.update(entity.metadata, entity.spec)
                    return entity.spec.x

                location.entity_procedure(
                    "moveX",
                    procedure_description,
                    movex_handler
                )
                await sdk.register()

                user_s2s_key = await create_user_s2s_key(sdk)
                async with EntityCRUD(
                        PAPIEA_URL, "location_provider", "0.1.0", "Location", user_s2s_key
                ) as entity_client:

                    entity = await entity_client.create(
                        {"spec": Spec(x=10, y=11)}
                    )
                    await entity_client.invoke_procedure("moveX", entity.metadata, 5)
                    updated_entity = await entity_client.get(entity.metadata)

                    assert updated_entity.metadata.spec_version == 2
                    assert updated_entity.spec.x == 15
            except Exception as e:
                print(str(e))
            finally:
                await sdk.server.close()
Ejemplo n.º 7
0
    async def test_provider_on_create_should_change_request_format_when_set(self):
        async with ProviderSdk.create_provider(PAPIEA_URL, ADMIN_KEY, SERVER_CONFIG_HOST, SERVER_CONFIG_PORT) as sdk:
            try:
                sdk.version(PROVIDER_VERSION)
                sdk.prefix("location_provider_on_create")

                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)

                location = sdk.new_kind(self.location_yaml)
                location.on_create(ConstructorProcedureDescription(input_schema={
                    "Location": {
                        "properties": {
                            "x": {
                                "type": "number"
                            },
                            "y": {
                                "type": "number"
                            }
                        }
                    }
                }), on_create_handler)

                await sdk.register()

                user_s2s_key = await create_user_s2s_key(sdk)

                async with EntityCRUD(
                        PAPIEA_URL, "location_provider_on_create", "0.1.0", "Location", user_s2s_key
                ) as entity_client:

                    entity = await entity_client.create(
                        {"spec": Spec(x=10, y=11)}
                    )
                    updated_entity = await entity_client.get(entity.metadata)

                    assert updated_entity.metadata.spec_version == 1
                    assert updated_entity.status.x == 10

            except Exception as ex:
                print(str(ex))
            finally:
                await sdk.server.close()
Ejemplo n.º 8
0
 async def test_valid_kind_yaml(self):
     async with ProviderSdk.create_provider(PAPIEA_URL, ADMIN_KEY, SERVER_CONFIG_HOST, SERVER_CONFIG_PORT) as sdk:
         location_manager = sdk.new_kind(self.location_yaml)
         assert location_manager.kind.name == "Location"
Ejemplo n.º 9
0
 async def test_empty_kind_yaml(self):
     async with ProviderSdk.create_provider(PAPIEA_URL, ADMIN_KEY, SERVER_CONFIG_HOST, SERVER_CONFIG_PORT) as sdk:
         with pytest.raises(Exception) as excinfo:
             sdk.new_kind({})
         assert str(excinfo.value) == "Wrong kind description specified"
Ejemplo n.º 10
0
async def setup_and_register_sdk() -> ProviderSdk:
    try:
        async with ProviderSdk.create_provider(
                papiea_test.PAPIEA_URL,
                papiea_test.PAPIEA_ADMIN_S2S_KEY,
                papiea_test.SERVER_CONFIG_HOST,
                papiea_test.SERVER_CONFIG_PORT,
                logger=papiea_test.logger) as sdk:
            sdk.version(papiea_test.PROVIDER_VERSION)
            sdk.prefix(papiea_test.PROVIDER_PREFIX)

            # TODO: Add security policy to set the secure_with parameters
            sdk.metadata_extension(metadata_extension)

            await create_provider_admin_s2s_key(
                sdk, papiea_test.PROVIDER_ADMIN_S2S_KEY)

            bucket = sdk.new_kind(bucket_yaml)
            obj = sdk.new_kind(object_yaml)

            bucket.on_create(
                ConstructorProcedureDescription(
                    input_schema={
                        "Bucket": {
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "owner": {
                                    "type": "string"
                                },
                                "objects": {
                                    "type": "array"
                                }
                            }
                        }
                    }), procedure_handlers.bucket_create_handler)
            obj.on_create(
                ConstructorProcedureDescription(
                    input_schema={
                        "Object": {
                            "properties": {
                                "content": {
                                    "type": "string"
                                },
                                "owner": {
                                    "type": "string"
                                }
                            }
                        }
                    }), procedure_handlers.object_create_handler)

            bucket.on("name", procedure_handlers.bucket_name_handler)
            bucket.on("objects.+{name}",
                      procedure_handlers.on_object_added_handler)
            bucket.on("objects.-{name}",
                      procedure_handlers.on_object_removed_handler)

            obj.on("content", procedure_handlers.object_content_handler)

            bucket.kind_procedure(
                "ensure_bucket_exists",
                ProcedureDescription(
                    input_schema=procedure_handlers.ensure_bucket_exists_takes,
                    output_schema=procedure_handlers.
                    ensure_bucket_exists_returns,
                    description=
                    "Description for ensure_bucket_exists kind-level procedure"
                ), procedure_handlers.ensure_bucket_exists)

            bucket.entity_procedure(
                "change_bucket_name",
                ProcedureDescription(
                    input_schema=procedure_handlers.change_bucket_name_takes,
                    output_schema=procedure_handlers.
                    change_bucket_name_returns,
                    description=
                    "Description for change_bucket_name entity-level procedure"
                ), procedure_handlers.change_bucket_name)

            bucket.entity_procedure(
                "create_object",
                ProcedureDescription(
                    input_schema=procedure_handlers.create_object_takes,
                    output_schema=procedure_handlers.create_object_returns,
                    description=
                    "Description for create_object entity-level procedure"),
                procedure_handlers.create_object)

            bucket.entity_procedure(
                "link_object",
                ProcedureDescription(
                    input_schema=procedure_handlers.link_object_takes,
                    output_schema=procedure_handlers.link_object_returns,
                    description=
                    "Description for link_object entity-level procedure"),
                procedure_handlers.link_object)

            bucket.entity_procedure(
                "unlink_object",
                ProcedureDescription(
                    input_schema=procedure_handlers.unlink_object_takes,
                    output_schema=procedure_handlers.unlink_object_returns,
                    description=
                    "Description for unlink_object entity-level procedure"),
                procedure_handlers.unlink_object)

            await sdk.register()
            papiea_test.USER_S2S_KEY = await create_user_s2s_key(sdk)

            return sdk
    except SecurityApiError as err:
        raise SecurityApiError.from_error(err, str(err))
    except:
        raise