Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: check preconditions and postconditions for ephemeral resources #35920

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/checks/state_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func collectInitialStatuses(into addrs.Map[addrs.ConfigCheckable, *configCheckab
addr := rc.Addr().InModule(moduleAddr)
collectInitialStatusForResource(into, addr, rc)
}
for _, rc := range cfg.Module.EphemeralResources {
addr := rc.Addr().InModule(moduleAddr)
collectInitialStatusForResource(into, addr, rc)
}

for _, oc := range cfg.Module.Outputs {
addr := oc.Addr().InModule(moduleAddr)
Expand Down
48 changes: 48 additions & 0 deletions internal/terraform/context_plan_ephemeral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,54 @@ module "child" {
})
},
},
"resource precondition": {
module: map[string]string{
"main.tf": `
locals {
test_value = 2
}
ephemeral "ephem_resource" "data" {
lifecycle {
precondition {
condition = local.test_value != 2
error_message = "value should not be 2"
}
}
}
`,
},
expectPlanDiagnostics: func(m *configs.Config) (diags tfdiags.Diagnostics) {
return diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Resource precondition failed",
Detail: "value should not be 2",
})
},
},
"resource postcondition": {
module: map[string]string{
"main.tf": `
locals {
test_value = 2
}
ephemeral "ephem_resource" "data" {
lifecycle {
postcondition {
condition = self.value == "pass"
error_message = "value should be \"pass\""
}
}
}
`,
},
expectPlanDiagnostics: func(m *configs.Config) (diags tfdiags.Diagnostics) {
return diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Resource postcondition failed",
Detail: `value should be "pass"`,
})
},
},
} {
t.Run(name, func(t *testing.T) {
if tc.toBeImplemented {
Expand Down
12 changes: 12 additions & 0 deletions internal/terraform/node_resource_ephemeral.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ func ephemeralResourceOpen(ctx EvalContext, inp ephemeralResourceInput) (*provid
Private: resp.Private,
})

// Postconditions for ephemerals validate only what is returned by
// OpenEphemeralResource. These will block downstream dependency operations
// if an error is returned, but don't prevent renewal or closing of the
// resource.
checkDiags = evalCheckRules(
addrs.ResourcePostcondition,
config.Postconditions,
ctx, inp.addr, keyData,
tfdiags.Error,
)
diags = diags.Append(checkDiags)

return nil, diags
}

Expand Down
Loading