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