Skip to content

Commit

Permalink
Ensure all sdkv2 test providers have an id and an update method (#2723)
Browse files Browse the repository at this point in the history
This change tweaks the `EnsureProviderIsValid` method in `pulcheck` to
always give resources an `id` property and ensure they pass the TF
validation with respect to Update methods.

Previously we'd run into trouble with TF validation when a resource
needs an Update and doesn't have one or it doesn't need an Update and
has one. The rules are non-trivial so I've opted to just make all
resources need an Update and ensure they have one.
  • Loading branch information
VenelinMartinov authored Dec 13, 2024
1 parent 5b45441 commit 4842bf4
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pkg/internal/tests/pulcheck/pulcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func propNeedsUpdate(prop *schema.Schema) bool {
return true
}

// resourceNeedsUpdate returns true if the TF SDK schema validation would consider the
// resource to need an update method.
func resourceNeedsUpdate(res *schema.Resource) bool {
// If any of the properties need an update, then the resource needs an update.
for _, s := range res.Schema {
Expand All @@ -53,6 +55,26 @@ func resourceNeedsUpdate(res *schema.Resource) bool {
// This is an experimental API.
func EnsureProviderValid(t T, tfp *schema.Provider) {
for _, r := range tfp.ResourcesMap {
if r.Schema["id"] == nil {
r.Schema["id"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
}
}

// If the resource will be flagged as not needing an Update method by the TF SDK schema
// validation then add a no-op update_prop property that will instead force the resource
// to need an Update method.
// This is necessary to work around a bug in the TF SDK schema validation where some resources which
// do need an Update method will instead be flagged as not needing an Update method.
// See https://github.com/pulumi/pulumi-terraform-bridge/pull/2723#issuecomment-2541518646
if !resourceNeedsUpdate(r) {
r.Schema["update_prop"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
}
}

if r.ReadContext == nil {
r.ReadContext = func(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
Expand All @@ -75,7 +97,9 @@ func EnsureProviderValid(t T, tfp *schema.Provider) {
}
}

if resourceNeedsUpdate(r) && r.UpdateContext == nil {
// Because of the no-op update_prop property added above, all resources will now
// need an Update method.
if r.UpdateContext == nil {
r.UpdateContext = func(
ctx context.Context, rd *schema.ResourceData, i interface{},
) diag.Diagnostics {
Expand Down

0 comments on commit 4842bf4

Please sign in to comment.