From 0f7929299508ed29bd33c6b0ec7b8a17968044f7 Mon Sep 17 00:00:00 2001 From: apollorion Date: Mon, 25 Nov 2024 12:55:23 -0500 Subject: [PATCH 1/2] feat: do not save imported state to statefile --- spacelift/resource_stack.go | 24 ++++++++++++++++++++++++ spacelift/resource_stack_test.go | 21 +++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/spacelift/resource_stack.go b/spacelift/resource_stack.go index 20372a9c..f55b11d8 100644 --- a/spacelift/resource_stack.go +++ b/spacelift/resource_stack.go @@ -17,6 +17,16 @@ import ( "github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/validations" ) +func resourceStackResourceV0() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "import_state": { + Type: schema.TypeString, + }, + }, + } +} + func resourceStack() *schema.Resource { return &schema.Resource{ Description: "" + @@ -34,6 +44,19 @@ func resourceStack() *schema.Resource { StateContext: resourceStackImport, }, + SchemaVersion: 1, + + StateUpgraders: []schema.StateUpgrader{ + { + Version: 0, + Type: resourceStackResourceV0().CoreConfigSchema().ImpliedType(), + Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + rawState["import_state"] = "" + return rawState, nil + }, + }, + }, + Schema: map[string]*schema.Schema{ "administrative": { Type: schema.TypeBool, @@ -662,6 +685,7 @@ 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) + d.Set("import_state", "") } path, ok := d.GetOk("import_state_file") diff --git a/spacelift/resource_stack_test.go b/spacelift/resource_stack_test.go index a51e0c18..da9194c2 100644 --- a/spacelift/resource_stack_test.go +++ b/spacelift/resource_stack_test.go @@ -1637,6 +1637,27 @@ func TestStackResourceSpace(t *testing.T) { }, }) }) + + t.Run("with import_state", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + 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("")), + ), + }, + }) + }) } // getConfig returns a stack config with injected vendor config From 25b9415b18844e72c044cdf4b09c41f553b7debf Mon Sep 17 00:00:00 2001 From: apollorion Date: Mon, 9 Dec 2024 09:52:44 -0500 Subject: [PATCH 2/2] style: add comment explaining whats going on --- spacelift/resource_stack.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spacelift/resource_stack.go b/spacelift/resource_stack.go index f55b11d8..bc8c3dfb 100644 --- a/spacelift/resource_stack.go +++ b/spacelift/resource_stack.go @@ -51,6 +51,7 @@ func resourceStack() *schema.Resource { Version: 0, Type: resourceStackResourceV0().CoreConfigSchema().ImpliedType(), Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + // This function will upgrade any existing state files and will remove any data saved to the `import_state` key. rawState["import_state"] = "" return rawState, nil }, @@ -685,6 +686,8 @@ 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) + // After we've saved the imported state to memory, set the value to an empty string so it doesn't get saved to this state file. + // We purposefully ignore this value after creation, so we have no reason to save it. d.Set("import_state", "") }