Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default values to attributes to documentation #65

Open
mavogel opened this issue May 17, 2021 · 4 comments
Open

feat: add default values to attributes to documentation #65

mavogel opened this issue May 17, 2021 · 4 comments

Comments

@mavogel
Copy link

mavogel commented May 17, 2021

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

  • terraform-plugin-docs: v0.4.0
  • terraform-plugin-sdk/v2 v2.6.1
@mavogel mavogel changed the title feat: add default and optional/required to attributes to documentation feat: add default values to attributes to documentation May 17, 2021
@jacobbednarz
Copy link
Contributor

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
Copy link

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

@jacobbednarz
Copy link
Contributor

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

@maksym-nazarenko
Copy link

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"),
				},
			},
		},
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants