|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
1 | 4 | package fwserver |
2 | 5 |
|
3 | 6 | import ( |
@@ -71,13 +74,40 @@ func proposedNew(ctx context.Context, s fwschema.Schema, path *tftypes.Attribute |
71 | 74 | } |
72 | 75 |
|
73 | 76 | for name, blockType := range s.GetBlocks() { |
74 | | - attrVal, _ := prior.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name)) |
75 | | - priorVal := attrVal.(tftypes.Value) |
| 77 | + attrVal, err := prior.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name)) |
| 78 | + if err != nil { |
| 79 | + fwPath, fwPathDiags := fromtftypes.AttributePath(ctx, path, s) |
| 80 | + diags.Append(fwPathDiags...) |
| 81 | + |
| 82 | + diags.Append(diag.NewAttributeErrorDiagnostic(fwPath, |
| 83 | + "Invalid Prior State Attribute Path", |
| 84 | + "An unexpected error occurred while trying to retrieve a value from prior state. "+ |
| 85 | + "This is an error in terraform-plugin-framework used by the provider. "+ |
| 86 | + "Please report the following to the provider developers.\n\n"+ |
| 87 | + fmt.Sprintf("Original Error: %s", err), |
| 88 | + )) |
| 89 | + return tftypes.Value{}, diags |
| 90 | + } |
| 91 | + |
| 92 | + priorVal := attrVal.(tftypes.Value) //nolint |
| 93 | + |
| 94 | + attrVal, err = config.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name)) |
| 95 | + if err != nil { |
| 96 | + fwPath, fwPathDiags := fromtftypes.AttributePath(ctx, path, s) |
| 97 | + diags.Append(fwPathDiags...) |
76 | 98 |
|
77 | | - attrVal, _ = config.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name)) |
78 | | - configVal := attrVal.(tftypes.Value) |
| 99 | + diags.Append(diag.NewAttributeErrorDiagnostic(fwPath, |
| 100 | + "Invalid Config Attribute Path", |
| 101 | + "An unexpected error occurred while trying to retrieve a value from config. "+ |
| 102 | + "This is an error in terraform-plugin-framework used by the provider. "+ |
| 103 | + "Please report the following to the provider developers.\n\n"+ |
| 104 | + fmt.Sprintf("Original Error: %s", err), |
| 105 | + )) |
| 106 | + return tftypes.Value{}, diags |
| 107 | + } |
| 108 | + configVal := attrVal.(tftypes.Value) //nolint |
79 | 109 |
|
80 | | - nestedBlockDiags := diag.Diagnostics{} |
| 110 | + nestedBlockDiags := diag.Diagnostics{} //nolint |
81 | 111 | newAttrs[name], nestedBlockDiags = proposeNewNestedBlock(ctx, s, blockType, path.WithAttributeName(name), priorVal, configVal) |
82 | 112 | diags.Append(nestedBlockDiags...) |
83 | 113 | if diags.HasError() { |
@@ -242,7 +272,7 @@ func proposedNewAttributes(ctx context.Context, s fwschema.Schema, attrs map[str |
242 | 272 | newVal = configVal |
243 | 273 | } |
244 | 274 | } else if nestedAttr, isNested := attr.(fwschema.NestedAttribute); isNested { |
245 | | - nestedAttrDiags := diag.Diagnostics{} |
| 275 | + nestedAttrDiags := diag.Diagnostics{} //nolint |
246 | 276 |
|
247 | 277 | newVal, nestedAttrDiags = proposeNewNestedAttribute(ctx, s, nestedAttr, attrPath, priorVal, configVal) |
248 | 278 | diags.Append(nestedAttrDiags...) |
@@ -274,28 +304,28 @@ func proposeNewNestedAttribute(ctx context.Context, s fwschema.Schema, attr fwsc |
274 | 304 | if config.IsNull() { |
275 | 305 | break |
276 | 306 | } |
277 | | - nestedDiags := diag.Diagnostics{} |
| 307 | + nestedDiags := diag.Diagnostics{} //nolint |
278 | 308 | newVal, nestedDiags = proposedNewNestedObjectAttributes(ctx, s, attr, path, prior, config) |
279 | 309 | diags.Append(nestedDiags...) |
280 | 310 | if nestedDiags.HasError() { |
281 | 311 | return tftypes.Value{}, diags |
282 | 312 | } |
283 | 313 | case fwschema.NestingModeList: |
284 | | - nestedDiags := diag.Diagnostics{} |
| 314 | + nestedDiags := diag.Diagnostics{} //nolint |
285 | 315 | newVal, nestedDiags = proposedNewListNested(ctx, s, attr, path, prior, config) |
286 | 316 | diags.Append(nestedDiags...) |
287 | 317 | if nestedDiags.HasError() { |
288 | 318 | return tftypes.Value{}, diags |
289 | 319 | } |
290 | 320 | case fwschema.NestingModeMap: |
291 | | - nestedDiags := diag.Diagnostics{} |
| 321 | + nestedDiags := diag.Diagnostics{} //nolint |
292 | 322 | newVal, nestedDiags = proposedNewMapNested(ctx, s, attr, path, prior, config) |
293 | 323 | diags.Append(nestedDiags...) |
294 | 324 | if nestedDiags.HasError() { |
295 | 325 | return tftypes.Value{}, diags |
296 | 326 | } |
297 | 327 | case fwschema.NestingModeSet: |
298 | | - nestedDiags := diag.Diagnostics{} |
| 328 | + nestedDiags := diag.Diagnostics{} //nolint |
299 | 329 | newVal, nestedDiags = proposedNewSetNested(ctx, s, attr, path, prior, config) |
300 | 330 | diags.Append(nestedDiags...) |
301 | 331 | if nestedDiags.HasError() { |
@@ -406,7 +436,7 @@ func proposedNewMapNested(ctx context.Context, s fwschema.Schema, attr fwschema. |
406 | 436 | } |
407 | 437 | } |
408 | 438 |
|
409 | | - nestedDiags := diag.Diagnostics{} |
| 439 | + nestedDiags := diag.Diagnostics{} //nolint |
410 | 440 | newVals[name], nestedDiags = proposedNewNestedObjectAttributes(ctx, s, attr, path.WithElementKeyString(name), priorEV, configEV) |
411 | 441 | diags.Append(nestedDiags...) |
412 | 442 | if diags.HasError() { |
@@ -685,21 +715,21 @@ func proposeNewNestedBlock(ctx context.Context, s fwschema.Schema, block fwschem |
685 | 715 | if config.IsNull() { |
686 | 716 | break |
687 | 717 | } |
688 | | - blockDiags := diag.Diagnostics{} |
| 718 | + blockDiags := diag.Diagnostics{} //nolint |
689 | 719 | newVal, blockDiags = proposedNewNestedBlockObjectAttributes(ctx, s, block, path, prior, config) |
690 | 720 | diags.Append(blockDiags...) |
691 | 721 | if blockDiags.HasError() { |
692 | 722 | return tftypes.Value{}, diags |
693 | 723 | } |
694 | 724 | case fwschema.BlockNestingModeList: |
695 | | - blockDiags := diag.Diagnostics{} |
| 725 | + blockDiags := diag.Diagnostics{} //nolint |
696 | 726 | newVal, blockDiags = proposedNewBlockListNested(ctx, s, block, path, prior, config) |
697 | 727 | diags.Append(blockDiags...) |
698 | 728 | if blockDiags.HasError() { |
699 | 729 | return tftypes.Value{}, diags |
700 | 730 | } |
701 | 731 | case fwschema.BlockNestingModeSet: |
702 | | - blockDiags := diag.Diagnostics{} |
| 732 | + blockDiags := diag.Diagnostics{} //nolint |
703 | 733 | newVal, blockDiags = proposedNewBlockSetNested(ctx, s, block, path, prior, config) |
704 | 734 | diags.Append(blockDiags...) |
705 | 735 | if blockDiags.HasError() { |
@@ -748,12 +778,25 @@ func proposedNewNestedBlockObjectAttributes(ctx context.Context, s fwschema.Sche |
748 | 778 | )) |
749 | 779 | return tftypes.Value{}, diags |
750 | 780 | } |
751 | | - priorVal := attrVal.(tftypes.Value) |
| 781 | + priorVal := attrVal.(tftypes.Value) //nolint |
752 | 782 |
|
753 | | - attrVal, _ = config.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name)) |
754 | | - configVal := attrVal.(tftypes.Value) |
| 783 | + attrVal, err = config.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name)) |
| 784 | + if err != nil { |
| 785 | + fwPath, fwPathDiags := fromtftypes.AttributePath(ctx, path, s) |
| 786 | + diags.Append(fwPathDiags...) |
| 787 | + |
| 788 | + diags.Append(diag.NewAttributeErrorDiagnostic(fwPath, |
| 789 | + "Invalid Config Attribute Path", |
| 790 | + "An unexpected error occurred while trying to retrieve a value from config. "+ |
| 791 | + "This is an error in terraform-plugin-framework used by the provider. "+ |
| 792 | + "Please report the following to the provider developers.\n\n"+ |
| 793 | + fmt.Sprintf("Original Error: %s", err), |
| 794 | + )) |
| 795 | + return tftypes.Value{}, diags |
| 796 | + } |
| 797 | + configVal := attrVal.(tftypes.Value) //nolint |
755 | 798 |
|
756 | | - nestedBlockDiags := diag.Diagnostics{} |
| 799 | + nestedBlockDiags := diag.Diagnostics{} //nolint |
757 | 800 | valuesMap[name], nestedBlockDiags = proposeNewNestedBlock(ctx, s, blockType, tftypes.NewAttributePath().WithAttributeName(name).WithElementKeyInt(0), priorVal, configVal) |
758 | 801 | diags.Append(nestedBlockDiags...) |
759 | 802 | if diags.HasError() { |
@@ -1037,9 +1080,9 @@ func proposeNewNestedBlockObject(ctx context.Context, s fwschema.Schema, nestedB |
1037 | 1080 | } |
1038 | 1081 |
|
1039 | 1082 | attrVal, _ := config.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name)) |
1040 | | - configVal := attrVal.(tftypes.Value) |
| 1083 | + configVal := attrVal.(tftypes.Value) //nolint |
1041 | 1084 |
|
1042 | | - nestedBlockDiags := diag.Diagnostics{} |
| 1085 | + nestedBlockDiags := diag.Diagnostics{} //nolint |
1043 | 1086 | valuesMap[name], nestedBlockDiags = proposeNewNestedBlock(ctx, s, blockType, path.WithAttributeName(name), priorVal, configVal) |
1044 | 1087 | diags.Append(nestedBlockDiags...) |
1045 | 1088 | if nestedBlockDiags.HasError() { |
@@ -1167,7 +1210,7 @@ func validPriorFromConfig(ctx context.Context, s fwschema.Schema, absPath *tftyp |
1167 | 1210 | valid = false |
1168 | 1211 | return false, stop |
1169 | 1212 | } |
1170 | | - configV := configIface.(tftypes.Value) |
| 1213 | + configV := configIface.(tftypes.Value) //nolint |
1171 | 1214 |
|
1172 | 1215 | // we don't need to know the schema if both are equal |
1173 | 1216 | if configV.Equal(priorV) { |
|
0 commit comments