Skip to content

Commit bc3de6e

Browse files
Merge of cross-cutting work for new provider naming
This merge introduces various work across the whole codebase to prepare codepaths to deal with the new decentralized provider naming scheme and to use the new provider installation codepaths that are aware of the new scheme. The incoming branch of commits here (the second commit in the merge) contains a period where the tests were not passing as a tradeoff to keep the individual changes separated while accepting that that makes that part of the history unsuitable for "git bisect" usage. If you are using git bisect on this portion of the history, exclude the commits from the incoming branch.
2 parents 3ac0410 + e404074 commit bc3de6e

File tree

191 files changed

+8986
-8960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

191 files changed

+8986
-8960
lines changed

addrs/provider.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,48 @@ type Provider struct {
2323
// not have an explicit hostname.
2424
const DefaultRegistryHost = svchost.Hostname("registry.terraform.io")
2525

26+
// BuiltInProviderHost is the pseudo-hostname used for the "built-in" provider
27+
// namespace. Built-in provider addresses must also have their namespace set
28+
// to BuiltInProviderNamespace in order to be considered as built-in.
29+
const BuiltInProviderHost = svchost.Hostname("terraform.io")
30+
31+
// BuiltInProviderNamespace is the provider namespace used for "built-in"
32+
// providers. Built-in provider addresses must also have their hostname
33+
// set to BuiltInProviderHost in order to be considered as built-in.
34+
//
35+
// The this namespace is literally named "builtin", in the hope that users
36+
// who see FQNs containing this will be able to infer the way in which they are
37+
// special, even if they haven't encountered the concept formally yet.
38+
const BuiltInProviderNamespace = "builtin"
39+
2640
// LegacyProviderNamespace is the special string used in the Namespace field
2741
// of type Provider to mark a legacy provider address. This special namespace
2842
// value would normally be invalid, and can be used only when the hostname is
2943
// DefaultRegistryHost because that host owns the mapping from legacy name to
3044
// FQN.
3145
const LegacyProviderNamespace = "-"
3246

33-
// String returns an FQN string, indended for use in output.
47+
// String returns an FQN string, indended for use in machine-readable output.
3448
func (pt Provider) String() string {
3549
if pt.IsZero() {
3650
panic("called String on zero-value addrs.Provider")
3751
}
3852
return pt.Hostname.ForDisplay() + "/" + pt.Namespace + "/" + pt.Type
3953
}
4054

55+
// ForDisplay returns a user-friendly FQN string, simplified for readability. If
56+
// the provider is using the default hostname, the hostname is omitted.
57+
func (pt Provider) ForDisplay() string {
58+
if pt.IsZero() {
59+
panic("called ForDisplay on zero-value addrs.Provider")
60+
}
61+
62+
if pt.Hostname == DefaultRegistryHost {
63+
return pt.Namespace + "/" + pt.Type
64+
}
65+
return pt.Hostname.ForDisplay() + "/" + pt.Namespace + "/" + pt.Type
66+
}
67+
4168
// NewProvider constructs a provider address from its parts, and normalizes
4269
// the namespace and type parts to lowercase using unicode case folding rules
4370
// so that resulting addrs.Provider values can be compared using standard
@@ -64,6 +91,30 @@ func NewProvider(hostname svchost.Hostname, namespace, typeName string) Provider
6491
}
6592
}
6693

94+
// ImpliedProviderForUnqualifiedType represents the rules for inferring what
95+
// provider FQN a user intended when only a naked type name is available.
96+
//
97+
// For all except the type name "terraform" this returns a so-called "default"
98+
// provider, which is under the registry.terraform.io/hashicorp/ namespace.
99+
//
100+
// As a special case, the string "terraform" maps to
101+
// "terraform.io/builtin/terraform" because that is the more likely user
102+
// intent than the now-unmaintained "registry.terraform.io/hashicorp/terraform"
103+
// which remains only for compatibility with older Terraform versions.
104+
func ImpliedProviderForUnqualifiedType(typeName string) Provider {
105+
switch typeName {
106+
case "terraform":
107+
// Note for future maintainers: any additional strings we add here
108+
// as implied to be builtin must never also be use as provider names
109+
// in the registry.terraform.io/hashicorp/... namespace, because
110+
// otherwise older versions of Terraform could implicitly select
111+
// the registry name instead of the internal one.
112+
return NewBuiltInProvider(typeName)
113+
default:
114+
return NewDefaultProvider(typeName)
115+
}
116+
}
117+
67118
// NewDefaultProvider returns the default address of a HashiCorp-maintained,
68119
// Registry-hosted provider.
69120
func NewDefaultProvider(name string) Provider {
@@ -74,6 +125,16 @@ func NewDefaultProvider(name string) Provider {
74125
}
75126
}
76127

