-
Notifications
You must be signed in to change notification settings - Fork 150
/
plan.go
128 lines (111 loc) · 3.18 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
package output
import (
"encoding/json"
"fmt"
"os"
"github.com/snyk/driftctl/pkg/analyser"
"github.com/snyk/driftctl/pkg/resource"
)
const FormatVersion = "0.1"
const PlanOutputType = "plan"
const PlanOutputExample = "plan://PATH/TO/FILE.json"
type plan struct {
FormatVersion string `json:"format_version,omitempty"`
PlannedValues plannedValues `json:"planned_values,omitempty"`
ResourceChanges []rscChange `json:"resource_changes,omitempty"`
}
type plannedValues struct {
RootModule module `json:"root_module,omitempty"`
}
type rscChange struct {
Address string `json:"address,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Change change `json:"change,omitempty"`
}
type change struct {
Actions []string `json:"actions,omitempty"`
Before map[string]interface{} `json:"before,omitempty"`
After map[string]interface{} `json:"after,omitempty"`
}
type module struct {
Resources []rsc `json:"resources,omitempty"`
}
type rsc struct {
Address string `json:"address,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
AttributeValues map[string]interface{} `json:"values,omitempty"`
}
type Plan struct {
path string
}
func NewPlan(path string) *Plan {
return &Plan{path}
}
func (c *Plan) Write(analysis *analyser.Analysis) error {
file := os.Stdout
if !isStdOut(c.path) {
f, err := os.OpenFile(c.path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer f.Close()
file = f
}
output := plan{FormatVersion: FormatVersion}
output.PlannedValues.RootModule = addPlannedValues(analysis)
output.ResourceChanges = addResourceChanges(analysis)
jsonPlan, err := json.MarshalIndent(output, "", "\t")
if err != nil {
return err
}
if _, err := file.Write(jsonPlan); err != nil {
return err
}
return nil
}
func addPlannedValues(analysis *analyser.Analysis) module {
managedRsc := listRsc(analysis.Managed())
unmanagedRsc := listRsc(analysis.Unmanaged())
return module{
Resources: append(managedRsc, unmanagedRsc...),
}
}
func listRsc(resources []*resource.Resource) []rsc {
var ret []rsc
for _, res := range resources {
r := rsc{
Address: fmt.Sprintf("%s.%s", res.ResourceType(), res.ResourceId()),
Type: res.ResourceType(),
Name: res.ResourceId(),
AttributeValues: *res.Attributes(),
}
ret = append(ret, r)
}
return ret
}
func addResourceChanges(analysis *analyser.Analysis) []rscChange {
managedRsc := listRscChange(analysis.Managed(), "no-op")
unmanagedRsc := listRscChange(analysis.Unmanaged(), "create")
return append(managedRsc, unmanagedRsc...)
}
func listRscChange(resources []*resource.Resource, action string) []rscChange {
var ret []rscChange
for _, res := range resources {
r := rscChange{
Address: fmt.Sprintf("%s.%s", res.ResourceType(), res.ResourceId()),
Type: res.ResourceType(),
Name: res.ResourceId(),
Change: change{
Actions: []string{action},
After: *res.Attributes(),
},
}
if action == "no-op" {
r.Change.Before = *res.Attributes()
}
ret = append(ret, r)
}
return ret
}