github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/resource_provider_mock.go (about) 1 package terraform 2 3 import ( 4 "sync" 5 ) 6 7 // MockResourceProvider implements ResourceProvider but mocks out all the 8 // calls for testing purposes. 9 type MockResourceProvider struct { 10 sync.Mutex 11 12 // Anything you want, in case you need to store extra data with the mock. 13 Meta interface{} 14 15 CloseCalled bool 16 CloseError error 17 GetSchemaCalled bool 18 GetSchemaRequest *ProviderSchemaRequest 19 GetSchemaReturn *ProviderSchema 20 GetSchemaReturnError error 21 InputCalled bool 22 InputInput UIInput 23 InputConfig *ResourceConfig 24 InputReturnConfig *ResourceConfig 25 InputReturnError error 26 InputFn func(UIInput, *ResourceConfig) (*ResourceConfig, error) 27 ApplyCalled bool 28 ApplyInfo *InstanceInfo 29 ApplyState *InstanceState 30 ApplyDiff *InstanceDiff 31 ApplyFn func(*InstanceInfo, *InstanceState, *InstanceDiff) (*InstanceState, error) 32 ApplyReturn *InstanceState 33 ApplyReturnError error 34 ConfigureCalled bool 35 ConfigureConfig *ResourceConfig 36 ConfigureFn func(*ResourceConfig) error 37 ConfigureReturnError error 38 DiffCalled bool 39 DiffInfo *InstanceInfo 40 DiffState *InstanceState 41 DiffDesired *ResourceConfig 42 DiffFn func(*InstanceInfo, *InstanceState, *ResourceConfig) (*InstanceDiff, error) 43 DiffReturn *InstanceDiff 44 DiffReturnError error 45 RefreshCalled bool 46 RefreshInfo *InstanceInfo 47 RefreshState *InstanceState 48 RefreshFn func(*InstanceInfo, *InstanceState) (*InstanceState, error) 49 RefreshReturn *InstanceState 50 RefreshReturnError error 51 ResourcesCalled bool 52 ResourcesReturn []ResourceType 53 ReadDataApplyCalled bool 54 ReadDataApplyInfo *InstanceInfo 55 ReadDataApplyDiff *InstanceDiff 56 ReadDataApplyFn func(*InstanceInfo, *InstanceDiff) (*InstanceState, error) 57 ReadDataApplyReturn *InstanceState 58 ReadDataApplyReturnError error 59 ReadDataDiffCalled bool 60 ReadDataDiffInfo *InstanceInfo 61 ReadDataDiffDesired *ResourceConfig 62 ReadDataDiffFn func(*InstanceInfo, *ResourceConfig) (*InstanceDiff, error) 63 ReadDataDiffReturn *InstanceDiff 64 ReadDataDiffReturnError error 65 StopCalled bool 66 StopFn func() error 67 StopReturnError error 68 DataSourcesCalled bool 69 DataSourcesReturn []DataSource 70 ValidateCalled bool 71 ValidateConfig *ResourceConfig 72 ValidateFn func(*ResourceConfig) ([]string, []error) 73 ValidateReturnWarns []string 74 ValidateReturnErrors []error 75 ValidateResourceFn func(string, *ResourceConfig) ([]string, []error) 76 ValidateResourceCalled bool 77 ValidateResourceType string 78 ValidateResourceConfig *ResourceConfig 79 ValidateResourceReturnWarns []string 80 ValidateResourceReturnErrors []error 81 ValidateDataSourceFn func(string, *ResourceConfig) ([]string, []error) 82 ValidateDataSourceCalled bool 83 ValidateDataSourceType string 84 ValidateDataSourceConfig *ResourceConfig 85 ValidateDataSourceReturnWarns []string 86 ValidateDataSourceReturnErrors []error 87 88 ImportStateCalled bool 89 ImportStateInfo *InstanceInfo 90 ImportStateID string 91 ImportStateReturn []*InstanceState 92 ImportStateReturnError error 93 ImportStateFn func(*InstanceInfo, string) ([]*InstanceState, error) 94 } 95 96 func (p *MockResourceProvider) Close() error { 97 p.CloseCalled = true 98 return p.CloseError 99 } 100 101 func (p *MockResourceProvider) GetSchema(req *ProviderSchemaRequest) (*ProviderSchema, error) { 102 p.Lock() 103 defer p.Unlock() 104 105 p.GetSchemaCalled = true 106 p.GetSchemaRequest = req 107 return p.GetSchemaReturn, p.GetSchemaReturnError 108 } 109 110 func (p *MockResourceProvider) Input( 111 input UIInput, c *ResourceConfig) (*ResourceConfig, error) { 112 p.Lock() 113 defer p.Unlock() 114 p.InputCalled = true 115 p.InputInput = input 116 p.InputConfig = c 117 if p.InputFn != nil { 118 return p.InputFn(input, c) 119 } 120 return p.InputReturnConfig, p.InputReturnError 121 } 122 123 func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) { 124 p.Lock() 125 defer p.Unlock() 126 127 p.ValidateCalled = true 128 p.ValidateConfig = c 129 if p.ValidateFn != nil { 130 return p.ValidateFn(c) 131 } 132 return p.ValidateReturnWarns, p.ValidateReturnErrors 133 } 134 135 func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) { 136 p.Lock() 137 defer p.Unlock() 138 139 p.ValidateResourceCalled = true 140 p.ValidateResourceType = t 141 p.ValidateResourceConfig = c 142 143 if p.ValidateResourceFn != nil { 144 return p.ValidateResourceFn(t, c) 145 } 146 147 return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors 148 } 149 150 func (p *MockResourceProvider) Configure(c *ResourceConfig) error { 151 p.Lock() 152 defer p.Unlock() 153 154 p.ConfigureCalled = true 155 p.ConfigureConfig = c 156 157 if p.ConfigureFn != nil { 158 return p.ConfigureFn(c) 159 } 160 161 return p.ConfigureReturnError 162 } 163 164 func (p *MockResourceProvider) Stop() error { 165 p.Lock() 166 defer p.Unlock() 167 168 p.StopCalled = true 169 if p.StopFn != nil { 170 return p.StopFn() 171 } 172 173 return p.StopReturnError 174 } 175 176 func (p *MockResourceProvider) Apply( 177 info *InstanceInfo, 178 state *InstanceState, 179 diff *InstanceDiff) (*InstanceState, error) { 180 // We only lock while writing data. Reading is fine 181 p.Lock() 182 p.ApplyCalled = true 183 p.ApplyInfo = info 184 p.ApplyState = state 185 p.ApplyDiff = diff 186 p.Unlock() 187 188 if p.ApplyFn != nil { 189 return p.ApplyFn(info, state, diff) 190 } 191 192 return p.ApplyReturn.DeepCopy(), p.ApplyReturnError 193 } 194 195 func (p *MockResourceProvider) Diff( 196 info *InstanceInfo, 197 state *InstanceState, 198 desired *ResourceConfig) (*InstanceDiff, error) { 199 p.Lock() 200 defer p.Unlock() 201 202 p.DiffCalled = true 203 p.DiffInfo = info 204 p.DiffState = state 205 p.DiffDesired = desired 206 207 if p.DiffFn != nil { 208 return p.DiffFn(info, state, desired) 209 } 210 211 return p.DiffReturn.DeepCopy(), p.DiffReturnError 212 } 213 214 func (p *MockResourceProvider) Refresh( 215 info *InstanceInfo, 216 s *InstanceState) (*InstanceState, error) { 217 p.Lock() 218 defer p.Unlock() 219 220 p.RefreshCalled = true 221 p.RefreshInfo = info 222 p.RefreshState = s 223 224 if p.RefreshFn != nil { 225 return p.RefreshFn(info, s) 226 } 227 228 return p.RefreshReturn.DeepCopy(), p.RefreshReturnError 229 } 230 231 func (p *MockResourceProvider) Resources() []ResourceType { 232 p.Lock() 233 defer p.Unlock() 234 235 p.ResourcesCalled = true 236 return p.ResourcesReturn 237 } 238 239 func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) { 240 p.Lock() 241 defer p.Unlock() 242 243 p.ImportStateCalled = true 244 p.ImportStateInfo = info 245 p.ImportStateID = id 246 if p.ImportStateFn != nil { 247 return p.ImportStateFn(info, id) 248 } 249 250 var result []*InstanceState 251 if p.ImportStateReturn != nil { 252 result = make([]*InstanceState, len(p.ImportStateReturn)) 253 for i, v := range p.ImportStateReturn { 254 result[i] = v.DeepCopy() 255 } 256 } 257 258 return result, p.ImportStateReturnError 259 } 260 261 func (p *MockResourceProvider) ValidateDataSource(t string, c *ResourceConfig) ([]string, []error) { 262 p.Lock() 263 defer p.Unlock() 264 265 p.ValidateDataSourceCalled = true 266 p.ValidateDataSourceType = t 267 p.ValidateDataSourceConfig = c 268 269 if p.ValidateDataSourceFn != nil { 270 return p.ValidateDataSourceFn(t, c) 271 } 272 273 return p.ValidateDataSourceReturnWarns, p.ValidateDataSourceReturnErrors 274 } 275 276 func (p *MockResourceProvider) ReadDataDiff( 277 info *InstanceInfo, 278 desired *ResourceConfig) (*InstanceDiff, error) { 279 p.Lock() 280 defer p.Unlock() 281 282 p.ReadDataDiffCalled = true 283 p.ReadDataDiffInfo = info 284 p.ReadDataDiffDesired = desired 285 if p.ReadDataDiffFn != nil { 286 return p.ReadDataDiffFn(info, desired) 287 } 288 289 return p.ReadDataDiffReturn.DeepCopy(), p.ReadDataDiffReturnError 290 } 291 292 func (p *MockResourceProvider) ReadDataApply( 293 info *InstanceInfo, 294 d *InstanceDiff) (*InstanceState, error) { 295 p.Lock() 296 defer p.Unlock() 297 298 p.ReadDataApplyCalled = true 299 p.ReadDataApplyInfo = info 300 p.ReadDataApplyDiff = d 301 302 if p.ReadDataApplyFn != nil { 303 return p.ReadDataApplyFn(info, d) 304 } 305 306 return p.ReadDataApplyReturn.DeepCopy(), p.ReadDataApplyReturnError 307 } 308 309 func (p *MockResourceProvider) DataSources() []DataSource { 310 p.Lock() 311 defer p.Unlock() 312 313 p.DataSourcesCalled = true 314 return p.DataSourcesReturn 315 }