hashicorp / terraform-plugin-framework

A next-generation framework for building Terraform providers.
https://developer.hashicorp.com/terraform/plugin/framework
Mozilla Public License 2.0
304 stars 93 forks source link

Provide something to generate schema automatically #642

Open shiyuhang0 opened 1 year ago

shiyuhang0 commented 1 year ago

Module version

v1.1.1

Use-cases

When I want to develop a datasource or a resource. I need to define a model first.

type ExampleResourceModel struct {
    ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
    Id                    types.String `tfsdk:"id"`
}

Then define the corresponding schema.

func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
    resp.Schema = schema.Schema{
        // This description is used by the documentation generator and the language server.
        MarkdownDescription: "Example resource",

        Attributes: map[string]schema.Attribute{
            "configurable_attribute": schema.StringAttribute{
                MarkdownDescription: "Example configurable attribute",
                Optional:            true,
            },
            "id": schema.StringAttribute{
                Computed:            true,
                MarkdownDescription: "Example identifier",
                PlanModifiers: []planmodifier.String{
                    stringplanmodifier.UseStateForUnknown(),
                },
            },
        },
    }
}

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

bflad commented 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.

shiyuhang0 commented 1 year ago

Sorry, I missed this issue. As you mentioned above, I think:

  1. Generate data models from schema also sounds great!
  2. 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.

shufanhao commented 7 months ago

is it available for now ? I am also eager this feature.

bflad commented 7 months ago

@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.