2015-02-12 21:34:21 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-02-13 11:49:29 -06:00
|
|
|
"strconv"
|
2015-02-12 21:34:21 -06:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EvalApply is an EvalNode implementation that writes the diff to
|
|
|
|
// the full diff.
|
|
|
|
type EvalApply struct {
|
2015-02-13 12:09:35 -06:00
|
|
|
Info *InstanceInfo
|
|
|
|
State **InstanceState
|
|
|
|
Diff **InstanceDiff
|
|
|
|
Provider *ResourceProvider
|
|
|
|
Output **InstanceState
|
|
|
|
CreateNew *bool
|
|
|
|
Error *error
|
2015-02-12 21:34:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
2015-02-14 00:58:41 -06:00
|
|
|
func (n *EvalApply) Eval(ctx EvalContext) (interface{}, error) {
|
2015-02-12 21:34:21 -06:00
|
|
|
diff := *n.Diff
|
|
|
|
provider := *n.Provider
|
|
|
|
state := *n.State
|
|
|
|
|
|
|
|
// If we have no diff, we have nothing to do!
|
|
|
|
if diff.Empty() {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] apply: %s: diff is empty, doing nothing.", n.Info.Id)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any output values from the diff
|
2016-07-29 12:17:48 -05:00
|
|
|
for k, ad := range diff.CopyAttributes() {
|
2015-02-12 21:34:21 -06:00
|
|
|
if ad.Type == DiffAttrOutput {
|
2016-07-29 12:17:48 -05:00
|
|
|
diff.DelAttribute(k)
|
2015-02-12 21:34:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:53:33 -06:00
|
|
|
// If the state is nil, make it non-nil
|
|
|
|
if state == nil {
|
|
|
|
state = new(InstanceState)
|
|
|
|
}
|
|
|
|
state.init()
|
|
|
|
|
2015-02-13 12:09:35 -06:00
|
|
|
// Flag if we're creating a new instance
|
|
|
|
if n.CreateNew != nil {
|
2016-07-29 12:17:48 -05:00
|
|
|
*n.CreateNew = state.ID == "" && !diff.GetDestroy() || diff.RequiresNew()
|
2015-02-13 12:09:35 -06:00
|
|
|
}
|
|
|
|
|
2015-02-12 21:34:21 -06:00
|
|
|
// With the completed diff, apply!
|
|
|
|
log.Printf("[DEBUG] apply: %s: executing Apply", n.Info.Id)
|
|
|
|
state, err := provider.Apply(n.Info, state, diff)
|
|
|
|
if state == nil {
|
|
|
|
state = new(InstanceState)
|
|
|
|
}
|
|
|
|
state.init()
|
|
|
|
|
|
|
|
// Force the "id" attribute to be our ID
|
|
|
|
if state.ID != "" {
|
|
|
|
state.Attributes["id"] = state.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
for ak, av := range state.Attributes {
|
|
|
|
if av == config.UnknownVariableValue {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"Attribute with unknown value: %s", ak))
|
|
|
|
delete(state.Attributes, ak)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the final state
|
|
|
|
if n.Output != nil {
|
|
|
|
*n.Output = state
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:49:29 -06:00
|
|
|
// If there are no errors, then we append it to our output error
|
|
|
|
// if we have one, otherwise we just output it.
|
|
|
|
if err != nil {
|
|
|
|
if n.Error != nil {
|
2015-07-22 05:50:06 -05:00
|
|
|
helpfulErr := fmt.Errorf("%s: %s", n.Info.Id, err.Error())
|
|
|
|
*n.Error = multierror.Append(*n.Error, helpfulErr)
|
2015-02-13 11:49:29 -06:00
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 21:34:21 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 22:36:53 -06:00
|
|
|
// EvalApplyPre is an EvalNode implementation that does the pre-Apply work
|
|
|
|
type EvalApplyPre struct {
|
|
|
|
Info *InstanceInfo
|
|
|
|
State **InstanceState
|
|
|
|
Diff **InstanceDiff
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
|
|
|
func (n *EvalApplyPre) Eval(ctx EvalContext) (interface{}, error) {
|
|
|
|
state := *n.State
|
|
|
|
diff := *n.Diff
|
|
|
|
|
|
|
|
// If the state is nil, make it non-nil
|
|
|
|
if state == nil {
|
|
|
|
state = new(InstanceState)
|
|
|
|
}
|
|
|
|
state.init()
|
|
|
|
|
2017-08-23 19:23:33 -05:00
|
|
|
if resourceHasUserVisibleApply(n.Info) {
|
2017-01-20 22:36:53 -06:00
|
|
|
// Call post-apply hook
|
|
|
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
return h.PreApply(n.Info, state, diff)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:52:11 -06:00
|
|
|
// EvalApplyPost is an EvalNode implementation that does the post-Apply work
|
|
|
|
type EvalApplyPost struct {
|
|
|
|
Info *InstanceInfo
|
|
|
|
State **InstanceState
|
|
|
|
Error *error
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
2015-02-14 00:58:41 -06:00
|
|
|
func (n *EvalApplyPost) Eval(ctx EvalContext) (interface{}, error) {
|
2015-02-13 11:52:11 -06:00
|
|
|
state := *n.State
|
|
|
|
|
2017-08-23 19:23:33 -05:00
|
|
|
if resourceHasUserVisibleApply(n.Info) {
|
2015-02-13 11:52:11 -06:00
|
|
|
// Call post-apply hook
|
|
|
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
return h.PostApply(n.Info, state, *n.Error)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, *n.Error
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:23:33 -05:00
|
|
|
// resourceHasUserVisibleApply returns true if the given resource is one where
|
|
|
|
// apply actions should be exposed to the user.
|
|
|
|
//
|
|
|
|
// Certain resources do apply actions only as an implementation detail, so
|
|
|
|
// these should not be advertised to code outside of this package.
|
|
|
|
func resourceHasUserVisibleApply(info *InstanceInfo) bool {
|
|
|
|
addr := info.ResourceAddress()
|
|
|
|
|
|
|
|
// Only managed resources have user-visible apply actions.
|
|
|
|
// In particular, this excludes data resources since we "apply" these
|
|
|
|
// only as an implementation detail of removing them from state when
|
|
|
|
// they are destroyed. (When reading, they don't get here at all because
|
|
|
|
// we present them as "Refresh" actions.)
|
|
|
|
return addr.Mode == config.ManagedResourceMode
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:49:29 -06:00
|
|
|
// EvalApplyProvisioners is an EvalNode implementation that executes
|
|
|
|
// the provisioners for a resource.
|
|
|
|
//
|
|
|
|
// TODO(mitchellh): This should probably be split up into a more fine-grained
|
|
|
|
// ApplyProvisioner (single) that is looped over.
|
|
|
|
type EvalApplyProvisioners struct {
|
|
|
|
Info *InstanceInfo
|
|
|
|
State **InstanceState
|
|
|
|
Resource *config.Resource
|
|
|
|
InterpResource *Resource
|
2015-02-13 12:09:35 -06:00
|
|
|
CreateNew *bool
|
2015-02-13 11:49:29 -06:00
|
|
|
Error *error
|
2017-01-20 20:07:51 -06:00
|
|
|
|
|
|
|
// When is the type of provisioner to run at this point
|
|
|
|
When config.ProvisionerWhen
|
2015-02-13 11:49:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
2015-02-14 00:58:41 -06:00
|
|
|
func (n *EvalApplyProvisioners) Eval(ctx EvalContext) (interface{}, error) {
|
2015-02-13 11:49:29 -06:00
|
|
|
state := *n.State
|
|
|
|
|
2017-01-20 20:07:51 -06:00
|
|
|
if n.CreateNew != nil && !*n.CreateNew {
|
2015-02-13 14:50:13 -06:00
|
|
|
// If we're not creating a new resource, then don't run provisioners
|
2015-02-13 11:49:29 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 20:07:51 -06:00
|
|
|
provs := n.filterProvisioners()
|
|
|
|
if len(provs) == 0 {
|
2015-02-13 14:50:13 -06:00
|
|
|
// We have no provisioners, so don't do anything
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 20:26:22 -06:00
|
|
|
// taint tells us whether to enable tainting.
|
|
|
|
taint := n.When == config.ProvisionerWhenCreate
|
|
|
|
|
2015-02-13 14:50:13 -06:00
|
|
|
if n.Error != nil && *n.Error != nil {
|
2017-01-20 20:26:22 -06:00
|
|
|
if taint {
|
|
|
|
state.Tainted = true
|
|
|
|
}
|
2015-02-13 14:50:13 -06:00
|
|
|
|
|
|
|
// We're already tainted, so just return out
|
2015-02-13 12:09:35 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:49:29 -06:00
|
|
|
{
|
|
|
|
// Call pre hook
|
|
|
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
return h.PreProvisionResource(n.Info, state)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no errors, then we append it to our output error
|
|
|
|
// if we have one, otherwise we just output it.
|
2017-01-20 20:07:51 -06:00
|
|
|
err := n.apply(ctx, provs)
|
2015-02-13 11:49:29 -06:00
|
|
|
if err != nil {
|
2017-01-20 20:26:22 -06:00
|
|
|
if taint {
|
|
|
|
state.Tainted = true
|
|
|
|
}
|
2016-04-21 14:59:10 -05:00
|
|
|
|
2018-03-15 13:29:15 -05:00
|
|
|
*n.Error = multierror.Append(*n.Error, err)
|
|
|
|
return nil, err
|
2015-02-13 11:49:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Call post hook
|
|
|
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
return h.PostProvisionResource(n.Info, state)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 20:07:51 -06:00
|
|
|
// filterProvisioners filters the provisioners on the resource to only
|
|
|
|
// the provisioners specified by the "when" option.
|
|
|
|
func (n *EvalApplyProvisioners) filterProvisioners() []*config.Provisioner {
|
|
|
|
// Fast path the zero case
|
|
|
|
if n.Resource == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(n.Resource.Provisioners) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*config.Provisioner, 0, len(n.Resource.Provisioners))
|
|
|
|
for _, p := range n.Resource.Provisioners {
|
|
|
|
if p.When == n.When {
|
|
|
|
result = append(result, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalApplyProvisioners) apply(ctx EvalContext, provs []*config.Provisioner) error {
|
2015-02-13 11:49:29 -06:00
|
|
|
state := *n.State
|
|
|
|
|
|
|
|
// Store the original connection info, restore later
|
|
|
|
origConnInfo := state.Ephemeral.ConnInfo
|
|
|
|
defer func() {
|
|
|
|
state.Ephemeral.ConnInfo = origConnInfo
|
|
|
|
}()
|
|
|
|
|
2017-01-20 20:07:51 -06:00
|
|
|
for _, prov := range provs {
|
2015-02-13 11:49:29 -06:00
|
|
|
// Get the provisioner
|
|
|
|
provisioner := ctx.Provisioner(prov.Type)
|
|
|
|
|
|
|
|
// Interpolate the provisioner config
|
2016-09-26 13:24:13 -05:00
|
|
|
provConfig, err := ctx.Interpolate(prov.RawConfig.Copy(), n.InterpResource)
|
2015-02-13 11:49:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate the conn info, since it may contain variables
|
2016-09-26 13:24:13 -05:00
|
|
|
connInfo, err := ctx.Interpolate(prov.ConnInfo.Copy(), n.InterpResource)
|
2015-02-13 11:49:29 -06:00
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.Ephemeral.ConnInfo = overlay
|
|
|
|
|
|
|
|
{
|
|
|
|
// Call pre hook
|
|
|
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
return h.PreProvision(n.Info, prov.Type)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The output function
|
|
|
|
outputFn := func(msg string) {
|
|
|
|
ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
h.ProvisionOutput(n.Info, prov.Type, msg)
|
|
|
|
return HookActionContinue, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke the Provisioner
|
|
|
|
output := CallbackUIOutput{OutputFn: outputFn}
|
2017-01-20 21:55:32 -06:00
|
|
|
applyErr := provisioner.Apply(&output, state, provConfig)
|
2017-01-20 22:21:12 -06:00
|
|
|
|
|
|
|
// Call post hook
|
|
|
|
hookErr := ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
return h.PostProvision(n.Info, prov.Type, applyErr)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Handle the error before we deal with the hook
|
2017-01-20 21:55:32 -06:00
|
|
|
if applyErr != nil {
|
|
|
|
// Determine failure behavior
|
|
|
|
switch prov.OnFailure {
|
|
|
|
case config.ProvisionerOnFailureContinue:
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] apply: %s [%s]: error during provision, continue requested",
|
|
|
|
n.Info.Id, prov.Type)
|
|
|
|
|
|
|
|
case config.ProvisionerOnFailureFail:
|
|
|
|
return applyErr
|
|
|
|
}
|
2015-02-13 11:49:29 -06:00
|
|
|
}
|
|
|
|
|
2017-01-20 22:21:12 -06:00
|
|
|
// Deal with the hook
|
|
|
|
if hookErr != nil {
|
|
|
|
return hookErr
|
2015-02-13 11:49:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|