summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/terraform/shadow_resource_provider.go
blob: 9741d7e7964f210bd1aefce488b0f9984a538323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
package terraform

import (
	"fmt"
	"log"
	"sync"

	"github.com/hashicorp/go-multierror"
	"github.com/hashicorp/terraform/helper/shadow"
)

// shadowResourceProvider implements ResourceProvider for the shadow
// eval context defined in eval_context_shadow.go.
//
// This is used to verify behavior with a real provider. This shouldn't
// be used directly.
type shadowResourceProvider interface {
	ResourceProvider
	Shadow
}

// newShadowResourceProvider creates a new shadowed ResourceProvider.
//
// This will assume a well behaved real ResourceProvider. For example,
// it assumes that the `Resources` call underneath doesn't change values
// since once it is called on the real provider, it will be cached and
// returned in the shadow since number of calls to that shouldn't affect
// actual behavior.
//
// However, with calls like Apply, call order is taken into account,
// parameters are checked for equality, etc.
func newShadowResourceProvider(p ResourceProvider) (ResourceProvider, shadowResourceProvider) {
	// Create the shared data
	shared := shadowResourceProviderShared{}

	// Create the real provider that does actual work
	real := &shadowResourceProviderReal{
		ResourceProvider: p,
		Shared:           &shared,
	}

	// Create the shadow that watches the real value
	shadow := &shadowResourceProviderShadow{
		Shared: &shared,

		resources:   p.Resources(),
		dataSources: p.DataSources(),
	}

	return real, shadow
}

// shadowResourceProviderReal is the real resource provider. Function calls
// to this will perform real work. This records the parameters and return
// values and call order for the shadow to reproduce.
type shadowResourceProviderReal struct {
	ResourceProvider

	Shared *shadowResourceProviderShared
}

func (p *shadowResourceProviderReal) Close() error {
	var result error
	if c, ok := p.ResourceProvider.(ResourceProviderCloser); ok {
		result = c.Close()
	}

	p.Shared.CloseErr.SetValue(result)
	return result
}

func (p *shadowResourceProviderReal) Input(
	input UIInput, c *ResourceConfig) (*ResourceConfig, error) {
	cCopy := c.DeepCopy()

	result, err := p.ResourceProvider.Input(input, c)
	p.Shared.Input.SetValue(&shadowResourceProviderInput{
		Config:    cCopy,
		Result:    result.DeepCopy(),
		ResultErr: err,
	})

	return result, err
}

func (p *shadowResourceProviderReal) Validate(c *ResourceConfig) ([]string, []error) {
	warns, errs := p.ResourceProvider.Validate(c)
	p.Shared.Validate.SetValue(&shadowResourceProviderValidate{
		Config:     c.DeepCopy(),
		ResultWarn: warns,
		ResultErr:  errs,
	})

	return warns, errs
}

func (p *shadowResourceProviderReal) Configure(c *ResourceConfig) error {
	cCopy := c.DeepCopy()

	err := p.ResourceProvider.Configure(c)
	p.Shared.Configure.SetValue(&shadowResourceProviderConfigure{
		Config: cCopy,
		Result: err,
	})

	return err
}

func (p *shadowResourceProviderReal) Stop() error {
	return p.ResourceProvider.Stop()
}

func (p *shadowResourceProviderReal) ValidateResource(
	t string, c *ResourceConfig) ([]string, []error) {
	key := t
	configCopy := c.DeepCopy()

	// Real operation
	warns, errs := p.ResourceProvider.ValidateResource(t, c)

	// Initialize to ensure we always have a wrapper with a lock
	p.Shared.ValidateResource.Init(
		key, &shadowResourceProviderValidateResourceWrapper{})

	// Get the result
	raw := p.Shared.ValidateResource.Value(key)
	wrapper, ok := raw.(*shadowResourceProviderValidateResourceWrapper)
	if !ok {
		// If this fails then we just continue with our day... the shadow
		// will fail to but there isn't much we can do.
		log.Printf(
			"[ERROR] unknown value in ValidateResource shadow value: %#v", raw)
		return warns, errs
	}

	// Lock the wrapper for writing and record our call
	wrapper.Lock()
	defer wrapper.Unlock()

	wrapper.Calls = append(wrapper.Calls, &shadowResourceProviderValidateResource{
		Config: configCopy,
		Warns:  warns,
		Errors: errs,
	})

	// With it locked, call SetValue again so that it triggers WaitForChange
	p.Shared.ValidateResource.SetValue(key, wrapper)

	// Return the result
	return warns, errs
}

