resource/example: Add defaulted attribute (#136)

Reference: https://developer.hashicorp.com/terraform/plugin/framework/resources/default

New example attribute highlights terraform-plugin-framework v1.2.0+ `Default` functionality for resource attributes. While the scaffolding code is not meant to be exhaustive of all available framework functionality, this new built-in feature is important enough to highlight.
This commit is contained in:
Brian Flad 2023-03-22 08:27:52 -04:00 committed by GitHub
parent ac4a101c97
commit be04f08750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View File

@ -24,6 +24,7 @@ resource "scaffolding_example" "example" {
### Optional ### Optional
- `configurable_attribute` (String) Example configurable attribute - `configurable_attribute` (String) Example configurable attribute
- `defaulted` (String) Example configurable attribute with default value
### Read-Only ### Read-Only

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
@ -30,6 +31,7 @@ type ExampleResource struct {
// ExampleResourceModel describes the resource data model. // ExampleResourceModel describes the resource data model.
type ExampleResourceModel struct { type ExampleResourceModel struct {
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"` ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
Defaulted types.String `tfsdk:"defaulted"`
Id types.String `tfsdk:"id"` Id types.String `tfsdk:"id"`
} }
@ -47,6 +49,12 @@ func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest
MarkdownDescription: "Example configurable attribute", MarkdownDescription: "Example configurable attribute",
Optional: true, Optional: true,
}, },
"defaulted": schema.StringAttribute{
MarkdownDescription: "Example configurable attribute with default value",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("example value when not configured"),
},
"id": schema.StringAttribute{ "id": schema.StringAttribute{
Computed: true, Computed: true,
MarkdownDescription: "Example identifier", MarkdownDescription: "Example identifier",

View File

@ -17,6 +17,7 @@ func TestAccExampleResource(t *testing.T) {
Config: testAccExampleResourceConfig("one"), Config: testAccExampleResourceConfig("one"),
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "one"), resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "one"),
resource.TestCheckResourceAttr("scaffolding_example.test", "defaulted", "example value when not configured"),
resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"), resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"),
), ),
}, },
@ -29,7 +30,7 @@ func TestAccExampleResource(t *testing.T) {
// example code does not have an actual upstream service. // example code does not have an actual upstream service.
// Once the Read method is able to refresh information from // Once the Read method is able to refresh information from
// the upstream service, this can be removed. // the upstream service, this can be removed.
ImportStateVerifyIgnore: []string{"configurable_attribute"}, ImportStateVerifyIgnore: []string{"configurable_attribute", "defaulted"},
}, },
// Update and Read testing // Update and Read testing
{ {