-
Notifications
You must be signed in to change notification settings - Fork 24
/
json.go
93 lines (80 loc) · 2.54 KB
/
json.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
package exporter
import (
"bytes"
"context"
"encoding/json"
"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/packaging"
"github.com/jetstack/preflight/pkg/results"
"github.com/jetstack/preflight/pkg/version"
)
// JSONExporter is an Exporter that outputs the results enriched with metadata in a JSON format
type JSONExporter struct {
}
// NewJSONExporter creates a new JSONExporter
func NewJSONExporter() *JSONExporter {
return &JSONExporter{}
}
// Export writes a report with the evaluated results enriched with the metadata from the policy manifest in a JSON format.
func (e *JSONExporter) Export(ctx context.Context, policyManifest *packaging.PolicyManifest, intermediateJSON []byte, rc *results.ResultCollection) (*bytes.Buffer, error) {
report := api.Report{
// TODO: we are omitting ID, Timestamp and Cluster for now, but it will get fixed with #1
PreflightVersion: version.PreflightVersion,
Package: policyManifest.ID,
PackageInformation: api.PackageInformation{
Namespace: policyManifest.Namespace,
ID: policyManifest.ID,
Version: policyManifest.PackageVersion,
},
Name: policyManifest.Name,
Description: policyManifest.Description,
Sections: make([]api.ReportSection, len(policyManifest.Sections)),
}
for idxSection, section := range policyManifest.Sections {
report.Sections[idxSection] = api.ReportSection{
ID: section.ID,
Name: section.Name,
Description: section.Description,
Rules: make([]api.ReportRule, len(section.Rules)),
}
for idxRule, rule := range section.Rules {
links := make([]string, len(rule.Links))
copy(links, rule.Links)
results := rc.ByID()
result := results[ruleToResult(rule.ID)]
var value interface{}
success := false
missing := false
switch {
case result == nil:
missing = true
case result.IsFailureState():
success = false
case result.IsSuccessState():
success = true
default:
value = result.Value
}
report.Sections[idxSection].Rules[idxRule] = api.ReportRule{
ID: rule.ID,
Name: rule.Name,
Description: rule.Description,
Manual: rule.Manual,
Remediation: rule.Remediation,
Links: links,
Success: success,
Value: value,
Missing: missing,
}
}
}
b, err := json.Marshal(report)
if err != nil {
return nil, err
}
return bytes.NewBuffer(b), nil
}
// FileExtension returns the file extension for this exporter's format
func (e *JSONExporter) FileExtension() string {
return ".json"
}