Skip to content

Commit 6812355

Browse files
committed
Validate identity to match identity schema
1 parent 063757f commit 6812355

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

internal/terraform/node_resource_abstract_instance.go

+25
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ func (n *NodeAbstractResourceInstance) refresh(ctx EvalContext, deposedKey state
661661
diags = diags.Append(n.validateIdentityKnown(resp.Identity))
662662
diags = diags.Append(n.validateIdentity(resp.Identity))
663663
diags = diags.Append(n.validateIdentityDidNotChange(state, resp.Identity))
664+
diags = diags.Append(n.validateIdentityMatchesSchema(resp.Identity, schema.Identity))
664665
}
665666
if resp.Deferred != nil {
666667
deferred = resp.Deferred
@@ -1114,6 +1115,7 @@ func (n *NodeAbstractResourceInstance) plan(
11141115
// If the identity is not known we can not validate it did not change
11151116
if !diags.HasErrors() {
11161117
diags = diags.Append(n.validateIdentityDidNotChange(currentState, plannedIdentity))
1118+
diags = diags.Append(n.validateIdentityMatchesSchema(plannedIdentity, schema.Identity))
11171119
}
11181120
}
11191121

@@ -2637,6 +2639,7 @@ func (n *NodeAbstractResourceInstance) apply(
26372639
if !resp.NewIdentity.IsNull() {
26382640
diags = diags.Append(n.validateIdentityKnown(resp.NewIdentity))
26392641
diags = diags.Append(n.validateIdentity(resp.NewIdentity))
2642+
diags = diags.Append(n.validateIdentityMatchesSchema(resp.NewIdentity, schema.Identity))
26402643
if !change.Action.IsReplace() {
26412644
diags = diags.Append(n.validateIdentityDidNotChange(state, resp.NewIdentity))
26422645
}
@@ -2935,6 +2938,28 @@ func (n *NodeAbstractResourceInstance) validateIdentity(newIdentity cty.Value) (
29352938
return diags
29362939
}
29372940

2941+
func (n *NodeAbstractResourceInstance) validateIdentityMatchesSchema(newIdentity cty.Value, identitySchema *configschema.Object) (diags tfdiags.Diagnostics) {
2942+
if identitySchema == nil {
2943+
return diags
2944+
}
2945+
newType := newIdentity.Type()
2946+
currentType := identitySchema.ImpliedType()
2947+
if errs := newType.TestConformance(currentType); len(errs) > 0 {
2948+
for _, err := range errs {
2949+
diags = diags.Append(tfdiags.Sourceless(
2950+
tfdiags.Error,
2951+
"Provider produced an identity that doesn't match the schema",
2952+
fmt.Sprintf(
2953+
"Provider %q returned an identity for %s that doesn't match the identity schema: %s. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker.",
2954+
n.ResolvedProvider.Provider, n.Addr, tfdiags.FormatError(err),
2955+
),
2956+
))
2957+
}
2958+
}
2959+
2960+
return diags
2961+
}
2962+
29382963
func resourceInstancePrevRunAddr(ctx EvalContext, currentAddr addrs.AbsResourceInstance) addrs.AbsResourceInstance {
29392964
table := ctx.MoveResults()
29402965
return table.OldAddr(currentAddr)

0 commit comments

Comments
 (0)