Skip to content

Commit 65b3247

Browse files
committed
fix issue when adding timeouts to schema and handle unknown values
1 parent 9419032 commit 65b3247

File tree

4 files changed

+155
-9
lines changed

4 files changed

+155
-9
lines changed

helper/schema/resource_data.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ func (d *ResourceData) TfTypeResourceState() (*tftypes.Value, error) {
138138
}
139139
}
140140

141-
s.BlockTypes[TimeoutsConfigKey] = &configschema.NestedBlock{
142-
Nesting: configschema.NestingSingle,
143-
Block: timeouts,
141+
if len(timeouts.Attributes) != 0 {
142+
s.BlockTypes[TimeoutsConfigKey] = &configschema.NestedBlock{
143+
Nesting: configschema.NestingSingle,
144+
Block: timeouts,
145+
}
144146
}
145147
}
146148

@@ -149,6 +151,9 @@ func (d *ResourceData) TfTypeResourceState() (*tftypes.Value, error) {
149151
return nil, fmt.Errorf("state is nil, call SetId() on ResourceData first")
150152
}
151153

154+
// Although we handle adding/omitting timeouts to the schema depending on how it's been defined on the resource
155+
// we don't process or convert the timeout values since they reside in Meta and aren't needed for the purposes
156+
// of this function and in the context of a List.
152157
stateVal, err := hcl2shim.HCL2ValueFromFlatmap(state.Attributes, s.ImpliedType())
153158
if err != nil {
154159
return nil, fmt.Errorf("converting resource state flatmap to cty value: %+v", err)

helper/schema/resource_data_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4420,6 +4420,58 @@ func TestResourceData_TfTypeResourceState(t *testing.T) {
44204420
"id": tftypes.NewValue(tftypes.String, "baz"),
44214421
}),
44224422
},
4423+
{
4424+
d: &ResourceData{
4425+
schema: map[string]*Schema{
4426+
"location": {
4427+
Type: TypeString,
4428+
Optional: true,
4429+
},
4430+
},
4431+
timeouts: &ResourceTimeout{},
4432+
},
4433+
expected: tftypes.NewValue(tftypes.Object{
4434+
AttributeTypes: map[string]tftypes.Type{
4435+
"location": tftypes.String,
4436+
"id": tftypes.String,
4437+
}}, map[string]tftypes.Value{
4438+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
4439+
"id": tftypes.NewValue(tftypes.String, "baz"),
4440+
}),
4441+
},
4442+
{
4443+
d: &ResourceData{
4444+
schema: map[string]*Schema{
4445+
"location": {
4446+
Type: TypeString,
4447+
Optional: true,
4448+
},
4449+
},
4450+
timeouts: &ResourceTimeout{
4451+
Create: DefaultTimeout(30 * time.Minute),
4452+
},
4453+
},
4454+
expected: tftypes.NewValue(tftypes.Object{
4455+
AttributeTypes: map[string]tftypes.Type{
4456+
"location": tftypes.String,
4457+
"id": tftypes.String,
4458+
"timeouts": tftypes.Object{
4459+
AttributeTypes: map[string]tftypes.Type{
4460+
"create": tftypes.String,
4461+
},
4462+
},
4463+
}}, map[string]tftypes.Value{
4464+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
4465+
"id": tftypes.NewValue(tftypes.String, "baz"),
4466+
"timeouts": tftypes.NewValue(tftypes.Object{
4467+
AttributeTypes: map[string]tftypes.Type{
4468+
"create": tftypes.String,
4469+
},
4470+
}, map[string]tftypes.Value{
4471+
"create": tftypes.NewValue(tftypes.String, nil),
4472+
}),
4473+
}),
4474+
},
44234475
}
44244476

