rusty_cdk/
diff.rs

1use std::process::exit;
2use aws_sdk_cloudformation::Client;
3use rusty_cdk_core::stack::{Stack, StackDiff};
4use rusty_cdk_core::wrappers::StringWithOnlyAlphaNumericsAndHyphens;
5use crate::util::get_existing_template;
6
7/// Creates a diff that will show what ids are being added / removed to an existing stack, as well as showing ids that remain without being added or removed.
8/// Currently, the diff does not show modifications to resources.
9/// 
10/// # Parameters
11///
12/// * `name` - The existing CloudFormation stack name
13/// * `stack` - The new stack
14///
15/// # AWS Credentials
16///
17/// This function requires valid AWS credentials.
18/// The AWS credentials must have permissions for:
19/// - `cloudformation:DescribeStacks`, `cloudformation:GetTemplate`
20#[allow(unused)]
21pub async fn diff(name: StringWithOnlyAlphaNumericsAndHyphens, stack: Stack) {
22    let config = aws_config::defaults(aws_config::BehaviorVersion::latest())
23        .load()
24        .await;
25    let cloudformation_client = Client::new(&config);
26    
27    match get_existing_template(&cloudformation_client, &name.0).await {
28        None => {
29            eprintln!("could not find existing stack with name {}", name.0);
30            exit(1);
31        }
32        Some(existing) => {
33            let diff = stack.get_diff(&existing);
34            
35            match diff {
36                Ok(diff) => {
37                    let StackDiff { new_ids, unchanged_ids, ids_to_be_removed} = diff;
38                    println!("- added ids: {}", print_ids(new_ids));
39                    println!("- removed ids: {}", print_ids(ids_to_be_removed));
40                    println!("- id that stay: {}", print_ids(unchanged_ids));
41                }
42                Err(e) => {
43                    eprintln!("{}", e);
44                    exit(1);
45                }
46            }
47        }
48    }
49}
50
51fn print_ids(ids: Vec<(String, String)>) -> String {
52    if ids.is_empty() {
53        "(none)".to_string()
54    } else {
55        ids.into_iter().map(|v| format!("{} (resource {})", v.0, v.1)).collect::<Vec<_>>().join(", ")
56    }
57}