Resources

class statey.Resource

A resource represents a stateful object of some kind, and it can have one or more “states” that that object can exist in.

async finalize(data: statey.resource.StateSnapshot) → statey.resource.StateSnapshot

Called on states before they are committed to the resource graph. By default a noop

abstract property null_state

Return the null state of this resource, at which point it will removed from resource graphs

abstract async plan(current: statey.resource.StateSnapshot, config: statey.resource.StateConfig, session: statey.task.TaskSession) → statey.syms.object_.Object

Given a task session, the current state of a resource, and a task session with corresponding input reference, return an output reference that can be fully resolved when all the tasks in the task session have been complete successfully.

abstract async refresh(current: statey.resource.StateSnapshot) → statey.resource.StateSnapshot

Given the current bound state, return a new bound state that represents that actual current state of the object

class statey.State(name: str, input_type: statey.syms.types.Type, output_type: statey.syms.types.Type, null: bool = False)

A state corresponds to some type for a resource.

classmethod from_dict(data: Dict[str, Any], registry: Registry)State

Render a State from the output of to_dict()

to_dict(registry: Registry) → Dict[str, Any]

Render this state to a JSON-serializable dictionary

class statey.ResourceState(state: statey.resource.AbstractState, resource: str, provider: statey.provider.ProviderId)

A resource state is a state that is bound to some resource.

classmethod from_dict(data: Dict[str, Any], registry: Registry)ResourceState

Render a ResourceState from the output of to_dict()

snapshot(data: Any) → statey.resource.StateSnapshot

Create a StateSnapshot from this resource state and the given data

to_dict(registry: Registry) → Dict[str, Any]

Render this resource state to a JSON-serializable dictionary

class statey.BoundState(state: statey.resource.ResourceState, input: statey.syms.object_.Object)

Describes an input configuration for a resource

bind(registry: Registry) → None

Bind input and output objects to the given registry

clone(**kwargs) → statey.syms.utils.Cloneable

Return a copy of the current object with the given attributes replaced. This current object should be an instance of a dataclass

class statey.Machine(name: str, provider: Optional[statey.provider.Provider] = None)

Class with a metaclass to automatically collect states and transitions into class variables.

statey.transition(from_name: str, to_name: str, name: str = MISSING) → Any

Generate a decorate to wrap a function as a transition

class statey.SingleStateMachine(name: str, provider: Optional[statey.provider.Provider] = None)

A simple machine is an FSM which can only have two states: UP and DOWN.

Note that a SimpleMachine’s UP state should have all of the same fields available in its output type as its input type.

abstract async create(session: statey.task.TaskSession, config: statey.resource.StateConfig) → statey.syms.object_.Object

Create this resource with the given configuration

abstract async delete(session: statey.task.TaskSession, current: statey.resource.StateSnapshot) → statey.syms.object_.Object

Delete the resource with the given data

async finalize(current: statey.resource.BoundState) → statey.resource.BoundState

Called on states before they are committed to the resource graph. By default a noop

abstract async get_action(current: statey.resource.StateSnapshot, config: statey.resource.StateConfig, session: statey.task.TaskSession) → statey.fsm.ModificationAction

From the current, and config values, determine which modification action should be taken.

property null_state

Return the null state of this resource, at which point it will removed from resource graphs

async plan(current: statey.resource.StateSnapshot, config: statey.resource.StateConfig, session: statey.task.TaskSession) → statey.syms.object_.Object

Given a task session, the current state of a resource, and a task session with corresponding input reference, return an output reference that can be fully resolved when all the tasks in the task session have been complete successfully.

async refresh(current: statey.resource.StateSnapshot) → statey.resource.StateSnapshot

Same as Resource.refresh()

async refresh_config(config: statey.syms.object_.Object) → statey.syms.object_.Object

Transform a configuration before planning

abstract async refresh_state(data: Any) → Optional[Any]

Get a refreshed version of data (which is in the state UP). Return None to indicate the resource no longer exists.

