terraform/state/remote/state.go

55 lines
1.0 KiB
Go
Raw Normal View History

2015-02-21 20:52:55 +01:00
package remote
import (
"bytes"
"github.com/hashicorp/terraform/terraform"
)
// State implements the State interfaces in the state package to handle
// reading and writing the remote state. This State on its own does no
// local caching so every persist will go to the remote storage and local
// writes will go to memory.
type State struct {
Client Client
state *terraform.State
}
// StateReader impl.
func (s *State) State() *terraform.State {
return s.state
}
// StateWriter impl.
func (s *State) WriteState(state *terraform.State) error {
s.state = state
return nil
}
// StateRefresher impl.
func (s *State) RefreshState() error {
payload, err := s.Client.Get()
if err != nil {
return err
}
state, err := terraform.ReadState(bytes.NewReader(payload.Data))
if err != nil {
return err
}
s.state = state
return nil
}
// StatePersister impl.
func (s *State) PersistState() error {
var buf bytes.Buffer
if err := terraform.WriteState(s.state, &buf); err != nil {
return err
}
return s.Client.Put(buf.Bytes())
}