-
Notifications
You must be signed in to change notification settings - Fork 78
/
diff.go
72 lines (62 loc) · 1.79 KB
/
diff.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
package operation
import (
"github.com/pkg/errors"
"kusionstack.io/kusion/pkg/engine/models"
opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/util"
"kusionstack.io/kusion/pkg/util/diff"
jsonutil "kusionstack.io/kusion/pkg/util/json"
"kusionstack.io/kusion/third_party/dyff"
)
type Diff struct {
StateStorage states.StateStorage
}
type DiffRequest struct {
opsmodels.Request
}
func (d *Diff) Diff(request *DiffRequest) (string, error) {
log.Infof("invoke Diff")
defer func() {
if err := recover(); err != nil {
log.Error("Diff panic:%v", err)
}
}()
util.CheckNotNil(request, "request is nil")
util.CheckNotNil(request.Spec, "resource is nil")
// Get plan state resources
plan := request.Spec
// Get the latest state resources
latestState, err := d.StateStorage.GetLatestState(
&states.StateQuery{
Tenant: request.Tenant,
Stack: request.Stack,
Project: request.Project,
},
)
if err != nil {
return "", errors.Wrap(err, "GetLatestState failed")
}
if latestState == nil {
log.Infof("can't find states by request: %v.", jsonutil.MustMarshal2String(request))
}
// Get diff result
return DiffWithRequestResourceAndState(plan, latestState)
}
func DiffWithRequestResourceAndState(plan *models.Spec, latest *states.State) (string, error) {
planString := jsonutil.MustMarshal2String(plan.Resources)
var report *dyff.Report
var err error
if latest == nil {
report, err = diff.ToReport("", planString)
} else {
latestResources := latest.Resources
priorString := jsonutil.MustMarshal2String(latestResources)
report, err = diff.ToReport(priorString, priorString)
}
if err != nil {
return "", err
}
return diff.ToHumanString(diff.NewHumanReport(report))
}