github.com/opentofu/opentofu@v1.7.1/internal/providers/schemas.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package providers
     7  
     8  import (
     9  	"github.com/opentofu/opentofu/internal/addrs"
    10  	"github.com/opentofu/opentofu/internal/configs/configschema"
    11  )
    12  
    13  // ProviderSchema is an overall container for all of the schemas for all
    14  // configurable objects defined within a particular provider. All storage of
    15  // provider schemas should use this type.
    16  type ProviderSchema = GetProviderSchemaResponse
    17  
    18  // SchemaForResourceType attempts to find a schema for the given mode and type.
    19  // Returns nil if no such schema is available.
    20  func (ss ProviderSchema) SchemaForResourceType(mode addrs.ResourceMode, typeName string) (schema *configschema.Block, version uint64) {
    21  	switch mode {
    22  	case addrs.ManagedResourceMode:
    23  		res := ss.ResourceTypes[typeName]
    24  		return res.Block, uint64(res.Version)
    25  	case addrs.DataResourceMode:
    26  		// Data resources don't have schema versions right now, since state is discarded for each refresh
    27  		return ss.DataSources[typeName].Block, 0
    28  	default:
    29  		// Shouldn't happen, because the above cases are comprehensive.
    30  		return nil, 0
    31  	}
    32  }
    33  
    34  // SchemaForResourceAddr attempts to find a schema for the mode and type from
    35  // the given resource address. Returns nil if no such schema is available.
    36  func (ss ProviderSchema) SchemaForResourceAddr(addr addrs.Resource) (schema *configschema.Block, version uint64) {
    37  	return ss.SchemaForResourceType(addr.Mode, addr.Type)
    38  }