github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/states/statemgr/helper.go (about) 1 package statemgr 2 3 // The functions in this file are helper wrappers for common sequences of 4 // operations done against full state managers. 5 6 import ( 7 "github.com/hashicorp/terraform/internal/states" 8 "github.com/hashicorp/terraform/internal/states/statefile" 9 "github.com/hashicorp/terraform/internal/terraform" 10 "github.com/hashicorp/terraform/version" 11 ) 12 13 // NewStateFile creates a new statefile.File object, with a newly-minted 14 // lineage identifier and serial 0, and returns a pointer to it. 15 func NewStateFile() *statefile.File { 16 return &statefile.File{ 17 Lineage: NewLineage(), 18 TerraformVersion: version.SemVer, 19 State: states.NewState(), 20 } 21 } 22 23 // RefreshAndRead refreshes the persistent snapshot in the given state manager 24 // and then returns it. 25 // 26 // This is a wrapper around calling RefreshState and then State on the given 27 // manager. 28 func RefreshAndRead(mgr Storage) (*states.State, error) { 29 err := mgr.RefreshState() 30 if err != nil { 31 return nil, err 32 } 33 return mgr.State(), nil 34 } 35 36 // WriteAndPersist writes a snapshot of the given state to the given state 37 // manager's transient store and then immediately persists it. 38 // 39 // The caller must ensure that the given state is not concurrently modified 40 // while this function is running, but it is safe to modify it after this 41 // function has returned. 42 // 43 // If an error is returned, it is undefined whether the state has been saved 44 // to the transient store or not, and so the only safe response is to bail 45 // out quickly with a user-facing error. In situations where more control 46 // is required, call WriteState and PersistState on the state manager directly 47 // and handle their errors. 48 func WriteAndPersist(mgr Storage, state *states.State, schemas *terraform.Schemas) error { 49 err := mgr.WriteState(state) 50 if err != nil { 51 return err 52 } 53 return mgr.PersistState(schemas) 54 }