github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/backend/remote-state/inmem/client.go (about) 1 package inmem 2 3 import ( 4 "crypto/md5" 5 "errors" 6 "time" 7 8 "github.com/hashicorp/terraform/state" 9 "github.com/hashicorp/terraform/state/remote" 10 ) 11 12 // RemoteClient is a remote client that stores data in memory for testing. 13 type RemoteClient struct { 14 Data []byte 15 MD5 []byte 16 17 LockInfo *state.LockInfo 18 } 19 20 func (c *RemoteClient) Get() (*remote.Payload, error) { 21 if c.Data == nil { 22 return nil, nil 23 } 24 25 return &remote.Payload{ 26 Data: c.Data, 27 MD5: c.MD5, 28 }, nil 29 } 30 31 func (c *RemoteClient) Put(data []byte) error { 32 md5 := md5.Sum(data) 33 34 c.Data = data 35 c.MD5 = md5[:] 36 return nil 37 } 38 39 func (c *RemoteClient) Delete() error { 40 c.Data = nil 41 c.MD5 = nil 42 return nil 43 } 44 45 func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) { 46 lockErr := &state.LockError{ 47 Info: &state.LockInfo{}, 48 } 49 50 if c.LockInfo != nil { 51 lockErr.Err = errors.New("state locked") 52 // make a copy of the lock info to avoid any testing shenanigans 53 *lockErr.Info = *c.LockInfo 54 return "", lockErr 55 } 56 57 info.Created = time.Now().UTC() 58 c.LockInfo = info 59 60 return c.LockInfo.ID, nil 61 } 62 63 func (c *RemoteClient) Unlock(id string) error { 64 if c.LockInfo == nil { 65 return errors.New("state not locked") 66 } 67 68 lockErr := &state.LockError{ 69 Info: &state.LockInfo{}, 70 } 71 if id != c.LockInfo.ID { 72 lockErr.Err = errors.New("invalid lock id") 73 *lockErr.Info = *c.LockInfo 74 return lockErr 75 } 76 77 c.LockInfo = nil 78 return nil 79 }