func (p *shadowResourceProviderReal) Apply(
	info *InstanceInfo,
	state *InstanceState,
	diff *InstanceDiff) (*InstanceState, error) {
	// Thse have to be copied before the call since call can modify
	stateCopy := state.DeepCopy()
	diffCopy := diff.DeepCopy()

	result, err := p.ResourceProvider.Apply(info, state, diff)
	p.Shared.Apply.SetValue(info.uniqueId(), &shadowResourceProviderApply{
		State:     stateCopy,
		Diff:      diffCopy,
		Result:    result.DeepCopy(),
		ResultErr: err,
	})

	return result, err
}

func (p *shadowResourceProviderReal) Diff(
	info *InstanceInfo,
	state *InstanceState,
	desired *ResourceConfig) (*InstanceDiff, error) {
	// Thse have to be copied before the call since call can modify
	stateCopy := state.DeepCopy()
	desiredCopy := desired.DeepCopy()

	result, err := p.ResourceProvider.Diff(info, state, desired)
	p.Shared.Diff.SetValue(info.uniqueId(), &shadowResourceProviderDiff{
		State:     stateCopy,
		Desired:   desiredCopy,
		Result:    result.DeepCopy(),
		ResultErr: err,
	})

	return result, err
}

func (p *shadowResourceProviderReal) Refresh(
	info *InstanceInfo,
	state *InstanceState) (*InstanceState, error) {
	// Thse have to be copied before the call since call can modify
	stateCopy := state.DeepCopy()

	result, err := p.ResourceProvider.Refresh(info, state)
	p.Shared.Refresh.SetValue(info.uniqueId(), &shadowResourceProviderRefresh{
		State:     stateCopy,
		Result:    result.DeepCopy(),
		ResultErr: err,
	})

	return result, err
}

func (p *shadowResourceProviderReal) ValidateDataSource(
	t string, c *ResourceConfig) ([]string, []error) {
	key := t
	configCopy := c.DeepCopy()

	// Real operation
	warns, errs := p.ResourceProvider.ValidateDataSource(t, c)

	// Initialize
	p.Shared.ValidateDataSource.Init(
		key, &shadowResourceProviderValidateDataSourceWrapper{})

	// Get the result
	raw := p.Shared.ValidateDataSource.Value(key)
	wrapper, ok := raw.(*shadowResourceProviderValidateDataSourceWrapper)
	if !ok {
		// If this fails then we just continue with our day... the shadow
		// will fail to but there isn't much we can do.
		log.Printf(
			"[ERROR] unknown value in ValidateDataSource shadow value: %#v", raw)
		return warns, errs
	}

	// Lock the wrapper for writing and record our call
	wrapper.Lock()
	defer wrapper.Unlock()

	wrapper.Calls = append(wrapper.Calls, &shadowResourceProviderValidateDataSource{
		Config: configCopy,
		Warns:  warns,
		Errors: errs,
	})

	// Set it
	p.Shared.ValidateDataSource.SetValue(key, wrapper)

	// Return the result
	return warns, errs
}

func (p *shadowResourceProviderReal) ReadDataDiff(
	info *InstanceInfo,
	desired *ResourceConfig) (*InstanceDiff, error) {
	// These have to be copied before the call since call can modify
	desiredCopy := desired.DeepCopy()

	result, err := p.ResourceProvider.ReadDataDiff(info, desired)
	p.Shared.ReadDataDiff.SetValue(info.uniqueId(), &shadowResourceProviderReadDataDiff{
		Desired:   desiredCopy,
		Result:    result.DeepCopy(),
		ResultErr: err,
	})

	return result, err
}

func (p *shadowResourceProviderReal) ReadDataApply(
	info *InstanceInfo,
	diff *InstanceDiff) (*InstanceState, error) {
	// Thse have to be copied before the call since call can modify
	diffCopy := diff.DeepCopy()

	result, err := p.ResourceProvider.ReadDataApply(info, diff)
	p.Shared.ReadDataApply.SetValue(info.uniqueId(), &shadowResourceProviderReadDataApply{
		Diff:      diffCopy,
		Result:    result.DeepCopy(),
		ResultErr: err,
	})

	return result, err
}

