Skip to content

Commit 47eff8c

Browse files
authored
Apply required inputs when combining partial schema (#436)
1 parent 57e041c commit 47eff8c

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pkg/modprovider/tfmodules.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ func combineInferredModuleSchema(
502502
inferredSchema *InferredModuleSchema,
503503
partialInferredSchema *InferredModuleSchema,
504504
) *InferredModuleSchema {
505+
if partialInferredSchema == nil {
506+
return inferredSchema
507+
}
505508

506509
// add required outputs to the inferred schema if they are not already present
507510
for _, requiredOutput := range partialInferredSchema.NonNilOutputs {
@@ -518,6 +521,20 @@ func combineInferredModuleSchema(
518521
}
519522
}
520523

524+
// add required inputs to the inferred schema if they are not already present
525+
for _, requiredInput := range partialInferredSchema.RequiredInputs {
526+
alreadyExists := false
527+
for _, existingRequiredInput := range inferredSchema.RequiredInputs {
528+
if existingRequiredInput == requiredInput {
529+
alreadyExists = true
530+
break
531+
}
532+
}
533+
if !alreadyExists {
534+
inferredSchema.RequiredInputs = append(inferredSchema.RequiredInputs, requiredInput)
535+
}
536+
}
537+
521538
for name, input := range partialInferredSchema.Inputs {
522539
if _, ok := inferredSchema.Inputs[name]; !ok {
523540
inferredSchema.Inputs[name] = input

pkg/modprovider/tfmodules_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ func TestParsingModuleSchemaOverrides(t *testing.T) {
226226
},
227227
},
228228
})
229+
230+
assert.Equal(t, testSchemaOverride.PartialSchema.RequiredInputs, []resource.PropertyKey{
231+
"example_input",
232+
})
233+
234+
assert.Equal(t, testSchemaOverride.PartialSchema.NonNilOutputs, []resource.PropertyKey{
235+
"example_output",
236+
})
229237
}
230238

231239
func TestApplyModuleOverrides(t *testing.T) {
@@ -243,13 +251,14 @@ func TestApplyModuleOverrides(t *testing.T) {
243251
// We cannot infer which outputs are required or not so everything is optional, initially.
244252
assert.Empty(t, awsVpcSchema.NonNilOutputs, "required outputs is empty")
245253

246-
t.Run("required outputs are updated", func(t *testing.T) {
254+
t.Run("required inputs/outputs are updated", func(t *testing.T) {
247255
moduleOverrides := []*ModuleSchemaOverride{
248256
{
249257
Source: string(source),
250258
MaximumVersion: "6.0.0",
251259
PartialSchema: &InferredModuleSchema{
252-
NonNilOutputs: []resource.PropertyKey{"vpc_id"},
260+
NonNilOutputs: []resource.PropertyKey{"vpc_id"},
261+
RequiredInputs: []resource.PropertyKey{"cidr"},
253262
},
254263
},
255264
}
@@ -259,6 +268,7 @@ func TestApplyModuleOverrides(t *testing.T) {
259268
overridenSchema := combineInferredModuleSchema(awsVpcSchema, partialAwsVpcSchemaOverride)
260269
assert.NotNil(t, overridenSchema, "overridden module schema is nil")
261270
assert.Contains(t, overridenSchema.NonNilOutputs, resource.PropertyKey("vpc_id"), "vpc_id should be required")
271+
assert.Contains(t, overridenSchema.RequiredInputs, resource.PropertyKey("cidr"), "cidr should be required")
262272
})
263273

264274
t.Run("specific fields can be updated", func(t *testing.T) {

0 commit comments

Comments
 (0)