-
Notifications
You must be signed in to change notification settings - Fork 17
/
state.go
93 lines (76 loc) · 2.55 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Copyright The Terranova Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package terranova
import (
"bytes"
"io"
"io/ioutil"
"os"
"github.com/hashicorp/terraform/states/statemgr"
"github.com/hashicorp/terraform/states/statefile"
)
// WriteState takes a io.Writer as input to write the Terraform state
func (p *Platform) WriteState(w io.Writer) (*Platform, error) {
p.mu.Lock()
defer p.mu.Unlock()
sf := statefile.New(p.State, "", 0)
return p, statefile.Write(sf, w)
}
// ReadState takes a io.Reader as input to read from it the Terraform state
func (p *Platform) ReadState(r io.Reader) (*Platform, error) {
p.mu.Lock()
defer p.mu.Unlock()
sf, err := statefile.Read(r)
if err != nil {
return p, err
}
p.State = sf.State
return p, nil
}
// WriteStateToFile save the state of the Terraform state to a file
func (p *Platform) WriteStateToFile(filename string) (*Platform, error) {
var state bytes.Buffer
if _, err := p.WriteState(&state); err != nil {
return p, err
}
return p, ioutil.WriteFile(filename, state.Bytes(), 0644)
}
// ReadStateFromFile will load the Terraform state from a file and assign it to the
// Platform state.
func (p *Platform) ReadStateFromFile(filename string) (*Platform, error) {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return p, err
}
return p.ReadState(file)
}
// PersistStateToFile reads the state from the given file, if exists. Then will save
// the current state to the given file every time it changes during the Terraform
// actions.
func (p *Platform) PersistStateToFile(filename string) (*Platform, error) {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
// If the file exists, read the state from the file and make it a backup of the file
if _, err := p.ReadStateFromFile(filename); err != nil {
return p, err
}
os.Rename(filename, filename+".bkp")
}
// The files does not exists, create it with the current state: empty or loaded
if _, err := p.WriteStateToFile(filename); err != nil {
return p, err
}
fsStateMgr := statemgr.NewFilesystem(filename)
p.stateMgr = fsStateMgr
return p, nil
}