crossplane / terrajet

Generate Crossplane Providers from any Terraform Provider
https://crossplane.io
Apache License 2.0
289 stars 38 forks source link

Error while generating google_compute_instance #134

Closed turkenh closed 2 years ago

turkenh commented 2 years ago

What happened?

I am trying to generate google_compute_instance in provider-tf-gcp but Terrajet failed with the following log:

panic: cannot generate crd for resource google_compute_instance: cannot build types for Instance: cannot build the types: cannot infer type from schema of field boot_disk: cannot infer type from resource schema of element type of Instance.BootDisk: cannot infer type from schema of field initialize_params: cannot infer type from resource schema of element type of Instance.BootDisk.InitializeParams: cannot infer type from schema of field labels: element type of Instance.BootDisk.InitializeParams.Labels should be either schema.Resource or schema.Schema

goroutine 1 [running]:
github.com/crossplane-contrib/terrajet/pkg/pipeline.Run(0xc000bc6000)
Add examples for providerconfig and bucket
    /Users/hasanturken/Workspace/crossplane-contrib/provider-tf-gcp/.work/pkg/pkg/mod/github.com/crossplane-contrib/terrajet@v0.1.1-0.20211104212137-874bb6ad5cff/pkg/pipeline/run.go:82 +0x2053
main.main()
    /Users/hasanturken/Workspace/crossplane-contrib/provider-tf-gcp/cmd/generator/main.go:10 +0x2a
exit status 2
make[1]: *** [terrajet.run] Error 1
make: *** [generate] Error 2

Debugger window at that point:

Screen Shot 2021-11-08 at 14 44 51

How can we reproduce it?

Try generating google_compute_instance in provider-tf-gcp

turkenh commented 2 years ago

We synced on this with @muvaf and agreed on fixing this at provider level for such resources using the existing configuration mechanism.

Example configurator for future reference:

    p.AddResourceConfigurator("google_compute_instance_from_template", func(r *config.Resource) {
        // Note(turkenh): We have to modify schema of
        // "boot_disk.initialize_params.labels", since it is a map where
        // elements configured as nil, defaulting to map[string]string:
        // https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance#nested_initialize_params
        r.TerraformResource.
            Schema["boot_disk"].Elem.(*schema.Resource).
            Schema["initialize_params"].Elem.(*schema.Resource).
            Schema["labels"].Elem = schema.TypeString

        r.TerraformResource.Schema["metadata"].Elem = schema.TypeString
    })