graphcore-kernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import time
|
|
3
|
+
from typing import Any, ClassVar, Dict, List, Optional
|
|
4
|
+
from graphcore.registry.decorators import register_entity
|
|
5
|
+
from graphcore.types.entity_id import is_entity_id
|
|
6
|
+
from graphcore.types.strategy import StrategyRequestPayload
|
|
7
|
+
import logging
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@register_entity()
|
|
13
|
+
class Entity():
|
|
14
|
+
"""
|
|
15
|
+
Base entity class for the entity service framework.
|
|
16
|
+
|
|
17
|
+
All entities inherit from this class and are identified by their class name
|
|
18
|
+
instead of an enum value.
|
|
19
|
+
"""
|
|
20
|
+
_graphcore_entity_type_id = None
|
|
21
|
+
# Entities of an ownerless type never get an owner, so no one holds
|
|
22
|
+
# owner-implicit Admin on them.
|
|
23
|
+
ownerless: ClassVar[bool] = False
|
|
24
|
+
# Fields that cannot change once they hold a value.
|
|
25
|
+
write_once_fields: ClassVar[tuple[str, ...]] = ()
|
|
26
|
+
# Typed reference fields this entity cannot outlive: deleting the local
|
|
27
|
+
# entity a field names deletes this one (docs/ENTITY_REFERENCE_TDD.md).
|
|
28
|
+
dependent_on: ClassVar[tuple[str, ...]] = ()
|
|
29
|
+
|
|
30
|
+
# Keys `to_dict()`/`to_client()` emit that are derived, never hydrated:
|
|
31
|
+
# the type id comes from the class, the projections from the repository.
|
|
32
|
+
_DERIVED_KEYS: ClassVar[frozenset] = frozenset({
|
|
33
|
+
'entity_type_id',
|
|
34
|
+
'children_ids', 'parent_ids', 'ancestor_ids', 'reference_ids',
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
def __init__(self, entity_id: str):
|
|
38
|
+
# `gc://<backend_id>/<local_id>` is the only identifier form above the
|
|
39
|
+
# repository; a bare local id is not one (docs/ENTITY_REFERENCE_TDD.md).
|
|
40
|
+
# Minting is `EntityService.create`'s alone - it is the only thing
|
|
41
|
+
# that knows this deployment's identity.
|
|
42
|
+
if not is_entity_id(entity_id):
|
|
43
|
+
raise ValueError(f"Invalid entity identifier: {entity_id}")
|
|
44
|
+
|
|
45
|
+
self.entity_id = entity_id
|
|
46
|
+
self.entity_type_id = self.get_entity_type_id()
|
|
47
|
+
# A plain bag of caller-owned data, serialized as its own nested
|
|
48
|
+
# `attributes` key - never expanded to the top level. An entity field
|
|
49
|
+
# is declared on the entity (and as a column on its Django model);
|
|
50
|
+
# this map holds only what a caller chooses to stash.
|
|
51
|
+
self._attributes: Dict[str, Any] = {}
|
|
52
|
+
# Single-parent containment (see docs/AUTHORIZATION_TECHNICAL_TDD.md,
|
|
53
|
+
# Containment): `parent_id` is the only persisted edge. `children_ids`
|
|
54
|
+
# is a read-only, ordered projection populated by EntityService on
|
|
55
|
+
# hydration - never assigned directly. Sibling order is
|
|
56
|
+
# `sibling_position`, a field on the child, so reordering never
|
|
57
|
+
# writes the parent.
|
|
58
|
+
self.parent_id: Optional[str] = None
|
|
59
|
+
self.sibling_position: float = 0.0
|
|
60
|
+
self._children_ids: tuple = ()
|
|
61
|
+
# Every containing scope, root-first, maintained by the repository:
|
|
62
|
+
# ancestor_ids(e) = ancestor_ids(parent) + [parent_id]. Read-only
|
|
63
|
+
# here for the same reason as `children_ids` - containment is a
|
|
64
|
+
# write to the child's `parent_id`, never a field edit on this list.
|
|
65
|
+
self._ancestor_ids: tuple = ()
|
|
66
|
+
# Every identifier any of this entity's fields hold, maintained by the
|
|
67
|
+
# repository on save. Read-only for the same reason as the two above.
|
|
68
|
+
self._reference_ids: tuple = ()
|
|
69
|
+
self.version = 0 # Optimistic locking version
|
|
70
|
+
# Creator/owner User UUID (immutable after creation)
|
|
71
|
+
self.owner: Optional[str] = None
|
|
72
|
+
|
|
73
|
+
def on_create(self, param_config: Optional[Dict[str, Any]] = None) -> Optional[List[StrategyRequestPayload]]:
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def children_ids(self) -> tuple:
|
|
78
|
+
"""Ordered (by `sibling_position`), read-only projection over children whose
|
|
79
|
+
`parent_id` names this entity. Populated by EntityService on
|
|
80
|
+
hydration - not settable. `.append`/`.remove`/assignment all raise
|
|
81
|
+
`AttributeError` by design: mutating containment is a write to the
|
|
82
|
+
*child* (`set_parent`), never a field edit on the
|
|
83
|
+
parent, so a strategy outside a child's own allowlist cannot detach
|
|
84
|
+
it (e.g. a `Grant`) by editing this list."""
|
|
85
|
+
return self._children_ids
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def ancestor_ids(self) -> tuple:
|
|
89
|
+
"""Read-only projection of the containment chain above this entity,
|
|
90
|
+
root-first, populated by the repository on hydration."""
|
|
91
|
+
return self._ancestor_ids
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def reference_ids(self) -> tuple:
|
|
95
|
+
"""Read-only projection of the entity identifiers held in any of this
|
|
96
|
+
entity's fields other than its own id, populated by the repository on
|
|
97
|
+
save and on hydration. Backs reverse lookup (`EntityService.referrers`)."""
|
|
98
|
+
return self._reference_ids
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def parent_ids(self) -> tuple:
|
|
102
|
+
"""Derived view of `parent_id` as a single-element tuple (or
|
|
103
|
+
empty). Not settable - use `set_parent`."""
|
|
104
|
+
return (self.parent_id,) if self.parent_id else ()
|
|
105
|
+
|
|
106
|
+
def set_parent(self, parent_id: Optional[str]):
|
|
107
|
+
'''Set this entity's single parent, or detach it with `None`. Writes
|
|
108
|
+
only this entity - the child is what callers save. A new parent
|
|
109
|
+
defaults `sibling_position` to now, so children append in order
|
|
110
|
+
without a sibling query; reordering uses `sibling_position` directly
|
|
111
|
+
(see ReorderChild).'''
|
|
112
|
+
self.parent_id = parent_id
|
|
113
|
+
if parent_id is not None:
|
|
114
|
+
self.sibling_position = time.time()
|
|
115
|
+
|
|
116
|
+
def set_attribute(self, name: str, value: Any):
|
|
117
|
+
'''Set an attribute on the entity'''
|
|
118
|
+
self._attributes[name] = value
|
|
119
|
+
|
|
120
|
+
def get_attribute(self, name: str) -> Any:
|
|
121
|
+
'''Get an attribute from the entity'''
|
|
122
|
+
return self._attributes[name]
|
|
123
|
+
|
|
124
|
+
def remove_attribute(self, name: str):
|
|
125
|
+
'''Remove an attribute from the entity'''
|
|
126
|
+
del self._attributes[name]
|
|
127
|
+
|
|
128
|
+
def has_attribute(self, name: str) -> bool:
|
|
129
|
+
'''Check if an attribute exists on the entity'''
|
|
130
|
+
return name in self._attributes
|
|
131
|
+
|
|
132
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
133
|
+
"""The complete, JSON-compatible, round-trippable state of this
|
|
134
|
+
entity - the write-side record. Fields are top-level keys; the
|
|
135
|
+
attributes bag is nested under `attributes`. Never includes derived
|
|
136
|
+
projections (`children_ids`, `parent_ids`, `ancestor_ids`); those
|
|
137
|
+
live only on `to_client()`."""
|
|
138
|
+
attributes = {}
|
|
139
|
+
for key, value in self._attributes.items():
|
|
140
|
+
try:
|
|
141
|
+
json.dumps(value)
|
|
142
|
+
except (TypeError, ValueError):
|
|
143
|
+
logger.warning(
|
|
144
|
+
"Attribute '%s' with value '%r' cannot be serialized to JSON. It will be dropped.",
|
|
145
|
+
key, value,
|
|
146
|
+
)
|
|
147
|
+
else:
|
|
148
|
+
attributes[key] = value
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
'entity_type_id': self.entity_type_id,
|
|
152
|
+
'entity_id': self.entity_id,
|
|
153
|
+
'parent_id': self.parent_id,
|
|
154
|
+
'sibling_position': self.sibling_position,
|
|
155
|
+
'version': self.version,
|
|
156
|
+
'owner': self.owner,
|
|
157
|
+
'attributes': attributes,
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
def to_client(self) -> Dict[str, Any]:
|
|
161
|
+
"""Readable, client-bound view: `to_dict()` plus the derived
|
|
162
|
+
projections a client needs to render containment. Override per-type
|
|
163
|
+
to also trim fields not meant for the wire (e.g. correlation keys,
|
|
164
|
+
large payloads)."""
|
|
165
|
+
return {
|
|
166
|
+
**self.to_dict(),
|
|
167
|
+
'children_ids': list(self._children_ids),
|
|
168
|
+
'parent_ids': list(self.parent_ids),
|
|
169
|
+
'ancestor_ids': list(self._ancestor_ids),
|
|
170
|
+
'reference_ids': list(self._reference_ids),
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@classmethod
|
|
174
|
+
def from_dict(cls, data: Dict[str, Any]) -> 'Entity':
|
|
175
|
+
"""Hydrate a new entity instance from a `to_dict()`-shaped mapping.
|
|
176
|
+
Every top-level key must be a field this entity declares (or a
|
|
177
|
+
derived key, which is ignored); the bag comes only from `attributes`."""
|
|
178
|
+
entity = cls(entity_id=data.get('entity_id'))
|
|
179
|
+
attributes = data.get('attributes')
|
|
180
|
+
if attributes is not None and not isinstance(attributes, dict):
|
|
181
|
+
raise ValueError('attributes must be an object or null')
|
|
182
|
+
entity._attributes = dict(attributes or {})
|
|
183
|
+
fields = entity.to_dict().keys() & vars(entity).keys()
|
|
184
|
+
|
|
185
|
+
for key, value in data.items():
|
|
186
|
+
if key in cls._DERIVED_KEYS or key in ('entity_id', 'attributes'):
|
|
187
|
+
continue
|
|
188
|
+
if key.startswith('_') or key not in fields:
|
|
189
|
+
raise ValueError(f"{cls.__name__} has no field {key!r}")
|
|
190
|
+
if key == 'version' and value is None:
|
|
191
|
+
continue
|
|
192
|
+
setattr(entity, key, value)
|
|
193
|
+
|
|
194
|
+
return entity
|
|
195
|
+
|
|
196
|
+
@classmethod
|
|
197
|
+
def get_entity_type_id(cls) -> str:
|
|
198
|
+
"""Get the stable entity type identifier (e.g., 'graphcore.Document')"""
|
|
199
|
+
entity_type_id = cls.__dict__.get('_graphcore_entity_type_id')
|
|
200
|
+
if entity_type_id is None:
|
|
201
|
+
raise ValueError(
|
|
202
|
+
f"Entity class {cls.__name__} has not been registered."
|
|
203
|
+
)
|
|
204
|
+
return entity_type_id
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from typing import ClassVar
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.Entity import Entity
|
|
4
|
+
from graphcore.registry.decorators import register_entity
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@register_entity()
|
|
8
|
+
class Deployment(Entity):
|
|
9
|
+
"""The deployment as a party that grants authority: grants on it are
|
|
10
|
+
the identity-based policy. Exactly one exists, seeded by the kernel with
|
|
11
|
+
no owner, and it contains nothing. See
|
|
12
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md (Identity-based policy)."""
|
|
13
|
+
|
|
14
|
+
ownerless: ClassVar[bool] = True
|
|
15
|
+
|
|
16
|
+
def __init__(self, entity_id: str):
|
|
17
|
+
super().__init__(entity_id)
|
|
18
|
+
self.new_user_group_ids: list[str] = []
|
|
19
|
+
self.new_user_roles: list[str] = []
|
|
20
|
+
|
|
21
|
+
def to_dict(self) -> dict:
|
|
22
|
+
data = super().to_dict()
|
|
23
|
+
data['new_user_group_ids'] = list(self.new_user_group_ids)
|
|
24
|
+
data['new_user_roles'] = list(self.new_user_roles)
|
|
25
|
+
return data
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from typing import ClassVar, Optional
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.Entity import Entity
|
|
4
|
+
from graphcore.registry.decorators import register_entity
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@register_entity()
|
|
8
|
+
class Grant(Entity):
|
|
9
|
+
"""A stored grant fact: `(subject_id, role_name)`, contained by the
|
|
10
|
+
resource it grants on.
|
|
11
|
+
|
|
12
|
+
`subject_id` and `role_name` are typed class fields, not attributes-bag
|
|
13
|
+
entries - the generic attribute strategies cannot express a grant
|
|
14
|
+
mutation. Mutated only through the kernel grant strategies (see
|
|
15
|
+
domain/strategy/GrantStrategies.py); not shareable, not a group. See
|
|
16
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md (Grant storage).
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
ownerless: ClassVar[bool] = True
|
|
20
|
+
|
|
21
|
+
def __init__(self, entity_id: str):
|
|
22
|
+
super().__init__(entity_id)
|
|
23
|
+
self.subject_id: Optional[str] = None
|
|
24
|
+
self.role_name: Optional[str] = None
|
|
25
|
+
|
|
26
|
+
def to_dict(self) -> dict:
|
|
27
|
+
data = super().to_dict()
|
|
28
|
+
data['subject_id'] = self.subject_id
|
|
29
|
+
data['role_name'] = self.role_name
|
|
30
|
+
return data
|
|
31
|
+
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from typing import ClassVar, Optional
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.Entity import Entity
|
|
4
|
+
from graphcore.registry.decorators import register_entity
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@register_entity()
|
|
8
|
+
class Group(Entity):
|
|
9
|
+
"""Pure subject: a name and a roster, nothing else.
|
|
10
|
+
|
|
11
|
+
Roster mutation is exposed only through
|
|
12
|
+
`AddMember`/`RemoveMember` (see
|
|
13
|
+
domain/strategy/GroupStrategies.py), never through the generic
|
|
14
|
+
attribute strategies. See docs/AUTHORIZATION_TECHNICAL_TDD.md (Groups).
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
write_once_fields: ClassVar[tuple[str, ...]] = ('key',)
|
|
18
|
+
|
|
19
|
+
def __init__(self, entity_id: str):
|
|
20
|
+
super().__init__(entity_id)
|
|
21
|
+
self.name: Optional[str] = None
|
|
22
|
+
# Stable, immutable once set; how code refers to a group across environments.
|
|
23
|
+
self.key: Optional[str] = None
|
|
24
|
+
self.member_ids: list[str] = []
|
|
25
|
+
|
|
26
|
+
def add_member(self, principal_entity_id: str):
|
|
27
|
+
"""Add a user to the membership roster (idempotent)."""
|
|
28
|
+
if principal_entity_id not in self.member_ids:
|
|
29
|
+
self.member_ids.append(principal_entity_id)
|
|
30
|
+
|
|
31
|
+
def remove_member(self, principal_entity_id: str):
|
|
32
|
+
"""Remove a user from the membership roster (no-op if absent)."""
|
|
33
|
+
if principal_entity_id in self.member_ids:
|
|
34
|
+
self.member_ids.remove(principal_entity_id)
|
|
35
|
+
|
|
36
|
+
def to_dict(self) -> dict:
|
|
37
|
+
data = super().to_dict()
|
|
38
|
+
data['name'] = self.name
|
|
39
|
+
data['key'] = self.key
|
|
40
|
+
data['member_ids'] = self.member_ids
|
|
41
|
+
return data
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def from_dict(cls, data: dict) -> 'Group':
|
|
45
|
+
group = super().from_dict(data)
|
|
46
|
+
group.member_ids = list(data.get('member_ids') or [])
|
|
47
|
+
return group
|
|
48
|
+
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Groups: `Group` and its subclasses
|
|
2
|
+
|
|
3
|
+
A group is a **subject** (see `docs/AUTHORIZATION_TECHNICAL_TDD.md`, Core
|
|
4
|
+
concepts): something that can receive a grant on behalf of more than one
|
|
5
|
+
principal. A group is `Group` or a subclass of it. There is no
|
|
6
|
+
registration flag: the access adapter, the grant strategies and the generic
|
|
7
|
+
create all recognize a group with `isinstance(entity, Group)`, and
|
|
8
|
+
each registered subclass of `Group` is a group type
|
|
9
|
+
(`docs/ARCHITECTURE_PRINCIPLES.md`, rule 7).
|
|
10
|
+
|
|
11
|
+
## `Group`
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
@register_entity()
|
|
15
|
+
class Group(Entity):
|
|
16
|
+
write_once_fields = ('key',)
|
|
17
|
+
|
|
18
|
+
name: Optional[str]
|
|
19
|
+
key: Optional[str] # how code refers to a group across environments
|
|
20
|
+
member_ids: list[str] # stored on the group table, GIN-indexed on PostgreSQL
|
|
21
|
+
|
|
22
|
+
def add_member(self, principal_entity_id: str): ...
|
|
23
|
+
def remove_member(self, principal_entity_id: str): ...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
What treats it as a group:
|
|
27
|
+
|
|
28
|
+
| Where | Behavior |
|
|
29
|
+
|---|---|
|
|
30
|
+
| `domain/strategy/GrantStrategies.py` `_validate_subject` | Accepts it as a grant subject |
|
|
31
|
+
| `auth/authz/adapter/default_access_adapter.py` | Expands it to its members when deciding access, for the lockout rule and for live updates; busts its members' cached group sets when its roster changes or it is deleted |
|
|
32
|
+
| `domain/strategy/BaseStrategy.py` `CreateEntity` | Seeds the creator as its first member |
|
|
33
|
+
|
|
34
|
+
Its policy (`auth/authz/kernel_policy.py`) declares it `shareable=True`,
|
|
35
|
+
`can_contain=[Grant]`, with an exhaustive strategy allowlist
|
|
36
|
+
(`other_strategies=False`):
|
|
37
|
+
|
|
38
|
+
| Strategy | Registered on | Required permission |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `AddMember`, `RemoveMember` | `Group` | `ManageAccess()` |
|
|
41
|
+
| `ModifyGroup` (rename) | `Group` | `Write()` |
|
|
42
|
+
| `CreateGroup` (create a group under the target, or at the top level with no target) | `Entity` | `Create()` on the target (the deployment when there is none); a `key` or `admin_managed` also needs `ManageAccess()` on the deployment |
|
|
43
|
+
| `CreateGrant` | `Entity` | `ManageAccess()` |
|
|
44
|
+
| `ChangeGrantRole`, `RevokeGrant` | `Grant` | `Write()` on the group; `ManageAccess()` on the grant |
|
|
45
|
+
| `SetAttributes`, `DeleteEntity` | generic | `Write()` |
|
|
46
|
+
|
|
47
|
+
`CreateGroup` and `CreateGrant` appear in the allowlist
|
|
48
|
+
because the create rule checks `current_strategy ∈
|
|
49
|
+
allowed_strategies(type(new_entity))`: whichever strategy creates a group
|
|
50
|
+
(or a grant on one) must be on `Group`'s own allowlist, whatever type
|
|
51
|
+
it is invoked against. See `docs/AUTHORIZATION_TECHNICAL_TDD.md`
|
|
52
|
+
(Creation).
|
|
53
|
+
|
|
54
|
+
## Your own group type
|
|
55
|
+
|
|
56
|
+
Subclass `Group` and register the subclass:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
@register_entity()
|
|
60
|
+
class Team(Group):
|
|
61
|
+
pass
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
It inherits the fields, the methods, the storage (a subclass that adds no
|
|
65
|
+
columns uses `GroupModel`), the policy - `shareable`, `can_contain`, the
|
|
66
|
+
roles and every strategy permission above - and every behavior the adapter
|
|
67
|
+
gives groups.
|
|
68
|
+
|
|
69
|
+
`CreateGroup` creates only `Group` itself, and the inherited
|
|
70
|
+
allowlist does not include the generic `CreateEntity`. To make the
|
|
71
|
+
subclass creatable, define its policy and list the strategies it allows,
|
|
72
|
+
each with its permission:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
policy.define(
|
|
76
|
+
Team,
|
|
77
|
+
strategies={
|
|
78
|
+
CreateEntity: Create(),
|
|
79
|
+
AddMember: ManageAccess(),
|
|
80
|
+
RemoveMember: ManageAccess(),
|
|
81
|
+
},
|
|
82
|
+
other_strategies=False,
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
A type that cannot subclass `Group` is not a group to the kernel's
|
|
87
|
+
adapter. An application that needs that writes its own `AccessPort`
|
|
88
|
+
adapter.
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from typing import ClassVar, Optional
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.Entity import Entity
|
|
4
|
+
from graphcore.registry.decorators import register_entity
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@register_entity()
|
|
8
|
+
class Invite(Entity):
|
|
9
|
+
"""A link that grants `role_name` on its parent resource to whoever
|
|
10
|
+
redeems it. Stores only the SHA-256 of the link secret. Created by
|
|
11
|
+
`CreateInvite`, redeemed by `GraphAPI.redeem_invite`. See
|
|
12
|
+
docs/AUTHORIZATION_TECHNICAL_TDD.md (Invites)."""
|
|
13
|
+
|
|
14
|
+
ownerless: ClassVar[bool] = True
|
|
15
|
+
|
|
16
|
+
def __init__(self, entity_id: str):
|
|
17
|
+
super().__init__(entity_id)
|
|
18
|
+
self.role_name: Optional[str] = None
|
|
19
|
+
self.token_hash: Optional[str] = None
|
|
20
|
+
self.expires_at: Optional[float] = None
|
|
21
|
+
self.max_uses: int = 1
|
|
22
|
+
self.redeemed_by: list[str] = []
|
|
23
|
+
|
|
24
|
+
def to_dict(self) -> dict:
|
|
25
|
+
data = super().to_dict()
|
|
26
|
+
data['role_name'] = self.role_name
|
|
27
|
+
data['token_hash'] = self.token_hash
|
|
28
|
+
data['expires_at'] = self.expires_at
|
|
29
|
+
data['max_uses'] = self.max_uses
|
|
30
|
+
data['redeemed_by'] = self.redeemed_by
|
|
31
|
+
return data
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from typing import ClassVar, Optional
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.Entity import Entity
|
|
4
|
+
from graphcore.registry.decorators import register_entity
|
|
5
|
+
from graphcore.types.schedules import ScheduleSpec
|
|
6
|
+
from graphcore.types.strategy import StrategyRequestPayload
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@register_entity()
|
|
10
|
+
class RequestTemplate(Entity):
|
|
11
|
+
"""A request to make later, optionally on a schedule; never run itself
|
|
12
|
+
(docs/SCHEDULING_TDD.md). Requests made from it run as its `owner`."""
|
|
13
|
+
|
|
14
|
+
dependent_on: ClassVar[tuple[str, ...]] = ('target_entity_id',)
|
|
15
|
+
|
|
16
|
+
def __init__(self, entity_id: str):
|
|
17
|
+
super().__init__(entity_id)
|
|
18
|
+
self.strategy_name: str = ''
|
|
19
|
+
self.param_config: dict = {}
|
|
20
|
+
self.target_entity_id: Optional[str] = None
|
|
21
|
+
self.callbacks: list = []
|
|
22
|
+
self.cron: Optional[str] = None
|
|
23
|
+
self.enabled: bool = True
|
|
24
|
+
|
|
25
|
+
def to_dict(self):
|
|
26
|
+
return {
|
|
27
|
+
**super().to_dict(),
|
|
28
|
+
'strategy_name': self.strategy_name,
|
|
29
|
+
'param_config': self.param_config,
|
|
30
|
+
'target_entity_id': self.target_entity_id,
|
|
31
|
+
'callbacks': self.callbacks,
|
|
32
|
+
'cron': self.cron,
|
|
33
|
+
'enabled': self.enabled,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
def schedule_spec(self) -> Optional[ScheduleSpec]:
|
|
37
|
+
if self.cron is None or self.owner is None:
|
|
38
|
+
return None
|
|
39
|
+
return ScheduleSpec(target_id=self.entity_id, principal_id=self.owner,
|
|
40
|
+
cron=self.cron, enabled=bool(self.enabled))
|
|
41
|
+
|
|
42
|
+
def to_payload(self) -> StrategyRequestPayload:
|
|
43
|
+
return StrategyRequestPayload(
|
|
44
|
+
strategy_name=self.strategy_name,
|
|
45
|
+
param_config=dict(self.param_config or {}),
|
|
46
|
+
target_entity_id=self.target_entity_id,
|
|
47
|
+
callbacks=list(self.callbacks or []) or None,
|
|
48
|
+
)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from django.test import SimpleTestCase
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.request_template.RequestTemplate import RequestTemplate
|
|
4
|
+
from graphcore.tests.ids import fixture_id
|
|
5
|
+
from graphcore.types.schedules import ScheduleSpec
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestRequestTemplate(SimpleTestCase):
|
|
9
|
+
def _template(self, **fields):
|
|
10
|
+
template = RequestTemplate(entity_id=fixture_id())
|
|
11
|
+
template.strategy_name = 'Probe'
|
|
12
|
+
template.param_config = {'amount': 1}
|
|
13
|
+
template.target_entity_id = fixture_id()
|
|
14
|
+
template.owner = fixture_id()
|
|
15
|
+
for name, value in fields.items():
|
|
16
|
+
setattr(template, name, value)
|
|
17
|
+
return template
|
|
18
|
+
|
|
19
|
+
def test_from_dict_roundtrip(self):
|
|
20
|
+
template = self._template(cron='*/5 * * * *', enabled=False,
|
|
21
|
+
callbacks=[{'strategy_name': 'Next', 'param_config': {}}])
|
|
22
|
+
|
|
23
|
+
record = template.to_dict()
|
|
24
|
+
self.assertEqual(RequestTemplate.from_dict(record).to_dict(), record)
|
|
25
|
+
|
|
26
|
+
def test_schedule_spec_carries_owner_cron_and_enabled(self):
|
|
27
|
+
template = self._template(cron='0 2 * * *', enabled=False)
|
|
28
|
+
|
|
29
|
+
self.assertEqual(
|
|
30
|
+
template.schedule_spec(),
|
|
31
|
+
ScheduleSpec(target_id=template.entity_id, principal_id=template.owner,
|
|
32
|
+
cron='0 2 * * *', enabled=False),
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def test_an_unscheduled_template_has_no_spec(self):
|
|
36
|
+
self.assertIsNone(self._template(cron=None).schedule_spec())
|
|
37
|
+
|
|
38
|
+
def test_to_payload_is_what_each_request_runs(self):
|
|
39
|
+
callback = {'strategy_name': 'Next', 'param_config': {}, 'target_entity_id': None}
|
|
40
|
+
template = self._template(callbacks=[callback])
|
|
41
|
+
|
|
42
|
+
payload = template.to_payload()
|
|
43
|
+
|
|
44
|
+
self.assertEqual(
|
|
45
|
+
(payload.strategy_name, payload.param_config, payload.target_entity_id, payload.callbacks),
|
|
46
|
+
('Probe', {'amount': 1}, template.target_entity_id, [callback]),
|
|
47
|
+
)
|
|
48
|
+
payload.param_config['amount'] = 2
|
|
49
|
+
self.assertEqual(template.param_config, {'amount': 1})
|
|
50
|
+
|
|
51
|
+
def test_a_template_goes_with_its_target(self):
|
|
52
|
+
self.assertEqual(RequestTemplate.dependent_on, ('target_entity_id',))
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.Entity import Entity
|
|
4
|
+
from graphcore.registry.decorators import register_entity
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@register_entity(principal=True)
|
|
8
|
+
class ServiceAccount(Entity):
|
|
9
|
+
"""
|
|
10
|
+
A non-human principal: a process an app ships, signing in with a client
|
|
11
|
+
the app declares in ``graphcore_service_clients``. Identified by its
|
|
12
|
+
declaration: ``client_id`` is the issuer's client (``<app_id>-<name>``),
|
|
13
|
+
``app_id`` and ``client_name`` the declaration, ``issuer`` the issuer its
|
|
14
|
+
first token came from. Contains nothing.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, entity_id: str, app_id: Optional[str] = None, client_name: Optional[str] = None,
|
|
18
|
+
client_id: Optional[str] = None, issuer: Optional[str] = None):
|
|
19
|
+
super().__init__(entity_id)
|
|
20
|
+
self.app_id = app_id
|
|
21
|
+
self.client_name = client_name
|
|
22
|
+
self.client_id = client_id
|
|
23
|
+
self.issuer = issuer
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def display_name(self) -> Optional[str]:
|
|
27
|
+
return self.client_id
|
|
28
|
+
|
|
29
|
+
def to_dict(self) -> dict:
|
|
30
|
+
data = super().to_dict()
|
|
31
|
+
data['app_id'] = self.app_id
|
|
32
|
+
data['client_name'] = self.client_name
|
|
33
|
+
data['client_id'] = self.client_id
|
|
34
|
+
data['issuer'] = self.issuer
|
|
35
|
+
return data
|
|
36
|
+
|
|
37
|
+
def to_client(self) -> dict:
|
|
38
|
+
"""Drop the issuer: an auth-layer correlation key, not client data."""
|
|
39
|
+
data = super().to_client()
|
|
40
|
+
del data['issuer']
|
|
41
|
+
return data
|
|
File without changes
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.entity.Entity import Entity
|
|
4
|
+
from graphcore.errors import KernelError
|
|
5
|
+
from graphcore.registry.decorators import register_entity
|
|
6
|
+
from graphcore.types.strategy import RequestStatus
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@register_entity()
|
|
10
|
+
class StrategyRequest(Entity):
|
|
11
|
+
"""One run of a strategy: what to run, on what, and its outcome. It runs
|
|
12
|
+
as its `owner`; only the executor writes it (docs/STRATEGY_EXECUTION_TDD.md).
|
|
13
|
+
|
|
14
|
+
No `dependent_on`: deleting a target keeps the history of runs on it,
|
|
15
|
+
including the run doing the deleting.
|
|
16
|
+
|
|
17
|
+
A failed run keeps its error as `error` (the message), `error_code`, and
|
|
18
|
+
`error_details`, so a waiter raises the same `KernelError` the run did."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, entity_id: str):
|
|
21
|
+
super().__init__(entity_id)
|
|
22
|
+
self.strategy_name: str = ''
|
|
23
|
+
self.param_config: dict = {}
|
|
24
|
+
self.target_entity_id: Optional[str] = None
|
|
25
|
+
self.callbacks: list = []
|
|
26
|
+
self.callback_channel: Optional[str] = None
|
|
27
|
+
self.client_request_id: Optional[str] = None
|
|
28
|
+
self.status: RequestStatus = 'pending'
|
|
29
|
+
self.attempt: int = 0
|
|
30
|
+
self.attempt_deadline: Optional[float] = None
|
|
31
|
+
self.ret_val: dict = {}
|
|
32
|
+
self.error: Optional[str] = None
|
|
33
|
+
self.error_code: Optional[str] = None
|
|
34
|
+
self.error_details: dict = {}
|
|
35
|
+
self.template_id: Optional[str] = None
|
|
36
|
+
|
|
37
|
+
def to_dict(self):
|
|
38
|
+
return {
|
|
39
|
+
**super().to_dict(),
|
|
40
|
+
'strategy_name': self.strategy_name,
|
|
41
|
+
'param_config': self.param_config,
|
|
42
|
+
'target_entity_id': self.target_entity_id,
|
|
43
|
+
'callbacks': self.callbacks,
|
|
44
|
+
'callback_channel': self.callback_channel,
|
|
45
|
+
'client_request_id': self.client_request_id,
|
|
46
|
+
'status': self.status,
|
|
47
|
+
'attempt': self.attempt,
|
|
48
|
+
'attempt_deadline': self.attempt_deadline,
|
|
49
|
+
'ret_val': self.ret_val,
|
|
50
|
+
'error': self.error,
|
|
51
|
+
'error_code': self.error_code,
|
|
52
|
+
'error_details': self.error_details,
|
|
53
|
+
'template_id': self.template_id,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
def failure(self) -> KernelError:
|
|
57
|
+
"""The error this failed run raised, as the class that raised it."""
|
|
58
|
+
return KernelError.from_dict({
|
|
59
|
+
**(self.error_details or {}), 'code': self.error_code, 'error': self.error,
|
|
60
|
+
'strategy_request_id': self.entity_id,
|
|
61
|
+
})
|
|
File without changes
|
|
File without changes
|