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
|
|
|
|
|
2015-02-24 06:30:59 +01:00
|
|
|
state, readState *terraform.State
|
2015-02-21 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// StateReader impl.
|
|
|
|
func (s *State) State() *terraform.State {
|
2015-02-24 06:36:35 +01:00
|
|
|
return s.state.DeepCopy()
|
2015-02-21 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-02-22 00:15:39 +01:00
|
|
|
var state *terraform.State
|
|
|
|
if payload != nil {
|
|
|
|
state, err = terraform.ReadState(bytes.NewReader(payload.Data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-21 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s.state = state
|
2015-02-24 06:30:59 +01:00
|
|
|
s.readState = state
|
2015-02-21 20:52:55 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatePersister impl.
|
|
|
|
func (s *State) PersistState() error {
|
2015-02-24 06:30:59 +01:00
|
|
|
s.state.IncrementSerialMaybe(s.readState)
|
|
|
|
|
2015-02-21 20:52:55 +01:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := terraform.WriteState(s.state, &buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Client.Put(buf.Bytes())
|
|
|
|
}
|