2015-02-03 19:46:11 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BuiltinEvalContext is an EvalContext implementation that is used by
|
|
|
|
// Terraform by default.
|
|
|
|
type BuiltinEvalContext struct {
|
2015-02-05 19:09:57 -06:00
|
|
|
Path []string
|
|
|
|
Interpolater *Interpolater
|
|
|
|
Providers map[string]ResourceProviderFactory
|
2015-02-03 19:46:11 -06:00
|
|
|
|
|
|
|
providers map[string]ResourceProvider
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *BuiltinEvalContext) InitProvider(n string) (ResourceProvider, error) {
|
|
|
|
ctx.once.Do(ctx.init)
|
|
|
|
|
|
|
|
if p := ctx.Provider(n); p != nil {
|
|
|
|
return nil, fmt.Errorf("Provider '%s' already initialized", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, ok := ctx.Providers[n]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Provider '%s' not found", n)
|
|
|
|
}
|
|
|
|
|
2015-02-04 19:02:18 -06:00
|
|
|
p, err := f()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.providers[n] = p
|
|
|
|
return p, nil
|
2015-02-03 19:46:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *BuiltinEvalContext) Provider(n string) ResourceProvider {
|
|
|
|
ctx.once.Do(ctx.init)
|
|
|
|
return ctx.providers[n]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *BuiltinEvalContext) Interpolate(
|
2015-02-05 19:09:57 -06:00
|
|
|
cfg *config.RawConfig, r *Resource) (*ResourceConfig, error) {
|
|
|
|
if cfg != nil {
|
|
|
|
scope := &InterpolationScope{
|
|
|
|
Path: ctx.Path,
|
|
|
|
Resource: r,
|
|
|
|
}
|
|
|
|
vs, err := ctx.Interpolater.Values(scope, cfg.Variables)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-02-04 10:30:53 -06:00
|
|
|
}
|
|
|
|
|
2015-02-05 19:09:57 -06:00
|
|
|
// Do the interpolation
|
|
|
|
if err := cfg.Interpolate(vs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-04 10:30:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
result := NewResourceConfig(cfg)
|
|
|
|
result.interpolateForce()
|
|
|
|
return result, nil
|
2015-02-03 19:46:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *BuiltinEvalContext) init() {
|
|
|
|
// We nil-check the things below because they're meant to be configured,
|
|
|
|
// and we just default them to non-nil.
|
|
|
|
if ctx.Providers == nil {
|
|
|
|
ctx.Providers = make(map[string]ResourceProviderFactory)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always reset the things below since we only call this once and
|
|
|
|
// they can't be initialized externally.
|
|
|
|
ctx.providers = make(map[string]ResourceProvider)
|
|
|
|
}
|