// shadowResourceProviderShadow is the shadow resource provider. Function
// calls never affect real resources. This is paired with the "real" side
// which must be called properly to enable recording.
type shadowResourceProviderShadow struct {
	Shared *shadowResourceProviderShared

	// Cached values that are expected to not change
	resources   []ResourceType
	dataSources []DataSource

	Error     error // Error is the list of errors from the shadow
	ErrorLock sync.Mutex
}

type shadowResourceProviderShared struct {
	// NOTE: Anytime a value is added here, be sure to add it to
	// the Close() method so that it is closed.

	CloseErr           shadow.Value
	Input              shadow.Value
	Validate           shadow.Value
	Configure          shadow.Value
	ValidateResource   shadow.KeyedValue
	Apply              shadow.KeyedValue
	Diff               shadow.KeyedValue
	Refresh            shadow.KeyedValue
	ValidateDataSource shadow.KeyedValue
	ReadDataDiff       shadow.KeyedValue
	ReadDataApply      shadow.KeyedValue
}

func (p *shadowResourceProviderShared) Close() error {
	return shadow.Close(p)
}

func (p *shadowResourceProviderShadow) CloseShadow() error {
	err := p.Shared.Close()
	if err != nil {
		err = fmt.Errorf("close error: %s", err)
	}

	return err
}

func (p *shadowResourceProviderShadow) ShadowError() error {
	return p.Error
}

func (p *shadowResourceProviderShadow) Resources() []ResourceType {
	return p.resources
}

func (p *shadowResourceProviderShadow) DataSources() []DataSource {
	return p.dataSources
}

func (p *shadowResourceProviderShadow) Close() error {
	v := p.Shared.CloseErr.Value()
	if v == nil {
		return nil
	}

	return v.(error)
}

func (p *shadowResourceProviderShadow) Input(
	input UIInput, c *ResourceConfig) (*ResourceConfig, error) {
	// Get the result of the input call
	raw := p.Shared.Input.Value()
	if raw == nil {
		return nil, nil
	}

	result, ok := raw.(*shadowResourceProviderInput)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'input' shadow value: %#v", raw))
		return nil, nil
	}

	// Compare the parameters, which should be identical
	if !c.Equal(result.Config) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Input had unequal configurations (real, then shadow):\n\n%#v\n\n%#v",
			result.Config, c))
		p.ErrorLock.Unlock()
	}

	// Return the results
	return result.Result, result.ResultErr
}

func (p *shadowResourceProviderShadow) Validate(c *ResourceConfig) ([]string, []error) {
	// Get the result of the validate call
	raw := p.Shared.Validate.Value()
	if raw == nil {
		return nil, nil
	}

	result, ok := raw.(*shadowResourceProviderValidate)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'validate' shadow value: %#v", raw))
		return nil, nil
	}

	// Compare the parameters, which should be identical
	if !c.Equal(result.Config) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Validate had unequal configurations (real, then shadow):\n\n%#v\n\n%#v",
			result.Config, c))
		p.ErrorLock.Unlock()
	}

	// Return the results
	return result.ResultWarn, result.ResultErr
}

func (p *shadowResourceProviderShadow) Configure(c *ResourceConfig) error {
	// Get the result of the call
	raw := p.Shared.Configure.Value()
	if raw == nil {
		return nil
	}

	result, ok := raw.(*shadowResourceProviderConfigure)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'configure' shadow value: %#v", raw))
		return nil
	}

	// Compare the parameters, which should be identical
	if !c.Equal(result.Config) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Configure had unequal configurations (real, then shadow):\n\n%#v\n\n%#v",
			result.Config, c))
		p.ErrorLock.Unlock()
	}

	// Return the results
	return result.Result
}

// Stop returns immediately.
func (p *shadowResourceProviderShadow) Stop() error {
	return nil
}

