Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to the new minimal terraform-registry-address API #112

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions earlydecoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/internal/addr"
"github.com/hashicorp/terraform-schema/module"
)

Expand Down Expand Up @@ -70,10 +71,10 @@ func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diag
if name == "" {
continue
}
src = tfaddr.NewLegacyProvider(name)
src = addr.NewLegacyProvider(name)
} else {
var err error
src, err = tfaddr.ParseRawProviderSourceString(req.Source)
src, err = tfaddr.ParseProviderSource(req.Source)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Expand Down Expand Up @@ -130,7 +131,7 @@ func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diag
LocalName: providerName,
}
if _, exists := refs[localRef]; !exists && providerName != "" {
src := tfaddr.NewLegacyProvider(providerName)
src := addr.NewLegacyProvider(providerName)
if _, exists := providerRequirements[src]; !exists {
providerRequirements[src] = version.Constraints{}
}
Expand All @@ -145,7 +146,7 @@ func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diag
LocalName: providerName,
}
if _, exists := refs[localRef]; !exists && providerName != "" {
src := tfaddr.NewLegacyProvider(providerName)
src := addr.NewLegacyProvider(providerName)
if _, exists := providerRequirements[src]; !exists {
providerRequirements[src] = version.Constraints{}
}
Expand Down
107 changes: 50 additions & 57 deletions earlydecoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/backend"
"github.com/hashicorp/terraform-schema/internal/addr"
"github.com/hashicorp/terraform-schema/module"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -86,16 +87,16 @@ provider "grafana" {
&module.Meta{
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: tfaddr.NewLegacyProvider("aws"),
{LocalName: "blah"}: tfaddr.NewLegacyProvider("blah"),
{LocalName: "google"}: tfaddr.NewLegacyProvider("google"),
{LocalName: "grafana"}: tfaddr.NewLegacyProvider("grafana"),
{LocalName: "aws"}: addr.NewLegacyProvider("aws"),
{LocalName: "blah"}: addr.NewLegacyProvider("blah"),
{LocalName: "google"}: addr.NewLegacyProvider("google"),
{LocalName: "grafana"}: addr.NewLegacyProvider("grafana"),
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
tfaddr.NewLegacyProvider("aws"): {},
tfaddr.NewLegacyProvider("blah"): {},
tfaddr.NewLegacyProvider("google"): {},
tfaddr.NewLegacyProvider("grafana"): {},
addr.NewLegacyProvider("aws"): {},
addr.NewLegacyProvider("blah"): {},
addr.NewLegacyProvider("google"): {},
addr.NewLegacyProvider("grafana"): {},
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Expand Down Expand Up @@ -129,14 +130,14 @@ provider "grafana" {
&module.Meta{
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: tfaddr.NewLegacyProvider("aws"),
{LocalName: "google"}: tfaddr.NewLegacyProvider("google"),
{LocalName: "grafana"}: tfaddr.NewLegacyProvider("grafana"),
{LocalName: "aws"}: addr.NewLegacyProvider("aws"),
{LocalName: "google"}: addr.NewLegacyProvider("google"),
{LocalName: "grafana"}: addr.NewLegacyProvider("grafana"),
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
tfaddr.NewLegacyProvider("aws"): version.MustConstraints(version.NewConstraint("1.2.0")),
tfaddr.NewLegacyProvider("google"): version.MustConstraints(version.NewConstraint(">= 3.0.0")),
tfaddr.NewLegacyProvider("grafana"): {},
addr.NewLegacyProvider("aws"): version.MustConstraints(version.NewConstraint("1.2.0")),
addr.NewLegacyProvider("google"): version.MustConstraints(version.NewConstraint(">= 3.0.0")),
addr.NewLegacyProvider("grafana"): {},
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Expand Down Expand Up @@ -174,14 +175,14 @@ provider "grafana" {
&module.Meta{
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: tfaddr.NewLegacyProvider("aws"),
{LocalName: "google"}: tfaddr.NewLegacyProvider("google"),
{LocalName: "grafana"}: tfaddr.NewLegacyProvider("grafana"),
{LocalName: "aws"}: addr.NewLegacyProvider("aws"),
{LocalName: "google"}: addr.NewLegacyProvider("google"),
{LocalName: "grafana"}: addr.NewLegacyProvider("grafana"),
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
tfaddr.NewLegacyProvider("aws"): version.MustConstraints(version.NewConstraint("1.2.0")),
tfaddr.NewLegacyProvider("google"): version.MustConstraints(version.NewConstraint(">= 3.0.0")),
tfaddr.NewLegacyProvider("grafana"): {},
addr.NewLegacyProvider("aws"): version.MustConstraints(version.NewConstraint("1.2.0")),
addr.NewLegacyProvider("google"): version.MustConstraints(version.NewConstraint(">= 3.0.0")),
addr.NewLegacyProvider("grafana"): {},
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Expand Down Expand Up @@ -226,34 +227,34 @@ provider "grafana" {
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "google"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
},
{LocalName: "grafana"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "grafana",
Type: "grafana",
},
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
}: version.MustConstraints(version.NewConstraint("1.0.0")),
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
}: version.MustConstraints(version.NewConstraint("2.0.0")),
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "grafana",
Type: "grafana",
}: version.MustConstraints(version.NewConstraint("2.1.0")),
Expand Down Expand Up @@ -301,24 +302,24 @@ resource "google_storage_bucket" "bucket" {
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "google"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
},
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
}: version.MustConstraints(version.NewConstraint(">= 1.0.0,1.1.0")),
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
}: version.MustConstraints(version.NewConstraint("2.0.0")),
Expand Down Expand Up @@ -367,24 +368,24 @@ resource "google_storage_bucket" "bucket" {
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "google"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
},
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
}: version.MustConstraints(version.NewConstraint(">= 1.0.0,1.1.0")),
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
}: version.MustConstraints(version.NewConstraint("2.0.0")),
Expand Down Expand Up @@ -420,29 +421,29 @@ provider "aws" {
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "aws", Alias: "euwest"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "google"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
},
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
}: version.MustConstraints(version.NewConstraint("1.0.0")),
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
}: version.MustConstraints(version.NewConstraint("2.0.0")),
Expand Down Expand Up @@ -479,34 +480,34 @@ provider "aws" {
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "aws", Alias: "east"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "aws", Alias: "west"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
},
{LocalName: "google"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
},
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "aws",
}: version.MustConstraints(version.NewConstraint("1.0.0")),
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google",
}: version.MustConstraints(version.NewConstraint("2.0.0")),
Expand Down Expand Up @@ -538,14 +539,14 @@ resource "google_something" "test" {
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{
{LocalName: "goo"}: {
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google-beta",
},
},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
{
Hostname: tfaddr.DefaultRegistryHost,
Hostname: tfaddr.DefaultProviderRegistryHost,
Namespace: "hashicorp",
Type: "google-beta",
}: version.MustConstraints(version.NewConstraint("2.0.0")),
Expand Down Expand Up @@ -1108,7 +1109,7 @@ module "name" {
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
SourceAddr: MustParseRawModuleSourceRegistry("registry.terraform.io/terraform-aws-modules/vpc/aws"),
SourceAddr: tfaddr.MustParseModuleSource("registry.terraform.io/terraform-aws-modules/vpc/aws"),
},
},
},
Expand Down Expand Up @@ -1153,7 +1154,7 @@ module "name" {
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
SourceAddr: MustParseRawModuleSourceRegistry("terraform-aws-modules/vpc/aws"),
SourceAddr: tfaddr.MustParseModuleSource("terraform-aws-modules/vpc/aws"),
Version: version.MustConstraints(version.NewConstraint("1.0.0")),
},
},
Expand Down Expand Up @@ -1192,11 +1193,3 @@ func runTestCases(testCases []testCase, t *testing.T, path string) {
func compareVersionConstraint(x, y *version.Constraint) bool {
return x.Equals(y)
}

func MustParseRawModuleSourceRegistry(source string) tfaddr.ModuleSourceRegistry {
m, err := tfaddr.ParseRawModuleSourceRegistry(source)
if err != nil {
panic(err)
}
return m
}
2 changes: 1 addition & 1 deletion earlydecoder/load_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func loadModuleFromFile(file *hcl.File, mod *decodedModule) hcl.Diagnostics {
}

var sourceAddr module.ModuleSourceAddr
registryAddr, err := tfaddr.ParseRawModuleSourceRegistry(source)
registryAddr, err := tfaddr.ParseModuleSource(source)
if err == nil {
sourceAddr = registryAddr
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/hcl-lang v0.0.0-20220421093840-480fdfd2ecb5
github.com/hashicorp/hcl/v2 v2.13.0
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/zclconf/go-cty v1.10.0
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/hashicorp/hcl/v2 v2.13.0 h1:0Apadu1w6M11dyGFxWnmhhcMjkbAiKCv7G1r/2QgC
github.com/hashicorp/hcl/v2 v2.13.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s=
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473 h1:Vp3YMcnM+TvVMV5TplAhGeuzz3A0562AywL32y71y3Y=
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473/go.mod h1:bdLC+qQlJIBHKbCMA6GipcuaKjmjcvZlnVdpU583z3Y=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down
34 changes: 34 additions & 0 deletions internal/addr/addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package addr

import (
tfaddr "github.com/hashicorp/terraform-registry-address"
)

// NewLegacyProvider returns a mock address for a provider.
func NewLegacyProvider(name string) tfaddr.Provider {
return tfaddr.Provider{
Type: tfaddr.MustParseProviderPart(name),
Namespace: tfaddr.LegacyProviderNamespace,
Hostname: tfaddr.DefaultProviderRegistryHost,
}
}

// NewDefaultProvider returns the default address of a HashiCorp-maintained,
// Registry-hosted provider.
func NewDefaultProvider(name string) tfaddr.Provider {
return tfaddr.Provider{
Type: tfaddr.MustParseProviderPart(name),
Namespace: "hashicorp",
Hostname: tfaddr.DefaultProviderRegistryHost,
}
}

// NewBuiltInProvider returns the address of a "built-in" provider. See
// the docs for Provider.IsBuiltIn for more information.
func NewBuiltInProvider(name string) tfaddr.Provider {
return tfaddr.Provider{
Type: tfaddr.MustParseProviderPart(name),
Namespace: tfaddr.BuiltInProviderNamespace,
Hostname: tfaddr.BuiltInProviderHost,
}
}
Loading