github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/hook_mock.go (about) 1 package terraform 2 3 import ( 4 "sync" 5 6 "github.com/zclconf/go-cty/cty" 7 8 "github.com/hashicorp/terraform-plugin-sdk/internal/addrs" 9 "github.com/hashicorp/terraform-plugin-sdk/internal/plans" 10 "github.com/hashicorp/terraform-plugin-sdk/internal/providers" 11 "github.com/hashicorp/terraform-plugin-sdk/internal/states" 12 ) 13 14 // MockHook is an implementation of Hook that can be used for tests. 15 // It records all of its function calls. 16 type MockHook struct { 17 sync.Mutex 18 19 PreApplyCalled bool 20 PreApplyAddr addrs.AbsResourceInstance 21 PreApplyGen states.Generation 22 PreApplyAction plans.Action 23 PreApplyPriorState cty.Value 24 PreApplyPlannedState cty.Value 25 PreApplyReturn HookAction 26 PreApplyError error 27 28 PostApplyCalled bool 29 PostApplyAddr addrs.AbsResourceInstance 30 PostApplyGen states.Generation 31 PostApplyNewState cty.Value 32 PostApplyError error 33 PostApplyReturn HookAction 34 PostApplyReturnError error 35 PostApplyFn func(addrs.AbsResourceInstance, states.Generation, cty.Value, error) (HookAction, error) 36 37 PreDiffCalled bool 38 PreDiffAddr addrs.AbsResourceInstance 39 PreDiffGen states.Generation 40 PreDiffPriorState cty.Value 41 PreDiffProposedState cty.Value 42 PreDiffReturn HookAction 43 PreDiffError error 44 45 PostDiffCalled bool 46 PostDiffAddr addrs.AbsResourceInstance 47 PostDiffGen states.Generation 48 PostDiffAction plans.Action 49 PostDiffPriorState cty.Value 50 PostDiffPlannedState cty.Value 51 PostDiffReturn HookAction 52 PostDiffError error 53 54 PreProvisionInstanceCalled bool 55 PreProvisionInstanceAddr addrs.AbsResourceInstance 56 PreProvisionInstanceState cty.Value 57 PreProvisionInstanceReturn HookAction 58 PreProvisionInstanceError error 59 60 PostProvisionInstanceCalled bool 61 PostProvisionInstanceAddr addrs.AbsResourceInstance 62 PostProvisionInstanceState cty.Value 63 PostProvisionInstanceReturn HookAction 64 PostProvisionInstanceError error 65 66 PreProvisionInstanceStepCalled bool 67 PreProvisionInstanceStepAddr addrs.AbsResourceInstance 68 PreProvisionInstanceStepProvisionerType string 69 PreProvisionInstanceStepReturn HookAction 70 PreProvisionInstanceStepError error 71 72 PostProvisionInstanceStepCalled bool 73 PostProvisionInstanceStepAddr addrs.AbsResourceInstance 74 PostProvisionInstanceStepProvisionerType string 75 PostProvisionInstanceStepErrorArg error 76 PostProvisionInstanceStepReturn HookAction 77 PostProvisionInstanceStepError error 78 79 ProvisionOutputCalled bool 80 ProvisionOutputAddr addrs.AbsResourceInstance 81 ProvisionOutputProvisionerType string 82 ProvisionOutputMessage string 83 84 PreRefreshCalled bool 85 PreRefreshAddr addrs.AbsResourceInstance 86 PreRefreshGen states.Generation 87 PreRefreshPriorState cty.Value 88 PreRefreshReturn HookAction 89 PreRefreshError error 90 91 PostRefreshCalled bool 92 PostRefreshAddr addrs.AbsResourceInstance 93 PostRefreshGen states.Generation 94 PostRefreshPriorState cty.Value 95 PostRefreshNewState cty.Value 96 PostRefreshReturn HookAction 97 PostRefreshError error 98 99 PreImportStateCalled bool 100 PreImportStateAddr addrs.AbsResourceInstance 101 PreImportStateID string 102 PreImportStateReturn HookAction 103 PreImportStateError error 104 105 PostImportStateCalled bool 106 PostImportStateAddr addrs.AbsResourceInstance 107 PostImportStateNewStates []providers.ImportedResource 108 PostImportStateReturn HookAction 109 PostImportStateError error 110 111 PostStateUpdateCalled bool 112 PostStateUpdateState *states.State 113 PostStateUpdateReturn HookAction 114 PostStateUpdateError error 115 } 116 117 var _ Hook = (*MockHook)(nil) 118 119 func (h *MockHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { 120 h.Lock() 121 defer h.Unlock() 122 123 h.PreApplyCalled = true 124 h.PreApplyAddr = addr 125 h.PreApplyGen = gen 126 h.PreApplyAction = action 127 h.PreApplyPriorState = priorState 128 h.PreApplyPlannedState = plannedNewState 129 return h.PreApplyReturn, h.PreApplyError 130 } 131 132 func (h *MockHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) { 133 h.Lock() 134 defer h.Unlock() 135 136 h.PostApplyCalled = true 137 h.PostApplyAddr = addr 138 h.PostApplyGen = gen 139 h.PostApplyNewState = newState 140 h.PostApplyError = err 141 142 if h.PostApplyFn != nil { 143 return h.PostApplyFn(addr, gen, newState, err) 144 } 145 146 return h.PostApplyReturn, h.PostApplyReturnError 147 } 148 149 func (h *MockHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) { 150 h.Lock() 151 defer h.Unlock() 152 153 h.PreDiffCalled = true 154 h.PreDiffAddr = addr 155 h.PreDiffGen = gen 156 h.PreDiffPriorState = priorState 157 h.PreDiffProposedState = proposedNewState 158 return h.PreDiffReturn, h.PreDiffError 159 } 160 161 func (h *MockHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { 162 h.Lock() 163 defer h.Unlock() 164 165 h.PostDiffCalled = true 166 h.PostDiffAddr = addr 167 h.PostDiffGen = gen 168 h.PostDiffAction = action 169 h.PostDiffPriorState = priorState 170 h.PostDiffPlannedState = plannedNewState 171 return h.PostDiffReturn, h.PostDiffError 172 } 173 174 func (h *MockHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { 175 h.Lock() 176 defer h.Unlock() 177 178 h.PreProvisionInstanceCalled = true 179 h.PreProvisionInstanceAddr = addr 180 h.PreProvisionInstanceState = state 181 return h.PreProvisionInstanceReturn, h.PreProvisionInstanceError 182 } 183 184 func (h *MockHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { 185 h.Lock() 186 defer h.Unlock() 187 188 h.PostProvisionInstanceCalled = true 189 h.PostProvisionInstanceAddr = addr 190 h.PostProvisionInstanceState = state 191 return h.PostProvisionInstanceReturn, h.PostProvisionInstanceError 192 } 193 194 func (h *MockHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) { 195 h.Lock() 196 defer h.Unlock() 197 198 h.PreProvisionInstanceStepCalled = true 199 h.PreProvisionInstanceStepAddr = addr 200 h.PreProvisionInstanceStepProvisionerType = typeName 201 return h.PreProvisionInstanceStepReturn, h.PreProvisionInstanceStepError 202 } 203 204 func (h *MockHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) { 205 h.Lock() 206 defer h.Unlock() 207 208 h.PostProvisionInstanceStepCalled = true 209 h.PostProvisionInstanceStepAddr = addr 210 h.PostProvisionInstanceStepProvisionerType = typeName 211 h.PostProvisionInstanceStepErrorArg = err 212 return h.PostProvisionInstanceStepReturn, h.PostProvisionInstanceStepError 213 } 214 215 func (h *MockHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) { 216 h.Lock() 217 defer h.Unlock() 218 219 h.ProvisionOutputCalled = true 220 h.ProvisionOutputAddr = addr 221 h.ProvisionOutputProvisionerType = typeName 222 h.ProvisionOutputMessage = line 223 } 224 225 func (h *MockHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) { 226 h.Lock() 227 defer h.Unlock() 228 229 h.PreRefreshCalled = true 230 h.PreRefreshAddr = addr 231 h.PreRefreshGen = gen 232 h.PreRefreshPriorState = priorState 233 return h.PreRefreshReturn, h.PreRefreshError 234 } 235 236 func (h *MockHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) { 237 h.Lock() 238 defer h.Unlock() 239 240 h.PostRefreshCalled = true 241 h.PostRefreshAddr = addr 242 h.PostRefreshPriorState = priorState 243 h.PostRefreshNewState = newState 244 return h.PostRefreshReturn, h.PostRefreshError 245 } 246 247 func (h *MockHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) { 248 h.Lock() 249 defer h.Unlock() 250 251 h.PreImportStateCalled = true 252 h.PreImportStateAddr = addr 253 h.PreImportStateID = importID 254 return h.PreImportStateReturn, h.PreImportStateError 255 } 256 257 func (h *MockHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) { 258 h.Lock() 259 defer h.Unlock() 260 261 h.PostImportStateCalled = true 262 h.PostImportStateAddr = addr 263 h.PostImportStateNewStates = imported 264 return h.PostImportStateReturn, h.PostImportStateError 265 } 266 267 func (h *MockHook) PostStateUpdate(new *states.State) (HookAction, error) { 268 h.Lock() 269 defer h.Unlock() 270 271 h.PostStateUpdateCalled = true 272 h.PostStateUpdateState = new 273 return h.PostStateUpdateReturn, h.PostStateUpdateError 274 }