terraform: Apply and Validate provisioners when walking

This commit is contained in:
Armon Dadgar 2014-07-08 14:01:27 -07:00
parent e8245f1a67
commit ee475e8178

View File

@ -480,6 +480,16 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc {
} }
} }
// Invoke any provisioners we have defined. This is only done
// if the resource was created, as updates or deletes do not
// invoke provisioners.
if r.State.ID == "" && len(r.Provisioners) > 0 {
rs, err = c.applyProvisioners(r, rs)
if err != nil {
errs = append(errs, err)
}
}
// Update the resulting diff // Update the resulting diff
c.sl.Lock() c.sl.Lock()
if rs.ID == "" { if rs.ID == "" {
@ -508,6 +518,26 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc {
return c.genericWalkFn(cb) return c.genericWalkFn(cb)
} }
// applyProvisioners is used to run any provisioners a resource has
// defined after the resource creation has already completed.
func (c *Context) applyProvisioners(r *Resource, rs *ResourceState) (*ResourceState, error) {
var err error
for _, prov := range r.Provisioners {
// Interpolate since we may have variables that depend on the
// local resource.
if err := r.Config.interpolate(c); err != nil {
return rs, err
}
// Invoke the Provisioner
rs, err = prov.Provisioner.Apply(rs, r.Config)
if err != nil {
return rs, err
}
}
return rs, nil
}
func (c *Context) planWalkFn(result *Plan) depgraph.WalkFunc { func (c *Context) planWalkFn(result *Plan) depgraph.WalkFunc {
var l sync.Mutex var l sync.Mutex
@ -677,9 +707,21 @@ func (c *Context) validateWalkFn(rws *[]string, res *[]error) depgraph.WalkFunc
for i, e := range es { for i, e := range es {
es[i] = fmt.Errorf("'%s' error: %s", rn.Resource.Id, e) es[i] = fmt.Errorf("'%s' error: %s", rn.Resource.Id, e)
} }
*rws = append(*rws, ws...) *rws = append(*rws, ws...)
*res = append(*res, es...) *res = append(*res, es...)
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)
}
*rws = append(*rws, ws...)
*res = append(*res, es...)
}
case *GraphNodeResourceProvider: case *GraphNodeResourceProvider:
if rn.Config == nil { if rn.Config == nil {
return nil return nil
@ -765,6 +807,14 @@ func (c *Context) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
} else { } else {
rn.Resource.Config = NewResourceConfig(rn.Config.RawConfig) rn.Resource.Config = NewResourceConfig(rn.Config.RawConfig)
} }
for _, prov := range rn.Resource.Provisioners {
if prov.RawConfig == nil {
prov.Config = new(ResourceConfig)
} else {
prov.Config = NewResourceConfig(prov.RawConfig)
}
}
} else { } else {
rn.Resource.Config = nil rn.Resource.Config = nil
} }