-
Notifications
You must be signed in to change notification settings - Fork 10
/
action.go
40 lines (37 loc) · 1.31 KB
/
action.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
package objchange
import (
"github.com/zclconf/go-cty/cty"
"github.com/snyk/policy-engine/pkg/internal/terraform/plans"
)
// ActionForChange determines which plans.Action value best describes a
// change from the value given in before to the value given in after.
//
// Because it has no context aside from the values, it can only return the
// basic actions NoOp, Create, Update, and Delete. Other codepaths with
// additional information might make this decision differently, such as by
// using the Replace action instead of the Update action where that makes
// sense.
//
// If the after value is unknown then the action can't be properly decided, and
// so ActionForChange will conservatively return either Create or Update
// depending on whether the before value is null. The before value must always
// be fully known; ActionForChange will panic if it contains any unknown values.
func ActionForChange(before, after cty.Value) plans.Action {
switch {
case !after.IsKnown():
if before.IsNull() {
return plans.Create
}
return plans.Update
case after.IsNull() && before.IsNull():
return plans.NoOp
case after.IsNull() && !before.IsNull():
return plans.Delete
case before.IsNull() && !after.IsNull():
return plans.Create
case after.RawEquals(before):
return plans.NoOp
default:
return plans.Update
}
}