github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/state/remote/state.go (about) 1 package remote 2 3 import ( 4 "bytes" 5 6 "github.com/hashicorp/terraform/state" 7 "github.com/hashicorp/terraform/terraform" 8 ) 9 10 // State implements the State interfaces in the state package to handle 11 // reading and writing the remote state. This State on its own does no 12 // local caching so every persist will go to the remote storage and local 13 // writes will go to memory. 14 type State struct { 15 Client Client 16 17 state, readState *terraform.State 18 } 19 20 // StateReader impl. 21 func (s *State) State() *terraform.State { 22 return s.state.DeepCopy() 23 } 24 25 // StateWriter impl. 26 func (s *State) WriteState(state *terraform.State) error { 27 s.state = state 28 return nil 29 } 30 31 // StateRefresher impl. 32 func (s *State) RefreshState() error { 33 payload, err := s.Client.Get() 34 if err != nil { 35 return err 36 } 37 38 // no remote state is OK 39 if payload == nil { 40 return nil 41 } 42 43 state, err := terraform.ReadState(bytes.NewReader(payload.Data)) 44 if err != nil { 45 return err 46 } 47 48 s.state = state 49 s.readState = state 50 return nil 51 } 52 53 // StatePersister impl. 54 func (s *State) PersistState() error { 55 s.state.IncrementSerialMaybe(s.readState) 56 57 var buf bytes.Buffer 58 if err := terraform.WriteState(s.state, &buf); err != nil { 59 return err 60 } 61 62 return s.Client.Put(buf.Bytes()) 63 } 64 65 // Lock calls the Client's Lock method if it's implemented. 66 func (s *State) Lock(info *state.LockInfo) (string, error) { 67 if c, ok := s.Client.(ClientLocker); ok { 68 return c.Lock(info) 69 } 70 return "", nil 71 } 72 73 // Unlock calls the Client's Unlock method if it's implemented. 74 func (s *State) Unlock(id string) error { 75 if c, ok := s.Client.(ClientLocker); ok { 76 return c.Unlock(id) 77 } 78 return nil 79 }