awsim-cloudformation 0.4.0

AWS CloudFormation emulator for AWSim
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use awsim_core::{AwsError, InternalEvent, RequestContext};
use serde_json::{Value, json};
use std::collections::HashMap;

use crate::{
    error::{missing_parameter, stack_already_exists, stack_not_found},
    ids::{new_uuid, now_iso8601, stack_arn},
    state::{CloudFormationState, Stack, StackEvent, StackResource},
    template,
};

use super::{opt_str, parse_parameters, parse_tags, require_str};

fn stack_to_value(stack: &Stack) -> Value {
    let params: Vec<Value> = stack
        .parameters
        .iter()
        .map(|(k, v)| json!({ "ParameterKey": k, "ParameterValue": v }))
        .collect();

    let tags: Vec<Value> = stack
        .tags
        .iter()
        .map(|(k, v)| json!({ "Key": k, "Value": v }))
        .collect();

    let outputs: Vec<Value> = stack
        .outputs
        .values()
        .map(|o| {
            let mut obj = json!({
                "OutputKey": o.output_key,
                "OutputValue": o.output_value,
            });
            if let Some(desc) = &o.description {
                obj["Description"] = Value::String(desc.clone());
            }
            obj
        })
        .collect();

    let mut result = json!({
        "StackId": stack.stack_id,
        "StackName": stack.stack_name,
        "StackStatus": stack.status,
        "CreationTime": stack.created_at,
        "Parameters": { "member": params },
        "Tags": { "member": tags },
        "Outputs": { "member": outputs },
    });

    if let Some(reason) = &stack.status_reason {
        result["StackStatusReason"] = Value::String(reason.clone());
    }
    if let Some(updated_at) = &stack.updated_at {
        result["LastUpdatedTime"] = Value::String(updated_at.clone());
    }

    result
}

fn resource_to_value(r: &StackResource, stack: &Stack) -> Value {
    let mut obj = json!({
        "StackName": stack.stack_name,
        "StackId": stack.stack_id,
        "LogicalResourceId": r.logical_resource_id,
        "ResourceType": r.resource_type,
        "ResourceStatus": r.resource_status,
        "Timestamp": r.timestamp,
    });
    if let Some(phys) = &r.physical_resource_id {
        obj["PhysicalResourceId"] = Value::String(phys.clone());
    }
    if let Some(reason) = &r.resource_status_reason {
        obj["ResourceStatusReason"] = Value::String(reason.clone());
    }
    obj
}

fn event_to_value(e: &StackEvent) -> Value {
    let mut obj = json!({
        "EventId": e.event_id,
        "StackId": e.stack_id,
        "StackName": e.stack_name,
        "LogicalResourceId": e.logical_resource_id,
        "ResourceType": e.resource_type,
        "Timestamp": e.timestamp,
        "ResourceStatus": e.resource_status,
    });
    if let Some(phys) = &e.physical_resource_id {
        obj["PhysicalResourceId"] = Value::String(phys.clone());
    }
    if let Some(reason) = &e.resource_status_reason {
        obj["ResourceStatusReason"] = Value::String(reason.clone());
    }
    obj
}

/// Build resources from parsed template, without actually creating them.
fn build_resources(parsed: &template::ParsedTemplate, now: &str) -> Vec<StackResource> {
    parsed
        .resources
        .iter()
        .filter(|r| {
            // Skip resources in false conditions
            if let Some(cond_name) = &r.condition {
                *parsed.conditions.get(cond_name).unwrap_or(&true)
            } else {
                true
            }
        })
        .map(|r| {
            // Generate a fake physical resource ID for now
            let physical_resource_id = Some(format!("awsim-{}-{}", r.logical_id, &new_uuid()[..8]));
            StackResource {
                logical_resource_id: r.logical_id.clone(),
                physical_resource_id,
                resource_type: r.resource_type.clone(),
                resource_status: "CREATE_COMPLETE".to_string(),
                resource_status_reason: None,
                timestamp: now.to_string(),
            }
        })
        .collect()
}

