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

Conversation

ulucinar
Copy link
Collaborator

@ulucinar ulucinar commented May 30, 2024

Description of your changes

For Terraform resources with single nested configuration blocks, we had previously introduced the config.Resource.SetEmbeddedObject that configures the field at a specified path as an embedded object rather than a singleton list. Such resources (an example is SecurityConfig.opensearchserverless's samlOptions field in crossplane-contrrib/provider-upjet-aws) already embedded nested blocks as objects despite their schemas are declared as singleton lists. Unfortunatelly, this nesting information is not immediately available in the JSON schema and this is also the reason we have Resource.SetEmbeddedObject in the very first place.

For such fields, we need to prevent the singleton list to embedded object conversion at the Crossplane level because that singleton list must have already been generated as an embedded object. This is because Terraform also expects a single nested block as opposed to a list for such configuration arguments.

Currently, the schema traverser responsible for converting singleton lists to embedded objects treats them as regular singleton lists as what it processes is the JSON schema and we don't have the nested block information in that schema. This PR adds config.Resource.RemoveSingletonListConversion so that it becomes possible to override the decisions made by the singleton list conversion schema traverser.

An example usage is as follows:

	p.AddResourceConfigurator("aws_opensearchserverless_security_config", func(r *config.Resource) {
		r.RemoveSingletonListConversion("saml_options")
		// set the path saml_options as an embedded object to honor
		// its single nested block schema. We need to have it converted
		// into an embedded object but there's no need for
		// the Terraform conversion (it already needs to be treated
		// as an object at the Terraform layer and in the current MR API,
		// it's already an embedded object).
		r.SchemaElementOptions.SetEmbeddedObject("saml_options")
	})

This effectively prevents a new API version for the SecurityConfig.opensearchserverless, which would be identical (apart from the version number) to the current version and the invalid API converters installed between these two identical versions.

This PR also introduces traverser.TFResourceSchema.Traverse, which can be used to traverse the Terraform schemas of all the resources of a config.Provider. Another traverser, traverser.maxItemsSync can be used to sync the MaxItems constraints from a source schema to a target schema. An example usage is as follows:

if err := traverser.TFResourceSchema(sdkProvider.ResourcesMap).Traverse(traverser.NewMaxItemsSync(p.ResourcesMap)); err != nil {
			return nil, errors.Wrap(err, "cannot sync the MaxItems constraints between the Go schema and the JSON schema")
		}

In this example, we would like to sync the MaxItems constraints from the Go schemas represented with sdkProvider.ResourcesMap into the JSON schemas represented with p.ResourcesMap. For more details, please refer to crossplane-contrib/provider-upjet-gcp#537.

I have:

  • Read and followed Upjet's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Has been validated in the context of crossplane-contrib/provider-upjet-aws#1332.

already configured singleton list conversions.

- The main use case is to prevent singleton list conversions for
  configuration arguments with single nested blocks.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
…as of

all the resources of a Provider.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
constraints between the JSON & Go schemas.

- 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.
- This traverser can mitigate such inconsistencies.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
Copy link
Member

@sergenyalcin sergenyalcin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ulucinar LGTM!

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
@ulucinar ulucinar merged commit 37c7f4e into crossplane:main Jun 12, 2024
6 checks passed
@ulucinar ulucinar deleted the fix-embedded-conversion branch June 12, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants