forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransient_inmem.go
41 lines (32 loc) · 951 Bytes
/
transient_inmem.go
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
package statemgr
import (
"sync"
"github.com/hashicorp/terraform/states"
)
// NewTransientInMemory returns a Transient implementation that retains
// transient snapshots only in memory, as part of the object.
//
// The given initial state, if any, must not be modified concurrently while
// this function is running, but may be freely modified once this function
// returns without affecting the stored transient snapshot.
func NewTransientInMemory(initial *states.State) Transient {
return &transientInMemory{
current: initial.DeepCopy(),
}
}
type transientInMemory struct {
lock sync.RWMutex
current *states.State
}
var _ Transient = (*transientInMemory)(nil)
func (m *transientInMemory) State() *states.State {
m.lock.RLock()
defer m.lock.RUnlock()
return m.current.DeepCopy()
}
func (m *transientInMemory) WriteState(new *states.State) error {
m.lock.Lock()
defer m.lock.Unlock()
m.current = new.DeepCopy()
return nil
}