Skip to content

Commit 6be6f69

Browse files
authored
Merge pull request #33482 from hashicorp/jbardin/schema-cache
Refactor providers.Schemas and add a global schema cache
2 parents d1a5dfa + a77baa8 commit 6be6f69

Some content is hidden

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

58 files changed

+752
-762
lines changed

internal/backend/local/backend_apply.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,7 @@ func (b *Local) opApply(
9999
// plan.Errored will be true in this case, which our plan
100100
// renderer can rely on to tailor its messaging.
101101
if plan != nil && (len(plan.Changes.Resources) != 0 || len(plan.Changes.Outputs) != 0) {
102-
schemas, moreDiags := lr.Core.Schemas(lr.Config, lr.InputState)
103-
// If schema loading returns errors then we'll just give up and
104-
// ignore them to avoid distracting from the plan-time errors we're
105-
// mainly trying to report here.
106-
if !moreDiags.HasErrors() {
107-
op.View.Plan(plan, schemas)
108-
}
102+
op.View.Plan(plan, schemas)
109103
}
110104
op.ReportResult(runningOp, diags)
111105
return

internal/backend/local/backend_apply_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/hashicorp/terraform/internal/states"
2828
"github.com/hashicorp/terraform/internal/states/statemgr"
2929
"github.com/hashicorp/terraform/internal/terminal"
30-
"github.com/hashicorp/terraform/internal/terraform"
3130
"github.com/hashicorp/terraform/internal/tfdiags"
3231
)
3332

@@ -123,7 +122,7 @@ func TestLocal_applyCheck(t *testing.T) {
123122
func TestLocal_applyEmptyDir(t *testing.T) {
124123
b := TestLocal(t)
125124

126-
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
125+
p := TestLocalProvider(t, b, "test", providers.ProviderSchema{})
127126
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{"id": cty.StringVal("yes")})}
128127

