Skip to main content

AnyEnvironmentState

Enum AnyEnvironmentState 

Source
pub enum AnyEnvironmentState {
Show 15 variants Created(Environment<Created>), Provisioning(Environment<Provisioning>), Provisioned(Environment<Provisioned>), Configuring(Environment<Configuring>), Configured(Environment<Configured>), Releasing(Environment<Releasing>), Released(Environment<Released>), Running(Environment<Running>), Destroying(Environment<Destroying>), ProvisionFailed(Environment<ProvisionFailed>), ConfigureFailed(Environment<ConfigureFailed>), ReleaseFailed(Environment<ReleaseFailed>), RunFailed(Environment<RunFailed>), DestroyFailed(Environment<DestroyFailed>), Destroyed(Environment<Destroyed>),
}
Expand description

Type-erased environment that can hold any typed Environment<S> at runtime

This enum enables runtime handling of Environment<S> instances without knowing their specific state type at compile time. This is essential for:

  • Serialization: Saving environments to disk (JSON files)
  • Deserialization: Loading environments from disk
  • Collections: Storing environments with different states together
  • Runtime Inspection: Checking state without compile-time type knowledge
  • Generic Interfaces: Passing through non-generic function parameters

§Type Erasure Pattern

Each variant wraps a typed Environment<S> where S is one of the state marker types defined in this module. The enum variant name acts as a discriminator (similar to a type column in database Single Table Inheritance).

§Usage Example

use torrust_tracker_deployer_lib::domain::environment::state::AnyEnvironmentState;

// Type erasure: typed -> runtime
// let env: Environment<Provisioned> = ...;
// let any_env: AnyEnvironmentState = env.into_any();

// Serialization
// let json = serde_json::to_string(&any_env)?;

// Deserialization
// let any_env: AnyEnvironmentState = serde_json::from_str(&json)?;

// Type restoration: runtime -> typed
// let env: Environment<Provisioned> = any_env.try_into_provisioned()?;

§Design Decision

See ADR: Type Erasure for Environment States for detailed rationale behind this design choice.

Variants§

§

Created(Environment<Created>)

Environment in Created state

§

Provisioning(Environment<Provisioning>)

Environment in Provisioning state

§

Provisioned(Environment<Provisioned>)

Environment in Provisioned state

§

Configuring(Environment<Configuring>)

Environment in Configuring state

§

Configured(Environment<Configured>)

Environment in Configured state

§

Releasing(Environment<Releasing>)

Environment in Releasing state

§

Released(Environment<Released>)

Environment in Released state

§

Running(Environment<Running>)

Environment in Running state

§

Destroying(Environment<Destroying>)

Environment in Destroying state

§

ProvisionFailed(Environment<ProvisionFailed>)

Environment in ProvisionFailed error state

§

ConfigureFailed(Environment<ConfigureFailed>)

Environment in ConfigureFailed error state

§

ReleaseFailed(Environment<ReleaseFailed>)

Environment in ReleaseFailed error state

§

RunFailed(Environment<RunFailed>)

Environment in RunFailed error state

§

DestroyFailed(Environment<DestroyFailed>)

Environment in DestroyFailed error state

§

Destroyed(Environment<Destroyed>)

Environment in Destroyed terminal state

Implementations§

Source§

impl AnyEnvironmentState

Source

pub fn try_into_configure_failed( self, ) -> Result<Environment<ConfigureFailed>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<ConfigureFailed>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in ConfigureFailed state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_configured( self, ) -> Result<Environment<Configured>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Configured>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Configured state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_configuring( self, ) -> Result<Environment<Configuring>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Configuring>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Configuring state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_created(self) -> Result<Environment<Created>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Created>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Created state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_destroy_failed( self, ) -> Result<Environment<DestroyFailed>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<DestroyFailed>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in DestroyFailed state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_destroyed( self, ) -> Result<Environment<Destroyed>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Destroyed>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Destroyed state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_destroying( self, ) -> Result<Environment<Destroying>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Destroying>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Destroying state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_provision_failed( self, ) -> Result<Environment<ProvisionFailed>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<ProvisionFailed>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in ProvisionFailed state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_provisioned( self, ) -> Result<Environment<Provisioned>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Provisioned>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Provisioned state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_provisioning( self, ) -> Result<Environment<Provisioning>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Provisioning>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Provisioning state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_release_failed( self, ) -> Result<Environment<ReleaseFailed>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<ReleaseFailed>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in ReleaseFailed state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_released(self) -> Result<Environment<Released>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Released>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Released state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_releasing( self, ) -> Result<Environment<Releasing>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Releasing>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Releasing state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_run_failed( self, ) -> Result<Environment<RunFailed>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<RunFailed>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in RunFailed state.

