opentofu/terraform/eval_provider.go

138 lines
3.6 KiB
Go
Raw Normal View History

2015-02-03 03:43:18 -06:00
package terraform
import (
"fmt"
2015-02-13 19:59:54 -06:00
"github.com/hashicorp/terraform/config"
2015-02-03 03:43:18 -06:00
)
// EvalBuildProviderConfig outputs a *ResourceConfig that is properly
// merged with parents and inputs on top of what is configured in the file.
type EvalBuildProviderConfig struct {
Provider string
2015-02-14 00:58:41 -06:00
Config **ResourceConfig
Output **ResourceConfig
2015-02-03 03:43:18 -06:00
}
func (n *EvalBuildProviderConfig) Eval(ctx EvalContext) (interface{}, error) {
2015-02-14 00:58:41 -06:00
cfg := *n.Config
2015-02-13 19:59:54 -06:00
// If we have an Input configuration set, then merge that in
2015-02-13 19:59:54 -06:00
if input := ctx.ProviderInput(n.Provider); input != nil {
// "input" is a map of the subset of config values that were known
// during the input walk, set by EvalInputProvider. Note that
// in particular it does *not* include attributes that had
// computed values at input time; those appear *only* in
// "cfg" here.
2015-02-13 19:59:54 -06:00
rc, err := config.NewRawConfig(input)
if err != nil {
return nil, err
}
merged := rc.Merge(cfg.raw)
2015-02-13 19:59:54 -06:00
cfg = NewResourceConfig(merged)
}
*n.Output = cfg
return nil, nil
}
// EvalConfigProvider is an EvalNode implementation that configures
// a provider that is already initialized and retrieved.
type EvalConfigProvider struct {
Provider string
Config **ResourceConfig
}
func (n *EvalConfigProvider) Eval(ctx EvalContext) (interface{}, error) {
return nil, ctx.ConfigureProvider(n.Provider, *n.Config)
2015-02-03 03:43:18 -06:00
}
// EvalInitProvider is an EvalNode implementation that initializes a provider
// and returns nothing. The provider can be retrieved again with the
// EvalGetProvider node.
type EvalInitProvider struct {
TypeName string
Name string
2015-02-03 03:43:18 -06:00
}
2015-02-14 00:58:41 -06:00
func (n *EvalInitProvider) Eval(ctx EvalContext) (interface{}, error) {
return ctx.InitProvider(n.TypeName, n.Name)
2015-02-03 03:43:18 -06:00
}
// EvalCloseProvider is an EvalNode implementation that closes provider
// connections that aren't needed anymore.
type EvalCloseProvider struct {
Name string
}
func (n *EvalCloseProvider) Eval(ctx EvalContext) (interface{}, error) {
ctx.CloseProvider(n.Name)
return nil, nil
}
2015-02-03 03:43:18 -06:00
// EvalGetProvider is an EvalNode implementation that retrieves an already
// initialized provider instance for the given name.
type EvalGetProvider struct {
2015-02-12 16:46:22 -06:00
Name string
Output *ResourceProvider
2015-02-03 03:43:18 -06:00
}
2015-02-14 00:58:41 -06:00
func (n *EvalGetProvider) Eval(ctx EvalContext) (interface{}, error) {
2015-02-03 03:43:18 -06:00
result := ctx.Provider(n.Name)
if result == nil {
return nil, fmt.Errorf("provider %s not initialized", n.Name)
}
2015-02-12 16:46:22 -06:00
if n.Output != nil {
*n.Output = result
}
2015-02-14 00:58:41 -06:00
return nil, nil
2015-02-03 03:43:18 -06:00
}
2015-02-13 19:59:54 -06:00
// EvalInputProvider is an EvalNode implementation that asks for input
// for the given provider configurations.
type EvalInputProvider struct {
Name string
Provider *ResourceProvider
Config **ResourceConfig
2015-02-13 19:59:54 -06:00
}
2015-02-14 00:58:41 -06:00
func (n *EvalInputProvider) Eval(ctx EvalContext) (interface{}, error) {
rc := *n.Config
orig := rc.DeepCopy()
2015-02-13 19:59:54 -06:00
// Wrap the input into a namespace
input := &PrefixUIInput{
IdPrefix: fmt.Sprintf("provider.%s", n.Name),
QueryPrefix: fmt.Sprintf("provider.%s.", n.Name),
UIInput: ctx.Input(),
}
// Go through each provider and capture the input necessary
// to satisfy it.
config, err := (*n.Provider).Input(input, rc)
if err != nil {
return nil, fmt.Errorf(
"Error configuring %s: %s", n.Name, err)
}
// We only store values that have changed through Input.
// The goal is to cache cache input responses, not to provide a complete
// config for other providers.
confMap := make(map[string]interface{})
2015-02-13 19:59:54 -06:00
if config != nil && len(config.Config) > 0 {
// any values that weren't in the original ResourcConfig will be cached
for k, v := range config.Config {
if _, ok := orig.Config[k]; !ok {
confMap[k] = v
}
}
2015-02-13 19:59:54 -06:00
}
ctx.SetProviderInput(n.Name, confMap)
2015-02-13 19:59:54 -06:00
return nil, nil
}