opentofu/internal/providers/schemas.go
James Bardin 9d9746560f 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.
2023-07-06 10:37:35 -04:00

36 lines
1.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package providers
import (
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs/configschema"
)
// Schemas is an overall container for all of the schemas for all configurable
// objects defined within a particular provider.
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) {
switch mode {
case addrs.ManagedResourceMode:
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].Block, 0
default:
// Shouldn't happen, because the above cases are comprehensive.
return nil, 0
}
}
// 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) {
return ss.SchemaForResourceType(addr.Mode, addr.Type)
}