-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Normalize Read result for empty collections #2069
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import ( | |
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge" | ||
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||
) | ||
|
@@ -254,13 +254,66 @@ func (p *planResourceChangeImpl) Refresh( | |
if rr.stateValue.IsNull() { | ||
return nil, nil | ||
} | ||
normStateValue := p.normalizeNullValues(ctx, res, c, rr.stateValue) | ||
return &v2InstanceState2{ | ||
resourceType: rr.resourceType, | ||
stateValue: rr.stateValue, | ||
stateValue: normStateValue, | ||
meta: rr.meta, | ||
}, nil | ||
} | ||
|
||
// Compensate for a TF problem causing spurious diffs between null and empty collections highlighted as dirty refresh in | ||
// Pulumi. When applying resource changes TF will call normalizeNullValues with apply=true which may erase the | ||
// distinction between empty and null collections. This makes state returned from Read differ from the state returned by | ||
// Create and Update and written to the state store. While the problem is present in both TF and Pulumi bridged | ||
// providers, it is worse in Pulumi because refresh is emitting a warning that something is changing. | ||
// | ||
// To compensate for this problem, this method corrects the Pulumi Read result during `pulumi refresh` to also erase | ||
// empty collections from the Read result if the old input is missing or null. | ||
// | ||
// This currently only handles top-level properties. | ||
// | ||
// See: https://github.com/pulumi/terraform-plugin-sdk/blob/upstream-v2.33.0/helper/schema/grpc_provider.go#L1514 | ||
func (p *planResourceChangeImpl) normalizeNullValues( | ||
ctx context.Context, | ||
res *schema.Resource, | ||
config shim.ResourceConfig, | ||
state cty.Value, | ||
) cty.Value { | ||
if config == nil { | ||
return state | ||
} | ||
sm := res.SchemaMap() | ||
m := state.AsValueMap() | ||
for tfName, v := range m { | ||
sch, ok := sm[tfName] | ||
if !ok { | ||
continue | ||
} | ||
t := sch.Type | ||
isCollection := t == schema.TypeList || t == schema.TypeMap || t == schema.TypeSet | ||
if !isCollection { | ||
continue | ||
} | ||
|
||
if _, ok := sch.Elem.(*schema.Resource); ok || sch.ConfigMode == schema.SchemaConfigModeBlock { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the best way to decide if it's a block or attr these days @VenelinMartinov There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can get the attributes of a resource from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like this: sch := res.CoreConfigSchema()
for key, attr := range sch.Attributes {
if !attr.Type.IsCollectionType() {
continue
}
if config.IsSet(key) {
continue
}
stateVal, ok := m[key]
if !ok {
continue
}
if !(stateVal.Type().IsCollectionType() && !stateVal.IsNull() && stateVal.LengthInt() == 0) {
continue
}
m[key] = cty.NullVal(attr.Type)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've continued this in #2065 |
||
continue | ||
} | ||
if config.IsSet(tfName) { | ||
continue | ||
} | ||
|
||
if !(v.Type().IsCollectionType() && !v.IsNull() && v.LengthInt() == 0) { | ||
continue | ||
} | ||
|
||
msg := fmt.Sprintf("normalizeNullValues: replacing %s=[] not found in config", tfName) | ||
tfbridge.GetLogger(ctx).Debug(msg) | ||
m[tfName] = cty.NullVal(v.Type()) | ||
} | ||
return cty.ObjectVal(m) | ||
} | ||
|
||
func (p *planResourceChangeImpl) NewDestroyDiff( | ||
ctx context.Context, t string, opts shim.TimeoutOptions, | ||
) shim.InstanceDiff { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We likely need to handle this for every block, right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like it, but I don't have an e2e example handy.