hashicorp / terraform-plugin-docs

Generate and validate Terraform plugin/provider documentation.
Mozilla Public License 2.0
221 stars 69 forks source link

feat: add default values to attributes to documentation #65

Open mavogel opened 3 years ago

mavogel commented 3 years ago

Current behavior

Given the schema entry:

"rm": {
    Type:        schema.TypeBool,
    Description: "If true, then the container will be automatically removed after his execution.",
    Default:     false,
    Optional:    true,
},

the rendered output is

- **rm** (Boolean) If true, then the container will be automatically removed after his execution.

Desired behavior

I'd like to have Defaults to abc in the generation as well as follows:

- **rm** (Boolean) If true, then the container will be automatically removed after his execution. Defaults to `false`

Versions

jacobbednarz commented 2 years ago

While not official, you can achieve this with a workaround in your own provider. Check out https://github.com/cloudflare/terraform-provider-cloudflare/blob/master/internal/provider/provider.go#L23-L48 for an example.

wojciechwroblewski commented 1 year ago

@jacobbednarz Any chance that you have similar workaround for providers based on terraform-plugin-framework?

jacobbednarz commented 1 year ago

nothing that exists today. it has been requested at https://github.com/hashicorp/terraform-plugin-framework/issues/625 though if you'd like to track it.

maksym-nazarenko commented 1 year ago

hi, let me put my 2 cents here. In PluginFramework it is possible using custom types.

Here is a naive implementation for String Terraform core type (you will have to implement it for all types which are in use):

type stringDefault struct {
    schema.StringAttribute
}

func (a stringDefault) GetDescription() string {
    origDescription := a.StringAttribute.GetDescription()
    if a.Default == nil {
        return origDescription
    }

    resp := defaults.StringResponse{}
    a.Default.DefaultString(context.TODO(), defaults.StringRequest{}, &resp)
    origDescription = fmt.Sprintf(`%s Default: %s`, origDescription, resp.PlanValue)

    return origDescription
}

and then use this wrapper in Schema method of the resource:

func (s *bridgeVlan) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{
            "string_attribute": stringDefault{
                schema.StringAttribute{
                    Optional: true,
                    Computed: true,
                    Default:     stringdefault.StaticString("default value"),
                },
            },
        },
    }
}