forked from crossplane/upjet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request crossplane#411 from ulucinar/fix-embedded-conversion
Add config.Resource.RemoveSingletonListConversion
- Loading branch information
Showing
9 changed files
with
495 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.