Skip to content

Commit

Permalink
fix: Work around 'unreliable' input data for Registry modules
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 11, 2023
1 parent d7cd582 commit abb535c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions internal/registry/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptrace"
"sort"
"time"

"github.com/hashicorp/go-version"
tfaddr "github.com/hashicorp/terraform-registry-address"
Expand All @@ -21,8 +22,9 @@ import (
)

type ModuleResponse struct {
Version string `json:"version"`
Root ModuleRoot `json:"root"`
Version string `json:"version"`
PublishedAt time.Time `json:"published_at"`
Root ModuleRoot `json:"root"`
}

type ModuleRoot struct {
Expand Down
21 changes: 20 additions & 1 deletion internal/terraform/module/module_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,11 @@ func GetModuleDataFromRegistry(ctx context.Context, regClient registry.Client, m

inputs := make([]tfregistry.Input, len(metaData.Root.Inputs))
for i, input := range metaData.Root.Inputs {
isRequired := isRegistryModuleInputRequired(metaData.PublishedAt, input)
inputs[i] = tfregistry.Input{
Name: input.Name,
Description: lang.Markdown(input.Description),
Required: input.Required,
Required: isRequired,
}

inputType := cty.DynamicPseudoType
Expand Down Expand Up @@ -1123,3 +1124,21 @@ func GetModuleDataFromRegistry(ctx context.Context, regClient registry.Client, m

return errs.ErrorOrNil()
}

// isRegistryModuleInputRequired checks whether the module input is required.
// It reflects the fact that modules ingested into the Registry
// may have used `default = null` (implying optional variable) which
// the Registry wasn't able to recognise until ~ 19th August 2022.
func isRegistryModuleInputRequired(publishTime time.Time, input registry.Input) bool {
fixTime := time.Date(2022, time.August, 20, 0, 0, 0, 0, time.UTC)
// Modules published after the date have "nullable" inputs
// (default = null) ingested as Required=false and Default="null".
//
// The same inputs ingested prior to the date make it impossible
// to distinguish variable with `default = null` and missing default.
if input.Required && input.Default == "" && publishTime.Before(fixTime) {
// To avoid false diagnostics, we safely assume the input is optional
return false
}
return input.Required
}

0 comments on commit abb535c

Please sign in to comment.