crossplane / upjet

A code generation framework and runtime for Crossplane providers
Apache License 2.0
318 stars 89 forks source link

Add config.Resource.RemoveSingletonListConversion #411

Closed ulucinar closed 5 months ago

ulucinar commented 5 months ago

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 https://github.com/crossplane-contrib/provider-upjet-gcp/pull/537.

I have:

How has this code been tested

Has been validated in the context of https://github.com/crossplane-contrib/provider-upjet-aws/pull/1332.