Skip to content

Commit

Permalink
fix: terraform registry docs prioritize website/docs (#1742)
Browse files Browse the repository at this point in the history
Sometimes terraform providers have both a `website/docs` and a `docs`
folder. This is probably a bug with the individual provider (why would
you want both?). The Terraform registry seems to prioritize the
`website/docs` folder if it exists, so we should to.

fixes #1609
  • Loading branch information
corymhall committed Mar 8, 2024
1 parent 95487a0 commit f3f4718
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
7 changes: 4 additions & 3 deletions pkg/tf2pulumi/internal/addrs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,10 @@ func (pt Provider) Equals(other Provider) bool {
// terraform-config-inspect.
//
// The following are valid source string formats:
// name
// namespace/name
// hostname/namespace/name
//
// name
// namespace/name
// hostname/namespace/name
func ParseProviderSourceString(str string) (Provider, hcl.Diagnostics) {
var ret Provider
var diags hcl.Diagnostics
Expand Down
24 changes: 12 additions & 12 deletions pkg/tf2pulumi/internal/addrs/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ var _ ProviderConfig = AbsProviderConfig{}
// address. The following are examples of traversals that can be successfully
// parsed as absolute provider configuration addresses:
//
// provider["registry.terraform.io/hashicorp/aws"]
// provider["registry.terraform.io/hashicorp/aws"].foo
// module.bar.provider["registry.terraform.io/hashicorp/aws"]
// module.bar.module.baz.provider["registry.terraform.io/hashicorp/aws"].foo
// provider["registry.terraform.io/hashicorp/aws"]
// provider["registry.terraform.io/hashicorp/aws"].foo
// module.bar.provider["registry.terraform.io/hashicorp/aws"]
// module.bar.module.baz.provider["registry.terraform.io/hashicorp/aws"].foo
//
// This type of address is used, for example, to record the relationships
// between resources and provider configurations in the state structure.
Expand Down Expand Up @@ -232,10 +232,10 @@ func ParseLegacyAbsProviderConfigStr(str string) (AbsProviderConfig, hcl.Diagnos
// provider address. The following are examples of traversals that can be
// successfully parsed as legacy absolute provider configuration addresses:
//
// provider.aws
// provider.aws.foo
// module.bar.provider.aws
// module.bar.module.baz.provider.aws.foo
// provider.aws
// provider.aws.foo
// module.bar.provider.aws
// module.bar.module.baz.provider.aws.foo
//
// This type of address is used in legacy state and may appear in state v4 if
// the provider config addresses have not been normalized to include provider
Expand Down Expand Up @@ -379,10 +379,10 @@ func (pc AbsProviderConfig) LegacyString() string {

// String() returns a string representation of an AbsProviderConfig in the following format:
//
// provider["example.com/namespace/name"]
// provider["example.com/namespace/name"].alias
// module.module-name.provider["example.com/namespace/name"]
// module.module-name.provider["example.com/namespace/name"].alias
// provider["example.com/namespace/name"]
// provider["example.com/namespace/name"].alias
// module.module-name.provider["example.com/namespace/name"]
// module.module-name.provider["example.com/namespace/name"].alias
func (pc AbsProviderConfig) String() string {
var parts []string
if len(pc.Module) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/tf2pulumi/internal/config/import_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type fileLoaderFunc func(fs afero.Fs, path string) (configurable, []string, erro
// This is not currently enabled for release builds.
//
// For example:
// go install -ldflags="-X github.com/hashicorp/terraform/config.enableHCL2Experiment=true" github.com/hashicorp/terraform
//
// go install -ldflags="-X github.com/hashicorp/terraform/config.enableHCL2Experiment=true" github.com/hashicorp/terraform
var enableHCL2Experiment = ""

// loadTreeFromString takes a string and treats it as an HCL file with the
Expand Down
13 changes: 7 additions & 6 deletions pkg/tfgen/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func readMarkdown(repo string, kind DocKind, possibleLocations []string) (*DocFi
}

// getDocsPath finds the correct docs path for the repo/kind
// add the legacy path first since the terraform registry docs also pick those first
func getDocsPath(repo string, kind DocKind) ([]string, error) {
var err error
exists := func(p string) bool {
Expand All @@ -235,17 +236,17 @@ func getDocsPath(repo string, kind DocKind) ([]string, error) {

var paths []string

// ${repo}/docs/resources
// ${repo}/website/docs/r
//
// This is TF's new and preferred way to describe docs.
if p := filepath.Join(repo, "docs", string(kind)); exists(p) {
// This is the legacy way to describe docs.
if p := filepath.Join(repo, "website", "docs", string(kind)[:1]); exists(p) {
paths = append(paths, p)
}

// ${repo}/website/docs/r
// ${repo}/docs/resources
//
// This is the legacy way to describe docs.
if p := filepath.Join(repo, "website", "docs", string(kind)[:1]); exists(p) {
// This is TF's new and preferred way to describe docs.
if p := filepath.Join(repo, "docs", string(kind)); exists(p) {
paths = append(paths, p)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/tfgen/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func TestGetDocsPath(t *testing.T) {
filepath.Join("website", "docs", "r", "bar.md"),
},
expectedResource: []string{
filepath.Join("docs", "resources"),
filepath.Join("website", "docs", "r"),
filepath.Join("docs", "resources"),
},
},
}
Expand Down

0 comments on commit f3f4718

Please sign in to comment.