Skip to content

Commit 681e2ea

Browse files
authored
Merge pull request #5851 from cloudflare/tamas/zero_trust_access_policy_
chore: modernize zero_trust_access_policy acceptance tests
2 parents fa1df78 + a0326ed commit 681e2ea

24 files changed

+738
-187
lines changed

internal/services/zero_trust_access_policy/normalizations.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package zero_trust_access_policy
33
import (
44
"context"
55
"github.com/hashicorp/terraform-plugin-framework/diag"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
67
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
78
)
89

@@ -39,3 +40,21 @@ func normalizeReadZeroTrustAccessPolicyAPIData(ctx context.Context, data, source
3940

4041
return diags
4142
}
43+
44+
// Specialized normalization for import operations where API omits false boolean values
45+
func normalizeImportZeroTrustAccessPolicyAPIData(ctx context.Context, data *ZeroTrustAccessPolicyModel) diag.Diagnostics {
46+
diags := make(diag.Diagnostics, 0)
47+
48+
// Set boolean fields to false if they are null (API omits false values during import)
49+
if data.ApprovalRequired.IsNull() {
50+
data.ApprovalRequired = types.BoolValue(false)
51+
}
52+
if data.IsolationRequired.IsNull() {
53+
data.IsolationRequired = types.BoolValue(false)
54+
}
55+
if data.PurposeJustificationRequired.IsNull() {
56+
data.PurposeJustificationRequired = types.BoolValue(false)
57+
}
58+
59+
return diags
60+
}

internal/services/zero_trust_access_policy/resource.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func (r *ZeroTrustAccessPolicyResource) Create(ctx context.Context, req resource
8484
resp.Diagnostics.AddError("failed to make http request", err.Error())
8585
return
8686
}
87+
if res == nil || res.Body == nil {
88+
resp.Diagnostics.AddError("failed to read response", "Response or response body is nil")
89+
return
90+
}
8791
bytes, _ := io.ReadAll(res.Body)
8892
err = apijson.UnmarshalComputed(bytes, &env)
8993
if err != nil {
@@ -133,6 +137,10 @@ func (r *ZeroTrustAccessPolicyResource) Update(ctx context.Context, req resource
133137
resp.Diagnostics.AddError("failed to make http request", err.Error())
134138
return
135139
}
140+
if res == nil || res.Body == nil {
141+
resp.Diagnostics.AddError("failed to read response", "Response or response body is nil")
142+
return
143+
}
136144
bytes, _ := io.ReadAll(res.Body)
137145
err = apijson.UnmarshalComputed(bytes, &env)
138146
if err != nil {
@@ -180,6 +188,10 @@ func (r *ZeroTrustAccessPolicyResource) Read(ctx context.Context, req resource.R
180188
resp.Diagnostics.AddError("failed to make http request", err.Error())
181189
return
182190
}
191+
if res == nil || res.Body == nil {
192+
resp.Diagnostics.AddError("failed to read response", "Response or response body is nil")
193+
return
194+
}
183195
bytes, _ := io.ReadAll(res.Body)
184196
err = apijson.Unmarshal(bytes, &env)
185197
if err != nil {
@@ -258,6 +270,10 @@ func (r *ZeroTrustAccessPolicyResource) ImportState(ctx context.Context, req res
258270
resp.Diagnostics.AddError("failed to make http request", err.Error())
259271
return
260272
}
273+
if res == nil || res.Body == nil {
274+
resp.Diagnostics.AddError("failed to read response", "Response or response body is nil")
275+
return
276+
}
261277
bytes, _ := io.ReadAll(res.Body)
262278
err = apijson.Unmarshal(bytes, &env)
263279
if err != nil {
@@ -266,6 +282,9 @@ func (r *ZeroTrustAccessPolicyResource) ImportState(ctx context.Context, req res
266282
}
267283
data = &env.Result
268284

285+
// Apply import-specific normalizations to handle API omissions
286+
resp.Diagnostics.Append(normalizeImportZeroTrustAccessPolicyAPIData(ctx, data)...)
287+
269288
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
270289
}
271290

0 commit comments

Comments
 (0)