From 9b4b7568948811dd6dda45060f3e384c883bde6c Mon Sep 17 00:00:00 2001 From: apollorion Date: Mon, 25 Nov 2024 12:55:23 -0500 Subject: [PATCH] feat: spacelift_stack save md5 hash of import_state to state and force new --- spacelift/resource_stack.go | 6 ++++ spacelift/resource_stack_test.go | 50 ++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/spacelift/resource_stack.go b/spacelift/resource_stack.go index 20372a9c..a78118d1 100644 --- a/spacelift/resource_stack.go +++ b/spacelift/resource_stack.go @@ -2,6 +2,7 @@ package spacelift import ( "context" + "crypto/md5" "fmt" "net/http" "os" @@ -405,6 +406,7 @@ func resourceStack() *schema.Resource { Optional: true, DiffSuppressFunc: ignoreOnceCreated, Sensitive: true, + ForceNew: true, }, "import_state_file": { Type: schema.TypeString, @@ -662,6 +664,10 @@ func resourceStackCreate(ctx context.Context, d *schema.ResourceData, meta inter return diag.Errorf(`"import_state" requires "manage_state" to be true`) } else if ok { stateContent = content.(string) + + // Save the hash to state instead of the content to prevent large state files and memory usage + hash := fmt.Sprintf("%x", md5.Sum([]byte(stateContent))) + d.Set("import_state", hash) } path, ok := d.GetOk("import_state_file") diff --git a/spacelift/resource_stack_test.go b/spacelift/resource_stack_test.go index a51e0c18..5238c841 100644 --- a/spacelift/resource_stack_test.go +++ b/spacelift/resource_stack_test.go @@ -1,6 +1,7 @@ package spacelift import ( + "crypto/md5" "fmt" "regexp" "testing" @@ -331,7 +332,7 @@ func TestStackResource(t *testing.T) { name = "labelled-module-%s" branch = "master" labels = [] - repository = "terraform-bacon-tasty" + repository = "demo" }`, randomID), Check: Resource( "spacelift_stack.test", @@ -1482,7 +1483,7 @@ func TestStackResourceSpace(t *testing.T) { name = "labelled-module-%s" branch = "master" labels = [] - repository = "terraform-bacon-tasty" + repository = "demo" }`, randomID), Check: Resource( "spacelift_stack.test", @@ -1637,6 +1638,51 @@ func TestStackResourceSpace(t *testing.T) { }, }) }) + + t.Run("with import_state", func(t *testing.T) { + + var originalId string + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + hash := fmt.Sprintf("%x", md5.Sum([]byte("{}"))) + + testSteps(t, []resource.TestStep{ + { + Config: fmt.Sprintf(` + resource "spacelift_stack" "state_import" { + branch = "master" + name = "Provider test stack workflow_tool default %s" + project_root = "root" + repository = "demo" + import_state = "{}" + } + `, randomID), + Check: Resource( + "spacelift_stack.state_import", + Attribute("import_state", Equals(hash)), + + func(attributes map[string]string) error { + originalId = attributes["id"] + return nil + }, + ), + }, + { + Config: fmt.Sprintf(` + resource "spacelift_stack" "state_import" { + branch = "master" + name = "Provider test stack workflow_tool default %s" + project_root = "root" + repository = "demo" + import_state = "{\"import_state\": \"changed\"}" + } + `, randomID), + Check: Resource( + "spacelift_stack.state_import", + Attribute("id", NotEquals(originalId)), + ), + }, + }) + }) } // getConfig returns a stack config with injected vendor config