Skip to content

Commit 5811f33

Browse files
committed
add tests for resource data tftype receiver funcs
1 parent 96a33e0 commit 5811f33

File tree

2 files changed

+134
-6
lines changed

2 files changed

+134
-6
lines changed

helper/schema/resource_data.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,26 @@ type getResult struct {
6363
Schema *Schema
6464
}
6565

66-
// TfTypeIdentity returns the identity data as a tftypes.Value.
67-
func (d *ResourceData) TfTypeIdentity() tftypes.Value {
66+
// TfTypeIdentityState returns the identity data as a tftypes.Value.
67+
func (d *ResourceData) TfTypeIdentityState() tftypes.Value {
6868
s := schemaMap(d.identitySchema).CoreConfigSchema()
6969

7070
state := d.State()
7171

72+
if state == nil {
73+
log.Panicf("state is nil")
74+
}
75+
7276
stateVal, err := hcl2shim.HCL2ValueFromFlatmap(state.Identity, s.ImpliedType())
7377
if err != nil {
74-
panic(fmt.Errorf("converting identity to tf value: %+v", err))
78+
log.Panicf("converting identity to tf value: %+v", err)
7579
}
7680

7781
return convert.ToTfValue(stateVal)
7882
}
7983

80-
// TfTypeResource returns the resource data as a tftypes.Value.
81-
func (d *ResourceData) TfTypeResource() tftypes.Value {
84+
// TfTypeResourceState returns the resource data as a tftypes.Value.
85+
func (d *ResourceData) TfTypeResourceState() tftypes.Value {
8286
s := schemaMap(d.schema).CoreConfigSchema()
8387

8488
// The CoreConfigSchema method on schemaMaps doesn't automatically handle adding the id
@@ -142,9 +146,13 @@ func (d *ResourceData) TfTypeResource() tftypes.Value {
142146

143147
state := d.State()
144148

149+
if state == nil {
150+
log.Panicf("state is nil")
151+
}
152+
145153
stateVal, err := hcl2shim.HCL2ValueFromFlatmap(state.Attributes, s.ImpliedType())
146154
if err != nil {
147-
panic(fmt.Errorf("converting resource state to tf value: %+v", err))
155+
log.Panicf("converting resource state to tf value: %+v", err)
148156
}
149157

150158
return convert.ToTfValue(stateVal)

helper/schema/resource_data_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package schema
55

66
import (
77
"fmt"
8+
"github.com/hashicorp/terraform-plugin-go/tftypes"
89
"math"
910
"reflect"
1011
"testing"
@@ -4314,6 +4315,125 @@ func TestResourceDataIdentity_no_schema(t *testing.T) {
43144315
}
43154316
}
43164317

4318+
func TestResourceData_TfTypeIdentityState(t *testing.T) {
4319+
d := &ResourceData{
4320+
identitySchema: map[string]*Schema{
4321+
"foo": {
4322+
Type: TypeString,
4323+
RequiredForImport: true,
4324+
},
4325+
},
4326+
}
4327+
4328+
d.SetId("baz") // just required to be able to call .State()
4329+
4330+
identity, err := d.Identity()
4331+
if err != nil {
4332+
t.Fatalf("err: %s", err)
4333+
}
4334+
4335+
err = identity.Set("foo", "bar")
4336+
if err != nil {
4337+
t.Fatalf("err: %s", err)
4338+
}
4339+
4340+
expectedIdentity := tftypes.NewValue(tftypes.Object{
4341+
AttributeTypes: map[string]tftypes.Type{
4342+
"foo": tftypes.String,
4343+
}}, map[string]tftypes.Value{
4344+
"foo": tftypes.NewValue(tftypes.String, "bar"),
4345+
})
4346+
4347+
tfTypeIdentity := d.TfTypeIdentityState()
4348+
4349+
if !tfTypeIdentity.Equal(expectedIdentity) {
4350+
t.Fatalf("expected tftype value of identity to be %+v, got %+v", expectedIdentity, tfTypeIdentity)
4351+
}
4352+
}
4353+
4354+
func TestResourceData_TfTypeResourceState(t *testing.T) {
4355+
cases := []struct {
4356+
d *ResourceData
4357+
expected tftypes.Value
4358+
}{
4359+
{
4360+
d: &ResourceData{
4361+
schema: map[string]*Schema{
4362+
"location": {
4363+
Type: TypeString,
4364+
Optional: true,
4365+
},
4366+
},
4367+
timeouts: timeoutForValues(30, 5, 30, 5, 5),
4368+
},
4369+
expected: tftypes.NewValue(tftypes.Object{
4370+
AttributeTypes: map[string]tftypes.Type{
4371+
"location": tftypes.String,
4372+
"id": tftypes.String,
4373+
"timeouts": tftypes.Object{
4374+
AttributeTypes: map[string]tftypes.Type{
4375+
"create": tftypes.String,
4376+
"delete": tftypes.String,
4377+
"read": tftypes.String,
4378+
"update": tftypes.String,
4379+
"default": tftypes.String,
4380+
},
4381+
},
4382+
}}, map[string]tftypes.Value{
4383+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
4384+
"id": tftypes.NewValue(tftypes.String, "baz"),
4385+
"timeouts": tftypes.NewValue(tftypes.Object{
4386+
AttributeTypes: map[string]tftypes.Type{
4387+
"create": tftypes.String,
4388+
"default": tftypes.String,
4389+
"delete": tftypes.String,
4390+
"read": tftypes.String,
4391+
"update": tftypes.String,
4392+
},
4393+
}, map[string]tftypes.Value{
4394+
"create": tftypes.NewValue(tftypes.String, nil),
4395+
"default": tftypes.NewValue(tftypes.String, nil),
4396+
"delete": tftypes.NewValue(tftypes.String, nil),
4397+
"read": tftypes.NewValue(tftypes.String, nil),
4398+
"update": tftypes.NewValue(tftypes.String, nil),
4399+
}),
4400+
}),
4401+
},
4402+
{
4403+
d: &ResourceData{
4404+
schema: map[string]*Schema{
4405+
"location": {
4406+
Type: TypeString,
4407+
Optional: true,
4408+
},
4409+
},
4410+
},
4411+
expected: tftypes.NewValue(tftypes.Object{
4412+
AttributeTypes: map[string]tftypes.Type{
4413+
"location": tftypes.String,
4414+
"id": tftypes.String,
4415+
}}, map[string]tftypes.Value{
4416+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
4417+
"id": tftypes.NewValue(tftypes.String, "baz"),
4418+
}),
4419+
},
4420+
}
4421+
4422+
for _, tc := range cases {
4423+
tc.d.SetId("baz") // just required to be able to call .State()
4424+
4425+
if err := tc.d.Set("location", "westeurope"); err != nil {
4426+
t.Fatalf("err: %s", err)
4427+
}
4428+
4429+
tfTypeIdentity := tc.d.TfTypeResourceState()
4430+
4431+
if !tfTypeIdentity.Equal(tc.expected) {
4432+
t.Fatalf("expected tftype value of identity to be %+v, got %+v", tc.expected, tfTypeIdentity)
4433+
}
4434+
}
4435+
}
4436+
43174437
func testPtrTo(raw interface{}) interface{} {
43184438
return &raw
43194439
}

0 commit comments

Comments
 (0)