github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/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  
    54  // NilHook is a Hook implementation that does nothing. It exists only to
    55  // simplify implementing hooks. You can embed this into your Hook implementation
    56  // and only implement the functions you are interested in.
    57  type NilHook struct{}
    58  
    59  func (*NilHook) PreApply(*InstanceInfo, *InstanceState, *InstanceDiff) (HookAction, error) {
    60  	return HookActionContinue, nil
    61  }
    62  
    63  func (*NilHook) PostApply(*InstanceInfo, *InstanceState, error) (HookAction, error) {
    64  	return HookActionContinue, nil
    65  }
    66  
    67  func (*NilHook) PreDiff(*InstanceInfo, *InstanceState) (HookAction, error) {
    68  	return HookActionContinue, nil
    69  }
    70  
    71  func (*NilHook) PostDiff(*InstanceInfo, *InstanceDiff) (HookAction, error) {
    72  	return HookActionContinue, nil
    73  }
    74  
    75  func (*NilHook) PreProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
    76  	return HookActionContinue, nil
    77  }
    78  
    79  func (*NilHook) PostProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
    80  	return HookActionContinue, nil
    81  }
    82  
    83  func (*NilHook) PreProvision(*InstanceInfo, string) (HookAction, error) {
    84  	return HookActionContinue, nil
    85  }
    86  
    87  func (*NilHook) PostProvision(*InstanceInfo, string) (HookAction, error) {
    88  	return HookActionContinue, nil
    89  }
    90  
    91  func (*NilHook) ProvisionOutput(
    92  	*InstanceInfo, string, string) {
    93  }
    94  
    95  func (*NilHook) PreRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
    96  	return HookActionContinue, nil
    97  }
    98  
    99  func (*NilHook) PostRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
   100  	return HookActionContinue, nil
   101  }
   102  
   103  // handleHook turns hook actions into panics. This lets you use the
   104  // panic/recover mechanism in Go as a flow control mechanism for hook
   105  // actions.
   106  func handleHook(a HookAction, err error) {
   107  	if err != nil {
   108  		// TODO: handle errors
   109  	}
   110  
   111  	switch a {
   112  	case HookActionContinue:
   113  		return
   114  	case HookActionHalt:
   115  		panic(HookActionHalt)
   116  	}
   117  }