func (p *shadowResourceProviderShadow) ValidateResource(t string, c *ResourceConfig) ([]string, []error) {
	// Unique key
	key := t

	// Get the initial value
	raw := p.Shared.ValidateResource.Value(key)

	// Find a validation with our configuration
	var result *shadowResourceProviderValidateResource
	for {
		// Get the value
		if raw == nil {
			p.ErrorLock.Lock()
			defer p.ErrorLock.Unlock()
			p.Error = multierror.Append(p.Error, fmt.Errorf(
				"Unknown 'ValidateResource' call for %q:\n\n%#v",
				key, c))
			return nil, nil
		}

		wrapper, ok := raw.(*shadowResourceProviderValidateResourceWrapper)
		if !ok {
			p.ErrorLock.Lock()
			defer p.ErrorLock.Unlock()
			p.Error = multierror.Append(p.Error, fmt.Errorf(
				"Unknown 'ValidateResource' shadow value for %q: %#v", key, raw))
			return nil, nil
		}

		// Look for the matching call with our configuration
		wrapper.RLock()
		for _, call := range wrapper.Calls {
			if call.Config.Equal(c) {
				result = call
				break
			}
		}
		wrapper.RUnlock()

		// If we found a result, exit
		if result != nil {
			break
		}

		// Wait for a change so we can get the wrapper again
		raw = p.Shared.ValidateResource.WaitForChange(key)
	}

	return result.Warns, result.Errors
}

func (p *shadowResourceProviderShadow) Apply(
	info *InstanceInfo,
	state *InstanceState,
	diff *InstanceDiff) (*InstanceState, error) {
	// Unique key
	key := info.uniqueId()
	raw := p.Shared.Apply.Value(key)
	if raw == nil {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'apply' call for %q:\n\n%#v\n\n%#v",
			key, state, diff))
		return nil, nil
	}

	result, ok := raw.(*shadowResourceProviderApply)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'apply' shadow value for %q: %#v", key, raw))
		return nil, nil
	}

	// Compare the parameters, which should be identical
	if !state.Equal(result.State) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Apply %q: state had unequal states (real, then shadow):\n\n%#v\n\n%#v",
			key, result.State, state))
		p.ErrorLock.Unlock()
	}

	if !diff.Equal(result.Diff) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Apply %q: unequal diffs (real, then shadow):\n\n%#v\n\n%#v",
			key, result.Diff, diff))
		p.ErrorLock.Unlock()
	}

	return result.Result, result.ResultErr
}

func (p *shadowResourceProviderShadow) Diff(
	info *InstanceInfo,
	state *InstanceState,
	desired *ResourceConfig) (*InstanceDiff, error) {
	// Unique key
	key := info.uniqueId()
	raw := p.Shared.Diff.Value(key)
	if raw == nil {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'diff' call for %q:\n\n%#v\n\n%#v",
			key, state, desired))
		return nil, nil
	}

	result, ok := raw.(*shadowResourceProviderDiff)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'diff' shadow value for %q: %#v", key, raw))
		return nil, nil
	}

	// Compare the parameters, which should be identical
	if !state.Equal(result.State) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Diff %q had unequal states (real, then shadow):\n\n%#v\n\n%#v",
			key, result.State, state))
		p.ErrorLock.Unlock()
	}
	if !desired.Equal(result.Desired) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Diff %q had unequal states (real, then shadow):\n\n%#v\n\n%#v",
			key, result.Desired, desired))
		p.ErrorLock.Unlock()
	}

	return result.Result, result.ResultErr
}

func (p *shadowResourceProviderShadow) Refresh(
	info *InstanceInfo,
	state *InstanceState) (*InstanceState, error) {
	// Unique key
	key := info.uniqueId()
	raw := p.Shared.Refresh.Value(key)
	if raw == nil {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'refresh' call for %q:\n\n%#v",
			key, state))
		return nil, nil
	}

	result, ok := raw.(*shadowResourceProviderRefresh)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'refresh' shadow value: %#v", raw))
		return nil, nil
	}

	// Compare the parameters, which should be identical
	if !state.Equal(result.State) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Refresh %q had unequal states (real, then shadow):\n\n%#v\n\n%#v",
			key, result.State, state))
		p.ErrorLock.Unlock()
	}

	return result.Result, result.ResultErr
}

func (p *shadowResourceProviderShadow) ValidateDataSource(
	t string, c *ResourceConfig) ([]string, []error) {
	// Unique key
	key := t

	// Get the initial value
	raw := p.Shared.ValidateDataSource.Value(key)

	// Find a validation with our configuration
	var result *shadowResourceProviderValidateDataSource
	for {
		// Get the value
		if raw == nil {
			p.ErrorLock.Lock()
			defer p.ErrorLock.Unlock()
			p.Error = multierror.Append(p.Error, fmt.Errorf(
				"Unknown 'ValidateDataSource' call for %q:\n\n%#v",
				key, c))
			return nil, nil
		}

		wrapper, ok := raw.(*shadowResourceProviderValidateDataSourceWrapper)
		if !ok {
			p.ErrorLock.Lock()
			defer p.ErrorLock.Unlock()
			p.Error = multierror.Append(p.Error, fmt.Errorf(
				"Unknown 'ValidateDataSource' shadow value: %#v", raw))
			return nil, nil
		}

		// Look for the matching call with our configuration
		wrapper.RLock()
		for _, call := range wrapper.Calls {
			if call.Config.Equal(c) {
				result = call
				break
			}
		}
		wrapper.RUnlock()

		// If we found a result, exit
		if result != nil {
			break
		}

		// Wait for a change so we can get the wrapper again
		raw = p.Shared.ValidateDataSource.WaitForChange(key)
	}

	return result.Warns, result.Errors
}

