-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make singleton-list-to-embedded-object API conversions optional
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
- Loading branch information
Showing
9 changed files
with
102 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/crossplane/upjet/pkg/schema/traverser" | ||
) | ||
|
||
var _ ResourceSetter = &SingletonListEmbedder{} | ||
|
||
// ResourceSetter allows the context Resource to be set for a traverser. | ||
type ResourceSetter interface { | ||
SetResource(r *Resource) | ||
} | ||
|
||
func traverseSchemas(tfName string, tfResource *schema.Resource, 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...) | ||
} | ||
|
||
type resourceContext struct { | ||
r *Resource | ||
} | ||
|
||
func (rc *resourceContext) SetResource(r *Resource) { | ||
rc.r = r | ||
} | ||
|
||
// SingletonListEmbedder is a schema traverser for embedding singleton lists | ||
// in the Terraform schema as objects. | ||
type SingletonListEmbedder struct { | ||
resourceContext | ||
traverser.NoopTraverser | ||
} | ||
|
||
func (l *SingletonListEmbedder) VisitResource(r *traverser.ResourceNode) error { | ||
// this visitor only works on sets and lists with the MaxItems constraint | ||
// of 1. | ||
if r.Schema.Type != schema.TypeList && r.Schema.Type != schema.TypeSet { | ||
return nil | ||
} | ||
if r.Schema.MaxItems != 1 { | ||
return nil | ||
} | ||
l.r.AddSingletonListConversion(traverser.FieldPathWithWildcard(r.TFPath)) | ||
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
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