Skip to content

Commit

Permalink
Switch to the new minimal terraform-registry-address API
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jun 13, 2022
1 parent 3d3992e commit 6bc5982
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 82 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ require (
github.com/hashicorp/hcl/v2 v2.12.0
github.com/hashicorp/terraform-exec v0.16.1
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473
github.com/hashicorp/terraform-schema v0.0.0-20220509053855-1e3acbcfd531
github.com/hashicorp/terraform-registry-address v0.0.0-20220610213838-4b195be62a2c
github.com/hashicorp/terraform-schema v0.0.0-20220613102243-087f761cefcc
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.4
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ github.com/hashicorp/terraform-exec v0.16.1/go.mod h1:aj0lVshy8l+MHhFNoijNHtqTJQ
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
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-schema v0.0.0-20220509053855-1e3acbcfd531 h1:CVBByNVwgdRBKz6hdrL547Rw6RU4QF7sDnxvISdoBxM=
github.com/hashicorp/terraform-schema v0.0.0-20220509053855-1e3acbcfd531/go.mod h1:rLQP6aOmOcA+C68h3Ea7utboW/UWwgn5m8i/pE5rm28=
github.com/hashicorp/terraform-registry-address v0.0.0-20220610213838-4b195be62a2c h1:0cc3Lrc+A3fg/Gk10bPiTiD1imahActENCZCQAVGgLo=
github.com/hashicorp/terraform-registry-address v0.0.0-20220610213838-4b195be62a2c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI=
github.com/hashicorp/terraform-schema v0.0.0-20220613102243-087f761cefcc h1:iWki6sYq50omYpkc8jWwx0lnuDWv9svUc69dcYGXtKA=
github.com/hashicorp/terraform-schema v0.0.0-20220613102243-087f761cefcc/go.mod h1:Bpz8NN1HQdkzaMD6i7vMFp0n4Z5Hkmq+Yh5pqJQNCT0=
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/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func TestLangServer_workspaceExecuteCommand_moduleProviders_basic(t *testing.T)
Path: modDir,
CoreRequirements: testConstraint(t, "~> 0.15"),
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
tfaddr.NewDefaultProvider("aws"): testConstraint(t, "1.2.3"),
tfaddr.NewDefaultProvider("google"): testConstraint(t, ">= 2.0.0"),
newDefaultProvider("aws"): testConstraint(t, "1.2.3"),
newDefaultProvider("google"): testConstraint(t, ">= 2.0.0"),
},
ProviderReferences: map[tfmod.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: tfaddr.NewDefaultProvider("aws"),
{LocalName: "google"}: tfaddr.NewDefaultProvider("google"),
{LocalName: "aws"}: newDefaultProvider("aws"),
{LocalName: "google"}: newDefaultProvider("google"),
},
}

Expand All @@ -94,8 +94,8 @@ func TestLangServer_workspaceExecuteCommand_moduleProviders_basic(t *testing.T)
}