/// Build stack events from resources.
fn build_events(
    resources: &[StackResource],
    stack_id: &str,
    stack_name: &str,
    now: &str,
) -> Vec<StackEvent> {
    let mut events = Vec::new();

    // Stack-level CREATE_IN_PROGRESS event
    events.push(StackEvent {
        event_id: new_uuid(),
        stack_id: stack_id.to_string(),
        stack_name: stack_name.to_string(),
        logical_resource_id: stack_name.to_string(),
        physical_resource_id: Some(stack_id.to_string()),
        resource_type: "AWS::CloudFormation::Stack".to_string(),
        timestamp: now.to_string(),
        resource_status: "CREATE_IN_PROGRESS".to_string(),
        resource_status_reason: Some("User Initiated".to_string()),
    });

    // Per-resource events
    for resource in resources {
        events.push(StackEvent {
            event_id: new_uuid(),
            stack_id: stack_id.to_string(),
            stack_name: stack_name.to_string(),
            logical_resource_id: resource.logical_resource_id.clone(),
            physical_resource_id: resource.physical_resource_id.clone(),
            resource_type: resource.resource_type.clone(),
            timestamp: now.to_string(),
            resource_status: "CREATE_COMPLETE".to_string(),
            resource_status_reason: None,
        });
    }

    // Stack-level CREATE_COMPLETE event
    events.push(StackEvent {
        event_id: new_uuid(),
        stack_id: stack_id.to_string(),
        stack_name: stack_name.to_string(),
        logical_resource_id: stack_name.to_string(),
        physical_resource_id: Some(stack_id.to_string()),
        resource_type: "AWS::CloudFormation::Stack".to_string(),
        timestamp: now.to_string(),
        resource_status: "CREATE_COMPLETE".to_string(),
        resource_status_reason: None,
    });

    events
}

pub fn create_stack(
    state: &CloudFormationState,
    input: &Value,
    ctx: &RequestContext,
) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?.to_string();

    if state.stacks.contains_key(&stack_name) {
        return Err(stack_already_exists(&stack_name));
    }

    let template_body = opt_str(input, "TemplateBody")
        .or_else(|| opt_str(input, "TemplateURL"))
        .ok_or_else(|| missing_parameter("TemplateBody"))?
        .to_string();

    let parameters = parse_parameters(input);
    let tags = parse_tags(input);

    // Validate and parse template
    let parsed = template::validate_and_parse(&template_body, &parameters)?;

    let now = now_iso8601();
    let stack_id = stack_arn(&ctx.region, &ctx.account_id, &stack_name);
    let resources = build_resources(&parsed, &now);
    let events = build_events(&resources, &stack_id, &stack_name, &now);

    let stack = Stack {
        stack_id: stack_id.clone(),
        stack_name: stack_name.clone(),
        template_body,
        parameters,
        tags,
        status: "CREATE_COMPLETE".to_string(),
        status_reason: None,
        resources,
        events,
        change_sets: HashMap::new(),
        created_at: now,
        updated_at: None,
        outputs: HashMap::new(),
    };

    // Emit one CreateResource event per resource so the background router
    // can provision each resource in the appropriate service.
    if let Some(ref bus) = ctx.event_bus {
        for resource in &stack.resources {
            // Find the matching parsed resource to get its properties.
            let properties = parsed
                .resources
                .iter()
                .find(|r| r.logical_id == resource.logical_resource_id)
                .map(|r| r.properties.clone())
                .unwrap_or(Value::Object(serde_json::Map::new()));

            bus.publish(InternalEvent {
                source: "cloudformation".to_string(),
                event_type: "cloudformation:CreateResource".to_string(),
                region: ctx.region.clone(),
                account_id: ctx.account_id.clone(),
                detail: json!({
                    "stackName": stack_name,
                    "logicalId": resource.logical_resource_id,
                    "resourceType": resource.resource_type,
                    "properties": properties,
                }),
            });
        }
    }

    state.stacks.insert(stack_name, stack);

    Ok(json!({ "StackId": stack_id }))
}

