github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/state/inmem.go (about) 1 package state 2 3 import ( 4 "errors" 5 "sync" 6 "time" 7 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 // InmemState is an in-memory state storage. 12 type InmemState struct { 13 state *terraform.State 14 } 15 16 func (s *InmemState) State() *terraform.State { 17 return s.state.DeepCopy() 18 } 19 20 func (s *InmemState) RefreshState() error { 21 return nil 22 } 23 24 func (s *InmemState) WriteState(state *terraform.State) error { 25 state.IncrementSerialMaybe(s.state) 26 s.state = state 27 return nil 28 } 29 30 func (s *InmemState) PersistState() error { 31 return nil 32 } 33 34 func (s *InmemState) Lock(*LockInfo) (string, error) { 35 return "", nil 36 } 37 38 func (s *InmemState) Unlock(string) error { 39 return nil 40 } 41 42 // inmemLocker is an in-memory State implementation for testing locks. 43 type inmemLocker struct { 44 *InmemState 45 46 mu sync.Mutex 47 lockInfo *LockInfo 48 // count the calls to Lock 49 lockCounter int 50 } 51 52 func (s *inmemLocker) Lock(info *LockInfo) (string, error) { 53 s.mu.Lock() 54 defer s.mu.Unlock() 55 s.lockCounter++ 56 57 lockErr := &LockError{ 58 Info: &LockInfo{}, 59 } 60 61 if s.lockInfo != nil { 62 lockErr.Err = errors.New("state locked") 63 *lockErr.Info = *s.lockInfo 64 return "", lockErr 65 } 66 67 info.Created = time.Now().UTC() 68 s.lockInfo = info 69 return s.lockInfo.ID, nil 70 } 71 72 func (s *inmemLocker) Unlock(id string) error { 73 s.mu.Lock() 74 defer s.mu.Unlock() 75 76 lockErr := &LockError{ 77 Info: &LockInfo{}, 78 } 79 80 if id != s.lockInfo.ID { 81 lockErr.Err = errors.New("invalid lock id") 82 *lockErr.Info = *s.lockInfo 83 return lockErr 84 } 85 86 s.lockInfo = nil 87 return nil 88 }