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 23, 2022
1 parent b53bd69 commit aed202d
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 105 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.13.0
github.com/hashicorp/terraform-exec v0.17.0
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-20220617163605-9aece33a9bbb
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
github.com/hashicorp/terraform-schema v0.0.0-20220623144511-7930e05d32f8
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
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/hashicorp/hcl-lang v0.0.0-20220421093840-480fdfd2ecb5 h1:A18R+0Emk5YuXo22u44wxwHAnNHLDK6NHxXl2CIQGlU=
github.com/hashicorp/hcl-lang v0.0.0-20220421093840-480fdfd2ecb5/go.mod h1:oQgcOV8OizFyZfZh3FbQSsQtvtTv8hD23MLAxfn3E+E=
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.12.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.13.0 h1:0Apadu1w6M11dyGFxWnmhhcMjkbAiKCv7G1r/2QgCNc=
github.com/hashicorp/hcl/v2 v2.13.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand All @@ -343,10 +342,10 @@ github.com/hashicorp/terraform-exec v0.17.0 h1:fbhFnrn9QLN2jt+TDDBfLmdZyW3w1d5Kc
github.com/hashicorp/terraform-exec v0.17.0/go.mod h1:P6V5KRHsLIu0vMlaKBgSbF+ADUXZhcE2n/wGX66Bcf0=
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-20220617163605-9aece33a9bbb h1:ZVpBMZlvIwHzFfSfNCspuH5WG8p5N8fn1Rjqayoxbmc=
github.com/hashicorp/terraform-schema v0.0.0-20220617163605-9aece33a9bbb/go.mod h1:zphXkRtXhZ1vC60ytqhUBSnKDyYHBaV321zNgYuLKxs=
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-schema v0.0.0-20220623144511-7930e05d32f8 h1:Smd1F42NuW2MCrS9MaolywhWnCPBosYa/JSniOusDiw=
github.com/hashicorp/terraform-schema v0.0.0-20220623144511-7930e05d32f8/go.mod h1:d1/gzOC/dLDNRI0lmhRPEtZoQl/9eaRjDGSb/GnliPo=
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
2 changes: 1 addition & 1 deletion internal/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ModuleReader interface {
List() ([]*state.Module, error)
ModuleCalls(modPath string) (tfmod.ModuleCalls, error)
LocalModuleMeta(modPath string) (*tfmod.Meta, error)
RegistryModuleMeta(addr tfaddr.ModuleSourceRegistry, cons version.Constraints) (*registry.ModuleData, error)
RegistryModuleMeta(addr tfaddr.Module, cons version.Constraints) (*registry.ModuleData, error)
}

type PathReader struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ func TestLangServer_DidChangeWatchedFiles_pluginChange(t *testing.T) {
ReqParams: "{}",
})

addr := tfaddr.MustParseRawProviderSourceString("-/test")
addr := tfaddr.MustParseProviderSource("-/test")
vc := version.MustConstraints(version.NewConstraint(">= 1.0"))

_, err = ss.ProviderSchemas.ProviderSchema(testHandle.Path(), addr, vc)
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
16 changes: 8 additions & 8 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewClient() Client {
}
}

func (c Client) GetModuleData(addr tfaddr.ModuleSourceRegistry, cons version.Constraints) (*ModuleResponse, error) {
func (c Client) GetModuleData(addr tfaddr.Module, cons version.Constraints) (*ModuleResponse, error) {
var response ModuleResponse

v, err := c.GetMatchingModuleVersion(addr, cons)
Expand All @@ -41,9 +41,9 @@ func (c Client) GetModuleData(addr tfaddr.ModuleSourceRegistry, cons version.Con
client.Timeout = defaultTimeout

url := fmt.Sprintf("%s/v1/modules/%s/%s/%s/%s", c.BaseURL,
addr.PackageAddr.Namespace,
addr.PackageAddr.Name,
addr.PackageAddr.TargetSystem,
addr.Package.Namespace,
addr.Package.Name,
addr.Package.TargetSystem,
v.String())
resp, err := client.Get(url)
if err != nil {
Expand All @@ -67,11 +67,11 @@ func (c Client) GetModuleData(addr tfaddr.ModuleSourceRegistry, cons version.Con
return &response, nil
}

func (c Client) GetMatchingModuleVersion(addr tfaddr.ModuleSourceRegistry, con version.Constraints) (*version.Version, error) {
func (c Client) GetMatchingModuleVersion(addr tfaddr.Module, con version.Constraints) (*version.Version, error) {
url := fmt.Sprintf("%s/v1/modules/%s/%s/%s/versions", c.BaseURL,
addr.PackageAddr.Namespace,
addr.PackageAddr.Name,
addr.PackageAddr.TargetSystem)
addr.Package.Namespace,
addr.Package.Name,
addr.Package.TargetSystem)

client := cleanhttp.DefaultClient()
client.Timeout = defaultTimeout
Expand Down
4 changes: 2 additions & 2 deletions internal/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestGetModuleData(t *testing.T) {
addr, err := tfaddr.ParseRawModuleSourceRegistry("puppetlabs/deployment/ec")
addr, err := tfaddr.ParseModuleSource("puppetlabs/deployment/ec")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestGetModuleData(t *testing.T) {
}

func TestGetMatchingModuleVersion(t *testing.T) {
addr, err := tfaddr.ParseRawModuleSourceRegistry("puppetlabs/deployment/ec")
addr, err := tfaddr.ParseModuleSource("puppetlabs/deployment/ec")
if err != nil {
t.Fatal(err)
}
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
2 changes: 1 addition & 1 deletion internal/state/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (s *ModuleStore) LocalModuleMeta(modPath string) (*tfmod.Meta, error) {
}, nil
}

func (s *ModuleStore) RegistryModuleMeta(addr tfaddr.ModuleSourceRegistry, cons version.Constraints) (*registry.ModuleData, error) {
func (s *ModuleStore) RegistryModuleMeta(addr tfaddr.Module, cons version.Constraints) (*registry.ModuleData, error) {
txn := s.db.Txn(false)

it, err := txn.Get(registryModuleTableName, "source_addr", addr)
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 @@ -241,12 +241,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 @@ -270,12 +270,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 aed202d

Please sign in to comment.