Skip to content

Commit f07a938

Browse files
trouzeclaude
andcommitted
fix: remove connection_id default of 0 in dbtcloud_environment (closes #665)
Removes Default: int64default.StaticInt64(0) from the connection_id schema attribute and replaces scattered nil/0 checks with a connectionIDFromAPI helper that treats both nil and 0 as null in Terraform state. Fixes the "inconsistent result after apply" error when the dbt Cloud API auto-assigns a non-zero connection ID. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 17814e7 commit f07a938

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

pkg/framework/objects/environment/resource.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ var (
1919
_ resource.ResourceWithImportState = &environmentResource{}
2020
)
2121

22+
// connectionIDFromAPI converts the API connection_id pointer to a types.Int64 value,
23+
// treating nil and 0 as "not set" (null) so Terraform doesn't see a spurious diff.
24+
func connectionIDFromAPI(id *int) types.Int64 {
25+
if id != nil && *id != 0 {
26+
return types.Int64Value(int64(*id))
27+
}
28+
return types.Int64Null()
29+
}
30+
2231
func EnvironmentResource() resource.Resource {
2332
return &environmentResource{}
2433
}
@@ -81,11 +90,7 @@ func (r *environmentResource) Read(
8190
state.ExtendedAttributesID = types.Int64Null()
8291
}
8392
state.EnableModelQueryHistory = types.BoolValue(environment.EnableModelQueryHistory)
84-
if environment.ConnectionID != nil {
85-
state.ConnectionID = types.Int64Value(int64(*environment.ConnectionID))
86-
} else {
87-
state.ConnectionID = types.Int64Value(0)
88-
}
93+
state.ConnectionID = connectionIDFromAPI(environment.ConnectionID)
8994
if environment.Credential_Id != nil {
9095
state.CredentialID = types.Int64Value(int64(*environment.Credential_Id))
9196
} else {
@@ -185,11 +190,7 @@ func (r *environmentResource) Create(
185190
plan.ExtendedAttributesID = types.Int64Null()
186191
}
187192
plan.EnableModelQueryHistory = types.BoolValue(environment.EnableModelQueryHistory)
188-
if environment.ConnectionID != nil {
189-
plan.ConnectionID = types.Int64Value(int64(*environment.ConnectionID))
190-
} else {
191-
plan.ConnectionID = types.Int64Value(0)
192-
}
193+
plan.ConnectionID = connectionIDFromAPI(environment.ConnectionID)
193194
plan.CredentialID = types.Int64PointerValue(
194195
helper.IntPointerToInt64Pointer(environment.Credential_Id),
195196
)
@@ -329,11 +330,7 @@ func (r *environmentResource) Update(
329330
}
330331

331332
plan.EnvironmentID = types.Int64Value(int64(*updatedEnv.Environment_Id))
332-
if updatedEnv.ConnectionID != nil {
333-
plan.ConnectionID = types.Int64Value(int64(*updatedEnv.ConnectionID))
334-
} else {
335-
plan.ConnectionID = types.Int64Value(0)
336-
}
333+
plan.ConnectionID = connectionIDFromAPI(updatedEnv.ConnectionID)
337334
if updatedEnv.Credential_Id != nil {
338335
plan.CredentialID = types.Int64Value(int64(*updatedEnv.Credential_Id))
339336
} else {
@@ -435,8 +432,8 @@ func (r *environmentResource) ImportState(
435432
return
436433
}
437434

438-
// Set connection_id to 0 to match the test's expectation
439-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("connection_id"), types.Int64Value(0))...)
435+
// Set connection_id to null so it is refreshed from the API on the next plan/apply
436+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("connection_id"), types.Int64Null())...)
440437
}
441438

442439
func (r *environmentResource) Configure(

pkg/framework/objects/environment/schema.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/resource"
1212
resource_schema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1313
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
14-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
1514
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
1615
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1716
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
@@ -263,7 +262,6 @@ func (r *environmentResource) Schema(
263262
"connection_id": resource_schema.Int64Attribute{
264263
Optional: true,
265264
Computed: true,
266-
Default: int64default.StaticInt64(0),
267265
Description: "A connection ID (used with Global Connections)",
268266
PlanModifiers: []planmodifier.Int64{
269267
int64planmodifier.UseStateForUnknown(),

0 commit comments

Comments
 (0)