Source§

impl AnyEnvironmentState

Source

pub fn try_into_running(self) -> Result<Environment<Running>, StateTypeError>

Attempts to convert AnyEnvironmentState to Environment<Running>

§Errors

Returns StateTypeError::UnexpectedState if the environment is not in Running state.

Source§

impl AnyEnvironmentState

Source

pub fn name(&self) -> &EnvironmentName

Get the environment name regardless of current state

This method provides access to the environment name without needing to pattern match on the specific state variant.

§Returns

A reference to the EnvironmentName contained within the environment.

Source

pub fn state_name(&self) -> &'static str

Get the state name as a string

Returns a static string identifier for the current state. This is useful for logging, error messages, and displaying state information to users.

§Returns

A static string representing the state name (e.g., “created”, “provisioning”).

Source

pub fn state_display_name(&self) -> &'static str

Returns a human-readable display name for the current state.

This provides a user-friendly representation suitable for CLI output, reports, and other user-facing contexts. Failed states include a space for readability (e.g., “Provision Failed”).

§Returns

A static string representing the display name (e.g., “Created”, “Provision Failed”).

Source

pub fn is_success_state(&self) -> bool

Check if the environment is in a success (non-error) state

Success states are those representing normal operation flow, including transient states (like Provisioning) and terminal success states (like Running, Destroyed).

§Returns

true if the environment is in a success state, false for error states.

Source

pub fn is_error_state(&self) -> bool

Check if the environment is in an error state

Error states indicate that an operation failed during the environment’s lifecycle (provisioning, configuration, release, or runtime).

§Returns

true if the environment is in an error state, false otherwise.

Source

pub fn is_terminal_state(&self) -> bool

Check if the environment is in a terminal state

Terminal states are final states where no more transitions are expected. This includes both successful terminal states (Running, Destroyed) and error states (all *Failed variants).

§Returns

true if the environment is in a terminal state, false otherwise.

Source

pub fn error_details(&self) -> Option<&str>

Get error details if the environment is in an error state

For error states (*Failed), this returns the description of the operation that failed. For non-error states, returns None.

§Returns
  • Some(&str) containing the failed operation description for error states
  • None for success states
Source

pub fn instance_name(&self) -> &InstanceName

Get the instance name regardless of current state

This method provides access to the instance name without needing to pattern match on the specific state variant.

§Returns

A reference to the InstanceName contained within the environment.

Source

pub fn profile_name(&self) -> &ProfileName

Get the LXD profile name regardless of current state

This method provides access to the profile name without needing to pattern match on the specific state variant.

§Returns

A reference to the ProfileName contained within the environment.

§Panics

Panics if called on a non-LXD environment.

Source

pub fn ssh_credentials(&self) -> &SshCredentials

Get the SSH credentials regardless of current state

This method provides access to the SSH credentials without needing to pattern match on the specific state variant.

§Returns

A reference to the SshCredentials contained within the environment.

Source

pub fn ssh_port(&self) -> u16

Get the SSH port regardless of current state

This method provides access to the SSH port without needing to pattern match on the specific state variant.

§Returns

The SSH port number.

Source

pub fn provider_name(&self) -> &'static str

Get the provider name regardless of current state

This method provides access to the provider name without needing to pattern match on the specific state variant.

§Returns

A static string representing the provider name (e.g., “lxd”, “hetzner”).

Source

pub fn provider_display_name(&self) -> &'static str

Get the human-readable provider display name regardless of current state

This method provides access to the provider display name without needing to pattern match on the specific state variant.

§Returns

A static string representing the provider display name (e.g., “LXD”, “Hetzner Cloud”).

Source

pub fn tracker_config(&self) -> &TrackerConfig

Get the tracker configuration regardless of current state

This method provides access to the tracker configuration without needing to pattern match on the specific state variant.

§Returns

A reference to the TrackerConfig contained within the environment.

Source

pub fn instance_ip(&self) -> Option<IpAddr>

Get the instance IP address if available, regardless of current state

This method provides access to the instance IP without needing to pattern match on the specific state variant.