class statey.SimpleMachine(name: str, provider: Optional[statey.provider.Provider] = None)

A simple machine has only a single state and each transition only consists of a single task

async create(session: statey.task.TaskSession, config: statey.resource.StateConfig) → statey.syms.object_.Object

Create this resource with the given configuration

async create_task(config: Any) → Any

Defines a single task called “create” that will create this resource

async delete(session: statey.task.TaskSession, current: statey.resource.StateSnapshot) → statey.syms.object_.Object

Delete the resource with the given data

async delete_task(current: Any) → Any

Defines a single task called “delete” that will delete this resource

async finalize(current: statey.resource.BoundState) → statey.resource.BoundState

Called on states before they are committed to the resource graph. By default a noop

async get_action(current: statey.resource.StateSnapshot, config: statey.resource.StateConfig, session: statey.task.TaskSession) → statey.fsm.ModificationAction

Split get_action into get_diff and get_action_from_diff

get_action_from_diff(diff: statey.syms.diff.Diff) → statey.fsm.ModificationAction

With the given diff, determine which action must be taken to get to the configured state. This is only called when both the current and configured state are UP.

Overriding this method is optional, by default it will always delete and recreate the resource.

get_diff(current: statey.resource.StateSnapshot, config: statey.resource.StateConfig, session: statey.task.TaskSession) → statey.syms.diff.Diff

Produce a diff given the current, config and session data

async get_expected(current: statey.resource.StateSnapshot, config: statey.resource.StateConfig, session: statey.task.TaskSession) → Any

Get the expected output for the given configuration. Default implementation is just passing through config fields and setting the rest as unknown

async modify_task(diff: statey.syms.diff.Diff, current: Any, config: Any) → Any

Defines a single task called “modify” that will modify this resource

property null_state

Return the null state of this resource, at which point it will removed from resource graphs

async plan(current: statey.resource.StateSnapshot, config: statey.resource.StateConfig, session: statey.task.TaskSession) → statey.syms.object_.Object

Given a task session, the current state of a resource, and a task session with corresponding input reference, return an output reference that can be fully resolved when all the tasks in the task session have been complete successfully.

async refresh(current: statey.resource.StateSnapshot) → statey.resource.StateSnapshot

Same as Resource.refresh()

async refresh_config(config: statey.syms.object_.Object) → statey.syms.object_.Object

Transform a configuration before planning

abstract async refresh_state(data: Any) → Optional[Any]

Get a refreshed version of data (which is in the state UP). Return None to indicate the resource no longer exists.

class statey.ResourceGraph

A resource graph is a wrapper around a simple graph that stores similar information to a resource session, but without objects. Resource graphs are serializable. Note that not every node in a resource graph is necessarily a resource, it can contain any name just like a session.

abstract add_dependencies(key: str, dependencies: Sequence[str]) → None

Add dependencies to the given key

abstract clone() → statey.resource.ResourceGraph

Return a copy of this resource graph

abstract classmethod from_dict(data: Any, registry: Optional[Registry] = None)ResourceGraph

Render a ResourceGraph from a JSON-serializable representation

abstract get(key: str) → Dict[str, Any]

Get the data about this key stored in the resource graph

abstract keys() → Sequence[str]

Get a list of keys currently stored in this graph

abstract async refresh(registry: Optional[statey.registry.Registry] = None, finalize: bool = False) → Iterator[str]

Refresh the current state of all resources in the graph. Returns an asynchronous generator that yields keys as they finish refreshing successfully.

abstract set(key: str, value: Any, type: statey.syms.types.Type, state: Optional[statey.resource.ResourceState] = None, remove_dependencies: bool = True) → None

Set the given value in the graph, with a state optionally specified for resources. Note that this operation will remove the current upstream edges of key unless remove_upstreams=False is specified as an argument

abstract to_dict(registry: Optional[Registry] = None) → Dict[str, Any]

Return a JSON-serializable dictionary representation of this resource graph.

abstract to_session() → statey.resource.ResourceSession

Construct a session with the same data and dependencies as this ResourceGraph