github.com/wikibal01/hashicorp-terraform@v0.11.12-beta1/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, error) (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  	// PreImportState and PostImportState are called before and after
    57  	// a single resource's state is being improted.
    58  	PreImportState(*InstanceInfo, string) (HookAction, error)
    59  	PostImportState(*InstanceInfo, []*InstanceState) (HookAction, error)
    60  }
    61  
    62  // NilHook is a Hook implementation that does nothing. It exists only to
    63  // simplify implementing hooks. You can embed this into your Hook implementation
    64  // and only implement the functions you are interested in.
    65  type NilHook struct{}
    66  
    67  func (*NilHook) PreApply(*InstanceInfo, *InstanceState, *InstanceDiff) (HookAction, error) {
    68  	return HookActionContinue, nil
    69  }
    70  
    71  func (*NilHook) PostApply(*InstanceInfo, *InstanceState, error) (HookAction, error) {
    72  	return HookActionContinue, nil
    73  }
    74  
    75  func (*NilHook) PreDiff(*InstanceInfo, *InstanceState) (HookAction, error) {
    76  	return HookActionContinue, nil
    77  }
    78  
    79  func (*NilHook) PostDiff(*InstanceInfo, *InstanceDiff) (HookAction, error) {
    80  	return HookActionContinue, nil
    81  }
    82  
    83  func (*NilHook) PreProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
    84  	return HookActionContinue, nil
    85  }
    86  
    87  func (*NilHook) PostProvisionResource(*InstanceInfo, *InstanceState) (HookAction, error) {
    88  	return HookActionContinue, nil
    89  }
    90  
    91  func (*NilHook) PreProvision(*InstanceInfo, string) (HookAction, error) {
    92  	return HookActionContinue, nil
    93  }
    94  
    95  func (*NilHook) PostProvision(*InstanceInfo, string, error) (HookAction, error) {
    96  	return HookActionContinue, nil
    97  }
    98  
    99  func (*NilHook) ProvisionOutput(
   100  	*InstanceInfo, string, string) {
   101  }
   102  
   103  func (*NilHook) PreRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
   104  	return HookActionContinue, nil
   105  }
   106  
   107  func (*NilHook) PostRefresh(*InstanceInfo, *InstanceState) (HookAction, error) {
   108  	return HookActionContinue, nil
   109  }
   110  
   111  func (*NilHook) PreImportState(*InstanceInfo, string) (HookAction, error) {
   112  	return HookActionContinue, nil
   113  }
   114  
   115  func (*NilHook) PostImportState(*InstanceInfo, []*InstanceState) (HookAction, error) {
   116  	return HookActionContinue, nil
   117  }
   118  
   119  func (*NilHook) PostStateUpdate(*State) (HookAction, error) {
   120  	return HookActionContinue, nil
   121  }
   122  
   123  // handleHook turns hook actions into panics. This lets you use the
   124  // panic/recover mechanism in Go as a flow control mechanism for hook
   125  // actions.
   126  func handleHook(a HookAction, err error) {
   127  	if err != nil {
   128  		// TODO: handle errors
   129  	}
   130  
   131  	switch a {
   132  	case HookActionContinue:
   133  		return
   134  	case HookActionHalt:
   135  		panic(HookActionHalt)
   136  	}
   137  }