pub fn delete_stack(
    state: &CloudFormationState,
    input: &Value,
    ctx: &RequestContext,
) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?;

    if let Some((_, mut stack)) = state.stacks.remove(stack_name) {
        // Emit one DeleteResource event per resource before marking deleted.
        if let Some(ref bus) = ctx.event_bus {
            for resource in &stack.resources {
                bus.publish(InternalEvent {
                    source: "cloudformation".to_string(),
                    event_type: "cloudformation:DeleteResource".to_string(),
                    region: ctx.region.clone(),
                    account_id: ctx.account_id.clone(),
                    detail: json!({
                        "stackName": stack_name,
                        "logicalId": resource.logical_resource_id,
                        "resourceType": resource.resource_type,
                        "physicalResourceId": resource.physical_resource_id,
                    }),
                });
            }
        }

        // Mark as DELETE_COMPLETE (keep entry with status for ListStacks)
        stack.status = "DELETE_COMPLETE".to_string();
        stack.updated_at = Some(now_iso8601());
        state.stacks.insert(stack_name.to_string(), stack);
    }
    // DeleteStack is idempotent — no error if not found

    Ok(json!({}))
}

pub fn update_stack(
    state: &CloudFormationState,
    input: &Value,
    _ctx: &RequestContext,
) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?;

    let mut stack = state
        .stacks
        .get_mut(stack_name)
        .ok_or_else(|| stack_not_found(stack_name))?;

    let template_body = opt_str(input, "TemplateBody")
        .map(|s| s.to_string())
        .unwrap_or_else(|| stack.template_body.clone());

    let new_parameters = parse_parameters(input);
    let effective_params = if new_parameters.is_empty() {
        stack.parameters.clone()
    } else {
        new_parameters
    };

    // Validate new template
    let parsed = template::validate_and_parse(&template_body, &effective_params)?;

    let now = now_iso8601();
    let resources = build_resources(&parsed, &now);

    let update_events = resources
        .iter()
        .map(|r| StackEvent {
            event_id: new_uuid(),
            stack_id: stack.stack_id.clone(),
            stack_name: stack.stack_name.clone(),
            logical_resource_id: r.logical_resource_id.clone(),
            physical_resource_id: r.physical_resource_id.clone(),
            resource_type: r.resource_type.clone(),
            timestamp: now.clone(),
            resource_status: "UPDATE_COMPLETE".to_string(),
            resource_status_reason: None,
        })
        .collect::<Vec<_>>();

    stack.template_body = template_body;
    stack.parameters = effective_params;
    stack.resources = resources;
    stack.events.extend(update_events);
    stack.status = "UPDATE_COMPLETE".to_string();
    stack.updated_at = Some(now);

    let stack_id = stack.stack_id.clone();
    Ok(json!({ "StackId": stack_id }))
}

pub fn describe_stacks(state: &CloudFormationState, input: &Value) -> Result<Value, AwsError> {
    let filter_name = opt_str(input, "StackName");

    let stacks: Vec<Value> = state
        .stacks
        .iter()
        .filter(|entry| {
            // Exclude DELETE_COMPLETE unless specifically queried
            if entry.status == "DELETE_COMPLETE" && filter_name.is_none() {
                return false;
            }
            if let Some(name) = filter_name {
                entry.stack_name == name || entry.stack_id == name
            } else {
                true
            }
        })
        .map(|entry| stack_to_value(&entry))
        .collect();

    if let Some(name) = filter_name
        && stacks.is_empty()
    {
        return Err(stack_not_found(name));
    }

    Ok(json!({ "Stacks": { "member": stacks } }))
}

pub fn describe_stack_events(
    state: &CloudFormationState,
    input: &Value,
) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?;

    let stack = state
        .stacks
        .get(stack_name)
        .ok_or_else(|| stack_not_found(stack_name))?;

    let events: Vec<Value> = stack.events.iter().map(event_to_value).collect();

    Ok(json!({ "StackEvents": { "member": events } }))
}

