forked from snyk/driftctl-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (70 loc) · 2.51 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/newrelic/go-agent"
"io/ioutil"
"log"
"os"
"time"
)
var newRelicApp newrelic.Application
type DrifctlSummary struct {
TotalResources int `json:"total_resources"`
TotalChanged int `json:"total_changed"`
TotalUnmanaged int `json:"total_unmanaged"`
TotalMissing int `json:"total_missing"`
TotalManaged int `json:"total_managed"`
}
func main() {
newRelicEventType := os.Getenv("INPUT_EVENT_TYPE_NAME")
newRelicLicenseKey := os.Getenv("INPUT_NEW_RELIC_LICENCE_KEY")
github_repository := os.Getenv("INPUT_GITHUB_REPOSITORY")
github_run_id := os.Getenv("INPUT_GITHUB_RUN_ID")
tagKey := os.Getenv("TAG_KEY")
tagValue := os.Getenv("TAG_VALUE")
stage := os.Getenv("INPUT_ENV")
summary := parseSummaryDrifctlOutput(os.Args[1])
app := initNewRelicClient(newRelicLicenseKey)
gha_url := fmt.Sprintf("https://github.com/%s/actions/runs/%s", github_repository, github_run_id)
if err := app.RecordCustomEvent(newRelicEventType,map[string]interface{}{
"env": stage,
tagKey: tagValue,
"total_resources": summary.TotalResources,
"total_changed": summary.TotalChanged,
"total_unmanaged" : summary.TotalUnmanaged,
"total_managed" : summary.TotalManaged,
"total_missing" : summary.TotalMissing,
"ghaUrl": gha_url,
}); err != nil {
log.Println("error in creating New Relic custom event: ", err)
return
}
app.Shutdown(5 * time.Second)
fmt.Println(fmt.Sprintf(`::set-output name=total_resources::%s`, summary.TotalResources))
fmt.Println(fmt.Sprintf(`::set-output name=total_changed::%s`, summary.TotalChanged))
fmt.Println(fmt.Sprintf(`::set-output name=total_unmanaged::%s`, summary.TotalUnmanaged))
fmt.Println(fmt.Sprintf(`::set-output name=total_managed::%s`, summary.TotalManaged))
fmt.Println(fmt.Sprintf(`::set-output name=total_missing::%s`, summary.TotalMissing))
}
func parseSummaryDrifctlOutput(fileName string) DrifctlSummary {
jsonFile, err := os.Open(fileName)
if err != nil {
log.Println("Unable to read Drifctl Output Summary: ", err)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
var summary DrifctlSummary
json.Unmarshal(byteValue, &summary)
defer jsonFile.Close()
return summary
}
func initNewRelicClient(newRelicLicenseKey string) newrelic.Application {
config := newrelic.NewConfig("GithubActionsMonitoring", newRelicLicenseKey)
newRelicApp, err := newrelic.NewApplication(config)
if err != nil {
log.Println("error in creating new relic application: ", err)
panic(err)
}
newRelicApp.WaitForConnection(5 * time.Second)
return newRelicApp
}