|
| 1 | +package state |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform/terraform" |
| 5 | +) |
| 6 | + |
| 7 | +// CacheState is an implementation of the state interfaces that uses |
| 8 | +// a StateReadWriter for a local cache. |
| 9 | +type CacheState struct { |
| 10 | + Cache CacheStateCache |
| 11 | + Durable CacheStateDurable |
| 12 | + |
| 13 | + state *terraform.State |
| 14 | +} |
| 15 | + |
| 16 | +// StateReader impl. |
| 17 | +func (s *CacheState) State() *terraform.State { |
| 18 | + return s.state |
| 19 | +} |
| 20 | + |
| 21 | +// WriteState will write and persist the state to the cache. |
| 22 | +// |
| 23 | +// StateWriter impl. |
| 24 | +func (s *CacheState) WriteState(state *terraform.State) error { |
| 25 | + if err := s.Cache.WriteState(state); err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + |
| 29 | + return s.Cache.PersistState() |
| 30 | +} |
| 31 | + |
| 32 | +// RefreshState will refresh both the cache and the durable states. It |
| 33 | +// can return a myriad of errors (defined at the top of this file) depending |
| 34 | +// on potential conflicts that can occur while doing this. |
| 35 | +// |
| 36 | +// If the durable state is newer than the local cache, then the local cache |
| 37 | +// will be replaced with the durable. |
| 38 | +// |
| 39 | +// StateRefresher impl. |
| 40 | +func (s *CacheState) RefreshState() error { |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// PersistState takes the local cache, assuming it is newer than the remote |
| 45 | +// state, and persists it to the durable storage. If you want to challenge the |
| 46 | +// assumption that the local state is the latest, call a RefreshState prior |
| 47 | +// to this. |
| 48 | +// |
| 49 | +// StatePersister impl. |
| 50 | +func (s *CacheState) PersistState() error { |
| 51 | + if err := s.Durable.WriteState(s.state); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + return s.Durable.PersistState() |
| 56 | +} |
| 57 | + |
| 58 | +// CacheStateCache is the meta-interface that must be implemented for |
| 59 | +// the cache for the CacheState. |
| 60 | +type CacheStateCache interface { |
| 61 | + StateReader |
| 62 | + StateWriter |
| 63 | + StatePersister |
| 64 | +} |
| 65 | + |
| 66 | +// CacheStateDurable is the meta-interface that must be implemented for |
| 67 | +// the durable storage for CacheState. |
| 68 | +type CacheStateDurable interface { |
| 69 | + StateWriter |
| 70 | + StatePersister |
| 71 | +} |
0 commit comments