Skip to content

Commit

Permalink
Add config.Provider.TraverseTFSchemas to traverse the Terraform schem…
Browse files Browse the repository at this point in the history
…as of

all the resources of a Provider.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed May 30, 2024
1 parent 5265292 commit cc76abb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
Expand Down
17 changes: 15 additions & 2 deletions pkg/config/schema_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -17,19 +18,31 @@ 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 {
if rs, ok := v.(ResourceSetter); ok {
rs.SetResource(r)
}
}
return traverser.Traverse(tfName, tfResource, visitors...)
return traverser.Traverse(tfName, r.TerraformResource, visitors...)
}

type resourceContext struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/schema_conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit cc76abb

Please sign in to comment.