2014-06-26 18:52:15 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
// HookAction is an enum of actions that can be taken as a result of a hook
|
|
|
|
// callback. This allows you to modify the behavior of Terraform at runtime.
|
|
|
|
type HookAction byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// HookActionContinue continues with processing as usual.
|
|
|
|
HookActionContinue HookAction = iota
|
|
|
|
|
|
|
|
// HookActionHalt halts immediately: no more hooks are processed
|
|
|
|
// and the action that Terraform was about to take is cancelled.
|
|
|
|
HookActionHalt
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hook is the interface that must be implemented to hook into various
|
|
|
|
// parts of Terraform, allowing you to inspect or change behavior at runtime.
|
|
|
|
//
|
|
|
|
// There are MANY hook points into Terraform. If you only want to implement
|
|
|
|
// some hook points, but not all (which is the likely case), then embed the
|
|
|
|
// NilHook into your struct, which implements all of the interface but does
|
|
|
|
// nothing. Then, override only the functions you want to implement.
|
|
|
|
type Hook interface {
|
2014-06-27 00:09:16 -05:00
|
|
|
// PreApply and PostApply are called before and after a single
|
2014-07-17 17:38:50 -05:00
|
|
|
// resource is applied. The error argument in PostApply is the
|
|
|
|
// error, if any, that was returned from the provider Apply call itself.
|
2014-09-25 12:40:44 -05:00
|
|
|
PreApply(*InstanceInfo, *InstanceState, *InstanceDiff) (HookAction, error)
|
|
|
|
PostApply(*InstanceInfo, *InstanceState, error) (HookAction, error)
|
2014-06-27 00:09:16 -05:00
|
|
|
|
2014-06-26 19:17:10 -05:00
|
|
|
// PreDiff and PostDiff are called before and after a single resource
|
|
|
|
// resource is diffed.
|
2014-09-25 12:40:44 -05:00
|
|
|
PreDiff(*InstanceInfo, *InstanceState) (HookAction, error)
|
|
|
|
PostDiff(*InstanceInfo, *InstanceDiff) (HookAction, error)
|
2014-06-26 18:52:15 -05:00
|
|
|
|
2014-07-27 11:00:34 -05:00
|
|
|
// Provisioning hooks
|
2014-10-04 18:31:48 -05:00
|
|
|
//
|
|
|
|
// All should be self-explanatory. ProvisionOutput is called with
|
|
|
|
// output sent back by the provisioners. This will be called multiple
|
|
|
|
// times as output comes in, but each call should represent a line of
|
|
|
|
// output. The ProvisionOutput method cannot control whether the
|
|
|
|
// hook continues running.
|
2014-09-25 12:40:44 -05:00
|
|
|
PreProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error)
|
|
|
|
PostProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error)
|
|
|
|
PreProvision(*InstanceInfo, string) (HookAction, error)
|
2017-01-20 22:21:12 -06:00
|
|
|
PostProvision(*InstanceInfo, string, error) (HookAction, error)
|
2014-10-04 18:24:07 -05:00
|
|
|
ProvisionOutput(*InstanceInfo, string, string)
|
2014-07-27 11:00:34 -05:00
|
|
|
|
2014-06-26 19:17:10 -05:00
|
|
|
// PreRefresh and PostRefresh are called before and after a single
|
|
|
|
// resource state is refreshed, respectively.
|
2014-09-25 12:40:44 -05:00
|
|
|
PreRefresh(*InstanceInfo, *InstanceState) (HookAction, error)
|
|
|
|
PostRefresh(*InstanceInfo, *InstanceState) (HookAction, error)
|
2015-02-23 21:09:48 -06:00
|
|
|
|
|
|
|
// PostStateUpdate is called after the state is updated.
|
|
|
|
PostStateUpdate(*State) (HookAction, error)
|
2016-05-04 13:53:56 -05:00
|
|
|
|
|
|
|
// PreImportState and PostImportState are called before and after
|
|
|
|
// a single resource's state is being improted.
|
2016-05-04 15:02:51 -05:00
|
|
|
PreImportState(*InstanceInfo, string) (HookAction, error)
|
2016-05-04 13:53:56 -05:00
|
|
|
PostImportState(*InstanceInfo, []*InstanceState) (HookAction, error)
|
2014-06-26 18:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NilHook is a Hook implementation that does nothing. It exists only to
|
|
|
|
// simplify implementing hooks. You can embed this into your Hook implementation
|
|
|
|
// and only implement the functions you are interested in.
|
|
|
|
type NilHook struct{}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PreApply(*InstanceInfo, *InstanceState, *InstanceDiff) (HookAction, error) {
|
2014-06-27 00:09:16 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PostApply(*InstanceInfo, *InstanceState, error) (HookAction, error) {
|
2014-06-27 00:09:16 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PreDiff(*InstanceInfo, *InstanceState) (HookAction, error) {
|
2014-06-26 19:17:10 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PostDiff(*InstanceInfo, *InstanceDiff) (HookAction, error) {
|
2014-06-26 19:17:10 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PreProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
|
2014-07-27 11:00:34 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PostProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
|
2014-07-27 11:00:34 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PreProvision(*InstanceInfo, string) (HookAction, error) {
|
2014-07-27 11:00:34 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 22:21:12 -06:00
|
|
|
func (*NilHook) PostProvision(*InstanceInfo, string, error) (HookAction, error) {
|
2014-07-27 11:00:34 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-10-04 18:24:07 -05:00
|
|
|
func (*NilHook) ProvisionOutput(
|
|
|
|
*InstanceInfo, string, string) {
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PreRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
|
2014-06-26 18:52:15 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func (*NilHook) PostRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
|
2014-06-26 18:52:15 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
2014-07-02 18:27:06 -05:00
|
|
|
|
2016-05-04 15:02:51 -05:00
|
|
|
func (*NilHook) PreImportState(*InstanceInfo, string) (HookAction, error) {
|
2016-05-04 13:53:56 -05:00
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*NilHook) PostImportState(*InstanceInfo, []*InstanceState) (HookAction, error) {
|
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:09:48 -06:00
|
|
|
func (*NilHook) PostStateUpdate(*State) (HookAction, error) {
|
|
|
|
return HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-07-02 18:27:06 -05:00
|
|
|
// handleHook turns hook actions into panics. This lets you use the
|
|
|
|
// panic/recover mechanism in Go as a flow control mechanism for hook
|
|
|
|
// actions.
|
|
|
|
func handleHook(a HookAction, err error) {
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle errors
|
|
|
|
}
|
|
|
|
|
|
|
|
switch a {
|
|
|
|
case HookActionContinue:
|
|
|
|
return
|
|
|
|
case HookActionHalt:
|
|
|
|
panic(HookActionHalt)
|
|
|
|
}
|
|
|
|
}
|