Skip to content

Commit 24d0140

Browse files
committed
Fix nested block support
1 parent 060ba0e commit 24d0140

File tree

2 files changed

+1399
-34
lines changed

2 files changed

+1399
-34
lines changed

internal/fwserver/schema_propose_new_plan.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ func proposeNewNestedBlock(ctx context.Context, s fwschema.Schema, block fwschem
138138
newVal = proposedNewBlockListNested(ctx, s, block, path, prior, config)
139139
case fwschema.BlockNestingModeSet:
140140
newVal = proposedNewBlockSetNested(ctx, s, block, path, prior, config)
141-
// TODO: handle set
142141
default:
143142
// TODO: Shouldn't happen, return diag
144143
panic(fmt.Sprintf("unsupported attribute nesting mode %d", block.GetNestingMode()))
@@ -147,6 +146,39 @@ func proposeNewNestedBlock(ctx context.Context, s fwschema.Schema, block fwschem
147146
return newVal
148147
}
149148

149+
func proposeNewNestedBlockObject(ctx context.Context, s fwschema.Schema, nestedBlock fwschema.NestedBlockObject, path *tftypes.AttributePath, prior, config tftypes.Value) tftypes.Value {
150+
if config.IsNull() {
151+
return config
152+
}
153+
valuesMap := proposedNewAttributes(ctx, s, nestedBlock.GetAttributes(), path, prior, config)
154+
155+
for name, blockType := range nestedBlock.GetBlocks() {
156+
var priorVal tftypes.Value
157+
if prior.IsNull() {
158+
priorObjType := prior.Type().(tftypes.Object) //nolint
159+
// TODO: validate before doing this? To avoid panic
160+
priorVal = tftypes.NewValue(priorObjType.AttributeTypes[name], nil)
161+
} else {
162+
// TODO: handle error
163+
attrVal, err := prior.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name))
164+
if err != nil {
165+
panic(err)
166+
}
167+
priorVal = attrVal.(tftypes.Value) //nolint
168+
}
169+
170+
attrVal, _ := config.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name))
171+
configVal := attrVal.(tftypes.Value)
172+
valuesMap[name] = proposeNewNestedBlock(ctx, s, blockType, path.WithAttributeName(name), priorVal, configVal)
173+
}
174+
175+
// TODO: validate before doing this? To avoid panic
176+
return tftypes.NewValue(
177+
nestedBlock.Type().TerraformType(ctx),
178+
valuesMap,
179+
)
180+
}
181+
150182
func proposeNewNestedAttribute(ctx context.Context, s fwschema.Schema, attr fwschema.NestedAttribute, path *tftypes.AttributePath, prior, config tftypes.Value) tftypes.Value {
151183
// if the config isn't known at all, then we must use that value
152184
if !config.IsKnown() {
@@ -273,8 +305,9 @@ func proposedNewBlockSetNested(ctx context.Context, s fwschema.Schema, block fws
273305
// TODO might have to come back to figure out how to get elem type
274306
priorEV = tftypes.NewValue(block.GetNestedObject().Type().TerraformType(ctx), nil)
275307
}
276-
277-
newVals = append(newVals, proposedNewBlockObjectAttributes(ctx, s, block, path.WithElementKeyValue(priorEV), priorEV, configEV))
308+
//block.GetNestedObject().GetAttributes()
309+
// TODO create proposed new nested block object
310+
newVals = append(newVals, proposeNewNestedBlockObject(ctx, s, block.GetNestedObject(), path.WithElementKeyValue(priorEV), priorEV, configEV))
278311
}
279312

280313
// TODO: should work for tuples + lists
@@ -344,11 +377,26 @@ func proposedNewBlockObjectAttributes(ctx context.Context, s fwschema.Schema, bl
344377
if config.IsNull() {
345378
return config
346379
}
380+
valuesMap := proposedNewAttributes(ctx, s, block.GetNestedObject().GetAttributes(), path, prior, config)
381+
382+
for name, blockType := range block.GetNestedObject().GetBlocks() {
383+
//maps.Copy(valuesMap, proposedNewAttributes(ctx, s, blockType.GetNestedObject().GetAttributes(), tftypes.NewAttributePath().WithAttributeName(name).WithElementKeyInt(0), prior, config))
384+
attrVal, err := prior.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name))
385+
//TODO handle panic
386+
if err != nil {
387+
panic(err)
388+
}
389+
priorVal := attrVal.(tftypes.Value)
390+
391+
attrVal, _ = config.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name))
392+
configVal := attrVal.(tftypes.Value)
393+
valuesMap[name] = proposeNewNestedBlock(ctx, s, blockType, tftypes.NewAttributePath().WithAttributeName(name).WithElementKeyInt(0), priorVal, configVal)
394+
}
347395

348396
// TODO: validate before doing this? To avoid panic
349397
return tftypes.NewValue(
350398
block.GetNestedObject().Type().TerraformType(ctx),
351-
proposedNewAttributes(ctx, s, block.GetNestedObject().GetAttributes(), path, prior, config),
399+
valuesMap,
352400
)
353401
}
354402

@@ -424,7 +472,7 @@ func validPriorFromConfig(ctx context.Context, s fwschema.Schema, absPath *tftyp
424472
return true, nil
425473
}
426474

427-
configIface, err := config.ApplyTerraform5AttributePathStep(path.LastStep())
475+
configIface, _, err := tftypes.WalkAttributePath(config, path)
428476
if err != nil {
429477
// most likely dynamic objects with different types
430478
valid = false

0 commit comments

Comments
 (0)