crossplane / terrajet

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

Nested status fields dirty fix #185

Closed vaspahomov closed 2 years ago

vaspahomov commented 2 years ago

Hello! I'm working on provider-jet-yc based on terraform-provider-yandex.

In our case there is fqdn field which is computed and should be Observation filed. Here stores FQDNs of hosts in cluster. We need this field in case building connection string.

fqdn field nested in host subresource and because #27 terrajet loses them.

This PostgresqlClusterHostObservation struct generated with fresh terrajet from main branch.

type PostgresqlClusterHostObservation struct {
    Fqdn *string `json:"fqdn,omitempty" tf:"fqdn,omitempty"`

    ReplicationSource *string `json:"replicationSource,omitempty" tf:"replication_source,omitempty"`

    Role *string `json:"role,omitempty" tf:"role,omitempty"`
}

But struct not present in main Observation:

type PostgresqlClusterObservation struct {
    CreatedAt *string `json:"createdAt,omitempty" tf:"created_at,omitempty"`

    Health *string `json:"health,omitempty" tf:"health,omitempty"`

    Status *string `json:"status,omitempty" tf:"status,omitempty"`
}

Description of your changes

I've made very dirty fix for #27 issue. Fix requires lot of changes in types/builder.go. And I've not found better solution then return multiple types from buildSchema and also wrap type with isObservation flag.

After fix I've generate apis and on postgresqlcluster resource I have:

type PostgresqlClusterObservation struct {
    Config []ConfigObservation `json:"config,omitempty" tf:"config,omitempty"`

    CreatedAt *string `json:"createdAt,omitempty" tf:"created_at,omitempty"`

    Database []PostgresqlClusterDatabaseObservation `json:"database,omitempty" tf:"database,omitempty"`

    Health *string `json:"health,omitempty" tf:"health,omitempty"`

    Host []PostgresqlClusterHostObservation `json:"host,omitempty" tf:"host,omitempty"`

    ID *string `json:"id,omitempty" tf:"id,omitempty"`

    MaintenanceWindow []PostgresqlClusterMaintenanceWindowObservation `json:"maintenanceWindow,omitempty" tf:"maintenance_window,omitempty"`

    Restore []RestoreObservation `json:"restore,omitempty" tf:"restore,omitempty"`

    Status *string `json:"status,omitempty" tf:"status,omitempty"`

    User []PostgresqlClusterUserObservation `json:"user,omitempty" tf:"user,omitempty"`
}

I have:

How has this code been tested

On my provider-jet-yc.

muvaf commented 2 years ago

Thanks @vaspahomov ! I'll review this next week. As a side note, what I had in mind for this issue was that we could possibly have a tool that takes the field path and type of the field, then create it in the status with that path, i.e. how mkdir -p works with directories. But I'm a bit rusty on this part of the codebase now, so I'll think about it more when I do the review. And totally open to other approaches as well.

muvaf commented 2 years ago

@vaspahomov The issue has been fixed in https://github.com/crossplane/terrajet/pull/230 together with an extensive refactor. Thanks for your PR!