|
19 | 19 | _ resource.ResourceWithImportState = &environmentResource{} |
20 | 20 | ) |
21 | 21 |
|
| 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 | + |
22 | 31 | func EnvironmentResource() resource.Resource { |
23 | 32 | return &environmentResource{} |
24 | 33 | } |
@@ -81,11 +90,7 @@ func (r *environmentResource) Read( |
81 | 90 | state.ExtendedAttributesID = types.Int64Null() |
82 | 91 | } |
83 | 92 | 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) |
89 | 94 | if environment.Credential_Id != nil { |
90 | 95 | state.CredentialID = types.Int64Value(int64(*environment.Credential_Id)) |
91 | 96 | } else { |
@@ -185,11 +190,7 @@ func (r *environmentResource) Create( |
185 | 190 | plan.ExtendedAttributesID = types.Int64Null() |
186 | 191 | } |
187 | 192 | 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) |
193 | 194 | plan.CredentialID = types.Int64PointerValue( |
194 | 195 | helper.IntPointerToInt64Pointer(environment.Credential_Id), |
195 | 196 | ) |
@@ -329,11 +330,7 @@ func (r *environmentResource) Update( |
329 | 330 | } |
330 | 331 |
|
331 | 332 | 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) |
337 | 334 | if updatedEnv.Credential_Id != nil { |
338 | 335 | plan.CredentialID = types.Int64Value(int64(*updatedEnv.Credential_Id)) |
339 | 336 | } else { |
@@ -435,8 +432,8 @@ func (r *environmentResource) ImportState( |
435 | 432 | return |
436 | 433 | } |
437 | 434 |
|
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())...) |
440 | 437 | } |
441 | 438 |
|
442 | 439 | func (r *environmentResource) Configure( |
|
0 commit comments