pub fn describe_stack_resources(
    state: &CloudFormationState,
    input: &Value,
) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?;

    let stack = state
        .stacks
        .get(stack_name)
        .ok_or_else(|| stack_not_found(stack_name))?;

    let resources: Vec<Value> = stack
        .resources
        .iter()
        .map(|r| resource_to_value(r, &stack))
        .collect();

    Ok(json!({ "StackResources": { "member": resources } }))
}

pub fn list_stacks(state: &CloudFormationState, input: &Value) -> Result<Value, AwsError> {
    // Parse StackStatusFilter
    let status_filters: Vec<String> = match input.get("StackStatusFilter") {
        Some(Value::Array(arr)) => arr
            .iter()
            .filter_map(|v| v.as_str().map(|s| s.to_string()))
            .collect(),
        Some(Value::Object(obj)) => {
            if let Some(Value::Array(arr)) = obj.get("member") {
                arr.iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect()
            } else {
                Vec::new()
            }
        }
        _ => Vec::new(),
    };

    let summaries: Vec<Value> = state
        .stacks
        .iter()
        .filter(|entry| status_filters.is_empty() || status_filters.contains(&entry.status))
        .map(|entry| {
            json!({
                "StackId": entry.stack_id,
                "StackName": entry.stack_name,
                "StackStatus": entry.status,
                "CreationTime": entry.created_at,
            })
        })
        .collect();

    Ok(json!({ "StackSummaries": { "member": summaries } }))
}

pub fn get_template(state: &CloudFormationState, input: &Value) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?;

    let stack = state
        .stacks
        .get(stack_name)
        .ok_or_else(|| stack_not_found(stack_name))?;

    Ok(json!({ "TemplateBody": stack.template_body }))
}

/// DescribeStackResource — get a single resource from a stack by logical ID.
pub fn describe_stack_resource(
    state: &CloudFormationState,
    input: &Value,
) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?;
    let logical_id = require_str(input, "LogicalResourceId")?;

    let stack = state
        .stacks
        .get(stack_name)
        .ok_or_else(|| stack_not_found(stack_name))?;

    let resource = stack
        .resources
        .iter()
        .find(|r| r.logical_resource_id == logical_id)
        .ok_or_else(|| {
            crate::error::missing_parameter(&format!(
                "Resource {logical_id} does not exist for stack {stack_name}"
            ))
        })?;

    let detail = resource_to_value(resource, &stack);

    Ok(json!({ "StackResourceDetail": detail }))
}

/// GetTemplateSummary — parse template and return metadata without creating a stack.
pub fn get_template_summary(
    _state: &CloudFormationState,
    input: &Value,
) -> Result<Value, AwsError> {
    let template_body =
        opt_str(input, "TemplateBody").ok_or_else(|| missing_parameter("TemplateBody"))?;

    let parsed = template::validate_and_parse(template_body, &HashMap::new())?;

    let params: Vec<Value> = parsed
        .parameters
        .iter()
        .map(|p| {
            let mut obj = json!({
                "ParameterKey": p.name,
                "ParameterType": p.param_type,
                "NoEcho": false,
            });
            if let Some(desc) = &p.description {
                obj["Description"] = Value::String(desc.clone());
            }
            if let Some(default) = &p.default {
                obj["DefaultValue"] = Value::String(default.clone());
            }
            obj
        })
        .collect();

    let resource_types: Vec<Value> = parsed
        .resources
        .iter()
        .map(|r| Value::String(r.resource_type.clone()))
        .collect();

    let mut result = json!({
        "Parameters": params,
        "ResourceTypes": { "member": resource_types },
        "Version": "2010-09-09",
        "Capabilities": { "member": [] },
        "CapabilitiesReason": null,
    });

    if let Some(desc) = parsed.description {
        result["Description"] = Value::String(desc);
    }

    Ok(result)
}

