mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
use the same struct for all schema access
Unify the struct used for Schemes and GetProviderSchemaResponse so that we can have a single cache which handles all schema access.
This commit is contained in:
parent
53901a7e62
commit
9d9746560f
@ -6,6 +6,7 @@ package providers
|
||||
import (
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/configs/configschema"
|
||||
"github.com/hashicorp/terraform/internal/states"
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
@ -73,13 +74,6 @@ type Interface interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// All access to a provider's schema internally should bt through the Schemas
|
||||
// type.
|
||||
// TODO: Acccess to schemas was split over two types with slightly different
|
||||
// structures in different parts of the code. These types need to be unified
|
||||
// for easier and more consistent access, but there is however a large amount
|
||||
// of code which directly uses this structure, so it is not easily refactored
|
||||
// out.
|
||||
type GetProviderSchemaResponse struct {
|
||||
// Provider is the schema for the provider itself.
|
||||
Provider Schema
|
||||
@ -100,6 +94,17 @@ type GetProviderSchemaResponse struct {
|
||||
ServerCapabilities ServerCapabilities
|
||||
}
|
||||
|
||||
// Schema pairs a provider or resource schema with that schema's version.
|
||||
// This is used to be able to upgrade the schema in UpgradeResourceState.
|
||||
//
|
||||
// This describes the schema for a single object within a provider. Type
|
||||
// "Schemas" (plural) instead represents the overall collection of schemas
|
||||
// for everything within a particular provider.
|
||||
type Schema struct {
|
||||
Version int64
|
||||
Block *configschema.Block
|
||||
}
|
||||
|
||||
// ServerCapabilities allows providers to communicate extra information
|
||||
// regarding supported protocol features. This is used to indicate availability
|
||||
// of certain forward-compatible changes which may be optional in a major
|
||||
|
@ -9,11 +9,11 @@ import (
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
)
|
||||
|
||||
// SchemaCache is a global cache of GetProviderSchemaResponses.
|
||||
// SchemaCache is a global cache of Schemas.
|
||||
// This will be accessed by both core and the provider clients to ensure that
|
||||
// large schemas are stored in a single location.
|
||||
var SchemaCache = &schemaCache{
|
||||
m: make(map[addrs.Provider]GetProviderSchemaResponse),
|
||||
m: make(map[addrs.Provider]Schemas),
|
||||
}
|
||||
|
||||
// Global cache for provider schemas
|
||||
@ -22,17 +22,17 @@ var SchemaCache = &schemaCache{
|
||||
// concurrent calls resulting in an error can be handled in the same manner.
|
||||
type schemaCache struct {
|
||||
mu sync.Mutex
|
||||
m map[addrs.Provider]GetProviderSchemaResponse
|
||||
m map[addrs.Provider]Schemas
|
||||
}
|
||||
|
||||
func (c *schemaCache) Set(p addrs.Provider, s GetProviderSchemaResponse) {
|
||||
func (c *schemaCache) Set(p addrs.Provider, s Schemas) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.m[p] = s
|
||||
}
|
||||
|
||||
func (c *schemaCache) Get(p addrs.Provider) (GetProviderSchemaResponse, bool) {
|
||||
func (c *schemaCache) Get(p addrs.Provider) (Schemas, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
|
@ -6,49 +6,22 @@ package providers
|
||||
import (
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
"github.com/hashicorp/terraform/internal/configs/configschema"
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
|
||||
// Schemas is an overall container for all of the schemas for all configurable
|
||||
// objects defined within a particular provider.
|
||||
//
|
||||
// The schema for each individual configurable object is represented by nested
|
||||
// instances of type Schema (singular) within this data structure.
|
||||
type Schemas struct {
|
||||
// Provider is the schema for the provider itself.
|
||||
Provider *configschema.Block
|
||||
|
||||
// ProviderMeta is the schema for the provider's meta info in a module
|
||||
ProviderMeta *configschema.Block
|
||||
|
||||
// ResourceTypes map the resource type name to that type's schema.
|
||||
ResourceTypes map[string]*configschema.Block
|
||||
ResourceTypeSchemaVersions map[string]uint64
|
||||
|
||||
// DataSources maps the data source name to that data source's schema.
|
||||
DataSources map[string]*configschema.Block
|
||||
|
||||
// ServerCapabilities lists optional features supported by the provider.
|
||||
ServerCapabilities ServerCapabilities
|
||||
|
||||
// Diagnostics contains any warnings or errors from the method call.
|
||||
// While diagnostics are only relevant to the initial call, we add these to
|
||||
// the cached structure so that concurrent calls can handle failures
|
||||
// gracefully when the original call did not succeed.
|
||||
// TODO: can we be sure the original failure get handled correctly, and
|
||||
// ignore this entirely?
|
||||
Diagnostics tfdiags.Diagnostics
|
||||
}
|
||||
type Schemas = GetProviderSchemaResponse
|
||||
|
||||
// SchemaForResourceType attempts to find a schema for the given mode and type.
|
||||
// Returns nil if no such schema is available.
|
||||
func (ss *Schemas) SchemaForResourceType(mode addrs.ResourceMode, typeName string) (schema *configschema.Block, version uint64) {
|
||||
func (ss Schemas) SchemaForResourceType(mode addrs.ResourceMode, typeName string) (schema *configschema.Block, version uint64) {
|
||||
switch mode {
|
||||
case addrs.ManagedResourceMode:
|
||||
return ss.ResourceTypes[typeName], ss.ResourceTypeSchemaVersions[typeName]
|
||||
res := ss.ResourceTypes[typeName]
|
||||
return res.Block, uint64(res.Version)
|
||||
case addrs.DataResourceMode:
|
||||
// Data resources don't have schema versions right now, since state is discarded for each refresh
|
||||
return ss.DataSources[typeName], 0
|
||||
return ss.DataSources[typeName].Block, 0
|
||||
default:
|
||||
// Shouldn't happen, because the above cases are comprehensive.
|
||||
return nil, 0
|
||||
@ -57,41 +30,6 @@ func (ss *Schemas) SchemaForResourceType(mode addrs.ResourceMode, typeName strin
|
||||
|
||||
// SchemaForResourceAddr attempts to find a schema for the mode and type from
|
||||
// the given resource address. Returns nil if no such schema is available.
|
||||
func (ss *Schemas) SchemaForResourceAddr(addr addrs.Resource) (schema *configschema.Block, version uint64) {
|
||||
func (ss Schemas) SchemaForResourceAddr(addr addrs.Resource) (schema *configschema.Block, version uint64) {
|
||||
return ss.SchemaForResourceType(addr.Mode, addr.Type)
|
||||
}
|
||||
|
||||
func SchemaResponseToSchemas(resp GetProviderSchemaResponse) *Schemas {
|
||||
var schemas = &Schemas{
|
||||
ResourceTypes: make(map[string]*configschema.Block),
|
||||
ResourceTypeSchemaVersions: make(map[string]uint64),
|
||||
DataSources: make(map[string]*configschema.Block),
|
||||
ServerCapabilities: resp.ServerCapabilities,
|
||||
Diagnostics: resp.Diagnostics,
|
||||
}
|
||||
|
||||
schemas.Provider = resp.Provider.Block
|
||||
schemas.ProviderMeta = resp.ProviderMeta.Block
|
||||
|
||||
for name, res := range resp.ResourceTypes {
|
||||
schemas.ResourceTypes[name] = res.Block
|
||||
schemas.ResourceTypeSchemaVersions[name] = uint64(res.Version)
|
||||
}
|
||||
|
||||
for name, dat := range resp.DataSources {
|
||||
schemas.DataSources[name] = dat.Block
|
||||
}
|
||||
|
||||
return schemas
|
||||
}
|
||||
|
||||
// Schema pairs a provider or resource schema with that schema's version.
|
||||
// This is used to be able to upgrade the schema in UpgradeResourceState.
|
||||
//
|
||||
// This describes the schema for a single object within a provider. Type
|
||||
// "Schemas" (plural) instead represents the overall collection of schemas
|
||||
// for everything within a particular provider.
|
||||
type Schema struct {
|
||||
Version int64
|
||||
Block *configschema.Block
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user