-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
208 lines (177 loc) · 4.31 KB
/
plan.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package plan
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"github.com/bhoriuchi/optimus/types"
"github.com/fatih/color"
"github.com/ghodss/yaml"
)
type Input struct {
Plan string
plan *types.Plan
State string
state *types.StateV4
Change string
change *types.ChangeFile
Out string
HideUpdates bool
}
func (in *Input) absPaths() error {
var err error
wd, err := os.Getwd()
if err != nil {
return err
}
if in.Plan != "" && !filepath.IsAbs(in.Plan) {
in.Plan, err = filepath.Abs(filepath.Join(wd, in.Plan))
if err != nil {
return err
}
}
if in.State != "" && !filepath.IsAbs(in.State) {
in.State, err = filepath.Abs(filepath.Join(wd, in.State))
if err != nil {
return err
}
}
if in.Change != "" && !filepath.IsAbs(in.Change) {
in.Change, err = filepath.Abs(filepath.Join(wd, in.Change))
if err != nil {
return err
}
}
if in.Out != "" && !filepath.IsAbs(in.Out) {
in.Out, err = filepath.Abs(filepath.Join(wd, in.Out))
if err != nil {
return err
}
}
return nil
}
func Plan(in *Input) error {
var plan []byte
in.plan = &types.Plan{}
in.state = &types.StateV4{}
err := in.absPaths()
if err != nil {
return err
}
// if plan was specified, process it
if in.Plan != "" {
plan, err = ioutil.ReadFile(in.Plan)
if err != nil {
return fmt.Errorf("failed to read plan file: %s", err)
}
if err := json.Unmarshal(plan, in.plan); err != nil {
return fmt.Errorf("failed to unmarshal plan file: %s", err)
}
}
if in.State == "" {
return fmt.Errorf("no state file specified")
}
state, err := ioutil.ReadFile(in.State)
if err != nil {
return fmt.Errorf("failed to read state file: %s", err)
}
if err := json.Unmarshal(state, in.state); err != nil {
return fmt.Errorf("failed to unmarshal state file: %s", err)
}
if in.Change != "" {
in.change = &types.ChangeFile{}
change, err := ioutil.ReadFile(in.Change)
if err != nil {
return fmt.Errorf("failed to read change file: %s", err)
}
if err := yaml.Unmarshal(change, in.change); err != nil {
return fmt.Errorf("failed to unmarshal change file: %s", err)
}
}
if in.change != nil {
applyChanges(in)
in.state.Serial++
}
// add plan changes
if len(plan) > 0 {
add := []string{}
remove := []string{}
for _, change := range in.plan.ResourceChanges {
for _, action := range change.Change.Actions {
switch action {
case "create":
add = append(add, change.Address)
case "delete":
remove = append(remove, change.Address)
}
}
}
sort.Strings(add)
sort.Strings(remove)
for _, addr := range remove {
found, _, _ := findStateResourceByAddress(in.state, addr)
if found {
color.Red("- %s", addr)
} else if !in.HideUpdates {
color.Blue("* %s", addr)
}
}
for _, addr := range add {
found, _, _ := findStateResourceByAddress(in.state, addr)
if !found {
color.Green("+ %s", addr)
} else if !in.HideUpdates {
color.Blue("* %s", addr)
}
}
}
if in.Out != "" {
b, err := json.MarshalIndent(in.state, "", " ")
if err != nil {
return fmt.Errorf("failed to serialize updated state file: %s", err)
}
if err := ioutil.WriteFile(in.Out, b, 0755); err != nil {
return fmt.Errorf("faild to write updated state file to disk: %s", err)
}
}
return nil
}
func findStateResourceByAddress(state *types.StateV4, address string) (bool, *types.ResourceStateV4, *types.InstanceObjectStateV4) {
for _, resource := range state.Resources {
for _, instance := range resource.Instances {
raddr := resource.Addr()
iaddr := resource.Addr(instance.IndexKey)
switch idx := instance.IndexKey.(type) {
case int, int32, int64, float32, float64:
if (idx == 0.0 && address == raddr) || (address == iaddr) {
return true, resource, instance
}
case string:
if address == iaddr {
return true, resource, instance
}
}
}
}
return false, nil, nil
}
func applyChanges(in *Input) {
for _, op := range in.change.Operations {
switch op.Operation {
case "count_to_map":
countToMap(in, op)
case "add_resource":
addResource(in, op)
case "remove_resource":
removeResource(in, op)
case "move_instance":
moveInstance(in, op)
case "upgrade_terraform":
upgradeTerraform(in, op)
case "provider_replace":
providerReplace(in, op)
}
}
}