-
Notifications
You must be signed in to change notification settings - Fork 7
/
causes_of_drift.go
69 lines (54 loc) · 1.96 KB
/
causes_of_drift.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
package markdowncreation
import (
"fmt"
"sort"
"github.com/atsushinee/go-markdown-generator/doc"
)
// CloudActor represents a cloud actor name
type CloudActor string
// CloudAction represents a cloud action (modified, creation)
type CloudAction string
// ActorActionCount represents the count of an actor action
type ActorActionCount struct {
Actor CloudActor
Action CloudAction
Count int
}
// setRootCausesOfDriftData sets the root causes of drift data in the markdown report
func (m *MarkdownCreator) setRootCausesOfDriftData(report *doc.MarkDownDoc) {
report.Write("# Root Causes of Drift").Writeln().Writeln()
report.Write("## Cloud Actors Causing Changes").Writeln().Writeln()
if len(m.resourcesToCloudActions) == 0 {
report.Write("No identified Cloud Actor actions.").Writeln().Writeln()
return
}
actorActions := map[CloudActor]map[CloudAction]int{}
for _, cloudActions := range m.resourcesToCloudActions {
key := "Create Resource"
detail := cloudActions["creation"]
if detail == (CloudActionDetail{}) {
key = "Modify Resource"
detail = cloudActions["modified"]
}
if actorActions[CloudActor(detail.Actor)] == nil {
actorActions[CloudActor(detail.Actor)] = map[CloudAction]int{}
}
actorActions[CloudActor(detail.Actor)][CloudAction(key)]++
}
var actorActionCounts []ActorActionCount
for actor, actions := range actorActions {
for action, count := range actions {
actorActionCounts = append(actorActionCounts, ActorActionCount{actor, action, count})
}
}
sort.Slice(actorActionCounts, func(i, j int) bool {
return actorActionCounts[i].Count > actorActionCounts[j].Count
})
report.Write("|Actor|Action|Count|\n| :---: | :---: | :---: |").Writeln()
for _, actorActionCount := range actorActionCounts {
report.Write(fmt.Sprintf("|%s", string(actorActionCount.Actor)))
report.Write(fmt.Sprintf("|%s", string(actorActionCount.Action)))
report.Write(fmt.Sprintf("|%d|", actorActionCount.Count)).Writeln()
}
report.Writeln()
}