Skip to content
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

Add config.Resource.RemoveSingletonListConversion #411

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,28 @@ func (r *Resource) AddSingletonListConversion(tfPath, crdPath string) {
r.listConversionPaths[tfPath] = crdPath
}

// RemoveSingletonListConversion removes the singleton list conversion
// for the specified Terraform configuration path. Also unsets the path's
// embedding mode. The specified fieldpath expression must be a Terraform
// field path with or without the wildcard segments. Returns true if
// the path has already been registered for singleton list conversion.
func (r *Resource) RemoveSingletonListConversion(tfPath string) bool {
nPath := strings.ReplaceAll(tfPath, "[*]", "")
nPath = strings.ReplaceAll(nPath, "[0]", "")
for p := range r.listConversionPaths {
n := strings.ReplaceAll(p, "[*]", "")
n = strings.ReplaceAll(n, "[0]", "")
if n == nPath {
delete(r.listConversionPaths, p)
if r.SchemaElementOptions[n] != nil {
r.SchemaElementOptions[n].EmbeddedObject = false
}
return true
}
}
return false
}

// SetEmbeddedObject sets the EmbeddedObject for the specified key.
// The key is a Terraform field path without the wildcard segments.
func (m SchemaElementOptions) SetEmbeddedObject(el string) {
Expand Down
24 changes: 22 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,15 +18,34 @@ type ResourceSetter interface {
SetResource(r *Resource)
}

func traverseSchemas(tfName string, tfResource *schema.Resource, r *Resource, visitors ...traverser.SchemaTraverser) error {
// ResourceSchema represents a provider's resource schema.
type ResourceSchema map[string]*Resource

// TraverseTFSchemas traverses the Terraform schemas of all the resources of
// the Provider `p` using the specified visitors. Reports any errors
// encountered.
func (s ResourceSchema) TraverseTFSchemas(visitors ...traverser.SchemaTraverser) error {
for name, cfg := range s {
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, 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
58 changes: 58 additions & 0 deletions pkg/schema/traverser/access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0

package traverser

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
)

// SchemaAccessor is a function that accesses and potentially modifies a
// Terraform schema.
type SchemaAccessor func(*schema.Schema) error

// AccessSchema accesses the schema element at the specified path and calls
// the given schema accessors in order. Reports any errors encountered by
// the accessors. The terminal node at the end of the specified path must be
// a *schema.Resource or an error will be reported. The specified path must
// have at least one component.
func AccessSchema(sch any, path []string, accessors ...SchemaAccessor) error { //nolint:gocyclo // easier to follow the flow
if len(path) == 0 {
return errors.New("empty path specified while accessing the Terraform resource schema")
}
k := path[0]
path = path[1:]
switch s := sch.(type) {
case *schema.Schema:
if len(path) == 0 {
return errors.Errorf("terminal node at key %q is a *schema.Schema", k)
}
if k != wildcard {
return errors.Errorf("expecting a wildcard key but encountered the key %q", k)
}
if err := AccessSchema(s.Elem, path, accessors...); err != nil {
return err
}
case *schema.Resource:
c := s.Schema[k]
if c == nil {
return errors.Errorf("schema element for key %q is nil", k)
}
if len(path) == 0 {
for _, a := range accessors {
if err := a(c); err != nil {
return errors.Wrapf(err, "failed to call the accessor function on the schema element at key %q", k)
}
}
return nil
}
if err := AccessSchema(c, path, accessors...); err != nil {
return err
}
default:
return errors.Errorf("unknown schema element type %T at key %q", s, k)
}
return nil
}
47 changes: 47 additions & 0 deletions pkg/schema/traverser/maxitemssync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0

package traverser

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
)

// maxItemsSync is a visitor to sync the MaxItems constraints from the
// Go schema to the JSON schema. We've observed that some MaxItems constraints
// in the JSON schemas are not set where the corresponding MaxItems constraints
// in the Go schemas are set to 1. This inconsistency results in some singleton
// lists not being properly converted in the MR API whereas at runtime we may
// try to invoke the corresponding Terraform conversion functions. This
// traverser can mitigate such inconsistencies by syncing the MaxItems
// constraints from the Go schema to the JSON schema.
type maxItemsSync struct {
NoopTraverser

jsonSchema TFResourceSchema
}

// NewMaxItemsSync returns a new schema traverser capable of
// syncing the MaxItems constraints from the Go schema to the specified JSON
// schema. This will ensure the generation time and the runtime schema MaxItems
// constraints will be consistent. This is needed only if the generation time
// and runtime schemas differ.
func NewMaxItemsSync(s TFResourceSchema) SchemaTraverser {
return &maxItemsSync{
jsonSchema: s,
}
}

func (m *maxItemsSync) VisitResource(r *ResourceNode) error {
// this visitor only works on singleton lists
if (r.Schema.Type != schema.TypeList && r.Schema.Type != schema.TypeSet) || r.Schema.MaxItems != 1 {
return nil
}
return errors.Wrapf(AccessSchema(m.jsonSchema[r.TFName], r.TFPath,
func(s *schema.Schema) error {
s.MaxItems = 1
return nil
}), "failed to access the schema element at path %v for resource %q", r.TFPath, r.TFName)
}
14 changes: 14 additions & 0 deletions pkg/schema/traverser/traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,17 @@ func (n NoopTraverser) VisitSchema(*SchemaNode) error {
func (n NoopTraverser) VisitResource(*ResourceNode) error {
return nil
}

// TFResourceSchema represents a provider's Terraform resource schema.
type TFResourceSchema map[string]*schema.Resource

// TraverseTFSchemas traverses the receiver schema using the specified
// visitors. Reports any errors encountered by the visitors.
func (s TFResourceSchema) TraverseTFSchemas(visitors ...SchemaTraverser) error {
for n, r := range s {
if err := Traverse(n, r, visitors...); err != nil {
return errors.Wrapf(err, "failed to traverse the schema of the Terraform resource with name %q", n)
}
}
return nil
}
Loading