44254477
for _, tc := range cases {

internal/plugin/convert/value.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ func primitiveTfValue(in cty.Value) (*tftypes.Value, error) {
1616
return nil, err
1717
}
1818

19-
if in.IsNull() || !in.IsKnown() {
19+
if in.IsNull() {
2020
return nullTfValue(primitiveType), nil
2121
}
2222

23+
if !in.IsKnown() {
24+
return unknownTfValue(primitiveType), nil
25+
}
26+
2327
var val tftypes.Value
2428
switch in.Type() {
2529
case cty.String:
@@ -39,10 +43,14 @@ func listTfValue(in cty.Value) (*tftypes.Value, error) {
3943
return nil, err
4044
}
4145

42-
if in.IsNull() || !in.IsKnown() {
46+
if in.IsNull() {
4347
return nullTfValue(listType), nil
4448
}
4549

50+
if !in.IsKnown() {
51+
return unknownTfValue(listType), nil
52+
}
53+
4654
vals := make([]tftypes.Value, 0)
4755

4856
for _, v := range in.AsValueSlice() {
@@ -64,10 +72,14 @@ func mapTfValue(in cty.Value) (*tftypes.Value, error) {
6472
return nil, err
6573
}
6674

67-
if in.IsNull() || !in.IsKnown() {
75+
if in.IsNull() {
6876
return nullTfValue(mapType), nil
6977
}
7078

79+
if !in.IsKnown() {
80+
return unknownTfValue(mapType), nil
81+
}
82+
7183
vals := make(map[string]tftypes.Value)
7284

7385
for k, v := range in.AsValueMap() {
@@ -89,10 +101,14 @@ func setTfValue(in cty.Value) (*tftypes.Value, error) {
89101
return nil, err
90102
}
91103

92-
if in.IsNull() || !in.IsKnown() {
104+
if in.IsNull() {
93105
return nullTfValue(setType), nil
94106
}
95107

108+
if !in.IsKnown() {
109+
return unknownTfValue(setType), nil
110+
}
111+
96112
vals := make([]tftypes.Value, 0)
97113

98114
for _, v := range in.AsValueSlice() {
@@ -114,10 +130,14 @@ func objectTfValue(in cty.Value) (*tftypes.Value, error) {
114130
return nil, err
115131
}
116132

117-
if in.IsNull() || !in.IsKnown() {
133+
if in.IsNull() {
118134
return nullTfValue(objType), nil
119135
}
120136

137+
if !in.IsKnown() {
138+
return unknownTfValue(objType), nil
139+
}
140+
121141
vals := make(map[string]tftypes.Value)
122142

123143
for k, v := range in.AsValueMap() {
@@ -139,10 +159,14 @@ func tupleTfValue(in cty.Value) (*tftypes.Value, error) {
139159
return nil, err
140160
}
141161

142-
if in.IsNull() || !in.IsKnown() {
162+
if in.IsNull() {
143163
return nullTfValue(tupleType), nil
144164
}
145165

166+
if !in.IsKnown() {
167+
return unknownTfValue(tupleType), nil
168+
}
169+
146170
vals := make([]tftypes.Value, 0)
147171

148172
for _, v := range in.AsValueSlice() {
@@ -182,3 +206,8 @@ func nullTfValue(ty tftypes.Type) *tftypes.Value {
182206
nullValue := tftypes.NewValue(ty, nil)
183207
return &nullValue
184208
}
209+
210+
func unknownTfValue(ty tftypes.Type) *tftypes.Value {
211+
unknownValue := tftypes.NewValue(ty, tftypes.UnknownValue)
212+
return &unknownValue
213+
}

internal/plugin/convert/value_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ func TestPrimitiveTfType(t *testing.T) {
3434
Value: cty.NumberFloatVal(3.14),
3535
Want: tftypes.NewValue(tftypes.Number, 3.14),
3636
},
37+
{
38+
Value: cty.NullVal(cty.String),
39+
Want: tftypes.NewValue(tftypes.String, nil),
40+
},
41+
{
42+
Value: cty.UnknownVal(cty.String),
43+
Want: tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
44+
},
3745
}
3846

3947
for _, test := range tests {
@@ -150,6 +158,14 @@ func TestListTfType(t *testing.T) {
150158
}),
151159
}),
152160
},
161+
{
162+
Value: cty.NullVal(cty.List(cty.String)),
163+
Want: tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, nil),
164+
},
165+
{
166+
Value: cty.UnknownVal(cty.List(cty.String)),
167+
Want: tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, tftypes.UnknownValue),
168+
},
153169
}
154170

155171
for _, test := range tests {
@@ -266,6 +282,14 @@ func TestSetTfType(t *testing.T) {
266282
}),
267283
}),
268284
},
285+
{
286+
Value: cty.NullVal(cty.Set(cty.String)),
287+
Want: tftypes.NewValue(tftypes.Set{ElementType: tftypes.String}, nil),
288+
},
289+
{
290+
Value: cty.UnknownVal(cty.Set(cty.String)),
291+
Want: tftypes.NewValue(tftypes.Set{ElementType: tftypes.String}, tftypes.UnknownValue),
292+
},
269293
}
270294

271295
for _, test := range tests {
@@ -347,6 +371,14 @@ func TestMapTfType(t *testing.T) {
347371
}),
348372
}),
349373
},
374+
{
375+
Value: cty.NullVal(cty.Map(cty.String)),
376+
Want: tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, nil),
377+
},
378+
{
379+
Value: cty.UnknownVal(cty.Map(cty.String)),
380+
Want: tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, tftypes.UnknownValue),
381+
},
350382
}
351383

352384
for _, test := range tests {
@@ -390,6 +422,14 @@ func TestTupleTfType(t *testing.T) {
390422
tftypes.NewValue(tftypes.Tuple{ElementTypes: []tftypes.Type{tftypes.String, tftypes.String}}, []tftypes.Value{tftypes.NewValue(tftypes.String, "banana"), tftypes.NewValue(tftypes.String, "pineapple")}),
391423
}),
392424
},
425+
{
426+
Value: cty.NullVal(cty.Tuple([]cty.Type{cty.String})),
427+
Want: tftypes.NewValue(tftypes.Tuple{ElementTypes: []tftypes.Type{tftypes.String}}, nil),
428+
},
429+
{
430+
Value: cty.UnknownVal(cty.Tuple([]cty.Type{cty.String})),
431+
Want: tftypes.NewValue(tftypes.Tuple{ElementTypes: []tftypes.Type{tftypes.String}}, tftypes.UnknownValue),
432+
},
393433
}
394434

395435
for _, test := range tests {
@@ -534,6 +574,26 @@ func TestObjectTfType(t *testing.T) {
534574
}),
535575
}),
536576
},
577+
{
578+
Value: cty.NullVal(cty.Object(map[string]cty.Type{
579+
"foo": cty.String,
580+
"bar": cty.Number,
581+
})),
582+
Want: tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
583+
"foo": tftypes.String,
584+
"bar": tftypes.Number,
585+
}}, nil),
586+
},
587+
{
588+
Value: cty.UnknownVal(cty.Object(map[string]cty.Type{
589+
"foo": cty.String,
590+
"bar": cty.Number,
591+
})),
592+
Want: tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
593+
"foo": tftypes.String,
594+
"bar": tftypes.Number,
595+
}}, tftypes.UnknownValue),
596+
},
537597
}
538598

539599
for _, test := range tests {

0 commit comments

Comments
 (0)