Skip to content

Commit

Permalink
Merge pull request #33807 from hashicorp/jbardin/cached-schema
Browse files Browse the repository at this point in the history
move `GetProviderSchemaOptional` check to correct block
  • Loading branch information
jbardin authored Sep 6, 2023
2 parents 35baa30 + f971348 commit a6789a7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
9 changes: 6 additions & 3 deletions internal/plugin/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ type GRPCProvider struct {
schema providers.GetProviderSchemaResponse
}

func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResponse) {
func (p *GRPCProvider) GetProviderSchema() providers.GetProviderSchemaResponse {
logger.Trace("GRPCProvider: GetProviderSchema")
p.mu.Lock()
defer p.mu.Unlock()

// check the global cache if we can
if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional {
if resp, ok := providers.SchemaCache.Get(p.Addr); ok {
if !p.Addr.IsZero() {
if resp, ok := providers.SchemaCache.Get(p.Addr); ok && resp.ServerCapabilities.GetProviderSchemaOptional {
logger.Trace("GRPCProvider: returning cached schema", p.Addr.String())
return resp
}
}
Expand All @@ -91,6 +92,8 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp
return p.schema
}

var resp providers.GetProviderSchemaResponse

resp.ResourceTypes = make(map[string]providers.Schema)
resp.DataSources = make(map[string]providers.Schema)

Expand Down
25 changes: 25 additions & 0 deletions internal/plugin/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs/hcl2shim"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
Expand Down Expand Up @@ -92,6 +93,9 @@ func providerProtoSchema() *proto.GetProviderSchema_Response {
},
},
},
ServerCapabilities: &proto.ServerCapabilities{
GetProviderSchemaOptional: true,
},
}
}

Expand All @@ -104,6 +108,27 @@ func TestGRPCProvider_GetSchema(t *testing.T) {
checkDiags(t, resp.Diagnostics)
}

// ensure that the global schema cache is used when the provider supports
// GetProviderSchemaOptional
func TestGRPCProvider_GetSchema_globalCache(t *testing.T) {
p := &GRPCProvider{
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
client: mockProviderClient(t),
}

// first call primes the cache
resp := p.GetProviderSchema()

// create a new provider instance which does not expect a GetProviderSchemaCall
p = &GRPCProvider{
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
client: mockproto.NewMockProviderClient(gomock.NewController(t)),
}

resp = p.GetProviderSchema()
checkDiags(t, resp.Diagnostics)
}

// Ensure that gRPC errors are returned early.
// Reference: https://github.com/hashicorp/terraform/issues/31047
func TestGRPCProvider_GetSchema_GRPCError(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions internal/plugin6/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ type GRPCProvider struct {
schema providers.GetProviderSchemaResponse
}

func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResponse) {
func (p *GRPCProvider) GetProviderSchema() providers.GetProviderSchemaResponse {
logger.Trace("GRPCProvider.v6: GetProviderSchema")
p.mu.Lock()
defer p.mu.Unlock()

// check the global cache if we can
if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional {
if resp, ok := providers.SchemaCache.Get(p.Addr); ok {
if !p.Addr.IsZero() {
if resp, ok := providers.SchemaCache.Get(p.Addr); ok && resp.ServerCapabilities.GetProviderSchemaOptional {
logger.Trace("GRPCProvider.v6: returning cached schema", p.Addr.String())
return resp
}
}
Expand All @@ -91,6 +92,8 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp
return p.schema
}

var resp providers.GetProviderSchemaResponse

resp.ResourceTypes = make(map[string]providers.Schema)
resp.DataSources = make(map[string]providers.Schema)

Expand Down
25 changes: 25 additions & 0 deletions internal/plugin6/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs/hcl2shim"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
Expand Down Expand Up @@ -99,6 +100,9 @@ func providerProtoSchema() *proto.GetProviderSchema_Response {
},
},
},
ServerCapabilities: &proto.ServerCapabilities{
GetProviderSchemaOptional: true,
},
}
}

Expand All @@ -111,6 +115,27 @@ func TestGRPCProvider_GetSchema(t *testing.T) {
checkDiags(t, resp.Diagnostics)
}

// ensure that the global schema cache is used when the provider supports
// GetProviderSchemaOptional
func TestGRPCProvider_GetSchema_globalCache(t *testing.T) {
p := &GRPCProvider{
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
client: mockProviderClient(t),
}

// first call primes the cache
resp := p.GetProviderSchema()

// create a new provider instance which does not expect a GetProviderSchemaCall
p = &GRPCProvider{
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
client: mockproto.NewMockProviderClient(gomock.NewController(t)),
}

resp = p.GetProviderSchema()
checkDiags(t, resp.Diagnostics)
}

// Ensure that gRPC errors are returned early.
// Reference: https://github.com/hashicorp/terraform/issues/31047
func TestGRPCProvider_GetSchema_GRPCError(t *testing.T) {
Expand Down

0 comments on commit a6789a7

Please sign in to comment.