129128
op, configCleanup, done := testOperationApply(t, "./testdata/empty")
@@ -157,7 +156,7 @@ func TestLocal_applyEmptyDir(t *testing.T) {
157156
func TestLocal_applyEmptyDirDestroy(t *testing.T) {
158157
b := TestLocal(t)
159158

160-
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
159+
p := TestLocalProvider(t, b, "test", providers.ProviderSchema{})
161160
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{}
162161

163162
op, configCleanup, done := testOperationApply(t, "./testdata/empty")
@@ -187,12 +186,14 @@ func TestLocal_applyEmptyDirDestroy(t *testing.T) {
187186
func TestLocal_applyError(t *testing.T) {
188187
b := TestLocal(t)
189188

190-
schema := &terraform.ProviderSchema{
191-
ResourceTypes: map[string]*configschema.Block{
189+
schema := providers.ProviderSchema{
190+
ResourceTypes: map[string]providers.Schema{
192191
"test_instance": {
193-
Attributes: map[string]*configschema.Attribute{
194-
"ami": {Type: cty.String, Optional: true},
195-
"id": {Type: cty.String, Computed: true},
192+
Block: &configschema.Block{
193+
Attributes: map[string]*configschema.Attribute{
194+
"ami": {Type: cty.String, Optional: true},
195+
"id": {Type: cty.String, Computed: true},
196+
},
196197
},
197198
},
198199
},
@@ -386,13 +387,15 @@ func testOperationApply(t *testing.T, configDir string) (*backend.Operation, fun
386387
// applyFixtureSchema returns a schema suitable for processing the
387388
// configuration in testdata/apply . This schema should be
388389
// assigned to a mock provider named "test".
389-
func applyFixtureSchema() *terraform.ProviderSchema {
390-
return &terraform.ProviderSchema{
391-
ResourceTypes: map[string]*configschema.Block{
390+
func applyFixtureSchema() providers.ProviderSchema {
391+
return providers.ProviderSchema{
392+
ResourceTypes: map[string]providers.Schema{
392393
"test_instance": {
393-
Attributes: map[string]*configschema.Attribute{
394-
"ami": {Type: cty.String, Optional: true},
395-
"id": {Type: cty.String, Computed: true},
394+
Block: &configschema.Block{
395+
Attributes: map[string]*configschema.Attribute{
396+
"ami": {Type: cty.String, Optional: true},
397+
"id": {Type: cty.String, Computed: true},
398+
},
396399
},
397400
},
398401
},

internal/backend/local/backend_plan_test.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hashicorp/terraform/internal/initwd"
2121
"github.com/hashicorp/terraform/internal/plans"
2222
"github.com/hashicorp/terraform/internal/plans/planfile"
23+
"github.com/hashicorp/terraform/internal/providers"
2324
"github.com/hashicorp/terraform/internal/states"
2425
"github.com/hashicorp/terraform/internal/terminal"
2526
"github.com/hashicorp/terraform/internal/terraform"
@@ -88,7 +89,7 @@ func TestLocal_planInAutomation(t *testing.T) {
8889

8990
func TestLocal_planNoConfig(t *testing.T) {
9091
b := TestLocal(t)
91-
TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
92+
TestLocalProvider(t, b, "test", providers.ProviderSchema{})
9293

9394
op, configCleanup, done := testOperationPlan(t, "./testdata/empty")
9495
defer configCleanup()
@@ -854,30 +855,34 @@ func testReadPlan(t *testing.T, path string) *plans.Plan {
854855
// planFixtureSchema returns a schema suitable for processing the
855856
// configuration in testdata/plan . This schema should be
856857
// assigned to a mock provider named "test".
857-
func planFixtureSchema() *terraform.ProviderSchema {
858-
return &terraform.ProviderSchema{
859-
ResourceTypes: map[string]*configschema.Block{
858+
func planFixtureSchema() providers.ProviderSchema {
859+
return providers.ProviderSchema{
860+
ResourceTypes: map[string]providers.Schema{
860861
"test_instance": {
861-
Attributes: map[string]*configschema.Attribute{
862-
"ami": {Type: cty.String, Optional: true},
863-
},
864-
BlockTypes: map[string]*configschema.NestedBlock{
865-
"network_interface": {
866-
Nesting: configschema.NestingList,
867-
Block: configschema.Block{
868-
Attributes: map[string]*configschema.Attribute{
869-
"device_index": {Type: cty.Number, Optional: true},
870-
"description": {Type: cty.String, Optional: true},
862+
Block: &configschema.Block{
863+
Attributes: map[string]*configschema.Attribute{
864+
"ami": {Type: cty.String, Optional: true},
865+
},
866+
BlockTypes: map[string]*configschema.NestedBlock{
867+
"network_interface": {
868+
Nesting: configschema.NestingList,
869+
Block: configschema.Block{
870+
Attributes: map[string]*configschema.Attribute{
871+
"device_index": {Type: cty.Number, Optional: true},
872+
"description": {Type: cty.String, Optional: true},
873+
},
871874
},
872875
},
873876
},
874877
},
875878
},
876879
},
877-
DataSources: map[string]*configschema.Block{
880+
DataSources: map[string]providers.Schema{
878881
"test_ds": {
879-
Attributes: map[string]*configschema.Attribute{
880-
"filter": {Type: cty.String, Required: true},
882+
Block: &configschema.Block{
883+
Attributes: map[string]*configschema.Attribute{
884+
"filter": {Type: cty.String, Required: true},
885+
},
881886
},
882887
},
883888
},

internal/backend/local/backend_refresh_test.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,22 @@ test_instance.foo:
6363
func TestLocal_refreshInput(t *testing.T) {
6464
b := TestLocal(t)
6565

66-
schema := &terraform.ProviderSchema{
67-
Provider: &configschema.Block{
68-
Attributes: map[string]*configschema.Attribute{
69-
"value": {Type: cty.String, Optional: true},
66+
schema := providers.ProviderSchema{
67+
Provider: providers.Schema{
68+
Block: &configschema.Block{
69+
Attributes: map[string]*configschema.Attribute{
70+
"value": {Type: cty.String, Optional: true},
71+
},
7072
},
7173
},
72-
ResourceTypes: map[string]*configschema.Block{
74+
ResourceTypes: map[string]providers.Schema{
7375
"test_instance": {
74-
Attributes: map[string]*configschema.Attribute{
75-
"id": {Type: cty.String, Computed: true},
76-
"foo": {Type: cty.String, Optional: true},
77-
"ami": {Type: cty.String, Optional: true},
76+
Block: &configschema.Block{
77+
Attributes: map[string]*configschema.Attribute{
78+
"id": {Type: cty.String, Computed: true},
79+
"foo": {Type: cty.String, Optional: true},
80+
"ami": {Type: cty.String, Optional: true},
81+
},
7882
},
7983
},
8084
},
@@ -154,17 +158,21 @@ test_instance.foo:
154158
func TestLocal_refreshValidateProviderConfigured(t *testing.T) {
155159
b := TestLocal(t)
156160

157-
schema := &terraform.ProviderSchema{
158-
Provider: &configschema.Block{
159-
Attributes: map[string]*configschema.Attribute{
160-
"value": {Type: cty.String, Optional: true},
161+
schema := providers.ProviderSchema{
162+
Provider: providers.Schema{
163+
Block: &configschema.Block{
164+
Attributes: map[string]*configschema.Attribute{
165+
"value": {Type: cty.String, Optional: true},
166+
},
161167
},
162168
},
163-
ResourceTypes: map[string]*configschema.Block{
169+
ResourceTypes: map[string]providers.Schema{
164170
"test_instance": {
165-
Attributes: map[string]*configschema.Attribute{
166-
"id": {Type: cty.String, Computed: true},
167-
"ami": {Type: cty.String, Optional: true},
171+
Block: &configschema.Block{
172+
Attributes: map[string]*configschema.Attribute{
173+
"id": {Type: cty.String, Computed: true},
174+
"ami": {Type: cty.String, Optional: true},
175+
},
168176
},
169177
},
170178
},
@@ -297,13 +305,15 @@ func testRefreshState() *states.State {
297305
// refreshFixtureSchema returns a schema suitable for processing the
298306
// configuration in testdata/refresh . This schema should be
299307
// assigned to a mock provider named "test".
300-
func refreshFixtureSchema() *terraform.ProviderSchema {
301-
return &terraform.ProviderSchema{
302-
ResourceTypes: map[string]*configschema.Block{
308+
func refreshFixtureSchema() providers.ProviderSchema {
309+
return providers.ProviderSchema{
310+
ResourceTypes: map[string]providers.Schema{
303311
"test_instance": {
304-
Attributes: map[string]*configschema.Attribute{
305-
"ami": {Type: cty.String, Optional: true},
306-
"id": {Type: cty.String, Computed: true},
312+
Block: &configschema.Block{
313+
Attributes: map[string]*configschema.Attribute{
314+
"ami": {Type: cty.String, Optional: true},
315+
"id": {Type: cty.String, Computed: true},
316+
},
307317
},
308318
},
309319
},

internal/backend/local/testing.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,11 @@ func TestLocal(t *testing.T) *Local {
4242

4343
// TestLocalProvider modifies the ContextOpts of the *Local parameter to
4444
// have a provider with the given name.
45-
func TestLocalProvider(t *testing.T, b *Local, name string, schema *terraform.ProviderSchema) *terraform.MockProvider {
45+
func TestLocalProvider(t *testing.T, b *Local, name string, schema providers.ProviderSchema) *terraform.MockProvider {
4646
// Build a mock resource provider for in-memory operations
4747
p := new(terraform.MockProvider)
4848

49-
if schema == nil {
50-
schema = &terraform.ProviderSchema{} // default schema is empty
51-
}
52-
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
53-
Provider: providers.Schema{Block: schema.Provider},
54-
ProviderMeta: providers.Schema{Block: schema.ProviderMeta},
55-
ResourceTypes: map[string]providers.Schema{},
56-
DataSources: map[string]providers.Schema{},
57-
}
58-
for name, res := range schema.ResourceTypes {
59-
p.GetProviderSchemaResponse.ResourceTypes[name] = providers.Schema{
60-
Block: res,
61-
Version: int64(schema.ResourceTypeSchemaVersions[name]),
62-
}
63-
}
64-
for name, dat := range schema.DataSources {
65-
p.GetProviderSchemaResponse.DataSources[name] = providers.Schema{Block: dat}
66-
}
49+
p.GetProviderSchemaResponse = &schema
6750

6851
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
6952
// this is a destroy plan,

internal/backend/remote/testing.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ func testLocalBackend(t *testing.T, remote *Remote) backend.Enhanced {
182182
b := backendLocal.NewWithBackend(remote)
183183

184184
// Add a test provider to the local backend.
185-
p := backendLocal.TestLocalProvider(t, b, "null", &terraform.ProviderSchema{
186-
ResourceTypes: map[string]*configschema.Block{
185+
p := backendLocal.TestLocalProvider(t, b, "null", providers.ProviderSchema{
186+
ResourceTypes: map[string]providers.Schema{
187187
"null_resource": {
188-
Attributes: map[string]*configschema.Attribute{
189-
"id": {Type: cty.String, Computed: true},
188+
Block: &configschema.Block{
189+
Attributes: map[string]*configschema.Attribute{
190+
"id": {Type: cty.String, Computed: true},
191+
},
190192
},
191193
},
192194
},

internal/cloud/testing.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,13 @@ func testLocalBackend(t *testing.T, cloud *Cloud) backend.Enhanced {
345345
b := backendLocal.NewWithBackend(cloud)
346346

347347
// Add a test provider to the local backend.
348-
p := backendLocal.TestLocalProvider(t, b, "null", &terraform.ProviderSchema{
349-
ResourceTypes: map[string]*configschema.Block{
348+
p := backendLocal.TestLocalProvider(t, b, "null", providers.ProviderSchema{
349+
ResourceTypes: map[string]providers.Schema{
350350
"null_resource": {
351-
Attributes: map[string]*configschema.Attribute{
352-
"id": {Type: cty.String, Computed: true},
351+
Block: &configschema.Block{
352+
Attributes: map[string]*configschema.Attribute{
353+
"id": {Type: cty.String, Computed: true},
354+
},
353355
},
354356
},
355357
},

internal/command/jsonformat/plan_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6987,13 +6987,17 @@ func runTestCases(t *testing.T, testCases map[string]testCase) {
69876987
}
69886988

69896989
tfschemas := &terraform.Schemas{
6990-
Providers: map[addrs.Provider]*providers.Schemas{
6990+
Providers: map[addrs.Provider]providers.ProviderSchema{
69916991
src.ProviderAddr.Provider: {
6992-
ResourceTypes: map[string]*configschema.Block{
6993-
src.Addr.Resource.Resource.Type: tc.Schema,
6992+
ResourceTypes: map[string]providers.Schema{
6993+
src.Addr.Resource.Resource.Type: {
6994+
Block: tc.Schema,
6995+
},
69946996
},
6995-
DataSources: map[string]*configschema.Block{
6996-
src.Addr.Resource.Resource.Type: tc.Schema,
6997+
DataSources: map[string]providers.Schema{
6998+
src.Addr.Resource.Resource.Type: {
6999+
Block: tc.Schema,
7000+
},
69977001
},
69987002
},
69997003
},

internal/command/jsonformat/state_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func testProviderSchema() *providers.GetProviderSchemaResponse {
156156
func testSchemas() *terraform.Schemas {
157157
provider := testProvider()
158158
return &terraform.Schemas{
159-
Providers: map[addrs.Provider]*terraform.ProviderSchema{
160-
addrs.NewDefaultProvider("test"): provider.ProviderSchema(),
159+
Providers: map[addrs.Provider]providers.ProviderSchema{
160+
addrs.NewDefaultProvider("test"): provider.GetProviderSchema(),
161161
},
162162
}
163163
}

internal/command/jsonplan/values_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform/internal/addrs"
1212
"github.com/hashicorp/terraform/internal/configs/configschema"
1313
"github.com/hashicorp/terraform/internal/plans"
14+
"github.com/hashicorp/terraform/internal/providers"
1415
"github.com/hashicorp/terraform/internal/terraform"
1516
"github.com/zclconf/go-cty/cty"
1617
)
@@ -344,19 +345,19 @@ func TestMarshalPlanValuesNoopDeposed(t *testing.T) {
344345

345346
func testSchemas() *terraform.Schemas {
346347
return &terraform.Schemas{
347-
Providers: map[addrs.Provider]*terraform.ProviderSchema{
348-
addrs.NewDefaultProvider("test"): &terraform.ProviderSchema{
349-
ResourceTypes: map[string]*configschema.Block{
348+
Providers: map[addrs.Provider]providers.ProviderSchema{
349+
addrs.NewDefaultProvider("test"): providers.ProviderSchema{
350+
ResourceTypes: map[string]providers.Schema{
350351
"test_thing": {
351-
Attributes: map[string]*configschema.Attribute{
352-
"woozles": {Type: cty.String, Optional: true, Computed: true},
353-
"foozles": {Type: cty.String, Optional: true},
352+
Version: 1,
353+
Block: &configschema.Block{
354+
Attributes: map[string]*configschema.Attribute{
355+
"woozles": {Type: cty.String, Optional: true, Computed: true},
356+
"foozles": {Type: cty.String, Optional: true},
357+
},
354358
},
355359
},
356360
},
357-
ResourceTypeSchemaVersions: map[string]uint64{
358-
"test_thing": 1,
359-
},
360361
},
361362
},
362363
}

0 commit comments

Comments
 (0)