pVersions := map[tfaddr.Provider]*version.Version{
tfaddr.NewDefaultProvider("aws"): version.Must(version.NewVersion("1.2.3")),
tfaddr.NewDefaultProvider("google"): version.Must(version.NewVersion("2.5.5")),
newDefaultProvider("aws"): version.Must(version.NewVersion("1.2.3")),
newDefaultProvider("google"): version.Must(version.NewVersion("2.5.5")),
}
err = s.Modules.UpdateInstalledProviders(modDir, pVersions)
if err != nil {
Expand Down Expand Up @@ -159,6 +159,10 @@ func TestLangServer_workspaceExecuteCommand_moduleProviders_basic(t *testing.T)
}`)
}

func newDefaultProvider(name string) tfaddr.Provider {
return tfaddr.NewProvider(tfaddr.DefaultProviderRegistryHost, "hashicorp", name)
}

func testConstraint(t *testing.T, v string) version.Constraints {
constraints, err := version.NewConstraint(v)
if err != nil {
Expand Down
9 changes: 3 additions & 6 deletions internal/schemas/preload_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ func PreloadSchemasToStore(pss *state.ProviderSchemaStore) error {
for rawAddr, pJsonSchema := range pOut.Schemas {
pv := vOut.Providers[rawAddr]

pAddr, err := tfaddr.ParseRawProviderSourceString(rawAddr)
pAddr, err := tfaddr.ParseProviderSource(rawAddr)
if err != nil {
// skip unparsable address
continue
}
// Given that we use Terraform >0.12 for the generation
// this should never happen
if pAddr.IsLegacy() {
iAddr, err := tfaddr.ParseAndInferProviderSourceString(rawAddr)
if err == nil {
pAddr = iAddr
}
if pAddr.IsLegacy() || !pAddr.HasKnownNamespace() {
pAddr.Hostname = "hashicorp"
}

pSchema := tfschema.ProviderSchemaFromJson(pJsonSchema, pAddr)
Expand Down
25 changes: 12 additions & 13 deletions internal/state/installed_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/hashicorp/go-version"
tfaddr "github.com/hashicorp/terraform-registry-address"
)

func TestInstalledProviders(t *testing.T) {
Expand All @@ -20,48 +19,48 @@ func TestInstalledProviders(t *testing.T) {
},
{
InstalledProviders{
tfaddr.NewBuiltInProvider("terraform"): version.Must(version.NewVersion("1.0")),
NewBuiltInProvider("terraform"): version.Must(version.NewVersion("1.0")),
},
InstalledProviders{
tfaddr.NewBuiltInProvider("terraform"): version.Must(version.NewVersion("1.0")),
NewBuiltInProvider("terraform"): version.Must(version.NewVersion("1.0")),
},
true,
},
{
InstalledProviders{
tfaddr.NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
},
InstalledProviders{
tfaddr.NewDefaultProvider("bar"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("bar"): version.Must(version.NewVersion("1.0")),
},
false,
},
{
InstalledProviders{
tfaddr.NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
},
InstalledProviders{
tfaddr.NewDefaultProvider("foo"): version.Must(version.NewVersion("1.1")),
NewDefaultProvider("foo"): version.Must(version.NewVersion("1.1")),
},
false,
},
{
InstalledProviders{
tfaddr.NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
tfaddr.NewDefaultProvider("bar"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("bar"): version.Must(version.NewVersion("1.0")),
},
InstalledProviders{
tfaddr.NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
},
false,
},
{
InstalledProviders{
tfaddr.NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
},
InstalledProviders{
tfaddr.NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
tfaddr.NewDefaultProvider("bar"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("foo"): version.Must(version.NewVersion("1.0")),
NewDefaultProvider("bar"): version.Must(version.NewVersion("1.0")),
},
false,
},
Expand Down
16 changes: 8 additions & 8 deletions internal/state/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ func TestModuleStore_UpdateMetadata(t *testing.T) {
Path: tmpDir,
CoreRequirements: testConstraint(t, "~> 0.15"),
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
tfaddr.NewDefaultProvider("aws"): testConstraint(t, "1.2.3"),
tfaddr.NewDefaultProvider("google"): testConstraint(t, ">= 2.0.0"),
NewDefaultProvider("aws"): testConstraint(t, "1.2.3"),
NewDefaultProvider("google"): testConstraint(t, ">= 2.0.0"),
},
ProviderReferences: map[tfmod.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: tfaddr.NewDefaultProvider("aws"),
{LocalName: "google"}: tfaddr.NewDefaultProvider("google"),
{LocalName: "aws"}: NewDefaultProvider("aws"),
{LocalName: "google"}: NewDefaultProvider("google"),
},
}

Expand All @@ -268,12 +268,12 @@ func TestModuleStore_UpdateMetadata(t *testing.T) {
Meta: ModuleMetadata{
CoreRequirements: testConstraint(t, "~> 0.15"),
ProviderRequirements: map[tfaddr.Provider]version.Constraints{
tfaddr.NewDefaultProvider("aws"): testConstraint(t, "1.2.3"),
tfaddr.NewDefaultProvider("google"): testConstraint(t, ">= 2.0.0"),
NewDefaultProvider("aws"): testConstraint(t, "1.2.3"),
NewDefaultProvider("google"): testConstraint(t, ">= 2.0.0"),
},
ProviderReferences: map[tfmod.ProviderRef]tfaddr.Provider{
{LocalName: "aws"}: tfaddr.NewDefaultProvider("aws"),
{LocalName: "google"}: tfaddr.NewDefaultProvider("google"),
{LocalName: "aws"}: NewDefaultProvider("aws"),
{LocalName: "google"}: NewDefaultProvider("google"),
},
},
MetaState: operation.OpStateLoaded,
Expand Down
30 changes: 27 additions & 3 deletions internal/state/provider_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ func (s *ProviderSchemaStore) ProviderSchema(modPath string, addr tfaddr.Provide
}
}

if len(schemas) == 0 && addr.Equals(tfaddr.NewDefaultProvider("terraform")) {
if len(schemas) == 0 && addr.Equals(NewDefaultProvider("terraform")) {
// assume that hashicorp/terraform is just the builtin provider
return s.ProviderSchema(modPath, tfaddr.NewBuiltInProvider("terraform"), vc)
return s.ProviderSchema(modPath, NewBuiltInProvider("terraform"), vc)
}

if len(schemas) == 0 && addr.IsLegacy() {
if addr.Type == "terraform" {
return s.ProviderSchema(modPath, tfaddr.NewBuiltInProvider("terraform"), vc)
return s.ProviderSchema(modPath, NewBuiltInProvider("terraform"), vc)
}

// Schema may be missing e.g. because Terraform 0.12
Expand Down Expand Up @@ -275,6 +275,30 @@ func (s *ProviderSchemaStore) ProviderSchema(modPath string, addr tfaddr.Provide

type ModuleLookupFunc func(string) (*Module, error)

func NewDefaultProvider(name string) tfaddr.Provider {
return tfaddr.Provider{
Type: tfaddr.MustParseProviderPart(name),
Namespace: "hashicorp",
Hostname: tfaddr.DefaultProviderRegistryHost,
}
}

func NewBuiltInProvider(name string) tfaddr.Provider {
return tfaddr.Provider{
Type: tfaddr.MustParseProviderPart(name),
Namespace: tfaddr.BuiltInProviderNamespace,
Hostname: tfaddr.BuiltInProviderHost,
}
}

func NewLegacyProvider(name string) tfaddr.Provider {
return tfaddr.Provider{
Type: tfaddr.MustParseProviderPart(name),
Namespace: tfaddr.LegacyProviderNamespace,
Hostname: tfaddr.DefaultProviderRegistryHost,
}
}

type sortableSchemas struct {
schemas []*ProviderSchema
lookupModule ModuleLookupFunc
Expand Down
Loading

0 comments on commit 6bc5982

Please sign in to comment.