Skip to content

Commit 88c1385

Browse files
Handles statement [] and updated docs
Signed-off-by: bdhivya <bdhivya@netapp.com>
1 parent cddbe20 commit 88c1385

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

docs/data-sources/s3_policies.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Optional:
6767
<a id="nestedatt--protocols_s3_policies"></a>
6868
### Nested Schema for `protocols_s3_policies`
6969

70+
Required:
71+
72+
- `cx_profile_name` (String) Connection profile name
73+
- `name` (String) The name of the S3 policy
74+
- `svm_name` (String) The name of the SVM
75+
7076
Read-Only:
7177

7278
- `comment` (String) Optional comment for the S3 policy

internal/provider/protocols/protocols_s3_policy_resource.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection"
99

10+
"github.com/hashicorp/terraform-plugin-framework/attr"
1011
"github.com/hashicorp/terraform-plugin-framework/diag"
1112
"github.com/hashicorp/terraform-plugin-framework/path"
1213
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -273,9 +274,19 @@ func (r *ProtocolsS3PolicyResource) Create(ctx context.Context, req resource.Cre
273274
// Set read_only from API response
274275
data.ReadOnly = types.BoolValue(restInfo.ReadOnly)
275276

276-
// Update statements with indices from API response - use nil for empty to preserve user intention
277+
// Update statements with indices from API response
277278
if len(restInfo.Statements) == 0 {
278-
data.Statements = nil
279+
// Check if statements was specified in config
280+
var configStatements attr.Value
281+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("statements"), &configStatements)...)
282+
283+
// If statements was specified (even as empty), preserve it as empty list
284+
// If statements was not specified (null), keep it as null
285+
if !configStatements.IsNull() {
286+
data.Statements = []ProtocolsS3PolicyStatement{}
287+
} else {
288+
data.Statements = nil
289+
}
279290
} else {
280291
data.Statements = statementsSliceToList(ctx, restInfo.Statements, &resp.Diagnostics)
281292
}
@@ -339,9 +350,17 @@ func (r *ProtocolsS3PolicyResource) Read(ctx context.Context, req resource.ReadR
339350

340351
data.SVMName = types.StringValue(restInfo.SVM.Name)
341352

342-
// Convert statements - handle empty array as nil for consistency
353+
// Convert statements - preserve null vs empty distinction from current state
343354
if len(restInfo.Statements) == 0 {
344-
data.Statements = nil
355+
// Preserve existing state: if was empty list, keep empty; if was null, keep null
356+
if data.Statements != nil && len(data.Statements) == 0 {
357+
data.Statements = []ProtocolsS3PolicyStatement{}
358+
} else if data.Statements == nil {
359+
data.Statements = nil
360+
} else {
361+
// Default case - if unclear, use empty for consistency
362+
data.Statements = []ProtocolsS3PolicyStatement{}
363+
}
345364
} else {
346365
data.Statements = statementsSliceToList(ctx, restInfo.Statements, &resp.Diagnostics)
347366
}
@@ -460,9 +479,19 @@ func (r *ProtocolsS3PolicyResource) Update(ctx context.Context, req resource.Upd
460479
// Set read_only from API response
461480
plan.ReadOnly = types.BoolValue(restInfo.ReadOnly)
462481

463-
// Update statements with indices from API response - use nil for empty to preserve user intention
482+
// Update statements with indices from API response
464483
if len(restInfo.Statements) == 0 {
465-
plan.Statements = nil
484+
// Check if statements was specified in config/plan
485+
var planStatements attr.Value
486+
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, path.Root("statements"), &planStatements)...)
487+
488+
// If statements was specified (even as empty), preserve it as empty list
489+
// If statements was not specified (null), keep it as null
490+
if !planStatements.IsNull() {
491+
plan.Statements = []ProtocolsS3PolicyStatement{}
492+
} else {
493+
plan.Statements = nil
494+
}
466495
} else {
467496
plan.Statements = statementsSliceToList(ctx, restInfo.Statements, &resp.Diagnostics)
468497
}

0 commit comments

Comments
 (0)