github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/state/remote/state.go (about)

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