github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/state/remote/state.go (about)

     1  package remote
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  var ErrRemoteStateNotFound = errors.New("no remote state found")
    11  
    12  // State implements the State interfaces in the state package to handle
    13  // reading and writing the remote state. This State on its own does no
    14  // local caching so every persist will go to the remote storage and local
    15  // writes will go to memory.
    16  type State struct {
    17  	Client Client
    18  
    19  	state, readState *terraform.State
    20  }
    21  
    22  // StateReader impl.
    23  func (s *State) State() *terraform.State {
    24  	return s.state.DeepCopy()
    25  }
    26  
    27  // StateWriter impl.
    28  func (s *State) WriteState(state *terraform.State) error {
    29  	s.state = state
    30  	return nil
    31  }
    32  
    33  // StateRefresher impl.
    34  func (s *State) RefreshState() error {
    35  	payload, err := s.Client.Get()
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	if payload == nil {
    41  		return ErrRemoteStateNotFound
    42  	}
    43  
    44  	state, err := terraform.ReadState(bytes.NewReader(payload.Data))
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	s.state = state
    50  	s.readState = state
    51  	return nil
    52  }
    53  
    54  // StatePersister impl.
    55  func (s *State) PersistState() error {
    56  	s.state.IncrementSerialMaybe(s.readState)
    57  
    58  	var buf bytes.Buffer
    59  	if err := terraform.WriteState(s.state, &buf); err != nil {
    60  		return err
    61  	}
    62  
    63  	return s.Client.Put(buf.Bytes())
    64  }