§Returns
  • Some(IpAddr) if the environment has been provisioned
  • None if the environment hasn’t been provisioned yet
Source

pub fn created_at(&self) -> DateTime<Utc>

Get when the environment was created

This method provides access to the creation timestamp without needing to pattern match on the specific state variant.

§Returns

The UTC timestamp when the environment was created.

Source

pub fn provision_method(&self) -> Option<ProvisionMethod>

Get the provision method if available, regardless of current state

This method provides access to the provision method without needing to pattern match on the specific state variant.

§Returns
  • Some(ProvisionMethod::Provisioned) if the instance was provisioned via OpenTofu
  • Some(ProvisionMethod::Registered) if the instance was registered from existing infrastructure
  • None if the provision method hasn’t been set yet (legacy or pre-provisioned state)
Source

pub fn service_endpoints(&self) -> Option<&ServiceEndpoints>

Get the service endpoints if available, regardless of current state

This method provides access to the service endpoints without needing to pattern match on the specific state variant.

§Returns
  • Some(&ServiceEndpoints) if services have been started and URLs are available
  • None if services haven’t been started yet or URLs weren’t recorded
Source

pub fn prometheus_config(&self) -> Option<&PrometheusConfig>

Get the Prometheus configuration if enabled, regardless of current state

This method provides access to the Prometheus configuration without needing to pattern match on the specific state variant.

§Returns
  • Some(&PrometheusConfig) if Prometheus is configured for this environment
  • None if Prometheus is not enabled
Source

pub fn grafana_config(&self) -> Option<&GrafanaConfig>

Get the Grafana configuration if enabled, regardless of current state

This method provides access to the Grafana configuration without needing to pattern match on the specific state variant.

§Returns
  • Some(&GrafanaConfig) if Grafana is configured for this environment
  • None if Grafana is not enabled
Source

pub fn https_config(&self) -> Option<&HttpsConfig>

Get the HTTPS configuration if enabled, regardless of current state

This method provides access to the HTTPS configuration without needing to pattern match on the specific state variant.

§Returns
  • Some(&HttpsConfig) if HTTPS/TLS is configured for this environment
  • None if HTTPS is not enabled
Source

pub fn is_registered(&self) -> bool

Check if this environment was registered from existing infrastructure

Registered environments have infrastructure that was created externally and cannot be destroyed by this tool. The destroy command will only clean up local state for registered environments.

§Returns

true if the environment was registered (not provisioned), false otherwise.

Source

pub fn collect_tls_domains(&self) -> Vec<DomainName>

Collect all TLS-enabled domains from the environment configuration

Gathers domains from all services that have TLS enabled: HTTP API, HTTP trackers, health check API, and Grafana.

This method is useful for operations that need to work with all configured domains, such as DNS resolution checks, certificate management, or reporting.

§Returns

A vector of all TLS domains configured in the environment. Returns an empty vector if no TLS domains are configured.

Source

pub fn destroy(self) -> Result<Environment<Destroyed>, StateTypeError>

Destroy the environment, transitioning it to the Destroyed state

This method provides a unified interface to destroy an environment regardless of its current state. It encapsulates the repetitive match pattern that would otherwise be needed in calling code.

§Returns
  • Ok(Environment<Destroyed>) if the environment was successfully destroyed
  • Err(StateTypeError) if the environment is already in the Destroyed state
§Errors

Returns StateTypeError::UnexpectedState if called on an environment already in the Destroyed state.

Source

pub fn tofu_build_dir(&self) -> PathBuf

Get the OpenTofu build directory path regardless of current state

This method provides a unified interface to access the build directory for OpenTofu operations without needing to pattern match on the specific state variant.

The path is returned consistently regardless of the environment’s state. The caller is responsible for determining how to use the path based on their specific needs and the environment’s current state.

§Returns

The path to the OpenTofu build directory for the LXD provider.

Trait Implementations§

Source§

impl Clone for AnyEnvironmentState

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AnyEnvironmentState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for AnyEnvironmentState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for AnyEnvironmentState

Display implementation for user-friendly state representation

Formats the environment state in a human-readable way, including the environment name, current state, and error details if applicable.

§Examples

Environment 'my-env' is in state: provisioning
Environment 'my-env' is in state: provision_failed (failed at: network timeout)
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for AnyEnvironmentState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> IntoResult<T> for T

Source§

type Err = !

Source§

fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>

Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more