Skip to content

Commit

Permalink
Resolve version correctly for backend schema (#1453)
Browse files Browse the repository at this point in the history
* desp: bump terraform-schema to c5aaaecd335e

* Decouple Terraform version resolution
  • Loading branch information
radeksimko authored Oct 10, 2023
1 parent a94cf7d commit d7cd582
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/hashicorp/terraform-exec v0.19.0
github.com/hashicorp/terraform-json v0.17.1
github.com/hashicorp/terraform-registry-address v0.2.2
github.com/hashicorp/terraform-schema v0.0.0-20231006123128-72331e948317
github.com/hashicorp/terraform-schema v0.0.0-20231010111619-c5aaaecd335e
github.com/mcuadros/go-defaults v1.2.0
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ github.com/hashicorp/terraform-json v0.17.1 h1:eMfvh/uWggKmY7Pmb3T85u86E2EQg6EQH
github.com/hashicorp/terraform-json v0.17.1/go.mod h1:Huy6zt6euxaY9knPAFKjUITn8QxUFIe9VuSzb4zn/0o=
github.com/hashicorp/terraform-registry-address v0.2.2 h1:lPQBg403El8PPicg/qONZJDC6YlgCVbWDtNmmZKtBno=
github.com/hashicorp/terraform-registry-address v0.2.2/go.mod h1:LtwNbCihUoUZ3RYriyS2wF/lGPB6gF9ICLRtuDk7hSo=
github.com/hashicorp/terraform-schema v0.0.0-20231006123128-72331e948317 h1:iErld30gWHZaDapPblnXIh7UpMtemzEfthRby+ub3O4=
github.com/hashicorp/terraform-schema v0.0.0-20231006123128-72331e948317/go.mod h1:e2ZdWyv2u1YQN1xZ6jhOps3KZNuK88Sf+39pvlmnqIc=
github.com/hashicorp/terraform-schema v0.0.0-20231010111619-c5aaaecd335e h1:Q9ydKXzO6X54Pubzp223BuRgEdGZv7MoL16VX5oFXew=
github.com/hashicorp/terraform-schema v0.0.0-20231010111619-c5aaaecd335e/go.mod h1:iPIBxz1xXSnq6UiCMtIJiRh4TIRkh39jXDUj5rzhJHc=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=
Expand Down
29 changes: 3 additions & 26 deletions internal/decoder/module_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
)

func schemaForModule(mod *state.Module, schemaReader state.SchemaReader, modReader state.ModuleCallReader) (*schema.BodySchema, error) {
sm := tfschema.NewSchemaMerger(coreSchema(mod))
resolvedVersion := tfschema.ResolveVersion(mod.TerraformVersion, mod.Meta.CoreRequirements)
sm := tfschema.NewSchemaMerger(mustCoreSchemaForVersion(resolvedVersion))
sm.SetSchemaReader(schemaReader)
sm.SetTerraformVersion(mod.TerraformVersion)
sm.SetTerraformVersion(resolvedVersion)
sm.SetModuleReader(modReader)

meta := &tfmodule.Meta{
Expand All @@ -30,30 +31,6 @@ func schemaForModule(mod *state.Module, schemaReader state.SchemaReader, modRead
return sm.SchemaForModule(meta)
}

func coreSchema(mod *state.Module) *schema.BodySchema {
if mod.TerraformVersion != nil {
s, err := tfschema.CoreModuleSchemaForVersion(mod.TerraformVersion)
if err == nil {
return s
}
if mod.TerraformVersion.LessThan(tfschema.OldestAvailableVersion) {
return mustCoreSchemaForVersion(tfschema.OldestAvailableVersion)
}

return mustCoreSchemaForVersion(tfschema.LatestAvailableVersion)
}

s, err := tfschema.CoreModuleSchemaForConstraint(mod.Meta.CoreRequirements)
if err == nil {
return s
}
if mod.Meta.CoreRequirements.Check(tfschema.OldestAvailableVersion) {
return mustCoreSchemaForVersion(tfschema.OldestAvailableVersion)
}

return mustCoreSchemaForVersion(tfschema.LatestAvailableVersion)
}

func mustCoreSchemaForVersion(v *version.Version) *schema.BodySchema {
s, err := tfschema.CoreModuleSchemaForVersion(v)
if err != nil {
Expand Down

0 comments on commit d7cd582

Please sign in to comment.