Skip to content

Commit cf6d8a9

Browse files
committed
Improve generation validation with plan-time error checking
- Add CustomizeDiff function for shield+generation validation during plan phase - Users now get immediate feedback during 'terraform plan' instead of waiting for apply - Remove redundant validation from Create function since CustomizeDiff handles it - Better UX: clear error messages during planning prevent surprises during apply
1 parent 50e4c75 commit cf6d8a9

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

heroku/resource_heroku_space.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ type spaceWithNAT struct {
2020

2121
func resourceHerokuSpace() *schema.Resource {
2222
return &schema.Resource{
23-
Create: resourceHerokuSpaceCreate,
24-
Read: resourceHerokuSpaceRead,
25-
Update: resourceHerokuSpaceUpdate,
26-
Delete: resourceHerokuSpaceDelete,
23+
Create: resourceHerokuSpaceCreate,
24+
Read: resourceHerokuSpaceRead,
25+
Update: resourceHerokuSpaceUpdate,
26+
Delete: resourceHerokuSpaceDelete,
27+
CustomizeDiff: resourceHerokuSpaceCustomizeDiff,
2728

2829
Importer: &schema.ResourceImporter{
2930
State: schema.ImportStatePassthrough,
@@ -103,11 +104,6 @@ func resourceHerokuSpaceCreate(d *schema.ResourceData, meta interface{}) error {
103104
if v := d.Get("shield"); v != nil {
104105
vs := v.(bool)
105106
if vs {
106-
// Validate shield support for the selected generation
107-
generation := d.Get("generation").(string)
108-
if !IsFeatureSupported(generation, "space", "shield") {
109-
return fmt.Errorf("shield spaces are not supported for %s generation", generation)
110-
}
111107
log.Printf("[DEBUG] Creating a shield space")
112108
}
113109
opts.Shield = &vs
@@ -242,3 +238,22 @@ func SpaceStateRefreshFunc(client *heroku.Service, id string) resource.StateRefr
242238
return &s, space.State, nil
243239
}
244240
}
241+
242+
// resourceHerokuSpaceCustomizeDiff validates generation-specific feature support during plan phase
243+
func resourceHerokuSpaceCustomizeDiff(ctx context.Context, diff *schema.ResourceDiff, v interface{}) error {
244+
generation, generationExists := diff.GetOk("generation")
245+
shield, shieldExists := diff.GetOk("shield")
246+
247+
// Only validate if both fields are present
248+
if generationExists && shieldExists {
249+
generationStr := generation.(string)
250+
shieldBool := shield.(bool)
251+
252+
// Check if shield is enabled for a generation that doesn't support it
253+
if shieldBool && !IsFeatureSupported(generationStr, "space", "shield") {
254+
return fmt.Errorf("shield spaces are not supported for %s generation", generationStr)
255+
}
256+
}
257+
258+
return nil
259+
}

0 commit comments

Comments
 (0)