From d53a4ac588f3d12a22d1bda9dc71e047c057c4a5 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Fri, 14 Aug 2020 14:56:22 -0400 Subject: [PATCH] addrs: Fix LegacyString for builtin provider addrs Builtin provider addrs (i.e. "terraform.io/builtin/terraform") should be able to convert to legacy string form (i.e. "terraform"). This ensures that we can safely round-trip through ParseLegacyAbsProviderConfig, which can return either a legacy or a builtin provider addr. --- addrs/provider.go | 4 ++-- addrs/provider_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/addrs/provider.go b/addrs/provider.go index c2e193f4e..c0055c66f 100644 --- a/addrs/provider.go +++ b/addrs/provider.go @@ -156,8 +156,8 @@ func (pt Provider) LegacyString() string { if pt.IsZero() { panic("called LegacyString on zero-value addrs.Provider") } - if pt.Namespace != LegacyProviderNamespace { - panic(pt.String() + " is not a legacy addrs.Provider") + if pt.Namespace != LegacyProviderNamespace && pt.Namespace != BuiltInProviderNamespace { + panic(pt.String() + " cannot be represented as a legacy string") } return pt.Type } diff --git a/addrs/provider_test.go b/addrs/provider_test.go index b58428593..0e02a61fb 100644 --- a/addrs/provider_test.go +++ b/addrs/provider_test.go @@ -54,6 +54,37 @@ func TestProviderString(t *testing.T) { } } +func TestProviderLegacyString(t *testing.T) { + tests := []struct { + Input Provider + Want string + }{ + { + Provider{ + Type: "test", + Hostname: DefaultRegistryHost, + Namespace: LegacyProviderNamespace, + }, + "test", + }, + { + Provider{ + Type: "terraform", + Hostname: BuiltInProviderHost, + Namespace: BuiltInProviderNamespace, + }, + "terraform", + }, + } + + for _, test := range tests { + got := test.Input.LegacyString() + if got != test.Want { + t.Errorf("wrong result for %s\ngot: %s\nwant: %s", test.Input.String(), got, test.Want) + } + } +} + func TestProviderDisplay(t *testing.T) { tests := []struct { Input Provider