fakecloud-stepfunctions 0.44.0

Step Functions implementation for FakeCloud
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::collections::BTreeMap;
use std::sync::Arc;

use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use serde_json::Value;

pub type SharedStepFunctionsState =
    Arc<RwLock<fakecloud_core::multi_account::MultiAccountState<StepFunctionsState>>>;

impl fakecloud_core::multi_account::AccountState for StepFunctionsState {
    fn new_for_account(account_id: &str, region: &str, _endpoint: &str) -> Self {
        Self::new(account_id, region)
    }
}

pub const STEPFUNCTIONS_SNAPSHOT_SCHEMA_VERSION: u32 = 2;

#[derive(Debug, Serialize, Deserialize)]
pub struct StepFunctionsSnapshot {
    pub schema_version: u32,
    #[serde(default)]
    pub accounts: Option<fakecloud_core::multi_account::MultiAccountState<StepFunctionsState>>,
    #[serde(default)]
    pub state: Option<StepFunctionsState>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepFunctionsState {
    pub account_id: String,
    pub region: String,
    /// State machines keyed by ARN.
    #[serde(default)]
    pub state_machines: BTreeMap<String, StateMachine>,
    /// Executions keyed by execution ARN.
    #[serde(default)]
    pub executions: BTreeMap<String, Execution>,
    #[serde(default)]
    pub activities: BTreeMap<String, Activity>,
    #[serde(default)]
    pub state_machine_versions: BTreeMap<String, StateMachineVersion>,
    #[serde(default)]
    pub state_machine_aliases: BTreeMap<String, StateMachineAlias>,
    #[serde(default)]
    pub map_runs: BTreeMap<String, MapRun>,
    /// Pending task tokens issued for sync activities + their outcome.
    #[serde(default)]
    pub task_tokens: BTreeMap<String, TaskTokenState>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Activity {
    pub name: String,
    pub arn: String,
    pub creation_date: DateTime<Utc>,
    pub tags: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateMachineVersion {
    pub state_machine_arn: String,
    pub version: i64,
    pub revision_id: String,
    pub description: String,
    pub creation_date: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateMachineAlias {
    pub name: String,
    pub arn: String,
    pub description: String,
    pub routing_configuration: Vec<AliasRoute>,
    pub creation_date: DateTime<Utc>,
    pub update_date: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AliasRoute {
    pub state_machine_version_arn: String,
    pub weight: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MapRun {
    pub map_run_arn: String,
    pub execution_arn: String,
    pub max_concurrency: i32,
    pub tolerated_failure_percentage: f64,
    pub tolerated_failure_count: i64,
    pub status: String,
    pub start_date: DateTime<Utc>,
    pub stop_date: Option<DateTime<Utc>>,
    /// Total number of items the distributed Map iterated over.
    #[serde(default)]
    pub total_count: i64,
    /// Iterations that completed successfully.
    #[serde(default)]
    pub succeeded_count: i64,
    /// Iterations that failed.
    #[serde(default)]
    pub failed_count: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskTokenState {
    pub activity_arn: String,
    /// PENDING (waiting for `GetActivityTask` to dequeue) /
    /// IN_PROGRESS (worker has picked it up) /
    /// SUCCEEDED / FAILED / TIMED_OUT.
    pub status: String,
    pub output: Option<String>,
    pub error: Option<String>,
    pub cause: Option<String>,
    /// Input the state machine wanted the worker to process. `None`
    /// for tokens minted by external `GetActivityTask` callers without
    /// any associated activity execution (legacy synthetic path).
    #[serde(default)]
    pub input: Option<String>,
    #[serde(default = "default_now")]
    pub created_at: DateTime<Utc>,
    #[serde(default)]
    pub last_heartbeat_at: Option<DateTime<Utc>>,
    /// Per AWS docs: state machine fails the task if no heartbeat in
    /// this many seconds while the worker is running.
    #[serde(default)]
    pub heartbeat_seconds: Option<i64>,
    /// Overall timeout for the task; counted from `created_at`.
    #[serde(default)]
    pub timeout_seconds: Option<i64>,
}

fn default_now() -> DateTime<Utc> {
    Utc::now()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateMachine {
    pub name: String,
    pub arn: String,
    pub definition: String,
    pub role_arn: String,
    pub machine_type: StateMachineType,
    pub status: StateMachineStatus,
    pub creation_date: DateTime<Utc>,
    pub update_date: DateTime<Utc>,
    pub tags: BTreeMap<String, String>,
    pub revision_id: String,
    pub logging_configuration: Option<Value>,
    pub tracing_configuration: Option<Value>,
    pub description: String,
    #[serde(default)]
    pub encryption_configuration: Option<Value>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StateMachineType {
    Standard,
    Express,
}

impl StateMachineType {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Standard => "STANDARD",
            Self::Express => "EXPRESS",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "STANDARD" => Some(Self::Standard),
            "EXPRESS" => Some(Self::Express),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StateMachineStatus {
    Active,
    Deleting,
}

impl StateMachineStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Active => "ACTIVE",
            Self::Deleting => "DELETING",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Execution {
    pub execution_arn: String,
    pub state_machine_arn: String,
    pub state_machine_name: String,
    pub name: String,
    pub status: ExecutionStatus,
    pub input: Option<String>,
    pub output: Option<String>,
    pub start_date: DateTime<Utc>,
    pub stop_date: Option<DateTime<Utc>>,
    pub error: Option<String>,
    pub cause: Option<String>,
    pub history_events: Vec<HistoryEvent>,
    /// Parent execution ARN when this execution was started by another
    /// state machine via `arn:aws:states:::states:startExecution[.sync]`.
    /// `None` for top-level executions started by external callers.
    #[serde(default)]
    pub parent_execution_arn: Option<String>,
    /// True when this execution was created by `StartSyncExecution`
    /// (EXPRESS state machines only). Distinguishes it from regular
    /// async executions in introspection endpoints.
    #[serde(default)]
    pub is_sync: bool,
    /// Billed duration in milliseconds, populated on terminal state for
    /// sync executions. Mirrors `billingDetails.billedDurationInMilliseconds`
    /// from the StartSyncExecution response.
    #[serde(default)]
    pub billed_duration_ms: Option<i64>,
    /// Billed memory in MB for sync executions. Mirrors
    /// `billingDetails.billedMemoryUsedInMB`.
    #[serde(default)]
    pub billed_memory_mb: Option<i64>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExecutionStatus {
    Running,
    Succeeded,
    Failed,
    TimedOut,
    Aborted,
    PendingRedrive,
}

impl ExecutionStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Running => "RUNNING",
            Self::Succeeded => "SUCCEEDED",
            Self::Failed => "FAILED",
            Self::TimedOut => "TIMED_OUT",
            Self::Aborted => "ABORTED",
            Self::PendingRedrive => "PENDING_REDRIVE",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryEvent {
    pub id: i64,
    pub event_type: String,
    pub timestamp: DateTime<Utc>,
    pub previous_event_id: i64,
    pub details: Value,
}

impl StepFunctionsState {
    pub fn new(account_id: &str, region: &str) -> Self {
        Self {
            account_id: account_id.to_string(),
            region: region.to_string(),
            state_machines: BTreeMap::new(),
            executions: BTreeMap::new(),
            activities: BTreeMap::new(),
            state_machine_versions: BTreeMap::new(),
            state_machine_aliases: BTreeMap::new(),
            map_runs: BTreeMap::new(),
            task_tokens: BTreeMap::new(),
        }
    }

    pub fn reset(&mut self) {
        self.state_machines.clear();
        self.executions.clear();
        self.activities.clear();
        self.state_machine_versions.clear();
        self.state_machine_aliases.clear();
        self.map_runs.clear();
        self.task_tokens.clear();
    }

    pub fn state_machine_arn(&self, region: &str, name: &str) -> String {
        format!(
            "arn:aws:states:{}:{}:stateMachine:{}",
            region, self.account_id, name
        )
    }

    pub fn execution_arn(
        &self,
        region: &str,
        state_machine_name: &str,
        execution_name: &str,
    ) -> String {
        format!(
            "arn:aws:states:{}:{}:execution:{}:{}",
            region, self.account_id, state_machine_name, execution_name
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn state_machine_type_as_str() {
        assert_eq!(StateMachineType::Standard.as_str(), "STANDARD");
        assert_eq!(StateMachineType::Express.as_str(), "EXPRESS");
    }

    #[test]
    fn state_machine_type_parse() {
        assert_eq!(
            StateMachineType::parse("STANDARD"),
            Some(StateMachineType::Standard)
        );
        assert_eq!(
            StateMachineType::parse("EXPRESS"),
            Some(StateMachineType::Express)
        );
        assert_eq!(StateMachineType::parse("bogus"), None);
    }

    #[test]
    fn state_machine_status_as_str() {
        assert_eq!(StateMachineStatus::Active.as_str(), "ACTIVE");
        assert_eq!(StateMachineStatus::Deleting.as_str(), "DELETING");
    }

    #[test]
    fn execution_status_as_str() {
        assert_eq!(ExecutionStatus::Running.as_str(), "RUNNING");
        assert_eq!(ExecutionStatus::Succeeded.as_str(), "SUCCEEDED");
        assert_eq!(ExecutionStatus::Failed.as_str(), "FAILED");
        assert_eq!(ExecutionStatus::TimedOut.as_str(), "TIMED_OUT");
        assert_eq!(ExecutionStatus::Aborted.as_str(), "ABORTED");
        assert_eq!(ExecutionStatus::PendingRedrive.as_str(), "PENDING_REDRIVE");
    }

    #[test]
    fn state_machine_arn_format() {
        let state = StepFunctionsState::new("123456789012", "us-east-1");
        assert_eq!(
            state.state_machine_arn("us-east-1", "my-sm"),
            "arn:aws:states:us-east-1:123456789012:stateMachine:my-sm"
        );
    }

    #[test]
    fn execution_arn_format() {
        let state = StepFunctionsState::new("123456789012", "us-east-1");
        assert_eq!(
            state.execution_arn("us-east-1", "sm", "exec-1"),
            "arn:aws:states:us-east-1:123456789012:execution:sm:exec-1"
        );
    }

    #[test]
    fn arns_use_request_region_not_server_default() {
        // Server default region is us-east-1, but a client whose credential
        // scope resolves to eu-central-1 must get eu-central-1 ARNs.
        let state = StepFunctionsState::new("123456789012", "us-east-1");
        assert_eq!(
            state.state_machine_arn("eu-central-1", "my-sm"),
            "arn:aws:states:eu-central-1:123456789012:stateMachine:my-sm"
        );
        assert_eq!(
            state.execution_arn("eu-central-1", "sm", "exec-1"),
            "arn:aws:states:eu-central-1:123456789012:execution:sm:exec-1"
        );
    }

    #[test]
    fn state_reset_clears_all() {
        let mut state = StepFunctionsState::new("123456789012", "us-east-1");
        state.state_machines.insert(
            "x".to_string(),
            StateMachine {
                name: "sm".to_string(),
                arn: "arn:aws:states:us-east-1:123:stateMachine:sm".to_string(),
                definition: "{}".to_string(),
                role_arn: "r".to_string(),
                machine_type: StateMachineType::Standard,
                status: StateMachineStatus::Active,
                creation_date: Utc::now(),
                update_date: Utc::now(),
                tags: BTreeMap::new(),
                revision_id: "v1".to_string(),
                logging_configuration: None,
                tracing_configuration: None,
                description: String::new(),
                encryption_configuration: None,
            },
        );
        state.reset();
        assert!(state.state_machines.is_empty());
        assert!(state.executions.is_empty());
    }
}