func (p *shadowResourceProviderShadow) ReadDataDiff(
	info *InstanceInfo,
	desired *ResourceConfig) (*InstanceDiff, error) {
	// Unique key
	key := info.uniqueId()
	raw := p.Shared.ReadDataDiff.Value(key)
	if raw == nil {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'ReadDataDiff' call for %q:\n\n%#v",
			key, desired))
		return nil, nil
	}

	result, ok := raw.(*shadowResourceProviderReadDataDiff)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'ReadDataDiff' shadow value for %q: %#v", key, raw))
		return nil, nil
	}

	// Compare the parameters, which should be identical
	if !desired.Equal(result.Desired) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"ReadDataDiff %q had unequal configs (real, then shadow):\n\n%#v\n\n%#v",
			key, result.Desired, desired))
		p.ErrorLock.Unlock()
	}

	return result.Result, result.ResultErr
}

func (p *shadowResourceProviderShadow) ReadDataApply(
	info *InstanceInfo,
	d *InstanceDiff) (*InstanceState, error) {
	// Unique key
	key := info.uniqueId()
	raw := p.Shared.ReadDataApply.Value(key)
	if raw == nil {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'ReadDataApply' call for %q:\n\n%#v",
			key, d))
		return nil, nil
	}

	result, ok := raw.(*shadowResourceProviderReadDataApply)
	if !ok {
		p.ErrorLock.Lock()
		defer p.ErrorLock.Unlock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"Unknown 'ReadDataApply' shadow value for %q: %#v", key, raw))
		return nil, nil
	}

	// Compare the parameters, which should be identical
	if !d.Equal(result.Diff) {
		p.ErrorLock.Lock()
		p.Error = multierror.Append(p.Error, fmt.Errorf(
			"ReadDataApply: unequal diffs (real, then shadow):\n\n%#v\n\n%#v",
			result.Diff, d))
		p.ErrorLock.Unlock()
	}

	return result.Result, result.ResultErr
}

func (p *shadowResourceProviderShadow) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) {
	panic("import not supported by shadow graph")
}

// The structs for the various function calls are put below. These structs
// are used to carry call information across the real/shadow boundaries.

type shadowResourceProviderInput struct {
	Config    *ResourceConfig
	Result    *ResourceConfig
	ResultErr error
}

type shadowResourceProviderValidate struct {
	Config     *ResourceConfig
	ResultWarn []string
	ResultErr  []error
}

type shadowResourceProviderConfigure struct {
	Config *ResourceConfig
	Result error
}

type shadowResourceProviderValidateResourceWrapper struct {
	sync.RWMutex

	Calls []*shadowResourceProviderValidateResource
}

type shadowResourceProviderValidateResource struct {
	Config *ResourceConfig
	Warns  []string
	Errors []error
}

type shadowResourceProviderApply struct {
	State     *InstanceState
	Diff      *InstanceDiff
	Result    *InstanceState
	ResultErr error
}

type shadowResourceProviderDiff struct {
	State     *InstanceState
	Desired   *ResourceConfig
	Result    *InstanceDiff
	ResultErr error
}

type shadowResourceProviderRefresh struct {
	State     *InstanceState
	Result    *InstanceState
	ResultErr error
}

type shadowResourceProviderValidateDataSourceWrapper struct {
	sync.RWMutex

	Calls []*shadowResourceProviderValidateDataSource
}

type shadowResourceProviderValidateDataSource struct {
	Config *ResourceConfig
	Warns  []string
	Errors []error
}

type shadowResourceProviderReadDataDiff struct {
	Desired   *ResourceConfig
	Result    *InstanceDiff
	ResultErr error
}

type shadowResourceProviderReadDataApply struct {
	Diff      *InstanceDiff
	Result    *InstanceState
	ResultErr error
}