Lock all ResourceState methods

Also rename receivers so they are consistent.
This commit is contained in:
James Bardin 2016-08-31 16:26:11 -04:00
parent 469e71a488
commit 8d5080fe72
1 changed files with 45 additions and 27 deletions

View File

@ -1322,6 +1322,9 @@ func (s *ResourceState) Unlock() { s.mu.Unlock() }
// Equal tests whether two ResourceStates are equal. // Equal tests whether two ResourceStates are equal.
func (s *ResourceState) Equal(other *ResourceState) bool { func (s *ResourceState) Equal(other *ResourceState) bool {
s.Lock()
defer s.Unlock()
if s.Type != other.Type { if s.Type != other.Type {
return false return false
} }
@ -1351,43 +1354,49 @@ func (s *ResourceState) Equal(other *ResourceState) bool {
} }
// Taint marks a resource as tainted. // Taint marks a resource as tainted.
func (r *ResourceState) Taint() { func (s *ResourceState) Taint() {
if r.Primary != nil { s.Lock()
r.Primary.Tainted = true defer s.Unlock()
if s.Primary != nil {
s.Primary.Tainted = true
} }
} }
// Untaint unmarks a resource as tainted. // Untaint unmarks a resource as tainted.
func (r *ResourceState) Untaint() { func (s *ResourceState) Untaint() {
if r.Primary != nil { s.Lock()
r.Primary.Tainted = false defer s.Unlock()
if s.Primary != nil {
s.Primary.Tainted = false
} }
} }
func (r *ResourceState) init() { func (s *ResourceState) init() {
r.Lock() s.Lock()
defer r.Unlock() defer s.Unlock()
if r.Primary == nil { if s.Primary == nil {
r.Primary = &InstanceState{} s.Primary = &InstanceState{}
} }
r.Primary.init() s.Primary.init()
if r.Dependencies == nil { if s.Dependencies == nil {
r.Dependencies = []string{} s.Dependencies = []string{}
} }
if r.Deposed == nil { if s.Deposed == nil {
r.Deposed = make([]*InstanceState, 0) s.Deposed = make([]*InstanceState, 0)
} }
for _, dep := range r.Deposed { for _, dep := range s.Deposed {
dep.init() dep.init()
} }
} }
func (r *ResourceState) deepcopy() *ResourceState { func (s *ResourceState) deepcopy() *ResourceState {
copy, err := copystructure.Config{Lock: true}.Copy(r) copy, err := copystructure.Config{Lock: true}.Copy(s)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -1396,26 +1405,35 @@ func (r *ResourceState) deepcopy() *ResourceState {
} }
// prune is used to remove any instances that are no longer required // prune is used to remove any instances that are no longer required
func (r *ResourceState) prune() { func (s *ResourceState) prune() {
n := len(r.Deposed) s.Lock()
defer s.Unlock()
n := len(s.Deposed)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
inst := r.Deposed[i] inst := s.Deposed[i]
if inst == nil || inst.ID == "" { if inst == nil || inst.ID == "" {
copy(r.Deposed[i:], r.Deposed[i+1:]) copy(s.Deposed[i:], s.Deposed[i+1:])
r.Deposed[n-1] = nil s.Deposed[n-1] = nil
n-- n--
i-- i--
} }
} }
r.Deposed = r.Deposed[:n] s.Deposed = s.Deposed[:n]
} }
func (r *ResourceState) sort() { func (s *ResourceState) sort() {
sort.Strings(r.Dependencies) s.Lock()
defer s.Unlock()
sort.Strings(s.Dependencies)
} }
func (s *ResourceState) String() string { func (s *ResourceState) String() string {
s.Lock()
defer s.Unlock()
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("Type = %s", s.Type)) buf.WriteString(fmt.Sprintf("Type = %s", s.Type))
return buf.String() return buf.String()