diff --git a/internal/plugin/grpc_provider.go b/internal/plugin/grpc_provider.go index c1b9a1af07..8a71ef7932 100644 --- a/internal/plugin/grpc_provider.go +++ b/internal/plugin/grpc_provider.go @@ -68,9 +68,9 @@ type GRPCProvider struct { ctx context.Context // schema stores the schema for this provider. This is used to properly - // serialize the state for requests. - mu sync.Mutex - schemas providers.GetProviderSchemaResponse + // serialize the requests for schemas. + mu sync.Mutex + schema providers.GetProviderSchemaResponse } func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResponse) { @@ -85,8 +85,8 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp } } - if p.schemas.Provider.Block != nil { - return p.schemas + if p.schema.Provider.Block != nil { + return p.schema } resp.ResourceTypes = make(map[string]providers.Schema) @@ -135,18 +135,16 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp if protoResp.ServerCapabilities != nil { resp.ServerCapabilities.PlanDestroy = protoResp.ServerCapabilities.PlanDestroy + resp.ServerCapabilities.GetProviderSchemaOptional = protoResp.ServerCapabilities.GetProviderSchemaOptional } - // FIXME: Waiting for a provider capability to prevent caching - // providers which always need GetProviderSchema called. // set the global cache if we can - //if !p.Addr.IsZero() { - // providers.SchemaCache.Set(p.Addr, resp) - //} else { - // // otherwise store it in the local cache - // p.schemas = resp - //} - p.schemas = resp + if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional { + providers.SchemaCache.Set(p.Addr, resp) + } else { + // otherwise store it in the local cache + p.schema = resp + } return resp } diff --git a/internal/plugin6/grpc_provider.go b/internal/plugin6/grpc_provider.go index c4888d709c..9b1022a533 100644 --- a/internal/plugin6/grpc_provider.go +++ b/internal/plugin6/grpc_provider.go @@ -68,9 +68,9 @@ type GRPCProvider struct { ctx context.Context // schema stores the schema for this provider. This is used to properly - // serialize the state for requests. - mu sync.Mutex - schemas providers.GetProviderSchemaResponse + // serialize the requests for schemas. + mu sync.Mutex + schema providers.GetProviderSchemaResponse } func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResponse) { @@ -85,8 +85,8 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp } } - if p.schemas.Provider.Block != nil { - return p.schemas + if p.schema.Provider.Block != nil { + return p.schema } resp.ResourceTypes = make(map[string]providers.Schema) @@ -135,18 +135,16 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp if protoResp.ServerCapabilities != nil { resp.ServerCapabilities.PlanDestroy = protoResp.ServerCapabilities.PlanDestroy + resp.ServerCapabilities.GetProviderSchemaOptional = protoResp.ServerCapabilities.GetProviderSchemaOptional } - // FIXME: Waiting for a provider capability to prevent caching - // providers which always need GetProviderSchema called. // set the global cache if we can - //if !p.Addr.IsZero() { - // providers.SchemaCache.Set(p.Addr, resp) - //} else { - // // otherwise store it in the local cache - // p.schemas = resp - //} - p.schemas = resp + if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional { + providers.SchemaCache.Set(p.Addr, resp) + } else { + // otherwise store it in the local cache + p.schema = resp + } return resp } diff --git a/internal/terraform/context_plugins.go b/internal/terraform/context_plugins.go index 1297afef81..f49ffed413 100644 --- a/internal/terraform/context_plugins.go +++ b/internal/terraform/context_plugins.go @@ -68,7 +68,10 @@ func (cp *contextPlugins) NewProvisionerInstance(typ string) (provisioners.Inter func (cp *contextPlugins) ProviderSchema(addr addrs.Provider) (providers.ProviderSchema, error) { log.Printf("[TRACE] terraform.contextPlugins: Initializing provider %q to read its schema", addr) - // check the global schema cache first + // Check the global schema cache first. + // This cache is only written by the provider client, and transparently + // used by GetProviderSchema, but we check it here because at this point we + // may be able to avoid spinning up the provider instance at all. schemas, ok := providers.SchemaCache.Get(addr) if ok { return schemas, nil