/// ListStackResources — paginated list of resources in a stack.
pub fn list_stack_resources(state: &CloudFormationState, input: &Value) -> Result<Value, AwsError> {
    let stack_name = require_str(input, "StackName")?;

    let stack = state
        .stacks
        .get(stack_name)
        .ok_or_else(|| stack_not_found(stack_name))?;

    let summaries: Vec<Value> = stack
        .resources
        .iter()
        .map(|r| {
            let mut obj = json!({
                "LogicalResourceId": r.logical_resource_id,
                "ResourceType": r.resource_type,
                "ResourceStatus": r.resource_status,
                "LastUpdatedTimestamp": r.timestamp,
            });
            if let Some(phys) = &r.physical_resource_id {
                obj["PhysicalResourceId"] = Value::String(phys.clone());
            }
            obj
        })
        .collect();

    Ok(json!({
        "StackResourceSummaries": { "member": summaries },
        "NextToken": null,
    }))
}

/// ListExports — stub returning empty list.
pub fn list_exports(_state: &CloudFormationState, _input: &Value) -> Result<Value, AwsError> {
    Ok(json!({ "Exports": { "member": [] }, "NextToken": null }))
}

/// ListImports — stub returning empty list.
pub fn list_imports(_state: &CloudFormationState, _input: &Value) -> Result<Value, AwsError> {
    Ok(json!({ "Imports": { "member": [] }, "NextToken": null }))
}

/// TagResource — add or update tags on a stack.
pub fn tag_resource(state: &CloudFormationState, input: &Value) -> Result<Value, AwsError> {
    let resource_arn = require_str(input, "ResourceArn")?;
    // Extract the stack name from the ARN (last segment after the final '/')
    let stack_name = resource_arn.split('/').nth(1).unwrap_or(resource_arn);

    let new_tags = parse_tags(input);

    let mut entry = state.stack_tags.entry(stack_name.to_string()).or_default();
    for (k, v) in new_tags {
        entry.insert(k, v);
    }

    Ok(json!({}))
}

/// UntagResource — remove tags from a stack.
pub fn untag_resource(state: &CloudFormationState, input: &Value) -> Result<Value, AwsError> {
    let resource_arn = require_str(input, "ResourceArn")?;
    let stack_name = resource_arn.split('/').nth(1).unwrap_or(resource_arn);

    let tag_keys: Vec<&str> = match input.get("TagKeys") {
        Some(Value::Array(arr)) => arr.iter().filter_map(|v| v.as_str()).collect(),
        Some(Value::Object(obj)) => {
            if let Some(Value::Array(arr)) = obj.get("member") {
                arr.iter().filter_map(|v| v.as_str()).collect()
            } else {
                Vec::new()
            }
        }
        _ => Vec::new(),
    };

    if let Some(mut tags) = state.stack_tags.get_mut(stack_name) {
        for key in &tag_keys {
            tags.remove(*key);
        }
    }

    Ok(json!({}))
}

/// SignalResource — accept and succeed silently.
pub fn signal_resource(_state: &CloudFormationState, _input: &Value) -> Result<Value, AwsError> {
    Ok(json!({}))
}

/// EstimateTemplateCost — stub returning a cost estimate URL.
pub fn estimate_template_cost(
    _state: &CloudFormationState,
    _input: &Value,
) -> Result<Value, AwsError> {
    Ok(json!({
        "Url": "http://calculator.s3.amazonaws.com/calc5.html?key=awsim-stub-estimate"
    }))
}

pub fn validate_template(_state: &CloudFormationState, input: &Value) -> Result<Value, AwsError> {
    let template_body =
        opt_str(input, "TemplateBody").ok_or_else(|| missing_parameter("TemplateBody"))?;

    let parsed = template::validate_and_parse(template_body, &HashMap::new())?;

    let params: Vec<Value> = parsed
        .parameters
        .iter()
        .map(|p| {
            let mut obj = json!({
                "ParameterKey": p.name,
                "ParameterType": p.param_type,
            });
            if let Some(desc) = &p.description {
                obj["Description"] = Value::String(desc.clone());
            }
            if let Some(default) = &p.default {
                obj["DefaultValue"] = Value::String(default.clone());
            }
            obj
        })
        .collect();

    let mut result = json!({ "Parameters": { "member": params } });
    if let Some(desc) = parsed.description {
        result["Description"] = Value::String(desc);
    }

    Ok(result)
}