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