Skip to main content

awsim_cloudformation/
lib.rs

1mod error;
2mod ids;
3mod operations;
4mod state;
5mod template;
6
7use std::sync::Arc;
8
9use async_trait::async_trait;
10use awsim_core::{AccountRegionStore, AwsError, Protocol, RequestContext, ServiceHandler};
11use serde_json::Value;
12use tracing::debug;
13
14use state::CloudFormationState;
15
16/// The AWSim CloudFormation service handler.
17pub struct CloudFormationService {
18    store: AccountRegionStore<CloudFormationState>,
19}
20
21impl CloudFormationService {
22    pub fn new() -> Self {
23        Self {
24            store: AccountRegionStore::new(),
25        }
26    }
27
28    fn get_state(&self, ctx: &RequestContext) -> Arc<CloudFormationState> {
29        self.store.get(&ctx.account_id, &ctx.region)
30    }
31}
32
33impl Default for CloudFormationService {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39#[async_trait]
40impl ServiceHandler for CloudFormationService {
41    fn service_name(&self) -> &str {
42        "cloudformation"
43    }
44
45    fn signing_name(&self) -> &str {
46        "cloudformation"
47    }
48
49    fn protocol(&self) -> Protocol {
50        Protocol::AwsQuery
51    }
52
53    async fn handle(
54        &self,
55        operation: &str,
56        input: Value,
57        ctx: &RequestContext,
58    ) -> Result<Value, AwsError> {
59        debug!(operation, "CloudFormation request");
60        let state = self.get_state(ctx);
61
62        match operation {
63            // Stacks
64            "CreateStack" => operations::stacks::create_stack(&state, &input, ctx),
65            "DeleteStack" => operations::stacks::delete_stack(&state, &input, ctx),
66            "UpdateStack" => operations::stacks::update_stack(&state, &input, ctx),
67            "DescribeStacks" => operations::stacks::describe_stacks(&state, &input),
68            "DescribeStackEvents" => operations::stacks::describe_stack_events(&state, &input),
69            "DescribeStackResources" => {
70                operations::stacks::describe_stack_resources(&state, &input)
71            }
72            "DescribeStackResource" => operations::stacks::describe_stack_resource(&state, &input),
73            "ListStacks" => operations::stacks::list_stacks(&state, &input),
74            "ListStackResources" => operations::stacks::list_stack_resources(&state, &input),
75            "GetTemplate" => operations::stacks::get_template(&state, &input),
76            "GetTemplateSummary" => operations::stacks::get_template_summary(&state, &input),
77            "ValidateTemplate" => operations::stacks::validate_template(&state, &input),
78            "UpdateTerminationProtection" => {
79                operations::stacks::update_termination_protection(&state, &input, ctx)
80            }
81            "SetStackPolicy" => operations::stacks::set_stack_policy(&state, &input),
82            "GetStackPolicy" => operations::stacks::get_stack_policy(&state, &input),
83
84            // Exports / Imports
85            "ListExports" => operations::stacks::list_exports(&state, &input),
86            "ListImports" => operations::stacks::list_imports(&state, &input),
87
88            // Tagging
89            "TagResource" => operations::stacks::tag_resource(&state, &input),
90            "UntagResource" => operations::stacks::untag_resource(&state, &input),
91
92            // Signals / Cost
93            "SignalResource" => operations::stacks::signal_resource(&state, &input),
94            "EstimateTemplateCost" => operations::stacks::estimate_template_cost(&state, &input),
95
96            // Change Sets
97            "CreateChangeSet" => operations::change_sets::create_change_set(&state, &input, ctx),
98            "ExecuteChangeSet" => operations::change_sets::execute_change_set(&state, &input, ctx),
99            "DeleteChangeSet" => operations::change_sets::delete_change_set(&state, &input),
100            "DescribeChangeSet" => operations::change_sets::describe_change_set(&state, &input),
101            "ListChangeSets" => operations::change_sets::list_change_sets(&state, &input),
102
103            _ => Err(AwsError::unknown_operation(operation)),
104        }
105    }
106}