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

Reproducing a bug with nested sets, lists and computed values #89

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions internal/tf6muxprovider/provider1/nested_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package provider1

import (
"context"
"math/rand"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ resource.Resource = &resourceNested{}
)

func NewNestedResource() resource.Resource {
return &resourceNested{}
}

type resourceNested struct{}

func (r resourceNested) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_nested"
}

func (r resourceNested) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"id": {
Type: types.StringType,
Computed: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.UseStateForUnknown(),
},
},
},
Blocks: map[string]tfsdk.Block{
"set": {
Attributes: map[string]tfsdk.Attribute{
"id": {
Type: types.StringType,
Computed: true,
Optional: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.UseStateForUnknown(),
},
},
},
Blocks: map[string]tfsdk.Block{
"list": {
Attributes: map[string]tfsdk.Attribute{
"id": {
Type: types.StringType,
Computed: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.UseStateForUnknown(),
},
},
},
NestingMode: tfsdk.BlockNestingModeList,
},
},
NestingMode: tfsdk.BlockNestingModeSet,
},
},
}, nil
}

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func id(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

type nested struct {
Id types.String `tfsdk:"id"`
NestedItem []*NestedItem `tfsdk:"set"`
}

type NestedItem struct {
Id types.String `tfsdk:"id"`
NestedNestedItem []*NestedNestedItem `tfsdk:"list"`
}

type NestedNestedItem struct {
Id types.String `tfsdk:"id"`
}

func (r resourceNested) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
nested := nested{}
diags := req.Plan.Get(ctx, &nested)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}

if nested.Id.IsUnknown() {
nested.Id = types.String{Value: id(8)}
}

for _, nst := range nested.NestedItem {
if nst.Id.IsUnknown() {
nst.Id = types.String{Value: id(8)}
}

for _, nstnst := range nst.NestedNestedItem {
nstnst.Id = types.String{Value: id(8)}
}
}

diags = resp.State.Set(ctx, nested)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
}

func (r resourceNested) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
resp.State.Set(ctx, req.State.Raw)
}

func (r resourceNested) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.State.Set(ctx, req.Plan.Raw)
}

func (r resourceNested) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
resp.State.RemoveResource(ctx)
}

func (r resourceNested) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
41 changes: 41 additions & 0 deletions internal/tf6muxprovider/provider1/nested_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package provider1

import (
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
)

func TestAccResourceNested(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"tf6muxprovider": providerserver.NewProtocol6WithError(New()),
},
Steps: []resource.TestStep{
{
Config: configResourceNestedBasic,
},
},
})
}

const configResourceNestedBasic = `
resource "tf6muxprovider_nested" "example" {
set {
id = "one"

list {

}

list {

}
}

set {

}
}
`
1 change: 1 addition & 0 deletions internal/tf6muxprovider/provider1/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (p *testProvider) Configure(ctx context.Context, req provider.ConfigureRequ

func (p *testProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewNestedResource,
NewUserResource,
}
}
Expand Down