github.com/opentofu/opentofu@v1.7.1/internal/tofu/eval_provider.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 tofu 7 8 import ( 9 "fmt" 10 "log" 11 12 "github.com/hashicorp/hcl/v2" 13 14 "github.com/opentofu/opentofu/internal/addrs" 15 "github.com/opentofu/opentofu/internal/configs" 16 "github.com/opentofu/opentofu/internal/providers" 17 ) 18 19 func buildProviderConfig(ctx EvalContext, addr addrs.AbsProviderConfig, config *configs.Provider) hcl.Body { 20 var configBody hcl.Body 21 if config != nil { 22 configBody = config.Config 23 } 24 25 var inputBody hcl.Body 26 inputConfig := ctx.ProviderInput(addr) 27 if len(inputConfig) > 0 { 28 inputBody = configs.SynthBody("<input-prompt>", inputConfig) 29 } 30 31 switch { 32 case configBody != nil && inputBody != nil: 33 log.Printf("[TRACE] buildProviderConfig for %s: merging explicit config and input", addr) 34 return hcl.MergeBodies([]hcl.Body{inputBody, configBody}) 35 case configBody != nil: 36 log.Printf("[TRACE] buildProviderConfig for %s: using explicit config only", addr) 37 return configBody 38 case inputBody != nil: 39 log.Printf("[TRACE] buildProviderConfig for %s: using input only", addr) 40 return inputBody 41 default: 42 log.Printf("[TRACE] buildProviderConfig for %s: no configuration at all", addr) 43 return hcl.EmptyBody() 44 } 45 } 46 47 // getProvider returns the providers.Interface and schema for a given provider. 48 func getProvider(ctx EvalContext, addr addrs.AbsProviderConfig) (providers.Interface, providers.ProviderSchema, error) { 49 if addr.Provider.Type == "" { 50 // Should never happen 51 panic("GetProvider used with uninitialized provider configuration address") 52 } 53 provider := ctx.Provider(addr) 54 if provider == nil { 55 return nil, providers.ProviderSchema{}, fmt.Errorf("provider %s not initialized", addr) 56 } 57 // Not all callers require a schema, so we will leave checking for a nil 58 // schema to the callers. 59 schema, err := ctx.ProviderSchema(addr) 60 if err != nil { 61 return nil, providers.ProviderSchema{}, fmt.Errorf("failed to read schema for provider %s: %w", addr, err) 62 } 63 return provider, schema, nil 64 }