From d8734a68d428dcf93f3129604ce5773f056a8fc5 Mon Sep 17 00:00:00 2001 From: Shia Date: Wed, 27 Nov 2024 18:52:09 +0900 Subject: [PATCH] Sort remote state tags by local tags order to make sure the diff result be consistent --- src/differ.rs | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/src/differ.rs b/src/differ.rs index cdb36f0..1989b99 100644 --- a/src/differ.rs +++ b/src/differ.rs @@ -264,6 +264,40 @@ fn build_sfn_tags_diff_ops( diff_ops } +// Sort remote tags by local tags order +// to make sure the diff result is consistent +pub fn sort_tags_by_local_tags_order( + remote_state: Option, + local_tags: &[ResourceTag], +) -> Option { + if remote_state.is_none() { + return remote_state; + } + let mut remote_state = remote_state.unwrap(); + let remote_tags = remote_state.tags; + + let local_tag_keys: HashSet<&String> = local_tags.iter().map(|tag| &tag.key).collect(); + let remote_tag_keys: HashSet<&String> = remote_tags.iter().map(|tag| &tag.key).collect(); + let remote_only_tag_keys = remote_tag_keys.difference(&local_tag_keys); + let mut sorted_remote_tags = vec![]; + + for local_tag in local_tags { + if let Some(remote_tag) = remote_tags.iter().find(|tag| tag.key == local_tag.key) { + sorted_remote_tags.push(remote_tag.clone()); + } + } + + for tag_key in remote_only_tag_keys { + if let Some(tag) = remote_tags.iter().find(|tag| tag.key == **tag_key) { + sorted_remote_tags.push(tag.clone()); + } + } + + remote_state.tags = sorted_remote_tags; + + Some(remote_state) +} + pub async fn diff(context: &FuburaContext, config: &Config) -> Result { let mut diff_result = DiffResult::default(); @@ -277,6 +311,7 @@ pub async fn diff(context: &FuburaContext, config: &Config) -> Result