Skip to content

Commit

Permalink
Merge pull request #589 from spacelift-io/recreate-stack
Browse files Browse the repository at this point in the history
feat: do not save imported state to statefile
  • Loading branch information
Apollorion authored Dec 9, 2024
2 parents 0b0d800 + 25b9415 commit ca71331
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions spacelift/resource_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "" +
Expand All @@ -34,6 +44,20 @@ 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) {
// 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
},
},
},

Schema: map[string]*schema.Schema{
"administrative": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -662,6 +686,9 @@ 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", "")
}

path, ok := d.GetOk("import_state_file")
Expand Down
21 changes: 21 additions & 0 deletions spacelift/resource_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca71331

Please sign in to comment.