terraform: use KeyedValue.Init to avoid initialization race

There were races with ValidateResource in the provider initializing the
data which resulting in lost data for the shadow. A new "Init" function
has been added to the shadow structs to support safe concurrent
initialization.
This commit is contained in:
Mitchell Hashimoto 2016-10-12 18:47:52 +08:00
parent 10bcdd04d4
commit a9f1166583
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
1 changed files with 10 additions and 13 deletions

View File

@ -115,12 +115,12 @@ func (p *shadowResourceProviderReal) ValidateResource(
// Real operation
warns, errs := p.ResourceProvider.ValidateResource(t, c)
// Get the result
raw, ok := p.Shared.ValidateResource.ValueOk(key)
if !ok {
raw = new(shadowResourceProviderValidateResourceWrapper)
}
// 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
@ -140,9 +140,6 @@ func (p *shadowResourceProviderReal) ValidateResource(
Errors: errs,
})
// Set it
p.Shared.ValidateResource.SetValue(key, wrapper)
// Return the result
return warns, errs
}
@ -209,12 +206,12 @@ func (p *shadowResourceProviderReal) ValidateDataSource(
// Real operation
warns, errs := p.ResourceProvider.ValidateDataSource(t, c)
// Get the result
raw, ok := p.Shared.ValidateDataSource.ValueOk(key)
if !ok {
raw = new(shadowResourceProviderValidateDataSourceWrapper)
}
// 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