2014-07-03 19:14:17 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2014-07-03 19:29:14 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-10-08 05:09:30 +02:00
|
|
|
"os"
|
2014-09-29 08:37:36 +02:00
|
|
|
"sort"
|
2014-07-15 20:30:21 +02:00
|
|
|
"strconv"
|
2014-07-04 06:24:17 +02:00
|
|
|
"strings"
|
2014-07-03 19:29:14 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-09-23 00:37:29 +02:00
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2014-07-03 19:29:14 +02:00
|
|
|
"github.com/hashicorp/terraform/depgraph"
|
2014-07-03 19:14:17 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/multierror"
|
|
|
|
)
|
|
|
|
|
2014-07-03 20:28:04 +02:00
|
|
|
// This is a function type used to implement a walker for the resource
|
|
|
|
// tree internally on the Terraform structure.
|
2014-09-23 22:47:20 +02:00
|
|
|
type genericWalkFunc func(*walkContext, *Resource) error
|
2014-07-03 20:28:04 +02:00
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
// Context represents all the context that Terraform needs in order to
|
|
|
|
// perform operations on infrastructure. This structure is built using
|
|
|
|
// ContextOpts and NewContext. See the documentation for those.
|
|
|
|
//
|
|
|
|
// Additionally, a context can be created from a Plan using Plan.Context.
|
|
|
|
type Context struct {
|
2014-09-29 18:13:15 +02:00
|
|
|
module *module.Tree
|
|
|
|
diff *Diff
|
|
|
|
hooks []Hook
|
|
|
|
state *State
|
|
|
|
providerConfig map[string]map[string]map[string]interface{}
|
|
|
|
providers map[string]ResourceProviderFactory
|
|
|
|
provisioners map[string]ResourceProvisionerFactory
|
|
|
|
variables map[string]string
|
|
|
|
uiInput UIInput
|
2014-07-03 20:27:30 +02:00
|
|
|
|
2014-10-16 19:04:24 +02:00
|
|
|
parallelSem Semaphore // Semaphore used to limit parallelism
|
|
|
|
l sync.Mutex // Lock acquired during any task
|
|
|
|
sl sync.RWMutex // Lock acquired to R/W internal data
|
|
|
|
runCh <-chan struct{}
|
|
|
|
sh *stopHook
|
2014-07-03 19:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContextOpts are the user-creatable configuration structure to create
|
|
|
|
// a context with NewContext.
|
|
|
|
type ContextOpts struct {
|
2014-07-08 23:45:45 +02:00
|
|
|
Diff *Diff
|
|
|
|
Hooks []Hook
|
2014-09-23 00:37:29 +02:00
|
|
|
Module *module.Tree
|
2014-07-08 23:45:45 +02:00
|
|
|
Parallelism int
|
|
|
|
State *State
|
|
|
|
Providers map[string]ResourceProviderFactory
|
|
|
|
Provisioners map[string]ResourceProvisionerFactory
|
|
|
|
Variables map[string]string
|
2014-09-29 08:37:36 +02:00
|
|
|
|
2014-10-08 05:09:30 +02:00
|
|
|
UIInput UIInput
|
2014-07-03 19:14:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:18:45 +02:00
|
|
|
// InputMode defines what sort of input will be asked for when Input
|
|
|
|
// is called on Context.
|
|
|
|
type InputMode byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// InputModeVar asks for variables
|
|
|
|
InputModeVar InputMode = 1 << iota
|
|
|
|
|
|
|
|
// InputModeProvider asks for provider variables
|
|
|
|
InputModeProvider
|
|
|
|
|
|
|
|
// InputModeStd is the standard operating mode and asks for both variables
|
|
|
|
// and providers.
|
|
|
|
InputModeStd = InputModeVar | InputModeProvider
|
|
|
|
)
|
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
// NewContext creates a new context.
|
|
|
|
//
|
|
|
|
// Once a context is created, the pointer values within ContextOpts should
|
|
|
|
// not be mutated in any way, since the pointers are copied, not the values
|
|
|
|
// themselves.
|
|
|
|
func NewContext(opts *ContextOpts) *Context {
|
2014-07-03 20:27:30 +02:00
|
|
|
sh := new(stopHook)
|
|
|
|
|
|
|
|
// Copy all the hooks and add our stop hook. We don't append directly
|
|
|
|
// to the Config so that we're not modifying that in-place.
|
|
|
|
hooks := make([]Hook, len(opts.Hooks)+1)
|
|
|
|
copy(hooks, opts.Hooks)
|
|
|
|
hooks[len(opts.Hooks)] = sh
|
|
|
|
|
2014-07-08 20:42:03 +02:00
|
|
|
// Make the parallelism channel
|
|
|
|
par := opts.Parallelism
|
|
|
|
if par == 0 {
|
|
|
|
par = 10
|
|
|
|
}
|
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
return &Context{
|
2014-09-29 18:13:15 +02:00
|
|
|
diff: opts.Diff,
|
|
|
|
hooks: hooks,
|
|
|
|
module: opts.Module,
|
|
|
|
state: opts.State,
|
|
|
|
providerConfig: make(map[string]map[string]map[string]interface{}),
|
|
|
|
providers: opts.Providers,
|
|
|
|
provisioners: opts.Provisioners,
|
|
|
|
variables: opts.Variables,
|
|
|
|
uiInput: opts.UIInput,
|
2014-07-03 20:27:30 +02:00
|
|
|
|
2014-10-16 19:04:24 +02:00
|
|
|
parallelSem: NewSemaphore(par),
|
|
|
|
sh: sh,
|
2014-07-03 19:14:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:04:04 +02:00
|
|
|
// Apply applies the changes represented by this context and returns
|
|
|
|
// the resulting state.
|
|
|
|
//
|
|
|
|
// In addition to returning the resulting state, this context is updated
|
|
|
|
// with the latest state.
|
|
|
|
func (c *Context) Apply() (*State, error) {
|
2014-07-03 20:27:30 +02:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2014-07-07 08:03:51 +02:00
|
|
|
// Set our state right away. No matter what, this IS our new state,
|
|
|
|
// even if there is an error below.
|
|
|
|
c.state = c.state.deepcopy()
|
2014-09-17 01:21:09 +02:00
|
|
|
if c.state == nil {
|
|
|
|
c.state = &State{}
|
|
|
|
}
|
|
|
|
c.state.init()
|
2014-07-03 20:04:04 +02:00
|
|
|
|
|
|
|
// Walk
|
2014-07-24 16:20:59 +02:00
|
|
|
log.Printf("[INFO] Apply walk starting")
|
2014-09-24 00:46:20 +02:00
|
|
|
err := c.walkContext(walkApply, rootModulePath).Walk()
|
2014-07-24 16:20:59 +02:00
|
|
|
log.Printf("[INFO] Apply walk complete")
|
2014-07-03 20:04:04 +02:00
|
|
|
|
2014-07-13 20:07:31 +02:00
|
|
|
// Prune the state so that we have as clean a state as possible
|
|
|
|
c.state.prune()
|
|
|
|
|
2014-07-07 08:03:51 +02:00
|
|
|
return c.state, err
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 04:23:56 +02:00
|
|
|
// Graph returns the graph for this context.
|
|
|
|
func (c *Context) Graph() (*depgraph.Graph, error) {
|
2014-09-25 04:36:00 +02:00
|
|
|
return Graph(&GraphOpts{
|
|
|
|
Diff: c.diff,
|
|
|
|
Module: c.module,
|
|
|
|
Providers: c.providers,
|
|
|
|
Provisioners: c.provisioners,
|
|
|
|
State: c.state,
|
|
|
|
})
|
2014-07-13 04:23:56 +02:00
|
|
|
}
|
|
|
|
|
2014-09-29 08:37:36 +02:00
|
|
|
// Input asks for input to fill variables and provider configurations.
|
|
|
|
// This modifies the configuration in-place, so asking for Input twice
|
|
|
|
// may result in different UI output showing different current values.
|
2014-10-08 19:18:45 +02:00
|
|
|
func (c *Context) Input(mode InputMode) error {
|
2014-09-29 08:37:36 +02:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2014-10-08 19:18:45 +02:00
|
|
|
if mode&InputModeVar != 0 {
|
|
|
|
// Walk the variables first for the root module. We walk them in
|
|
|
|
// alphabetical order for UX reasons.
|
|
|
|
rootConf := c.module.Config()
|
|
|
|
names := make([]string, len(rootConf.Variables))
|
|
|
|
m := make(map[string]*config.Variable)
|
|
|
|
for i, v := range rootConf.Variables {
|
|
|
|
names[i] = v.Name
|
|
|
|
m[v.Name] = v
|
2014-10-03 07:33:38 +02:00
|
|
|
}
|
2014-10-08 19:18:45 +02:00
|
|
|
sort.Strings(names)
|
|
|
|
for _, n := range names {
|
|
|
|
v := m[n]
|
|
|
|
switch v.Type() {
|
|
|
|
case config.VariableTypeMap:
|
|
|
|
continue
|
|
|
|
case config.VariableTypeString:
|
|
|
|
// Good!
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown variable type: %s", v.Type()))
|
2014-09-29 08:37:36 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:18:45 +02:00
|
|
|
var defaultString string
|
|
|
|
if v.Default != nil {
|
|
|
|
defaultString = v.Default.(string)
|
2014-09-29 08:37:36 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:18:45 +02:00
|
|
|
// Ask the user for a value for this variable
|
|
|
|
var value string
|
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
value, err = c.uiInput.Input(&InputOpts{
|
|
|
|
Id: fmt.Sprintf("var.%s", n),
|
|
|
|
Query: fmt.Sprintf("var.%s", n),
|
|
|
|
Default: defaultString,
|
|
|
|
Description: v.Description,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error asking for %s: %s", n, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" && v.Required() {
|
|
|
|
// Redo if it is required.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" {
|
|
|
|
// No value, just exit the loop. With no value, we just
|
|
|
|
// use whatever is currently set in variables.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-09-29 08:37:36 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:18:45 +02:00
|
|
|
if value != "" {
|
|
|
|
c.variables[n] = value
|
|
|
|
}
|
2014-09-29 08:37:36 +02:00
|
|
|
}
|
2014-10-08 19:18:45 +02:00
|
|
|
}
|
2014-09-29 08:37:36 +02:00
|
|
|
|
2014-10-08 19:18:45 +02:00
|
|
|
if mode&InputModeProvider != 0 {
|
|
|
|
// Create the walk context and walk the inputs, which will gather the
|
|
|
|
// inputs for any resource providers.
|
|
|
|
wc := c.walkContext(walkInput, rootModulePath)
|
|
|
|
wc.Meta = new(walkInputMeta)
|
|
|
|
return wc.Walk()
|
2014-09-29 08:37:36 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:18:45 +02:00
|
|
|
return nil
|
2014-09-29 08:37:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 19:44:30 +02:00
|
|
|
// Plan generates an execution plan for the given context.
|
|
|
|
//
|
|
|
|
// The execution plan encapsulates the context and can be stored
|
|
|
|
// in order to reinstantiate a context later for Apply.
|
2014-07-03 20:04:04 +02:00
|
|
|
//
|
|
|
|
// Plan also updates the diff of this context to be the diff generated
|
|
|
|
// by the plan, so Apply can be called after.
|
2014-07-03 19:44:30 +02:00
|
|
|
func (c *Context) Plan(opts *PlanOpts) (*Plan, error) {
|
2014-07-03 20:28:47 +02:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2014-07-03 19:44:30 +02:00
|
|
|
p := &Plan{
|
2014-09-24 23:56:48 +02:00
|
|
|
Module: c.module,
|
2014-07-03 19:44:30 +02:00
|
|
|
Vars: c.variables,
|
|
|
|
State: c.state,
|
|
|
|
}
|
2014-07-07 08:03:51 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
wc := c.walkContext(walkInvalid, rootModulePath)
|
|
|
|
wc.Meta = p
|
2014-07-07 08:03:51 +02:00
|
|
|
|
|
|
|
if opts != nil && opts.Destroy {
|
2014-09-24 00:46:20 +02:00
|
|
|
wc.Operation = walkPlanDestroy
|
2014-07-07 08:03:51 +02:00
|
|
|
} else {
|
|
|
|
// Set our state to be something temporary. We do this so that
|
|
|
|
// the plan can update a fake state so that variables work, then
|
|
|
|
// we replace it back with our old state.
|
|
|
|
old := c.state
|
2014-09-17 01:21:09 +02:00
|
|
|
if old == nil {
|
|
|
|
c.state = &State{}
|
|
|
|
c.state.init()
|
|
|
|
} else {
|
|
|
|
c.state = old.deepcopy()
|
|
|
|
}
|
2014-07-07 08:03:51 +02:00
|
|
|
defer func() {
|
|
|
|
c.state = old
|
|
|
|
}()
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
wc.Operation = walkPlan
|
2014-07-07 08:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk and run the plan
|
2014-09-24 00:46:20 +02:00
|
|
|
err := wc.Walk()
|
2014-07-03 20:04:04 +02:00
|
|
|
|
|
|
|
// Update the diff so that our context is up-to-date
|
|
|
|
c.diff = p.Diff
|
|
|
|
|
2014-07-03 19:44:30 +02:00
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2014-07-03 19:29:14 +02:00
|
|
|
// Refresh goes through all the resources in the state and refreshes them
|
|
|
|
// to their latest state. This will update the state that this context
|
|
|
|
// works with, along with returning it.
|
|
|
|
//
|
|
|
|
// Even in the case an error is returned, the state will be returned and
|
|
|
|
// will potentially be partially updated.
|
|
|
|
func (c *Context) Refresh() (*State, error) {
|
2014-07-03 20:28:47 +02:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2014-09-20 01:24:17 +02:00
|
|
|
// Update our state
|
|
|
|
c.state = c.state.deepcopy()
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
// Walk the graph
|
2014-09-24 00:46:20 +02:00
|
|
|
err := c.walkContext(walkRefresh, rootModulePath).Walk()
|
2014-09-20 01:24:17 +02:00
|
|
|
|
|
|
|
// Prune the state
|
|
|
|
c.state.prune()
|
2014-07-08 01:22:09 +02:00
|
|
|
return c.state, err
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 20:27:30 +02:00
|
|
|
// Stop stops the running task.
|
|
|
|
//
|
|
|
|
// Stop will block until the task completes.
|
|
|
|
func (c *Context) Stop() {
|
|
|
|
c.l.Lock()
|
|
|
|
ch := c.runCh
|
|
|
|
|
|
|
|
// If we aren't running, then just return
|
|
|
|
if ch == nil {
|
|
|
|
c.l.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the hook we want to stop
|
|
|
|
c.sh.Stop()
|
|
|
|
|
|
|
|
// Wait for us to stop
|
|
|
|
c.l.Unlock()
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
// Validate validates the configuration and returns any warnings or errors.
|
|
|
|
func (c *Context) Validate() ([]string, []error) {
|
|
|
|
var rerr *multierror.Error
|
|
|
|
|
|
|
|
// Validate the configuration itself
|
2014-09-25 04:31:30 +02:00
|
|
|
if err := c.module.Validate(); err != nil {
|
2014-07-03 19:14:17 +02:00
|
|
|
rerr = multierror.ErrorAppend(rerr, err)
|
|
|
|
}
|
|
|
|
|
2014-09-25 04:40:54 +02:00
|
|
|
// This only needs to be done for the root module, since inter-module
|
|
|
|
// variables are validated in the module tree.
|
2014-09-25 04:31:30 +02:00
|
|
|
if config := c.module.Config(); config != nil {
|
2014-09-25 00:48:46 +02:00
|
|
|
// Validate the user variables
|
|
|
|
if errs := smcUserVariables(config, c.variables); len(errs) > 0 {
|
|
|
|
rerr = multierror.ErrorAppend(rerr, errs...)
|
|
|
|
}
|
2014-07-03 19:14:17 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 04:31:30 +02:00
|
|
|
// Validate the entire graph
|
|
|
|
walkMeta := new(walkValidateMeta)
|
|
|
|
wc := c.walkContext(walkValidate, rootModulePath)
|
|
|
|
wc.Meta = walkMeta
|
|
|
|
if err := wc.Walk(); err != nil {
|
|
|
|
rerr = multierror.ErrorAppend(rerr, err)
|
2014-07-03 21:17:56 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 07:35:11 +02:00
|
|
|
// Flatten the warns/errs so that we get all the module errors as well,
|
|
|
|
// then aggregate.
|
|
|
|
warns, errs := walkMeta.Flatten()
|
|
|
|
if len(errs) > 0 {
|
|
|
|
rerr = multierror.ErrorAppend(rerr, errs...)
|
2014-07-03 21:17:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errs = nil
|
2014-07-03 19:14:17 +02:00
|
|
|
if rerr != nil && len(rerr.Errors) > 0 {
|
|
|
|
errs = rerr.Errors
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:17:56 +02:00
|
|
|
return warns, errs
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
func (c *Context) acquireRun() chan<- struct{} {
|
|
|
|
c.l.Lock()
|
|
|
|
defer c.l.Unlock()
|
2014-07-22 17:18:53 +02:00
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
// Wait for no channel to exist
|
|
|
|
for c.runCh != nil {
|
|
|
|
c.l.Unlock()
|
|
|
|
ch := c.runCh
|
|
|
|
<-ch
|
|
|
|
c.l.Lock()
|
2014-07-05 00:36:28 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
ch := make(chan struct{})
|
|
|
|
c.runCh = ch
|
|
|
|
return ch
|
2014-07-05 00:36:28 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
func (c *Context) releaseRun(ch chan<- struct{}) {
|
|
|
|
c.l.Lock()
|
|
|
|
defer c.l.Unlock()
|
2014-09-16 02:30:18 +02:00
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
close(ch)
|
|
|
|
c.runCh = nil
|
|
|
|
c.sh.Reset()
|
|
|
|
}
|
2014-07-05 19:48:47 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
func (c *Context) walkContext(op walkOperation, path []string) *walkContext {
|
2014-09-24 01:44:21 +02:00
|
|
|
// Get the config structure
|
|
|
|
m := c.module
|
|
|
|
for _, n := range path[1:] {
|
|
|
|
cs := m.Children()
|
|
|
|
m = cs[n]
|
2014-09-17 01:55:10 +02:00
|
|
|
}
|
2014-09-24 01:44:21 +02:00
|
|
|
var conf *config.Config
|
|
|
|
if m != nil {
|
|
|
|
conf = m.Config()
|
2014-08-31 02:25:34 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 01:44:21 +02:00
|
|
|
// Calculate the default variable values
|
|
|
|
defaultVars := make(map[string]string)
|
|
|
|
if conf != nil {
|
|
|
|
for _, v := range conf.Variables {
|
|
|
|
for k, val := range v.DefaultsMap() {
|
|
|
|
defaultVars[k] = val
|
2014-08-31 02:25:34 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-05 19:48:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
return &walkContext{
|
2014-09-24 00:46:20 +02:00
|
|
|
Context: c,
|
|
|
|
Operation: op,
|
|
|
|
Path: path,
|
2014-09-24 01:44:21 +02:00
|
|
|
Variables: c.variables,
|
|
|
|
|
|
|
|
defaultVariables: defaultVars,
|
2014-09-23 22:21:45 +02:00
|
|
|
}
|
2014-07-05 19:48:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
// walkContext is the context in which a graph walk is done. It stores
|
|
|
|
// much the same as a Context but works on a specific module.
|
|
|
|
type walkContext struct {
|
2014-09-24 00:46:20 +02:00
|
|
|
Context *Context
|
|
|
|
Meta interface{}
|
|
|
|
Operation walkOperation
|
|
|
|
Path []string
|
2014-09-24 01:44:21 +02:00
|
|
|
Variables map[string]string
|
2014-07-07 08:03:51 +02:00
|
|
|
|
2014-09-24 01:44:21 +02:00
|
|
|
defaultVariables map[string]string
|
2014-07-05 19:48:47 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
// This is only set manually by subsequent context creations
|
|
|
|
// in genericWalkFunc.
|
|
|
|
graph *depgraph.Graph
|
2014-09-23 22:21:45 +02:00
|
|
|
}
|
2014-09-16 02:30:18 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
// walkOperation is an enum which tells the walkContext what to do.
|
|
|
|
type walkOperation byte
|
2014-08-05 19:12:35 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
const (
|
|
|
|
walkInvalid walkOperation = iota
|
2014-09-29 08:37:36 +02:00
|
|
|
walkInput
|
2014-09-24 00:46:20 +02:00
|
|
|
walkApply
|
|
|
|
walkPlan
|
|
|
|
walkPlanDestroy
|
|
|
|
walkRefresh
|
2014-09-25 04:31:30 +02:00
|
|
|
walkValidate
|
2014-09-24 00:46:20 +02:00
|
|
|
)
|
2014-08-05 19:12:35 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
func (c *walkContext) Walk() error {
|
|
|
|
g := c.graph
|
|
|
|
if g == nil {
|
|
|
|
gopts := &GraphOpts{
|
|
|
|
Module: c.Context.module,
|
|
|
|
Providers: c.Context.providers,
|
|
|
|
Provisioners: c.Context.provisioners,
|
|
|
|
State: c.Context.state,
|
2014-07-05 19:48:47 +02:00
|
|
|
}
|
2014-09-24 00:46:20 +02:00
|
|
|
if c.Operation == walkApply {
|
|
|
|
gopts.Diff = c.Context.diff
|
2014-09-17 01:55:10 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
var err error
|
|
|
|
g, err = Graph(gopts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-05 19:48:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
var walkFn depgraph.WalkFunc
|
|
|
|
switch c.Operation {
|
2014-09-29 08:37:36 +02:00
|
|
|
case walkInput:
|
|
|
|
walkFn = c.inputWalkFn()
|
2014-09-24 00:46:20 +02:00
|
|
|
case walkApply:
|
|
|
|
walkFn = c.applyWalkFn()
|
|
|
|
case walkPlan:
|
|
|
|
walkFn = c.planWalkFn()
|
|
|
|
case walkPlanDestroy:
|
|
|
|
walkFn = c.planDestroyWalkFn()
|
|
|
|
case walkRefresh:
|
|
|
|
walkFn = c.refreshWalkFn()
|
2014-09-25 04:31:30 +02:00
|
|
|
case walkValidate:
|
|
|
|
walkFn = c.validateWalkFn()
|
2014-09-24 00:46:20 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown operation: %s", c.Operation))
|
2014-07-05 19:48:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
if err := g.Walk(walkFn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-05 19:48:47 +02:00
|
|
|
|
2014-09-29 08:37:36 +02:00
|
|
|
switch c.Operation {
|
|
|
|
case walkInput:
|
|
|
|
fallthrough
|
|
|
|
case walkValidate:
|
|
|
|
// Don't calculate outputs
|
2014-09-25 04:36:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-03 19:29:14 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
// We did an apply, so we need to calculate the outputs. If we have no
|
|
|
|
// outputs, then we're done.
|
|
|
|
m := c.Context.module
|
|
|
|
for _, n := range c.Path[1:] {
|
|
|
|
cs := m.Children()
|
|
|
|
m = cs[n]
|
|
|
|
}
|
2014-09-24 01:07:41 +02:00
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-09-24 01:27:38 +02:00
|
|
|
conf := m.Config()
|
|
|
|
if len(conf.Outputs) == 0 {
|
2014-09-24 00:46:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-03 20:27:30 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
// Likewise, if we have no resources in our state, we're done. This
|
|
|
|
// guards against the case that we destroyed.
|
|
|
|
mod := c.Context.state.ModuleByPath(c.Path)
|
2014-10-16 05:32:19 +02:00
|
|
|
if mod == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-09-24 01:07:41 +02:00
|
|
|
if c.Operation == walkApply {
|
|
|
|
// On Apply, we prune so that we don't do outputs if we destroyed
|
|
|
|
mod.prune()
|
|
|
|
}
|
2014-10-13 07:49:05 +02:00
|
|
|
if len(mod.Resources) == 0 {
|
|
|
|
mod.Outputs = nil
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-03 20:27:30 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
outputs := make(map[string]string)
|
2014-09-24 01:27:38 +02:00
|
|
|
for _, o := range conf.Outputs {
|
2014-10-03 07:02:59 +02:00
|
|
|
if err := c.computeVars(o.RawConfig, nil); err != nil {
|
2014-10-21 03:45:52 +02:00
|
|
|
// If we're refreshing, then we ignore output errors. This is
|
|
|
|
// properly not fully the correct behavior, but fixes a range
|
|
|
|
// of issues right now. As we expand test cases to find the
|
|
|
|
// correct behavior, this will likely be removed.
|
|
|
|
if c.Operation == walkRefresh {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
return err
|
|
|
|
}
|
2014-09-24 01:07:41 +02:00
|
|
|
vraw := o.RawConfig.Config()["value"]
|
2014-09-24 01:27:38 +02:00
|
|
|
if vraw == nil {
|
|
|
|
// This likely means that the result of the output is
|
|
|
|
// a computed variable.
|
|
|
|
if o.RawConfig.Raw["value"] != nil {
|
|
|
|
vraw = config.UnknownVariableValue
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 01:07:41 +02:00
|
|
|
if vraw != nil {
|
|
|
|
outputs[o.Name] = vraw.(string)
|
|
|
|
}
|
2014-09-24 00:46:20 +02:00
|
|
|
}
|
2014-07-03 20:27:30 +02:00
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
// Assign the outputs to the root module
|
|
|
|
mod.Outputs = outputs
|
2014-07-03 20:27:30 +02:00
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
return nil
|
2014-07-03 20:27:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-29 08:37:36 +02:00
|
|
|
func (c *walkContext) inputWalkFn() depgraph.WalkFunc {
|
|
|
|
meta := c.Meta.(*walkInputMeta)
|
|
|
|
meta.Lock()
|
|
|
|
if meta.Done == nil {
|
|
|
|
meta.Done = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
meta.Unlock()
|
|
|
|
|
|
|
|
return func(n *depgraph.Noun) error {
|
|
|
|
// If it is the root node, ignore
|
|
|
|
if n.Name == GraphRootNode {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rn := n.Meta.(type) {
|
|
|
|
case *GraphNodeModule:
|
|
|
|
// Build another walkContext for this module and walk it.
|
|
|
|
wc := c.Context.walkContext(c.Operation, rn.Path)
|
|
|
|
|
|
|
|
// Set the graph to specifically walk this subgraph
|
|
|
|
wc.graph = rn.Graph
|
|
|
|
|
|
|
|
// Preserve the meta
|
|
|
|
wc.Meta = c.Meta
|
|
|
|
|
|
|
|
return wc.Walk()
|
|
|
|
case *GraphNodeResource:
|
|
|
|
// Resources don't matter for input. Continue.
|
|
|
|
return nil
|
|
|
|
case *GraphNodeResourceProvider:
|
2014-09-29 18:13:15 +02:00
|
|
|
// Acquire the lock the whole time so we only ask for input
|
|
|
|
// one at a time.
|
|
|
|
meta.Lock()
|
|
|
|
defer meta.Unlock()
|
2014-09-29 08:37:36 +02:00
|
|
|
|
2014-09-29 18:13:15 +02:00
|
|
|
// If we already did this provider, then we're done.
|
|
|
|
if _, ok := meta.Done[rn.ID]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the raw configuration because this is what we
|
|
|
|
// pass into the API.
|
|
|
|
var raw *config.RawConfig
|
|
|
|
sharedProvider := rn.Provider
|
|
|
|
if sharedProvider.Config != nil {
|
|
|
|
raw = sharedProvider.Config.RawConfig
|
|
|
|
}
|
|
|
|
rc := NewResourceConfig(raw)
|
2014-10-18 23:54:42 +02:00
|
|
|
rc.Config = make(map[string]interface{})
|
2014-09-29 08:37:36 +02:00
|
|
|
|
2014-09-29 19:36:49 +02:00
|
|
|
// Wrap the input into a namespace
|
|
|
|
input := &PrefixUIInput{
|
2014-09-29 21:45:28 +02:00
|
|
|
IdPrefix: fmt.Sprintf("provider.%s", rn.ID),
|
2014-09-29 21:52:48 +02:00
|
|
|
QueryPrefix: fmt.Sprintf("provider.%s.", rn.ID),
|
2014-09-29 21:45:28 +02:00
|
|
|
UIInput: c.Context.uiInput,
|
2014-09-29 19:36:49 +02:00
|
|
|
}
|
|
|
|
|
2014-09-29 18:13:15 +02:00
|
|
|
// Go through each provider and capture the input necessary
|
|
|
|
// to satisfy it.
|
|
|
|
configs := make(map[string]map[string]interface{})
|
|
|
|
for k, p := range sharedProvider.Providers {
|
2014-09-29 19:36:49 +02:00
|
|
|
newc, err := p.Input(input, rc)
|
2014-09-29 18:13:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error configuring %s: %s", k, err)
|
2014-09-29 08:37:36 +02:00
|
|
|
}
|
2014-10-18 23:54:42 +02:00
|
|
|
if newc != nil && len(newc.Config) > 0 {
|
|
|
|
configs[k] = newc.Config
|
2014-09-29 18:13:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark this provider as done
|
|
|
|
meta.Done[rn.ID] = struct{}{}
|
|
|
|
|
|
|
|
// Set the configuration
|
|
|
|
c.Context.providerConfig[rn.ID] = configs
|
2014-09-29 08:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
func (c *walkContext) applyWalkFn() depgraph.WalkFunc {
|
2014-09-23 22:47:20 +02:00
|
|
|
cb := func(c *walkContext, r *Resource) error {
|
2014-07-18 00:32:19 +02:00
|
|
|
var err error
|
|
|
|
|
2014-07-03 20:04:04 +02:00
|
|
|
diff := r.Diff
|
|
|
|
if diff.Empty() {
|
2014-07-07 21:53:01 +02:00
|
|
|
log.Printf("[DEBUG] %s: Diff is empty. Will not apply.", r.Id)
|
2014-07-07 08:03:51 +02:00
|
|
|
return nil
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
is := r.State
|
|
|
|
if is == nil {
|
|
|
|
is = new(InstanceState)
|
|
|
|
}
|
|
|
|
is.init()
|
2014-09-16 21:12:15 +02:00
|
|
|
|
2014-07-03 20:04:04 +02:00
|
|
|
if !diff.Destroy {
|
2014-07-08 01:56:23 +02:00
|
|
|
// Since we need the configuration, interpolate the variables
|
2014-10-03 07:02:59 +02:00
|
|
|
if err := r.Config.interpolate(c, r); err != nil {
|
2014-07-08 01:56:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
diff, err = r.Provider.Diff(r.Info, is, r.Config)
|
2014-07-03 20:04:04 +02:00
|
|
|
if err != nil {
|
2014-07-07 08:03:51 +02:00
|
|
|
return err
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
2014-10-05 01:47:42 +02:00
|
|
|
// This can happen if we aren't actually applying anything
|
|
|
|
// except an ID (the "null" provider). It is not really an issue
|
|
|
|
// since the Same check later down will catch any real problems.
|
2014-07-23 04:32:46 +02:00
|
|
|
if diff == nil {
|
2014-10-05 01:47:42 +02:00
|
|
|
diff = new(InstanceDiff)
|
|
|
|
diff.init()
|
2014-07-23 04:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete id from the diff because it is dependent on
|
|
|
|
// our internal plan function.
|
|
|
|
delete(r.Diff.Attributes, "id")
|
|
|
|
delete(diff.Attributes, "id")
|
|
|
|
|
|
|
|
// Verify the diffs are the same
|
|
|
|
if !r.Diff.Same(diff) {
|
|
|
|
log.Printf(
|
|
|
|
"[ERROR] Diffs don't match.\n\nDiff 1: %#v"+
|
|
|
|
"\n\nDiff 2: %#v",
|
|
|
|
r.Diff, diff)
|
|
|
|
return fmt.Errorf(
|
2014-07-23 04:43:09 +02:00
|
|
|
"%s: diffs didn't match during apply. This is a "+
|
|
|
|
"bug with the resource provider, please report a bug.",
|
|
|
|
r.Id)
|
2014-07-23 04:32:46 +02:00
|
|
|
}
|
2014-07-12 05:20:08 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 21:00:41 +02:00
|
|
|
// Remove any output values from the diff
|
|
|
|
for k, ad := range diff.Attributes {
|
|
|
|
if ad.Type == DiffAttrOutput {
|
|
|
|
delete(diff.Attributes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PreApply(r.Info, is, diff))
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 20:15:22 +02:00
|
|
|
// We create a new instance if there was no ID
|
|
|
|
// previously or the diff requires re-creating the
|
|
|
|
// underlying instance
|
2014-10-18 20:37:26 +02:00
|
|
|
createNew := (is.ID == "" && !diff.Destroy) || diff.RequiresNew()
|
2014-09-22 20:15:22 +02:00
|
|
|
|
2014-07-03 20:04:04 +02:00
|
|
|
// With the completed diff, apply!
|
|
|
|
log.Printf("[DEBUG] %s: Executing Apply", r.Id)
|
2014-09-22 07:08:21 +02:00
|
|
|
is, applyerr := r.Provider.Apply(r.Info, is, diff)
|
2014-07-18 00:32:19 +02:00
|
|
|
|
|
|
|
var errs []error
|
|
|
|
if applyerr != nil {
|
|
|
|
errs = append(errs, applyerr)
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the result is instantiated
|
2014-09-17 02:10:34 +02:00
|
|
|
if is == nil {
|
|
|
|
is = new(InstanceState)
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
2014-09-17 02:19:28 +02:00
|
|
|
is.init()
|
2014-07-03 20:04:04 +02:00
|
|
|
|
2014-07-09 02:15:41 +02:00
|
|
|
// Force the "id" attribute to be our ID
|
2014-09-17 02:10:34 +02:00
|
|
|
if is.ID != "" {
|
|
|
|
is.Attributes["id"] = is.ID
|
2014-07-09 02:15:41 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 02:10:34 +02:00
|
|
|
for ak, av := range is.Attributes {
|
2014-07-03 20:04:04 +02:00
|
|
|
// If the value is the unknown variable value, then it is an error.
|
|
|
|
// In this case we record the error and remove it from the state
|
|
|
|
if av == config.UnknownVariableValue {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Attribute with unknown value: %s", ak))
|
2014-09-17 02:10:34 +02:00
|
|
|
delete(is.Attributes, ak)
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
// Set the result state
|
|
|
|
r.State = is
|
|
|
|
c.persistState(r)
|
2014-07-24 16:58:45 +02:00
|
|
|
|
2014-07-08 23:01:27 +02:00
|
|
|
// Invoke any provisioners we have defined. This is only done
|
|
|
|
// if the resource was created, as updates or deletes do not
|
|
|
|
// invoke provisioners.
|
2014-07-18 00:32:19 +02:00
|
|
|
//
|
|
|
|
// Additionally, we need to be careful to not run this if there
|
|
|
|
// was an error during the provider apply.
|
2014-07-22 19:09:11 +02:00
|
|
|
tainted := false
|
2014-10-17 08:19:07 +02:00
|
|
|
if createNew && len(r.Provisioners) > 0 {
|
|
|
|
if applyerr == nil {
|
|
|
|
// If the apply succeeded, we have to run the provisioners
|
|
|
|
for _, h := range c.Context.hooks {
|
|
|
|
handleHook(h.PreProvisionResource(r.Info, is))
|
|
|
|
}
|
2014-07-27 18:00:34 +02:00
|
|
|
|
2014-10-17 08:19:07 +02:00
|
|
|
if err := c.applyProvisioners(r, is); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
tainted = true
|
|
|
|
}
|
2014-07-27 18:00:34 +02:00
|
|
|
|
2014-10-17 08:19:07 +02:00
|
|
|
for _, h := range c.Context.hooks {
|
|
|
|
handleHook(h.PostProvisionResource(r.Info, is))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we failed to create properly and we have provisioners,
|
|
|
|
// then we have to mark ourselves as tainted to try again.
|
|
|
|
tainted = true
|
2014-07-27 18:00:34 +02:00
|
|
|
}
|
2014-07-08 23:01:27 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
// If we're tainted then we need to update some flags
|
|
|
|
if tainted && r.Flags&FlagTainted == 0 {
|
2014-09-21 02:02:31 +02:00
|
|
|
r.Flags &^= FlagPrimary
|
|
|
|
r.Flags &^= FlagHasTainted
|
|
|
|
r.Flags |= FlagTainted
|
2014-09-22 07:08:21 +02:00
|
|
|
r.TaintedIndex = -1
|
|
|
|
c.persistState(r)
|
2014-09-21 02:02:31 +02:00
|
|
|
}
|
2014-07-03 20:04:04 +02:00
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PostApply(r.Info, is, applyerr))
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the new state and update variables
|
|
|
|
err = nil
|
|
|
|
if len(errs) > 0 {
|
|
|
|
err = &multierror.Error{Errors: errs}
|
|
|
|
}
|
|
|
|
|
2014-07-07 08:03:51 +02:00
|
|
|
return err
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 06:53:22 +02:00
|
|
|
return c.genericWalkFn(cb)
|
2014-07-03 20:04:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
func (c *walkContext) planWalkFn() depgraph.WalkFunc {
|
2014-07-03 19:44:30 +02:00
|
|
|
var l sync.Mutex
|
|
|
|
|
|
|
|
// Initialize the result
|
2014-09-24 00:46:20 +02:00
|
|
|
result := c.Meta.(*Plan)
|
2014-07-03 19:44:30 +02:00
|
|
|
result.init()
|
|
|
|
|
2014-09-23 22:47:20 +02:00
|
|
|
cb := func(c *walkContext, r *Resource) error {
|
2014-09-21 02:02:31 +02:00
|
|
|
if r.Flags&FlagTainted != 0 {
|
2014-09-20 06:47:53 +02:00
|
|
|
// We don't diff tainted resources.
|
2014-09-20 06:35:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
var diff *InstanceDiff
|
2014-07-03 19:44:30 +02:00
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
is := r.State
|
2014-09-18 02:18:03 +02:00
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PreDiff(r.Info, is))
|
2014-07-03 19:44:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-21 02:02:31 +02:00
|
|
|
if r.Flags&FlagOrphan != 0 {
|
2014-07-03 19:44:30 +02:00
|
|
|
log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id)
|
|
|
|
|
|
|
|
// This is an orphan (no config), so we mark it to be destroyed
|
2014-09-18 01:33:24 +02:00
|
|
|
diff = &InstanceDiff{Destroy: true}
|
2014-07-03 19:44:30 +02:00
|
|
|
} else {
|
2014-07-08 01:56:23 +02:00
|
|
|
// Make sure the configuration is interpolated
|
2014-10-03 07:02:59 +02:00
|
|
|
if err := r.Config.interpolate(c, r); err != nil {
|
2014-07-08 01:56:23 +02:00
|
|
|
return err
|
|
|
|
}
|
2014-07-03 19:44:30 +02:00
|
|
|
|
|
|
|
// Get a diff from the newest state
|
2014-07-08 01:56:23 +02:00
|
|
|
log.Printf("[DEBUG] %s: Executing diff", r.Id)
|
2014-07-03 19:44:30 +02:00
|
|
|
var err error
|
2014-09-22 07:08:21 +02:00
|
|
|
|
|
|
|
diffIs := is
|
|
|
|
if diffIs == nil || r.Flags&FlagHasTainted != 0 {
|
2014-07-22 19:30:42 +02:00
|
|
|
// If we're tainted, we pretend to create a new thing.
|
2014-09-22 07:08:21 +02:00
|
|
|
diffIs = new(InstanceState)
|
2014-07-22 19:30:42 +02:00
|
|
|
}
|
2014-09-22 07:08:21 +02:00
|
|
|
diffIs.init()
|
|
|
|
|
|
|
|
diff, err = r.Provider.Diff(r.Info, diffIs, r.Config)
|
2014-07-03 19:44:30 +02:00
|
|
|
if err != nil {
|
2014-07-07 08:03:51 +02:00
|
|
|
return err
|
2014-07-03 19:44:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:58:31 +02:00
|
|
|
if diff == nil {
|
2014-09-18 01:33:24 +02:00
|
|
|
diff = new(InstanceDiff)
|
2014-07-09 01:58:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-21 02:02:31 +02:00
|
|
|
if r.Flags&FlagHasTainted != 0 {
|
|
|
|
// This primary has a tainted resource, so just mark for
|
|
|
|
// destroy...
|
|
|
|
log.Printf("[DEBUG] %s: Tainted children, marking for destroy", r.Id)
|
2014-09-20 06:47:53 +02:00
|
|
|
diff.DestroyTainted = true
|
2014-07-22 19:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-09-18 02:18:03 +02:00
|
|
|
if diff.RequiresNew() && is != nil && is.ID != "" {
|
2014-07-09 01:58:31 +02:00
|
|
|
// This will also require a destroy
|
|
|
|
diff.Destroy = true
|
|
|
|
}
|
|
|
|
|
2014-09-18 02:18:03 +02:00
|
|
|
if diff.RequiresNew() || is == nil || is.ID == "" {
|
2014-09-17 01:21:09 +02:00
|
|
|
var oldID string
|
2014-09-18 02:18:03 +02:00
|
|
|
if is != nil {
|
|
|
|
oldID = is.Attributes["id"]
|
2014-09-17 01:21:09 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 01:58:31 +02:00
|
|
|
// Add diff to compute new ID
|
|
|
|
diff.init()
|
|
|
|
diff.Attributes["id"] = &ResourceAttrDiff{
|
2014-09-17 01:21:09 +02:00
|
|
|
Old: oldID,
|
2014-07-09 01:58:31 +02:00
|
|
|
NewComputed: true,
|
|
|
|
RequiresNew: true,
|
|
|
|
Type: DiffAttrOutput,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 19:44:30 +02:00
|
|
|
if !diff.Empty() {
|
2014-10-10 08:15:42 +02:00
|
|
|
log.Printf("[DEBUG] %s: Diff: %#v", r.Id, diff)
|
|
|
|
|
2014-09-23 23:15:40 +02:00
|
|
|
l.Lock()
|
|
|
|
md := result.Diff.ModuleByPath(c.Path)
|
|
|
|
if md == nil {
|
|
|
|
md = result.Diff.AddModule(c.Path)
|
|
|
|
}
|
|
|
|
md.Resources[r.Id] = diff
|
|
|
|
l.Unlock()
|
2014-07-03 19:44:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PostDiff(r.Info, diff))
|
2014-07-03 19:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the new state and update variables
|
|
|
|
if !diff.Empty() {
|
2014-09-18 02:18:03 +02:00
|
|
|
is = is.MergeDiff(diff)
|
2014-07-03 19:44:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
// Set it so that it can be updated
|
|
|
|
r.State = is
|
|
|
|
c.persistState(r)
|
2014-07-07 08:03:51 +02:00
|
|
|
|
|
|
|
return nil
|
2014-07-03 19:44:30 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 06:53:22 +02:00
|
|
|
return c.genericWalkFn(cb)
|
2014-07-03 19:44:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
func (c *walkContext) planDestroyWalkFn() depgraph.WalkFunc {
|
2014-07-07 08:03:51 +02:00
|
|
|
var l sync.Mutex
|
|
|
|
|
|
|
|
// Initialize the result
|
2014-09-24 00:46:20 +02:00
|
|
|
result := c.Meta.(*Plan)
|
2014-07-07 08:03:51 +02:00
|
|
|
result.init()
|
|
|
|
|
2014-10-02 22:23:16 +02:00
|
|
|
var walkFn depgraph.WalkFunc
|
|
|
|
walkFn = func(n *depgraph.Noun) error {
|
2014-09-26 05:44:34 +02:00
|
|
|
switch m := n.Meta.(type) {
|
|
|
|
case *GraphNodeModule:
|
|
|
|
// Build another walkContext for this module and walk it.
|
|
|
|
wc := c.Context.walkContext(c.Operation, m.Path)
|
2014-07-07 08:03:51 +02:00
|
|
|
|
2014-09-26 05:44:34 +02:00
|
|
|
// Set the graph to specifically walk this subgraph
|
|
|
|
wc.graph = m.Graph
|
2014-07-07 08:03:51 +02:00
|
|
|
|
2014-09-26 05:44:34 +02:00
|
|
|
// Preserve the meta
|
|
|
|
wc.Meta = c.Meta
|
|
|
|
|
|
|
|
return wc.Walk()
|
|
|
|
case *GraphNodeResource:
|
2014-10-02 22:23:16 +02:00
|
|
|
// If we're expanding, then expand the nodes, and then rewalk the graph
|
|
|
|
if m.ExpandMode > ResourceExpandNone {
|
|
|
|
return c.genericWalkResource(m, walkFn)
|
|
|
|
}
|
|
|
|
|
2014-09-26 05:44:34 +02:00
|
|
|
r := m.Resource
|
2014-10-02 22:23:16 +02:00
|
|
|
|
2014-09-26 05:44:34 +02:00
|
|
|
if r.State != nil && r.State.ID != "" {
|
|
|
|
log.Printf("[DEBUG] %s: Making for destroy", r.Id)
|
|
|
|
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
md := result.Diff.ModuleByPath(c.Path)
|
|
|
|
if md == nil {
|
|
|
|
md = result.Diff.AddModule(c.Path)
|
|
|
|
}
|
|
|
|
md.Resources[r.Id] = &InstanceDiff{Destroy: true}
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] %s: Not marking for destroy, no ID", r.Id)
|
|
|
|
}
|
2014-07-07 08:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-02 22:23:16 +02:00
|
|
|
|
|
|
|
return walkFn
|
2014-07-07 08:03:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
func (c *walkContext) refreshWalkFn() depgraph.WalkFunc {
|
2014-09-23 22:47:20 +02:00
|
|
|
cb := func(c *walkContext, r *Resource) error {
|
2014-09-22 07:08:21 +02:00
|
|
|
is := r.State
|
2014-09-20 06:28:13 +02:00
|
|
|
|
|
|
|
if is == nil || is.ID == "" {
|
2014-07-08 06:12:21 +02:00
|
|
|
log.Printf("[DEBUG] %s: Not refreshing, ID is empty", r.Id)
|
2014-07-08 01:19:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PreRefresh(r.Info, is))
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 07:01:51 +02:00
|
|
|
is, err := r.Provider.Refresh(r.Info, is)
|
2014-07-03 19:29:14 +02:00
|
|
|
if err != nil {
|
2014-07-07 08:03:51 +02:00
|
|
|
return err
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
2014-09-17 02:19:28 +02:00
|
|
|
if is == nil {
|
|
|
|
is = new(InstanceState)
|
|
|
|
is.init()
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
// Set the updated state
|
|
|
|
r.State = is
|
|
|
|
c.persistState(r)
|
2014-07-03 19:29:14 +02:00
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PostRefresh(r.Info, is))
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 08:03:51 +02:00
|
|
|
return nil
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 06:28:13 +02:00
|
|
|
return c.genericWalkFn(cb)
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 04:31:30 +02:00
|
|
|
func (c *walkContext) validateWalkFn() depgraph.WalkFunc {
|
2014-07-10 22:36:06 +02:00
|
|
|
var l sync.Mutex
|
|
|
|
|
2014-09-25 04:31:30 +02:00
|
|
|
meta := c.Meta.(*walkValidateMeta)
|
2014-09-25 07:35:11 +02:00
|
|
|
if meta.Children == nil {
|
|
|
|
meta.Children = make(map[string]*walkValidateMeta)
|
|
|
|
}
|
2014-09-25 04:31:30 +02:00
|
|
|
|
2014-10-02 22:24:38 +02:00
|
|
|
var walkFn depgraph.WalkFunc
|
|
|
|
walkFn = func(n *depgraph.Noun) error {
|
2014-07-03 21:17:56 +02:00
|
|
|
// If it is the root node, ignore
|
|
|
|
if n.Name == GraphRootNode {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rn := n.Meta.(type) {
|
2014-09-25 04:31:30 +02:00
|
|
|
case *GraphNodeModule:
|
|
|
|
// Build another walkContext for this module and walk it.
|
|
|
|
wc := c.Context.walkContext(walkValidate, rn.Path)
|
|
|
|
|
|
|
|
// Set the graph to specifically walk this subgraph
|
|
|
|
wc.graph = rn.Graph
|
|
|
|
|
2014-09-25 07:35:11 +02:00
|
|
|
// Build the meta parameter. Do this by sharing the Children
|
|
|
|
// reference but copying the rest into our own Children list.
|
|
|
|
newMeta := new(walkValidateMeta)
|
|
|
|
newMeta.Children = meta.Children
|
|
|
|
wc.Meta = newMeta
|
2014-09-25 04:31:30 +02:00
|
|
|
|
2014-09-25 07:35:11 +02:00
|
|
|
if err := wc.Walk(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMeta.Children = nil
|
|
|
|
meta.Children[strings.Join(rn.Path, ".")] = newMeta
|
|
|
|
return nil
|
2014-07-03 21:17:56 +02:00
|
|
|
case *GraphNodeResource:
|
2014-07-03 21:30:51 +02:00
|
|
|
if rn.Resource == nil {
|
|
|
|
panic("resource should never be nil")
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:24:38 +02:00
|
|
|
// If we're expanding, then expand the nodes, and then rewalk the graph
|
|
|
|
if rn.ExpandMode > ResourceExpandNone {
|
2014-10-03 02:14:25 +02:00
|
|
|
// Interpolate the count and verify it is non-negative
|
|
|
|
rc := NewResourceConfig(rn.Config.RawCount)
|
2014-10-03 07:02:59 +02:00
|
|
|
rc.interpolate(c, rn.Resource)
|
2014-10-18 03:18:28 +02:00
|
|
|
if !rc.IsComputed(rn.Config.RawCount.Key) {
|
|
|
|
count, err := rn.Config.Count()
|
|
|
|
if err == nil {
|
|
|
|
if count < 0 {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"%s error: count must be positive", rn.Resource.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
meta.Errs = append(meta.Errs, err)
|
|
|
|
return nil
|
2014-10-03 02:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:24:38 +02:00
|
|
|
return c.genericWalkResource(rn, walkFn)
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:30:51 +02:00
|
|
|
// If it doesn't have a provider, that is a different problem
|
|
|
|
if rn.Resource.Provider == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-12 01:02:07 +02:00
|
|
|
// Don't validate orphans or tainted since they never have a config
|
2014-09-21 02:02:31 +02:00
|
|
|
if rn.Resource.Flags&FlagOrphan != 0 {
|
2014-07-11 20:09:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-10-12 01:02:07 +02:00
|
|
|
if rn.Resource.Flags&FlagTainted != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-11 20:09:19 +02:00
|
|
|
|
2014-10-09 00:59:50 +02:00
|
|
|
// If the resouce name doesn't match the name regular
|
|
|
|
// expression, show a warning.
|
2014-10-12 01:02:07 +02:00
|
|
|
if !config.NameRegexp.Match([]byte(rn.Config.Name)) {
|
|
|
|
l.Lock()
|
|
|
|
meta.Warns = append(meta.Warns, fmt.Sprintf(
|
|
|
|
"%s: module name can only contain letters, numbers, "+
|
|
|
|
"dashes, and underscores.\n"+
|
|
|
|
"This will be an error in Terraform 0.4",
|
|
|
|
rn.Resource.Id))
|
|
|
|
l.Unlock()
|
2014-10-09 00:59:50 +02:00
|
|
|
}
|
|
|
|
|
2014-10-17 00:54:56 +02:00
|
|
|
// Compute the variables in this resource
|
|
|
|
rn.Resource.Config.interpolate(c, rn.Resource)
|
|
|
|
|
2014-07-03 21:32:00 +02:00
|
|
|
log.Printf("[INFO] Validating resource: %s", rn.Resource.Id)
|
2014-07-03 21:30:51 +02:00
|
|
|
ws, es := rn.Resource.Provider.ValidateResource(
|
2014-09-20 07:01:51 +02:00
|
|
|
rn.Resource.Info.Type, rn.Resource.Config)
|
2014-07-03 21:30:51 +02:00
|
|
|
for i, w := range ws {
|
|
|
|
ws[i] = fmt.Sprintf("'%s' warning: %s", rn.Resource.Id, w)
|
|
|
|
}
|
|
|
|
for i, e := range es {
|
|
|
|
es[i] = fmt.Errorf("'%s' error: %s", rn.Resource.Id, e)
|
|
|
|
}
|
2014-07-10 22:36:06 +02:00
|
|
|
|
|
|
|
l.Lock()
|
2014-09-25 04:31:30 +02:00
|
|
|
meta.Warns = append(meta.Warns, ws...)
|
|
|
|
meta.Errs = append(meta.Errs, es...)
|
2014-07-10 22:36:06 +02:00
|
|
|
l.Unlock()
|
2014-07-08 23:01:27 +02:00
|
|
|
|
|
|
|
for idx, p := range rn.Resource.Provisioners {
|
|
|
|
ws, es := p.Provisioner.Validate(p.Config)
|
|
|
|
for i, w := range ws {
|
|
|
|
ws[i] = fmt.Sprintf("'%s.provisioner.%d' warning: %s", rn.Resource.Id, idx, w)
|
|
|
|
}
|
|
|
|
for i, e := range es {
|
|
|
|
es[i] = fmt.Errorf("'%s.provisioner.%d' error: %s", rn.Resource.Id, idx, e)
|
|
|
|
}
|
2014-07-10 22:36:06 +02:00
|
|
|
|
|
|
|
l.Lock()
|
2014-09-25 04:31:30 +02:00
|
|
|
meta.Warns = append(meta.Warns, ws...)
|
|
|
|
meta.Errs = append(meta.Errs, es...)
|
2014-07-10 22:36:06 +02:00
|
|
|
l.Unlock()
|
2014-07-08 23:01:27 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 21:17:56 +02:00
|
|
|
case *GraphNodeResourceProvider:
|
2014-09-25 04:31:30 +02:00
|
|
|
sharedProvider := rn.Provider
|
|
|
|
|
2014-09-29 21:04:14 +02:00
|
|
|
// Check if we have an override
|
|
|
|
cs, ok := c.Context.providerConfig[rn.ID]
|
|
|
|
if !ok {
|
|
|
|
cs = make(map[string]map[string]interface{})
|
2014-07-03 21:30:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 04:31:30 +02:00
|
|
|
for k, p := range sharedProvider.Providers {
|
2014-09-29 21:04:14 +02:00
|
|
|
// Merge the configurations to get what we use to configure with
|
|
|
|
rc := sharedProvider.MergeConfig(false, cs[k])
|
2014-10-03 07:02:59 +02:00
|
|
|
rc.interpolate(c, nil)
|
2014-09-29 21:04:14 +02:00
|
|
|
|
2014-07-03 21:17:56 +02:00
|
|
|
log.Printf("[INFO] Validating provider: %s", k)
|
|
|
|
ws, es := p.Validate(rc)
|
|
|
|
for i, w := range ws {
|
|
|
|
ws[i] = fmt.Sprintf("Provider '%s' warning: %s", k, w)
|
|
|
|
}
|
|
|
|
for i, e := range es {
|
|
|
|
es[i] = fmt.Errorf("Provider '%s' error: %s", k, e)
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:36:06 +02:00
|
|
|
l.Lock()
|
2014-09-25 04:31:30 +02:00
|
|
|
meta.Warns = append(meta.Warns, ws...)
|
|
|
|
meta.Errs = append(meta.Errs, es...)
|
2014-07-10 22:36:06 +02:00
|
|
|
l.Unlock()
|
2014-07-03 21:17:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-02 22:24:38 +02:00
|
|
|
|
|
|
|
return walkFn
|
2014-07-03 21:17:56 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
func (c *walkContext) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
|
2014-07-03 19:29:14 +02:00
|
|
|
// This will keep track of whether we're stopped or not
|
|
|
|
var stop uint32 = 0
|
|
|
|
|
2014-10-02 19:42:58 +02:00
|
|
|
var walkFn depgraph.WalkFunc
|
|
|
|
walkFn = func(n *depgraph.Noun) error {
|
2014-07-03 19:29:14 +02:00
|
|
|
// If it is the root node, ignore
|
|
|
|
if n.Name == GraphRootNode {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're stopped, return right away
|
|
|
|
if atomic.LoadUint32(&stop) != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m := n.Meta.(type) {
|
2014-09-23 01:48:18 +02:00
|
|
|
case *GraphNodeModule:
|
2014-09-23 22:47:20 +02:00
|
|
|
// Build another walkContext for this module and walk it.
|
2014-09-24 00:46:20 +02:00
|
|
|
wc := c.Context.walkContext(c.Operation, m.Path)
|
|
|
|
|
|
|
|
// Set the graph to specifically walk this subgraph
|
|
|
|
wc.graph = m.Graph
|
|
|
|
|
|
|
|
// Preserve the meta
|
|
|
|
wc.Meta = c.Meta
|
|
|
|
|
2014-09-24 01:44:21 +02:00
|
|
|
// Set the variables
|
|
|
|
if m.Config != nil {
|
|
|
|
wc.Variables = make(map[string]string)
|
|
|
|
|
|
|
|
rc := NewResourceConfig(m.Config.RawConfig)
|
2014-10-03 07:02:59 +02:00
|
|
|
rc.interpolate(c, nil)
|
2014-09-24 01:44:21 +02:00
|
|
|
for k, v := range rc.Config {
|
|
|
|
wc.Variables[k] = v.(string)
|
|
|
|
}
|
2014-09-24 02:05:44 +02:00
|
|
|
for k, _ := range rc.Raw {
|
|
|
|
if _, ok := wc.Variables[k]; !ok {
|
|
|
|
wc.Variables[k] = config.UnknownVariableValue
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 01:44:21 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 00:46:20 +02:00
|
|
|
return wc.Walk()
|
2014-07-03 19:29:14 +02:00
|
|
|
case *GraphNodeResource:
|
2014-07-07 08:03:51 +02:00
|
|
|
// Continue, we care about this the most
|
2014-07-03 19:29:14 +02:00
|
|
|
case *GraphNodeResourceProvider:
|
2014-09-24 22:31:35 +02:00
|
|
|
sharedProvider := m.Provider
|
|
|
|
|
2014-09-29 18:13:15 +02:00
|
|
|
// Check if we have an override
|
|
|
|
cs, ok := c.Context.providerConfig[m.ID]
|
|
|
|
if !ok {
|
|
|
|
cs = make(map[string]map[string]interface{})
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 22:31:35 +02:00
|
|
|
for k, p := range sharedProvider.Providers {
|
2014-10-18 22:58:01 +02:00
|
|
|
// Interpolate our own configuration before merging
|
|
|
|
if sharedProvider.Config != nil {
|
|
|
|
rc := NewResourceConfig(sharedProvider.Config.RawConfig)
|
|
|
|
rc.interpolate(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the configurations to get what we use to configure
|
|
|
|
// with. We don't need to interpolate this because the
|
|
|
|
// lines above verify that all parents are interpolated
|
|
|
|
// properly.
|
2014-09-29 18:13:15 +02:00
|
|
|
rc := sharedProvider.MergeConfig(false, cs[k])
|
|
|
|
|
2014-07-03 19:29:14 +02:00
|
|
|
log.Printf("[INFO] Configuring provider: %s", k)
|
|
|
|
err := p.Configure(rc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-07-04 06:24:17 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown graph node: %#v", n.Meta))
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rn := n.Meta.(*GraphNodeResource)
|
|
|
|
|
2014-10-02 19:42:58 +02:00
|
|
|
// If we're expanding, then expand the nodes, and then rewalk the graph
|
|
|
|
if rn.ExpandMode > ResourceExpandNone {
|
2014-10-02 22:23:16 +02:00
|
|
|
return c.genericWalkResource(rn, walkFn)
|
2014-10-02 19:42:58 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 19:29:14 +02:00
|
|
|
// Make sure that at least some resource configuration is set
|
2014-09-21 02:02:31 +02:00
|
|
|
if rn.Config == nil {
|
|
|
|
rn.Resource.Config = new(ResourceConfig)
|
2014-07-03 19:29:14 +02:00
|
|
|
} else {
|
2014-09-21 02:02:31 +02:00
|
|
|
rn.Resource.Config = NewResourceConfig(rn.Config.RawConfig)
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle recovery of special panic scenarios
|
|
|
|
defer func() {
|
|
|
|
if v := recover(); v != nil {
|
|
|
|
if v == HookActionHalt {
|
|
|
|
atomic.StoreUint32(&stop, 1)
|
|
|
|
} else {
|
|
|
|
panic(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-10-16 19:04:24 +02:00
|
|
|
// Limit parallelism
|
|
|
|
c.Context.parallelSem.Acquire()
|
|
|
|
defer c.Context.parallelSem.Release()
|
|
|
|
|
2014-07-03 19:29:14 +02:00
|
|
|
// Call the callack
|
2014-07-09 01:58:31 +02:00
|
|
|
log.Printf(
|
2014-09-23 22:47:20 +02:00
|
|
|
"[INFO] Module %s walking: %s (Graph node: %s)",
|
|
|
|
strings.Join(c.Path, "."),
|
2014-07-09 01:58:31 +02:00
|
|
|
rn.Resource.Id,
|
|
|
|
n.Name)
|
2014-09-23 22:47:20 +02:00
|
|
|
if err := cb(c, rn.Resource); err != nil {
|
2014-07-08 06:20:48 +02:00
|
|
|
log.Printf("[ERROR] Error walking '%s': %s", rn.Resource.Id, err)
|
2014-07-03 19:29:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-02 19:42:58 +02:00
|
|
|
|
|
|
|
return walkFn
|
2014-07-03 19:29:14 +02:00
|
|
|
}
|
2014-09-22 07:08:21 +02:00
|
|
|
|
2014-10-02 22:23:16 +02:00
|
|
|
func (c *walkContext) genericWalkResource(
|
|
|
|
rn *GraphNodeResource, fn depgraph.WalkFunc) error {
|
|
|
|
// Interpolate the count
|
|
|
|
rc := NewResourceConfig(rn.Config.RawCount)
|
2014-10-03 07:02:59 +02:00
|
|
|
rc.interpolate(c, rn.Resource)
|
2014-10-02 22:23:16 +02:00
|
|
|
|
2014-10-18 03:18:28 +02:00
|
|
|
// If we're validating, then we set the count to 1 if it is computed
|
|
|
|
if c.Operation == walkValidate {
|
|
|
|
if key := rn.Config.RawCount.Key; rc.IsComputed(key) {
|
|
|
|
// Preserve the old value so that we reset it properly
|
|
|
|
old := rn.Config.RawCount.Raw[key]
|
|
|
|
defer func() {
|
|
|
|
rn.Config.RawCount.Raw[key] = old
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Set th count to 1 for validation purposes
|
|
|
|
rn.Config.RawCount.Raw[key] = "1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:23:16 +02:00
|
|
|
// Expand the node to the actual resources
|
2014-10-12 17:57:08 +02:00
|
|
|
g, err := rn.Expand()
|
2014-10-02 22:23:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-12 17:57:08 +02:00
|
|
|
// Walk the graph with our function
|
|
|
|
if err := g.Walk(fn); err != nil {
|
|
|
|
return err
|
2014-10-02 22:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
// applyProvisioners is used to run any provisioners a resource has
|
|
|
|
// defined after the resource creation has already completed.
|
|
|
|
func (c *walkContext) applyProvisioners(r *Resource, is *InstanceState) error {
|
|
|
|
// Store the original connection info, restore later
|
|
|
|
origConnInfo := is.Ephemeral.ConnInfo
|
|
|
|
defer func() {
|
|
|
|
is.Ephemeral.ConnInfo = origConnInfo
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, prov := range r.Provisioners {
|
|
|
|
// Interpolate since we may have variables that depend on the
|
|
|
|
// local resource.
|
2014-10-03 07:02:59 +02:00
|
|
|
if err := prov.Config.interpolate(c, r); err != nil {
|
2014-09-23 22:21:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate the conn info, since it may contain variables
|
|
|
|
connInfo := NewResourceConfig(prov.ConnInfo)
|
2014-10-03 07:02:59 +02:00
|
|
|
if err := connInfo.interpolate(c, r); err != nil {
|
2014-09-23 22:21:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the connection information
|
|
|
|
overlay := make(map[string]string)
|
|
|
|
if origConnInfo != nil {
|
|
|
|
for k, v := range origConnInfo {
|
|
|
|
overlay[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range connInfo.Config {
|
|
|
|
switch vt := v.(type) {
|
|
|
|
case string:
|
|
|
|
overlay[k] = vt
|
|
|
|
case int64:
|
|
|
|
overlay[k] = strconv.FormatInt(vt, 10)
|
|
|
|
case int32:
|
|
|
|
overlay[k] = strconv.FormatInt(int64(vt), 10)
|
|
|
|
case int:
|
|
|
|
overlay[k] = strconv.FormatInt(int64(vt), 10)
|
|
|
|
case float32:
|
|
|
|
overlay[k] = strconv.FormatFloat(float64(vt), 'f', 3, 32)
|
|
|
|
case float64:
|
|
|
|
overlay[k] = strconv.FormatFloat(vt, 'f', 3, 64)
|
|
|
|
case bool:
|
|
|
|
overlay[k] = strconv.FormatBool(vt)
|
|
|
|
default:
|
|
|
|
overlay[k] = fmt.Sprintf("%v", vt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is.Ephemeral.ConnInfo = overlay
|
|
|
|
|
|
|
|
// Invoke the Provisioner
|
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PreProvision(r.Info, prov.Type))
|
2014-09-23 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2014-10-05 01:24:07 +02:00
|
|
|
output := ProvisionerUIOutput{
|
|
|
|
Info: r.Info,
|
|
|
|
Type: prov.Type,
|
|
|
|
Hooks: c.Context.hooks,
|
2014-10-04 19:38:46 +02:00
|
|
|
}
|
|
|
|
err := prov.Provisioner.Apply(&output, is, prov.Config)
|
2014-10-04 18:20:05 +02:00
|
|
|
if err != nil {
|
2014-09-23 22:21:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range c.Context.hooks {
|
2014-09-25 19:40:44 +02:00
|
|
|
handleHook(h.PostProvision(r.Info, prov.Type))
|
2014-09-23 22:21:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// persistState persists the state in a Resource to the actual final
|
|
|
|
// state location.
|
|
|
|
func (c *walkContext) persistState(r *Resource) {
|
2014-09-22 07:08:21 +02:00
|
|
|
// Acquire a state lock around this whole thing since we're updating that
|
2014-09-23 22:21:45 +02:00
|
|
|
c.Context.sl.Lock()
|
|
|
|
defer c.Context.sl.Unlock()
|
2014-09-22 07:08:21 +02:00
|
|
|
|
|
|
|
// If we have no state, then we don't persist.
|
2014-09-23 22:21:45 +02:00
|
|
|
if c.Context.state == nil {
|
2014-09-22 07:08:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the state for this resource. The resource state should always
|
|
|
|
// exist because we call graphInitState before anything that could
|
|
|
|
// potentially call this.
|
2014-09-23 22:21:45 +02:00
|
|
|
module := c.Context.state.ModuleByPath(c.Path)
|
2014-09-23 23:15:40 +02:00
|
|
|
if module == nil {
|
|
|
|
module = c.Context.state.AddModule(c.Path)
|
|
|
|
}
|
2014-09-22 07:08:21 +02:00
|
|
|
rs := module.Resources[r.Id]
|
|
|
|
if rs == nil {
|
2014-09-23 23:15:40 +02:00
|
|
|
rs = &ResourceState{Type: r.Info.Type}
|
|
|
|
rs.init()
|
|
|
|
module.Resources[r.Id] = rs
|
2014-09-22 07:08:21 +02:00
|
|
|
}
|
2014-09-23 23:20:26 +02:00
|
|
|
rs.Dependencies = r.Dependencies
|
2014-09-22 07:08:21 +02:00
|
|
|
|
|
|
|
// Assign the instance state to the proper location
|
2014-09-23 01:56:41 +02:00
|
|
|
if r.Flags&FlagDeposed != 0 {
|
|
|
|
// We were previously the primary and have been deposed, so
|
|
|
|
// now we are the final tainted resource
|
|
|
|
r.TaintedIndex = len(rs.Tainted) - 1
|
|
|
|
rs.Tainted[r.TaintedIndex] = r.State
|
|
|
|
|
|
|
|
} else if r.Flags&FlagTainted != 0 {
|
2014-09-22 07:08:21 +02:00
|
|
|
if r.TaintedIndex >= 0 {
|
|
|
|
// Tainted with a pre-existing index, just update that spot
|
|
|
|
rs.Tainted[r.TaintedIndex] = r.State
|
2014-09-23 01:56:41 +02:00
|
|
|
|
|
|
|
} else if r.Flags&FlagReplacePrimary != 0 {
|
|
|
|
// We just replaced the primary, so restore the primary
|
|
|
|
rs.Primary = rs.Tainted[len(rs.Tainted)-1]
|
|
|
|
|
|
|
|
// Set ourselves as tainted
|
|
|
|
rs.Tainted[len(rs.Tainted)-1] = r.State
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
} else {
|
|
|
|
// Newly tainted, so append it to the list, update the
|
|
|
|
// index, and remove the primary.
|
|
|
|
rs.Tainted = append(rs.Tainted, r.State)
|
|
|
|
r.TaintedIndex = len(rs.Tainted) - 1
|
2014-09-23 01:56:41 +02:00
|
|
|
rs.Primary = nil
|
2014-09-22 07:08:21 +02:00
|
|
|
}
|
2014-09-23 01:56:41 +02:00
|
|
|
|
|
|
|
} else if r.Flags&FlagReplacePrimary != 0 {
|
|
|
|
// If the ID is blank (there was an error), then we leave
|
|
|
|
// the primary that exists, and do not store this as a tainted
|
|
|
|
// instance
|
|
|
|
if r.State.ID == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the old primary into the tainted state
|
|
|
|
rs.Tainted = append(rs.Tainted, rs.Primary)
|
|
|
|
|
|
|
|
// Set this as the new primary
|
|
|
|
rs.Primary = r.State
|
|
|
|
|
2014-09-22 07:08:21 +02:00
|
|
|
} else {
|
|
|
|
// The primary instance, so just set it directly
|
|
|
|
rs.Primary = r.State
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a pruning so that empty resources are not saved
|
|
|
|
rs.prune()
|
|
|
|
}
|
2014-09-23 22:21:45 +02:00
|
|
|
|
|
|
|
// computeVars takes the State and given RawConfig and processes all
|
|
|
|
// the variables. This dynamically discovers the attributes instead of
|
|
|
|
// using a static map[string]string that the genericWalkFn uses.
|
2014-10-03 07:02:59 +02:00
|
|
|
func (c *walkContext) computeVars(
|
|
|
|
raw *config.RawConfig, r *Resource) error {
|
2014-09-23 22:21:45 +02:00
|
|
|
// If there isn't a raw configuration, don't do anything
|
|
|
|
if raw == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-24 01:44:21 +02:00
|
|
|
// Copy the default variables
|
2014-09-23 22:21:45 +02:00
|
|
|
vs := make(map[string]string)
|
2014-09-24 01:44:21 +02:00
|
|
|
for k, v := range c.defaultVariables {
|
2014-09-23 22:21:45 +02:00
|
|
|
vs[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, the actual computed variables
|
|
|
|
for n, rawV := range raw.Variables {
|
|
|
|
switch v := rawV.(type) {
|
2014-10-03 07:02:59 +02:00
|
|
|
case *config.CountVariable:
|
|
|
|
switch v.Type {
|
|
|
|
case config.CountValueIndex:
|
2014-10-03 07:24:01 +02:00
|
|
|
if r != nil {
|
|
|
|
vs[n] = strconv.FormatInt(int64(r.CountIndex), 10)
|
|
|
|
}
|
2014-10-03 07:02:59 +02:00
|
|
|
}
|
2014-09-24 01:07:41 +02:00
|
|
|
case *config.ModuleVariable:
|
2014-10-17 00:54:56 +02:00
|
|
|
if c.Operation == walkValidate {
|
|
|
|
vs[n] = config.UnknownVariableValue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-24 01:07:41 +02:00
|
|
|
value, err := c.computeModuleVariable(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vs[n] = value
|
2014-10-08 05:09:30 +02:00
|
|
|
case *config.PathVariable:
|
|
|
|
switch v.Type {
|
|
|
|
case config.PathValueCwd:
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Couldn't get cwd for var %s: %s",
|
|
|
|
v.FullKey(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vs[n] = wd
|
|
|
|
case config.PathValueModule:
|
|
|
|
if t := c.Context.module.Child(c.Path[1:]); t != nil {
|
|
|
|
vs[n] = t.Config().Dir
|
|
|
|
}
|
|
|
|
case config.PathValueRoot:
|
|
|
|
vs[n] = c.Context.module.Config().Dir
|
|
|
|
}
|
2014-09-23 22:21:45 +02:00
|
|
|
case *config.ResourceVariable:
|
2014-10-17 00:54:56 +02:00
|
|
|
if c.Operation == walkValidate {
|
|
|
|
vs[n] = config.UnknownVariableValue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
var attr string
|
|
|
|
var err error
|
|
|
|
if v.Multi && v.Index == -1 {
|
|
|
|
attr, err = c.computeResourceMultiVariable(v)
|
|
|
|
} else {
|
|
|
|
attr, err = c.computeResourceVariable(v)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vs[n] = attr
|
|
|
|
case *config.UserVariable:
|
2014-09-24 01:44:21 +02:00
|
|
|
val, ok := c.Variables[v.Name]
|
2014-09-23 22:21:45 +02:00
|
|
|
if ok {
|
|
|
|
vs[n] = val
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-18 03:06:54 +02:00
|
|
|
if _, ok := vs[n]; !ok && c.Operation == walkValidate {
|
2014-10-17 00:54:56 +02:00
|
|
|
vs[n] = config.UnknownVariableValue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
// Look up if we have any variables with this prefix because
|
|
|
|
// those are map overrides. Include those.
|
2014-09-24 01:44:21 +02:00
|
|
|
for k, val := range c.Variables {
|
2014-09-23 22:21:45 +02:00
|
|
|
if strings.HasPrefix(k, v.Name+".") {
|
|
|
|
vs["var."+k] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate the variables
|
|
|
|
return raw.Interpolate(vs)
|
|
|
|
}
|
|
|
|
|
2014-09-24 01:07:41 +02:00
|
|
|
func (c *walkContext) computeModuleVariable(
|
|
|
|
v *config.ModuleVariable) (string, error) {
|
|
|
|
// Build the path to our child
|
|
|
|
path := make([]string, len(c.Path), len(c.Path)+1)
|
|
|
|
copy(path, c.Path)
|
|
|
|
path = append(path, v.Name)
|
|
|
|
|
|
|
|
// Grab some locks
|
|
|
|
c.Context.sl.RLock()
|
|
|
|
defer c.Context.sl.RUnlock()
|
|
|
|
|
|
|
|
// Get that module from our state
|
|
|
|
mod := c.Context.state.ModuleByPath(path)
|
|
|
|
if mod == nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Module '%s' not found for variable '%s'",
|
|
|
|
strings.Join(path[1:], "."),
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := mod.Outputs[v.Field]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Output field '%s' not found for variable '%s'",
|
|
|
|
v.Field,
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
func (c *walkContext) computeResourceVariable(
|
|
|
|
v *config.ResourceVariable) (string, error) {
|
|
|
|
id := v.ResourceId()
|
|
|
|
if v.Multi {
|
|
|
|
id = fmt.Sprintf("%s.%d", id, v.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Context.sl.RLock()
|
|
|
|
defer c.Context.sl.RUnlock()
|
|
|
|
|
|
|
|
// Get the relevant module
|
2014-09-24 01:07:41 +02:00
|
|
|
module := c.Context.state.ModuleByPath(c.Path)
|
2014-09-23 22:21:45 +02:00
|
|
|
|
2014-10-21 08:08:17 +02:00
|
|
|
var r *ResourceState
|
|
|
|
if module != nil {
|
|
|
|
var ok bool
|
|
|
|
r, ok = module.Resources[id]
|
|
|
|
if !ok && v.Multi && v.Index == 0 {
|
2014-10-03 02:24:22 +02:00
|
|
|
r, ok = module.Resources[v.ResourceId()]
|
|
|
|
}
|
|
|
|
if !ok {
|
2014-10-21 08:08:17 +02:00
|
|
|
r = nil
|
2014-10-03 02:24:22 +02:00
|
|
|
}
|
2014-09-23 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 08:08:17 +02:00
|
|
|
if r == nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Resource '%s' not found for variable '%s'",
|
|
|
|
id,
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
if r.Primary == nil {
|
|
|
|
goto MISSING
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := r.Primary.Attributes[v.Field]; ok {
|
|
|
|
return attr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find the exact field, so lets separate the dots
|
|
|
|
// and see if anything along the way is a computed set. i.e. if
|
|
|
|
// we have "foo.0.bar" as the field, check to see if "foo" is
|
|
|
|
// a computed list. If so, then the whole thing is computed.
|
|
|
|
if parts := strings.Split(v.Field, "."); len(parts) > 1 {
|
|
|
|
for i := 1; i < len(parts); i++ {
|
|
|
|
key := fmt.Sprintf("%s.#", strings.Join(parts[:i], "."))
|
|
|
|
if attr, ok := r.Primary.Attributes[key]; ok {
|
|
|
|
return attr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MISSING:
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Resource '%s' does not have attribute '%s' "+
|
|
|
|
"for variable '%s'",
|
|
|
|
id,
|
|
|
|
v.Field,
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *walkContext) computeResourceMultiVariable(
|
|
|
|
v *config.ResourceVariable) (string, error) {
|
|
|
|
c.Context.sl.RLock()
|
|
|
|
defer c.Context.sl.RUnlock()
|
|
|
|
|
|
|
|
// Get the resource from the configuration so we can know how
|
|
|
|
// many of the resource there is.
|
|
|
|
var cr *config.Resource
|
2014-09-24 23:56:48 +02:00
|
|
|
for _, r := range c.Context.module.Config().Resources {
|
2014-09-23 22:21:45 +02:00
|
|
|
if r.Id() == v.ResourceId() {
|
|
|
|
cr = r
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cr == nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Resource '%s' not found for variable '%s'",
|
|
|
|
v.ResourceId(),
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the relevant module
|
|
|
|
// TODO: Not use only root module
|
|
|
|
module := c.Context.state.RootModule()
|
|
|
|
|
2014-10-02 20:48:00 +02:00
|
|
|
count, err := cr.Count()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Error reading %s count: %s",
|
|
|
|
v.ResourceId(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 02:18:40 +02:00
|
|
|
// If we have no count, return empty
|
|
|
|
if count == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:21:45 +02:00
|
|
|
var values []string
|
2014-10-02 20:48:00 +02:00
|
|
|
for i := 0; i < count; i++ {
|
2014-09-23 22:21:45 +02:00
|
|
|
id := fmt.Sprintf("%s.%d", v.ResourceId(), i)
|
|
|
|
|
|
|
|
// If we're dealing with only a single resource, then the
|
|
|
|
// ID doesn't have a trailing index.
|
2014-10-02 20:48:00 +02:00
|
|
|
if count == 1 {
|
2014-09-23 22:21:45 +02:00
|
|
|
id = v.ResourceId()
|
|
|
|
}
|
|
|
|
|
|
|
|
r, ok := module.Resources[id]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Primary == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attr, ok := r.Primary.Attributes[v.Field]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
values = append(values, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) == 0 {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Resource '%s' does not have attribute '%s' "+
|
|
|
|
"for variable '%s'",
|
|
|
|
v.ResourceId(),
|
|
|
|
v.Field,
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
2014-10-10 01:17:00 +02:00
|
|
|
return strings.Join(values, config.InterpSplitDelim), nil
|
2014-09-23 22:21:45 +02:00
|
|
|
}
|
2014-09-25 07:35:11 +02:00
|
|
|
|
2014-09-29 08:37:36 +02:00
|
|
|
type walkInputMeta struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
Done map[string]struct{}
|
|
|
|
}
|
|
|
|
|
2014-09-25 07:35:11 +02:00
|
|
|
type walkValidateMeta struct {
|
|
|
|
Errs []error
|
|
|
|
Warns []string
|
|
|
|
Children map[string]*walkValidateMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *walkValidateMeta) Flatten() ([]string, []error) {
|
|
|
|
// Prune out the empty children
|
|
|
|
for k, m2 := range m.Children {
|
|
|
|
if len(m2.Errs) == 0 && len(m2.Warns) == 0 {
|
|
|
|
delete(m.Children, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no children, then just return what we have
|
|
|
|
if len(m.Children) == 0 {
|
|
|
|
return m.Warns, m.Errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, copy the errors and warnings
|
|
|
|
errs := make([]error, len(m.Errs))
|
|
|
|
warns := make([]string, len(m.Warns))
|
|
|
|
for i, err := range m.Errs {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
for i, warn := range m.Warns {
|
|
|
|
warns[i] = warn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through each child and copy it in...
|
|
|
|
for k, c := range m.Children {
|
|
|
|
for _, err := range c.Errs {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Module %s: %s", k, err))
|
|
|
|
}
|
|
|
|
for _, warn := range c.Warns {
|
|
|
|
warns = append(warns, fmt.Sprintf(
|
|
|
|
"Module %s: %s", k, warn))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return warns, errs
|
|
|
|
}
|