Skip to content

Commit c3b5b84

Browse files
authored
Relax resource identity validation (#36989)
* Remove identity change validation * Remove validation for null checks in identities
1 parent 92e515b commit c3b5b84

File tree

2 files changed

+10
-50
lines changed

2 files changed

+10
-50
lines changed

internal/terraform/context_plan_identity_test.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func TestContext2Plan_resource_identity_refresh(t *testing.T) {
153153
ExpectedError: fmt.Errorf("failed to upgrade resource identity: provider was unable to do so"),
154154
},
155155
"identity sent to provider differs from returned one": {
156+
// We don't throw an error here, because there are resource types with mutable identities
156157
StoredIdentitySchemaVersion: 0,
157158
StoredIdentityJSON: []byte(`{"id": "foo"}`),
158159
IdentitySchema: providers.IdentitySchema{
@@ -171,9 +172,8 @@ func TestContext2Plan_resource_identity_refresh(t *testing.T) {
171172
"id": cty.StringVal("bar"),
172173
}),
173174
ExpectedIdentity: cty.ObjectVal(map[string]cty.Value{
174-
"id": cty.StringVal("foo"),
175+
"id": cty.StringVal("bar"),
175176
}),
176-
ExpectedError: fmt.Errorf("Provider produced different identity: Provider \"registry.terraform.io/hashicorp/aws\" returned a different identity for aws_instance.web than the previously stored one. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker."),
177177
},
178178
"identity with unknowns": {
179179
IdentitySchema: providers.IdentitySchema{
@@ -446,12 +446,13 @@ func TestContext2Plan_resource_identity_plan(t *testing.T) {
446446
},
447447
},
448448
"create - null planned identity schema": {
449+
// We allow null values in identities
449450
plannedIdentity: cty.ObjectVal(map[string]cty.Value{
450451
"id": cty.NullVal(cty.String),
451452
}),
452-
expectDiagnostics: tfdiags.Diagnostics{
453-
tfdiags.Sourceless(tfdiags.Error, "Provider produced an identity that doesn't match the schema", "Provider \"registry.terraform.io/hashicorp/test\" returned an identity for test_resource.test that doesn't match the identity schema: attributes \"id\" are required and must not be null. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker."),
454-
},
453+
expectedIdentity: cty.ObjectVal(map[string]cty.Value{
454+
"id": cty.NullVal(cty.String),
455+
}),
455456
},
456457
"update": {
457458
prevRunState: states.BuildState(func(s *states.SyncState) {
@@ -536,6 +537,7 @@ func TestContext2Plan_resource_identity_plan(t *testing.T) {
536537
},
537538

538539
"update - changing identity": {
540+
// We don't throw an error here, because there are resource types with mutable identities
539541
prevRunState: states.BuildState(func(s *states.SyncState) {
540542
s.SetResourceInstanceCurrent(
541543
addrs.Resource{
@@ -559,9 +561,9 @@ func TestContext2Plan_resource_identity_plan(t *testing.T) {
559561
"id": cty.StringVal("foo"),
560562
}),
561563

562-
expectDiagnostics: tfdiags.Diagnostics{
563-
tfdiags.Sourceless(tfdiags.Error, "Provider produced different identity", "Provider \"registry.terraform.io/hashicorp/test\" returned a different identity for test_resource.test than the previously stored one. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker."),
564-
},
564+
expectedIdentity: cty.ObjectVal(map[string]cty.Value{
565+
"id": cty.StringVal("foo"),
566+
}),
565567
},
566568

567569
"update - updating identity schema version": {

internal/terraform/node_resource_abstract_instance.go

-42
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,6 @@ func (n *NodeAbstractResourceInstance) refresh(ctx EvalContext, deposedKey state
671671
if !resp.Identity.IsNull() {
672672
diags = diags.Append(n.validateIdentityKnown(resp.Identity))
673673
diags = diags.Append(n.validateIdentity(resp.Identity, schema.Identity))
674-
diags = diags.Append(n.validateIdentityDidNotChange(state, resp.Identity))
675674
}
676675
if resp.Deferred != nil {
677676
deferred = resp.Deferred
@@ -1122,10 +1121,6 @@ func (n *NodeAbstractResourceInstance) plan(
11221121
if !plannedIdentity.IsNull() {
11231122
if !action.IsReplace() && action != plans.Create {
11241123
diags = diags.Append(n.validateIdentityKnown(plannedIdentity))
1125-
// If the identity is not known we can not validate it did not change
1126-
if !diags.HasErrors() {
1127-
diags = diags.Append(n.validateIdentityDidNotChange(currentState, plannedIdentity))
1128-
}
11291124
}
11301125

11311126
diags = diags.Append(n.validateIdentity(plannedIdentity, schema.Identity))
@@ -2648,9 +2643,6 @@ func (n *NodeAbstractResourceInstance) apply(
26482643
if !resp.NewIdentity.IsNull() {
26492644
diags = diags.Append(n.validateIdentityKnown(resp.NewIdentity))
26502645
diags = diags.Append(n.validateIdentity(resp.NewIdentity, schema.Identity))
2651-
if !change.Action.IsReplace() {
2652-
diags = diags.Append(n.validateIdentityDidNotChange(state, resp.NewIdentity))
2653-
}
26542646
}
26552647
}
26562648
applyDiags := resp.Diagnostics
@@ -2916,21 +2908,6 @@ func (n *NodeAbstractResourceInstance) validateIdentityKnown(newIdentity cty.Val
29162908
return diags
29172909
}
29182910

2919-
func (n *NodeAbstractResourceInstance) validateIdentityDidNotChange(state *states.ResourceInstanceObject, newIdentity cty.Value) (diags tfdiags.Diagnostics) {
2920-
if state != nil && !state.Identity.IsNull() && state.Identity.Equals(newIdentity).False() {
2921-
diags = diags.Append(tfdiags.Sourceless(
2922-
tfdiags.Error,
2923-
"Provider produced different identity",
2924-
fmt.Sprintf(
2925-
"Provider %q returned a different identity for %s than the previously stored one. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker.",
2926-
n.ResolvedProvider.Provider, n.Addr,
2927-
),
2928-
))
2929-
}
2930-
2931-
return diags
2932-
}
2933-
29342911
func (n *NodeAbstractResourceInstance) validateIdentity(newIdentity cty.Value, identitySchema *configschema.Object) (diags tfdiags.Diagnostics) {
29352912
if _, marks := newIdentity.UnmarkDeep(); len(marks) > 0 {
29362913
diags = diags.Append(tfdiags.Sourceless(
@@ -2972,25 +2949,6 @@ func (n *NodeAbstractResourceInstance) validateIdentity(newIdentity cty.Value, i
29722949
return diags
29732950
}
29742951

2975-
// Check for required attributes
2976-
names := make([]string, 0, len(identitySchema.Attributes))
2977-
for name, attrS := range identitySchema.Attributes {
2978-
if attrS.Required && newIdentity.GetAttr(name).IsNull() {
2979-
names = append(names, name)
2980-
}
2981-
}
2982-
if len(names) > 0 {
2983-
diags = diags.Append(tfdiags.Sourceless(
2984-
tfdiags.Error,
2985-
"Provider produced an identity that doesn't match the schema",
2986-
fmt.Sprintf(
2987-
"Provider %q returned an identity for %s that doesn't match the identity schema: attributes %q are required and must not be null. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker.",
2988-
n.ResolvedProvider.Provider, n.Addr,
2989-
strings.Join(names, ", "),
2990-
),
2991-
))
2992-
}
2993-
29942952
return diags
29952953
}
29962954

0 commit comments

Comments
 (0)