github.com/akazakov/terraform@v0.5.2-0.20160205142716-097441beafdf/state/backup.go (about) 1 package state 2 3 import ( 4 "github.com/hashicorp/terraform/terraform" 5 ) 6 7 // BackupState wraps a State that backs up the state on the first time that 8 // a WriteState or PersistState is called. 9 // 10 // If Path exists, it will be overwritten. 11 type BackupState struct { 12 Real State 13 Path string 14 15 done bool 16 } 17 18 func (s *BackupState) State() *terraform.State { 19 return s.Real.State() 20 } 21 22 func (s *BackupState) RefreshState() error { 23 return s.Real.RefreshState() 24 } 25 26 func (s *BackupState) WriteState(state *terraform.State) error { 27 if !s.done { 28 if err := s.backup(); err != nil { 29 return err 30 } 31 } 32 33 return s.Real.WriteState(state) 34 } 35 36 func (s *BackupState) PersistState() error { 37 if !s.done { 38 if err := s.backup(); err != nil { 39 return err 40 } 41 } 42 43 return s.Real.PersistState() 44 } 45 46 func (s *BackupState) backup() error { 47 state := s.Real.State() 48 if state == nil { 49 if err := s.Real.RefreshState(); err != nil { 50 return err 51 } 52 53 state = s.Real.State() 54 } 55 56 ls := &LocalState{Path: s.Path} 57 if err := ls.WriteState(state); err != nil { 58 return err 59 } 60 61 s.done = true 62 return nil 63 }