github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/terraform/hook.go (about)

     1  package terraform
     2  
     3  // HookAction is an enum of actions that can be taken as a result of a hook
     4  // callback. This allows you to modify the behavior of Terraform at runtime.
     5  type HookAction byte
     6  
     7  const (
     8  	// HookActionContinue continues with processing as usual.
     9  	HookActionContinue HookAction = iota
    10  
    11  	// HookActionHalt halts immediately: no more hooks are processed
    12  	// and the action that Terraform was about to take is cancelled.
    13  	HookActionHalt
    14  )
    15  
    16  // Hook is the interface that must be implemented to hook into various
    17  // parts of Terraform, allowing you to inspect or change behavior at runtime.
    18  //
    19  // There are MANY hook points into Terraform. If you only want to implement
    20  // some hook points, but not all (which is the likely case), then embed the
    21  // NilHook into your struct, which implements all of the interface but does
    22  // nothing. Then, override only the functions you want to implement.
    23  type Hook interface {
    24  	// PreApply and PostApply are called before and after a single
    25  	// resource is applied. The error argument in PostApply is the
    26  	// error, if any, that was returned from the provider Apply call itself.
    27  	PreApply(*InstanceInfo, *InstanceState, *InstanceDiff) (HookAction, error)
    28  	PostApply(*InstanceInfo, *InstanceState, error) (HookAction, error)
    29  
    30  	// PreDiff and PostDiff are called before and after a single resource
    31  	// resource is diffed.
    32  	PreDiff(*InstanceInfo, *InstanceState) (HookAction, error)
    33  	PostDiff(*InstanceInfo, *InstanceDiff) (HookAction, error)
    34  
    35  	// Provisioning hooks
    36  	//
    37  	// All should be self-explanatory. ProvisionOutput is called with
    38  	// output sent back by the provisioners. This will be called multiple
    39  	// times as output comes in, but each call should represent a line of
    40  	// output. The ProvisionOutput method cannot control whether the
    41  	// hook continues running.
    42  	PreProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error)
    43  	PostProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error)
    44  	PreProvision(*InstanceInfo, string) (HookAction, error)
    45  	PostProvision(*InstanceInfo, string) (HookAction, error)
    46  	ProvisionOutput(*InstanceInfo, string, string)
    47  
    48  	// PreRefresh and PostRefresh are called before and after a single
    49  	// resource state is refreshed, respectively.
    50  	PreRefresh(*InstanceInfo, *InstanceState) (HookAction, error)
    51  	PostRefresh(*InstanceInfo, *InstanceState) (HookAction, error)
    52  
    53  	// PostStateUpdate is called after the state is updated.
    54  	PostStateUpdate(*State) (HookAction, error)
    55  }
    56  
    57  // NilHook is a Hook implementation that does nothing. It exists only to
    58  // simplify implementing hooks. You can embed this into your Hook implementation
    59  // and only implement the functions you are interested in.
    60  type NilHook struct{}
    61  
    62  func (*NilHook) PreApply(*InstanceInfo, *InstanceState, *InstanceDiff) (HookAction, error) {
    63  	return HookActionContinue, nil
    64  }
    65  
    66  func (*NilHook) PostApply(*InstanceInfo, *InstanceState, error) (HookAction, error) {
    67  	return HookActionContinue, nil
    68  }
    69  
    70  func (*NilHook) PreDiff(*InstanceInfo, *InstanceState) (HookAction, error) {
    71  	return HookActionContinue, nil
    72  }
    73  
    74  func (*NilHook) PostDiff(*InstanceInfo, *InstanceDiff) (HookAction, error) {
    75  	return HookActionContinue, nil
    76  }
    77  
    78  func (*NilHook) PreProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
    79  	return HookActionContinue, nil
    80  }
    81  
    82  func (*NilHook) PostProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
    83  	return HookActionContinue, nil
    84  }
    85  
    86  func (*NilHook) PreProvision(*InstanceInfo, string) (HookAction, error) {
    87  	return HookActionContinue, nil
    88  }
    89  
    90  func (*NilHook) PostProvision(*InstanceInfo, string) (HookAction, error) {
    91  	return HookActionContinue, nil
    92  }
    93  
    94  func (*NilHook) ProvisionOutput(
    95  	*InstanceInfo, string, string) {
    96  }
    97  
    98  func (*NilHook) PreRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
    99  	return HookActionContinue, nil
   100  }
   101  
   102  func (*NilHook) PostRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
   103  	return HookActionContinue, nil
   104  }
   105  
   106  func (*NilHook) PostStateUpdate(*State) (HookAction, error) {
   107  	return HookActionContinue, nil
   108  }
   109  
   110  // handleHook turns hook actions into panics. This lets you use the
   111  // panic/recover mechanism in Go as a flow control mechanism for hook
   112  // actions.
   113  func handleHook(a HookAction, err error) {
   114  	if err != nil {
   115  		// TODO: handle errors
   116  	}
   117  
   118  	switch a {
   119  	case HookActionContinue:
   120  		return
   121  	case HookActionHalt:
   122  		panic(HookActionHalt)
   123  	}
   124  }