128+
// NewBuiltInProvider returns the address of a "built-in" provider. See
129+
// the docs for Provider.IsBuiltIn for more information.
130+
func NewBuiltInProvider(name string) Provider {
131+
return Provider{
132+
Type: MustParseProviderPart(name),
133+
Namespace: BuiltInProviderNamespace,
134+
Hostname: BuiltInProviderHost,
135+
}
136+
}
137+
77138
// NewLegacyProvider returns a mock address for a provider.
78139
// This will be removed when ProviderType is fully integrated.
79140
func NewLegacyProvider(name string) Provider {
@@ -109,6 +170,17 @@ func (pt Provider) IsZero() bool {
109170
return pt == Provider{}
110171
}
111172

173+
// IsBuiltIn returns true if the receiver is the address of a "built-in"
174+
// provider. That is, a provider under terraform.io/builtin/ which is
175+
// included as part of the Terraform binary itself rather than one to be
176+
// installed from elsewhere.
177+
//
178+
// These are ignored by the provider installer because they are assumed to
179+
// already be available without any further installation.
180+
func (pt Provider) IsBuiltIn() bool {
181+
return pt.Hostname == BuiltInProviderHost && pt.Namespace == BuiltInProviderNamespace
182+
}
183+
112184
// LessThan returns true if the receiver should sort before the other given
113185
// address in an ordered list of provider addresses.
114186
//

addrs/provider_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,84 @@ import (
77
svchost "github.com/hashicorp/terraform-svchost"
88
)
99

10+
func TestProviderString(t *testing.T) {
11+
tests := []struct {
12+
Input Provider
13+
Want string
14+
}{
15+
{
16+
Provider{
17+
Type: "test",
18+
Hostname: DefaultRegistryHost,
19+
Namespace: "hashicorp",
20+
},
21+
DefaultRegistryHost.ForDisplay() + "/hashicorp/test",
22+
},
23+
{
24+
Provider{
25+
Type: "test",
26+
Hostname: "registry.terraform.com",
27+
Namespace: "hashicorp",
28+
},
29+
"registry.terraform.com/hashicorp/test",
30+
},
31+
{
32+
Provider{
33+
Type: "test",
34+
Hostname: DefaultRegistryHost,
35+
Namespace: "othercorp",
36+
},
37+
DefaultRegistryHost.ForDisplay() + "/othercorp/test",
38+
},
39+
}
40+
41+
for _, test := range tests {
42+
got := test.Input.String()
43+
if got != test.Want {
44+
t.Errorf("wrong result for %s\n", test.Input.String())
45+
}
46+
}
47+
}
48+
49+
func TestProviderDisplay(t *testing.T) {
50+
tests := []struct {
51+
Input Provider
52+
Want string
53+
}{
54+
{
55+
Provider{
56+
Type: "test",
57+
Hostname: DefaultRegistryHost,
58+
Namespace: "hashicorp",
59+
},
60+
"hashicorp/test",
61+
},
62+
{
63+
Provider{
64+
Type: "test",
65+
Hostname: "registry.terraform.com",
66+
Namespace: "hashicorp",
67+
},
68+
"registry.terraform.com/hashicorp/test",
69+
},
70+
{
71+
Provider{
72+
Type: "test",
73+
Hostname: DefaultRegistryHost,
74+
Namespace: "othercorp",
75+
},
76+
"othercorp/test",
77+
},
78+
}
79+
80+
for _, test := range tests {
81+
got := test.Input.ForDisplay()
82+
if got != test.Want {
83+
t.Errorf("wrong result for %s\n", test.Input.String())
84+
}
85+
}
86+
}
87+
1088
func TestProviderIsDefault(t *testing.T) {
1189
tests := []struct {
1290
Input Provider
@@ -46,6 +124,77 @@ func TestProviderIsDefault(t *testing.T) {
46124
}
47125
}
48126

127+
func TestProviderIsBuiltIn(t *testing.T) {
128+
tests := []struct {
129+
Input Provider
130+
Want bool
131+
}{
132+
{
133+
Provider{
134+
Type: "test",
135+
Hostname: BuiltInProviderHost,
136+
Namespace: BuiltInProviderNamespace,
137+
},
138+
true,
139+
},
140+
{
141+
Provider{
142+
Type: "terraform",
143+
Hostname: BuiltInProviderHost,
144+
Namespace: BuiltInProviderNamespace,
145+
},
146+
true,
147+
},
148+
{
149+
Provider{
150+
Type: "test",
151+
Hostname: BuiltInProviderHost,
152+
Namespace: "boop",
153+
},
154+
false,
155+
},
156+
{
157+
Provider{
158+
Type: "test",
159+
Hostname: DefaultRegistryHost,
160+
Namespace: BuiltInProviderNamespace,
161+
},
162+
false,
163+
},
164+
{
165+
Provider{
166+
Type: "test",
167+
Hostname: DefaultRegistryHost,
168+
Namespace: "hashicorp",
169+
},
170+
false,
171+
},
172+
{
173+
Provider{
174+
Type: "test",
175+
Hostname: "registry.terraform.com",
176+
Namespace: "hashicorp",
177+
},
178+
false,
179+
},
180+
{
181+
Provider{
182+
Type: "test",
183+
Hostname: DefaultRegistryHost,
184+
Namespace: "othercorp",
185+
},
186+
false,
187+
},
188+
}
189+
190+
for _, test := range tests {
191+
got := test.Input.IsBuiltIn()
192+
if got != test.Want {
193+
t.Errorf("wrong result for %s\ngot: %#v\nwant: %#v", test.Input.String(), got, test.Want)
194+
}
195+
}
196+
}
197+
49198
func TestProviderIsLegacy(t *testing.T) {
50199
tests := []struct {
51200
Input Provider

backend/local/backend_apply_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestLocal_applyBasic(t *testing.T) {
5959
checkState(t, b.StateOutPath, `
6060
test_instance.foo:
6161
ID = yes
62-
provider = provider["registry.terraform.io/-/test"]
62+
provider = provider["registry.terraform.io/hashicorp/test"]
6363
ami = bar
6464
`)
6565
}
@@ -176,7 +176,7 @@ func TestLocal_applyError(t *testing.T) {
176176
checkState(t, b.StateOutPath, `
177177
test_instance.foo:
178178
ID = foo
179-
provider = provider["registry.terraform.io/-/test"]
179+
provider = provider["registry.terraform.io/hashicorp/test"]
180180
ami = bar
181181
`)
182182
}
@@ -226,7 +226,7 @@ func TestLocal_applyBackendFail(t *testing.T) {
226226
checkState(t, "errored.tfstate", `
227227
test_instance.foo:
228228
ID = yes
229-
provider = provider["registry.terraform.io/-/test"]
229+
provider = provider["registry.terraform.io/hashicorp/test"]
230230
ami = bar
231231
`)
232232
}

backend/local/backend_plan_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func TestLocal_planDeposedOnly(t *testing.T) {
216216
}`),
217217
},
218218
addrs.AbsProviderConfig{
219-
Provider: addrs.NewLegacyProvider("test"),
219+
Provider: addrs.NewDefaultProvider("test"),
220220
Module: addrs.RootModule,
221221
},
222222
)
@@ -660,7 +660,7 @@ func testPlanState() *states.State {
660660
}`),
661661
},
662662
addrs.AbsProviderConfig{
663-
Provider: addrs.NewLegacyProvider("test"),
663+
Provider: addrs.NewDefaultProvider("test"),
664664
Module: addrs.RootModule,
665665
},
666666
)
@@ -687,7 +687,7 @@ func testPlanState_withDataSource() *states.State {
687687
}`),
688688
},
689689
addrs.AbsProviderConfig{
690-
Provider: addrs.NewLegacyProvider("test"),
690+
Provider: addrs.NewDefaultProvider("test"),
691691
Module: addrs.RootModule,
692692
},
693693
)
@@ -704,7 +704,7 @@ func testPlanState_withDataSource() *states.State {
704704
}`),
705705
},
706706
addrs.AbsProviderConfig{
707-
Provider: addrs.NewLegacyProvider("test"),
707+
Provider: addrs.NewDefaultProvider("test"),
708708
Module: addrs.RootModule,
709709
},
710710
)
@@ -731,7 +731,7 @@ func testPlanState_tainted() *states.State {
731731
}`),
732732
},
733733
addrs.AbsProviderConfig{
734-
Provider: addrs.NewLegacyProvider("test"),
734+
Provider: addrs.NewDefaultProvider("test"),
735735
Module: addrs.RootModule,
736736
},
737737
)

0 commit comments

Comments
 (0)