From cc76abb78872762f6dae1a31da6e236fdd981e13 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Thu, 30 May 2024 15:40:46 +0300 Subject: [PATCH] Add config.Provider.TraverseTFSchemas to traverse the Terraform schemas of all the resources of a Provider. Signed-off-by: Alper Rifat Ulucinar --- pkg/config/provider.go | 2 +- pkg/config/schema_conversions.go | 17 +++++++++++++++-- pkg/config/schema_conversions_test.go | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/config/provider.go b/pkg/config/provider.go index 1bb66c42..d3642522 100644 --- a/pkg/config/provider.go +++ b/pkg/config/provider.go @@ -373,7 +373,7 @@ func NewProvider(schema []byte, prefix string, modulePath string, metadata []byt p.Resources[name].useTerraformPluginFrameworkClient = isPluginFrameworkResource // traverse the Terraform resource schema to initialize the upjet Resource // configurations - if err := TraverseSchemas(name, terraformResource, p.Resources[name], p.schemaTraversers...); err != nil { + if err := TraverseSchemas(name, p.Resources[name], p.schemaTraversers...); err != nil { panic(errors.Wrap(err, "failed to execute the Terraform schema traverser chain")) } } diff --git a/pkg/config/schema_conversions.go b/pkg/config/schema_conversions.go index 47aa94af..77f429b5 100644 --- a/pkg/config/schema_conversions.go +++ b/pkg/config/schema_conversions.go @@ -6,6 +6,7 @@ package config import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/pkg/errors" "github.com/crossplane/upjet/pkg/schema/traverser" ) @@ -17,11 +18,23 @@ type ResourceSetter interface { SetResource(r *Resource) } +// TraverseTFSchemas traverses the Terraform schemas of all the resources of +// the Provider `p` using the specified visitors. Reports any errors +// encountered. +func (p *Provider) TraverseTFSchemas(visitors ...traverser.SchemaTraverser) error { + for name, cfg := range p.Resources { + if err := TraverseSchemas(name, cfg, visitors...); err != nil { + return errors.Wrapf(err, "failed to traverse the schema of the Terraform resource with name %q", name) + } + } + return nil +} + // TraverseSchemas visits the specified schema belonging to the Terraform // resource with the given name and given upjet resource configuration using // the specified visitors. If any visitors report an error, traversal is // stopped and the error is reported to the caller. -func TraverseSchemas(tfName string, tfResource *schema.Resource, r *Resource, visitors ...traverser.SchemaTraverser) error { +func TraverseSchemas(tfName string, r *Resource, visitors ...traverser.SchemaTraverser) error { // set the upjet Resource configuration as context for the visitors that // satisfy the ResourceSetter interface. for _, v := range visitors { @@ -29,7 +42,7 @@ func TraverseSchemas(tfName string, tfResource *schema.Resource, r *Resource, vi rs.SetResource(r) } } - return traverser.Traverse(tfName, tfResource, visitors...) + return traverser.Traverse(tfName, r.TerraformResource, visitors...) } type resourceContext struct { diff --git a/pkg/config/schema_conversions_test.go b/pkg/config/schema_conversions_test.go index 698ca2c9..ccfe6518 100644 --- a/pkg/config/schema_conversions_test.go +++ b/pkg/config/schema_conversions_test.go @@ -155,7 +155,7 @@ func TestSingletonListEmbedder(t *testing.T) { t.Run(n, func(t *testing.T) { e := &SingletonListEmbedder{} r := DefaultResource(tt.args.name, tt.args.resource, nil, nil) - err := TraverseSchemas(tt.args.name, tt.args.resource, r, e) + err := TraverseSchemas(tt.args.name, r, e) if diff := cmp.Diff(tt.want.err, err, test.EquateErrors()); diff != "" { t.Fatalf("\n%s\ntraverseSchemas(name, schema, ...): -wantErr, +gotErr:\n%s", tt.reason, diff) }