2016-10-04 03:23:37 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2016-10-04 06:19:59 +02:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-10-04 03:23:37 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/shadow"
|
|
|
|
)
|
|
|
|
|
|
|
|
// newShadowComponentFactory creates a shadowed contextComponentFactory
|
|
|
|
// so that requests to create new components result in both a real and
|
|
|
|
// shadow side.
|
|
|
|
func newShadowComponentFactory(
|
|
|
|
f contextComponentFactory) (contextComponentFactory, *shadowComponentFactory) {
|
|
|
|
// Create the shared data
|
|
|
|
shared := &shadowComponentFactoryShared{contextComponentFactory: f}
|
|
|
|
|
|
|
|
// Create the real side
|
2016-10-04 04:39:27 +02:00
|
|
|
real := &shadowComponentFactory{
|
2016-10-04 03:23:37 +02:00
|
|
|
shadowComponentFactoryShared: shared,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the shadow
|
|
|
|
shadow := &shadowComponentFactory{
|
|
|
|
shadowComponentFactoryShared: shared,
|
2016-10-04 04:39:27 +02:00
|
|
|
Shadow: true,
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return real, shadow
|
|
|
|
}
|
|
|
|
|
|
|
|
// shadowComponentFactory is the shadow side. Any components created
|
|
|
|
// with this factory are fake and will not cause real work to happen.
|
2016-10-04 06:19:59 +02:00
|
|
|
//
|
|
|
|
// Unlike other shadowers, the shadow component factory will allow the
|
|
|
|
// shadow to create _any_ component even if it is never requested on the
|
|
|
|
// real side. This is because errors will happen later downstream as function
|
|
|
|
// calls are made to the shadows that are never matched on the real side.
|
2016-10-04 03:23:37 +02:00
|
|
|
type shadowComponentFactory struct {
|
|
|
|
*shadowComponentFactoryShared
|
2016-10-04 04:39:27 +02:00
|
|
|
|
|
|
|
Shadow bool // True if this should return the shadow
|
2016-10-04 06:19:59 +02:00
|
|
|
lock sync.Mutex
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *shadowComponentFactory) ResourceProvider(
|
|
|
|
n, uid string) (ResourceProvider, error) {
|
2016-10-04 06:19:59 +02:00
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
2016-10-04 04:39:27 +02:00
|
|
|
real, shadow, err := f.shadowComponentFactoryShared.ResourceProvider(n, uid)
|
|
|
|
var result ResourceProvider = real
|
|
|
|
if f.Shadow {
|
|
|
|
result = shadow
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *shadowComponentFactory) ResourceProvisioner(
|
|
|
|
n, uid string) (ResourceProvisioner, error) {
|
2016-10-04 06:19:59 +02:00
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
2016-10-04 04:39:27 +02:00
|
|
|
real, shadow, err := f.shadowComponentFactoryShared.ResourceProvisioner(n, uid)
|
|
|
|
var result ResourceProvisioner = real
|
|
|
|
if f.Shadow {
|
|
|
|
result = shadow
|
|
|
|
}
|
2016-10-04 03:23:37 +02:00
|
|
|
|
2016-10-04 04:39:27 +02:00
|
|
|
return result, err
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:19:59 +02:00
|
|
|
// CloseShadow is called when the _real_ side is complete. This will cause
|
|
|
|
// all future blocking operations to return immediately on the shadow to
|
|
|
|
// ensure the shadow also completes.
|
|
|
|
func (f *shadowComponentFactory) CloseShadow() error {
|
|
|
|
// If we aren't the shadow, just return
|
|
|
|
if !f.Shadow {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock ourselves so we don't modify state
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
|
|
|
// Grab our shared state
|
|
|
|
shared := f.shadowComponentFactoryShared
|
|
|
|
|
|
|
|
// If we're already closed, its an error
|
|
|
|
if shared.closed {
|
|
|
|
return fmt.Errorf("component factory shadow already closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close all the providers and provisioners and return the error
|
|
|
|
var result error
|
|
|
|
for _, n := range shared.providerKeys {
|
|
|
|
_, shadow, err := shared.ResourceProvider(n, n)
|
|
|
|
if err == nil && shadow != nil {
|
|
|
|
if err := shadow.CloseShadow(); err != nil {
|
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 23:56:02 +02:00
|
|
|
for _, n := range shared.provisionerKeys {
|
|
|
|
_, shadow, err := shared.ResourceProvisioner(n, n)
|
|
|
|
if err == nil && shadow != nil {
|
|
|
|
if err := shadow.CloseShadow(); err != nil {
|
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 06:19:59 +02:00
|
|
|
|
|
|
|
// Mark ourselves as closed
|
|
|
|
shared.closed = true
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:39:02 +02:00
|
|
|
func (f *shadowComponentFactory) ShadowError() error {
|
|
|
|
// If we aren't the shadow, just return
|
|
|
|
if !f.Shadow {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock ourselves so we don't modify state
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
|
|
|
// Grab our shared state
|
|
|
|
shared := f.shadowComponentFactoryShared
|
|
|
|
|
|
|
|
// If we're not closed, its an error
|
|
|
|
if !shared.closed {
|
|
|
|
return fmt.Errorf("component factory must be closed to retrieve errors")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close all the providers and provisioners and return the error
|
|
|
|
var result error
|
|
|
|
for _, n := range shared.providerKeys {
|
|
|
|
_, shadow, err := shared.ResourceProvider(n, n)
|
|
|
|
if err == nil && shadow != nil {
|
|
|
|
if err := shadow.ShadowError(); err != nil {
|
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 23:56:02 +02:00
|
|
|
for _, n := range shared.provisionerKeys {
|
|
|
|
_, shadow, err := shared.ResourceProvisioner(n, n)
|
|
|
|
if err == nil && shadow != nil {
|
|
|
|
if err := shadow.ShadowError(); err != nil {
|
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:39:02 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-10-04 03:23:37 +02:00
|
|
|
// shadowComponentFactoryShared is shared data between the two factories.
|
2016-10-04 06:19:59 +02:00
|
|
|
//
|
|
|
|
// It is NOT SAFE to run any function on this struct in parallel. Lock
|
|
|
|
// access to this struct.
|
2016-10-04 03:23:37 +02:00
|
|
|
type shadowComponentFactoryShared struct {
|
|
|
|
contextComponentFactory
|
|
|
|
|
2016-10-04 06:19:59 +02:00
|
|
|
closed bool
|
|
|
|
providers shadow.KeyedValue
|
|
|
|
providerKeys []string
|
|
|
|
provisioners shadow.KeyedValue
|
|
|
|
provisionerKeys []string
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// shadowResourceProviderFactoryEntry is the entry that is stored in
|
|
|
|
// the Shadows key/value for a provider.
|
|
|
|
type shadowComponentFactoryProviderEntry struct {
|
|
|
|
Real ResourceProvider
|
|
|
|
Shadow shadowResourceProvider
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type shadowComponentFactoryProvisionerEntry struct {
|
|
|
|
Real ResourceProvisioner
|
2016-10-06 23:56:02 +02:00
|
|
|
Shadow shadowResourceProvisioner
|
2016-10-04 03:23:37 +02:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *shadowComponentFactoryShared) ResourceProvider(
|
|
|
|
n, uid string) (ResourceProvider, shadowResourceProvider, error) {
|
|
|
|
// Determine if we already have a value
|
|
|
|
raw, ok := f.providers.ValueOk(uid)
|
|
|
|
if !ok {
|
|
|
|
// Build the entry
|
|
|
|
var entry shadowComponentFactoryProviderEntry
|
|
|
|
|
|
|
|
// No value, initialize. Create the original
|
|
|
|
p, err := f.contextComponentFactory.ResourceProvider(n, uid)
|
|
|
|
if err != nil {
|
|
|
|
entry.Err = err
|
|
|
|
p = nil // Just to be sure
|
|
|
|
}
|
|
|
|
|
|
|
|
if p != nil {
|
|
|
|
// Create the shadow
|
|
|
|
real, shadow := newShadowResourceProvider(p)
|
|
|
|
entry.Real = real
|
|
|
|
entry.Shadow = shadow
|
2016-10-19 23:10:46 +02:00
|
|
|
|
|
|
|
if f.closed {
|
|
|
|
shadow.CloseShadow()
|
|
|
|
}
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the value
|
|
|
|
f.providers.SetValue(uid, &entry)
|
2016-10-04 06:19:59 +02:00
|
|
|
f.providerKeys = append(f.providerKeys, uid)
|
2016-10-04 03:23:37 +02:00
|
|
|
raw = &entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the entry
|
|
|
|
entry, ok := raw.(*shadowComponentFactoryProviderEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("Unknown value for shadow provider: %#v", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return entry.Real, entry.Shadow, entry.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *shadowComponentFactoryShared) ResourceProvisioner(
|
2016-10-06 23:56:02 +02:00
|
|
|
n, uid string) (ResourceProvisioner, shadowResourceProvisioner, error) {
|
2016-10-04 03:23:37 +02:00
|
|
|
// Determine if we already have a value
|
|
|
|
raw, ok := f.provisioners.ValueOk(uid)
|
|
|
|
if !ok {
|
|
|
|
// Build the entry
|
|
|
|
var entry shadowComponentFactoryProvisionerEntry
|
|
|
|
|
|
|
|
// No value, initialize. Create the original
|
|
|
|
p, err := f.contextComponentFactory.ResourceProvisioner(n, uid)
|
|
|
|
if err != nil {
|
|
|
|
entry.Err = err
|
|
|
|
p = nil // Just to be sure
|
|
|
|
}
|
|
|
|
|
|
|
|
if p != nil {
|
|
|
|
// For now, just create a mock since we don't support provisioners yet
|
2016-10-06 23:56:02 +02:00
|
|
|
real, shadow := newShadowResourceProvisioner(p)
|
2016-10-04 03:23:37 +02:00
|
|
|
entry.Real = real
|
|
|
|
entry.Shadow = shadow
|
2016-10-19 23:10:46 +02:00
|
|
|
|
|
|
|
if f.closed {
|
|
|
|
shadow.CloseShadow()
|
|
|
|
}
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the value
|
|
|
|
f.provisioners.SetValue(uid, &entry)
|
2016-10-04 06:19:59 +02:00
|
|
|
f.provisionerKeys = append(f.provisionerKeys, uid)
|
2016-10-04 03:23:37 +02:00
|
|
|
raw = &entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the entry
|
|
|
|
entry, ok := raw.(*shadowComponentFactoryProvisionerEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("Unknown value for shadow provisioner: %#v", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return entry.Real, entry.Shadow, entry.Err
|
|
|
|
}
|