|
7 | 7 |
|
8 | 8 | "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection" |
9 | 9 |
|
| 10 | +"github.com/hashicorp/terraform-plugin-framework/attr" |
10 | 11 | "github.com/hashicorp/terraform-plugin-framework/diag" |
11 | 12 | "github.com/hashicorp/terraform-plugin-framework/path" |
12 | 13 | "github.com/hashicorp/terraform-plugin-framework/resource" |
@@ -273,9 +274,19 @@ func (r *ProtocolsS3PolicyResource) Create(ctx context.Context, req resource.Cre |
273 | 274 | // Set read_only from API response |
274 | 275 | data.ReadOnly = types.BoolValue(restInfo.ReadOnly) |
275 | 276 |
|
276 | | -// Update statements with indices from API response - use nil for empty to preserve user intention |
| 277 | +// Update statements with indices from API response |
277 | 278 | 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 | +} |
279 | 290 | } else { |
280 | 291 | data.Statements = statementsSliceToList(ctx, restInfo.Statements, &resp.Diagnostics) |
281 | 292 | } |
@@ -339,9 +350,17 @@ func (r *ProtocolsS3PolicyResource) Read(ctx context.Context, req resource.ReadR |
339 | 350 |
|
340 | 351 | data.SVMName = types.StringValue(restInfo.SVM.Name) |
341 | 352 |
|
342 | | -// Convert statements - handle empty array as nil for consistency |
| 353 | +// Convert statements - preserve null vs empty distinction from current state |
343 | 354 | 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 | +} |
345 | 364 | } else { |
346 | 365 | data.Statements = statementsSliceToList(ctx, restInfo.Statements, &resp.Diagnostics) |
347 | 366 | } |
@@ -460,9 +479,19 @@ func (r *ProtocolsS3PolicyResource) Update(ctx context.Context, req resource.Upd |
460 | 479 | // Set read_only from API response |
461 | 480 | plan.ReadOnly = types.BoolValue(restInfo.ReadOnly) |
462 | 481 |
|
463 | | -// Update statements with indices from API response - use nil for empty to preserve user intention |
| 482 | +// Update statements with indices from API response |
464 | 483 | 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 | +} |
466 | 495 | } else { |
467 | 496 | plan.Statements = statementsSliceToList(ctx, restInfo.Statements, &resp.Diagnostics) |
468 | 497 | } |
|
0 commit comments