Open shiyuhang0 opened 1 year ago
Hi @shiyuhang0 👋 Thank you for raising this feature request. This is something the maintainer team is actively investigating at the moment, so your timing is good.
Can you discuss more about what you desire in this area? Is your API defined by a specification, such as OpenAPI or another source?
Given your example, I think it would only be desirable to generate data models (e.g. Go struct types with tfsdk
field tags) from schemas and not the other way around, since schemas have much more cardinality beyond just type information that can be found in the current data model handling. Schemas will generally include other behaviors such as validation and plan modification. It is also unclear how tooling like this could differentiate between whether a schema should use attributes versus blocks and how nested levels of schemas would be signified without potentially extending field tags further.
Code generation should be able to work with a developer over time as modifications are needed, so if the other direction of model to schema were to be considered, then it might need a clearer workflow to ensure it can be updated over time without undoing customizations. I think this is where going from schema to data model is more applicable. The schema would be written with the desired structure/types/behaviors, then the generated data model Go types would not need customizations and it would be update-able over time without affecting other provider code.
All this being said though, we are generally looking at provider code generation from the lens that the source information may be coming from existing machine-readable data. Even if it was not, that a machine-readable format could be used to eliminate boilerplate Go coding. In that case, the "schema/data" information would be defined once, while both the schema code and data code would be generated from that single source.
Sorry, I missed this issue. As you mentioned above, I think:
Generate schema code and data model code from that single source
is the best solution if it's possible.As for me, Generating the schema code with name and type from the model is enough. Then I can add other options such as validation and plan modification without defining the whole scheme structure. In general, what I want is a schema code template including the basic information.
is it available for now ? I am also eager this feature.
@shufanhao you may find https://discuss.hashicorp.com/t/sdk-provider-development-anyone-ever-used-code-generation-or-other-tools-to-simplify-their-provider-development/20301/6 and the associated website documentation at https://developer.hashicorp.com/terraform/plugin/code-generation potentially helpful in this space.
Module version
Use-cases
When I want to develop a datasource or a resource. I need to define a model first.
Then define the corresponding schema.
Is there any tool to generate the schema according to the model. It is usually repeatable work.
Attempted Solutions
Provide something, for example, a terraform cli to help develop the provider
Proposal
References