mirror of
https://github.com/turbot/steampipe.git
synced 2026-04-13 22:00:05 -04:00
Add JSON template for check export. Closes #1337
This commit is contained in:
12
cmd/check.go
12
cmd/check.go
@@ -6,7 +6,6 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -470,17 +469,6 @@ func parseExportArg(arg string) (formatter controldisplay.Formatter, targetFileN
|
||||
return
|
||||
}
|
||||
|
||||
// no defined formatter
|
||||
// Tactical fix to resolve CSV and JSON filename exports
|
||||
// We need this till we build out the templates for CSV and JSON
|
||||
ext := filepath.Ext(arg)
|
||||
if ext == constants.CsvExtension {
|
||||
return controldisplay.CSVFormatter{}, arg, nil
|
||||
}
|
||||
if ext == constants.JsonExtension && !strings.HasSuffix(arg, "asff.json") {
|
||||
return controldisplay.JSONFormatter{}, arg, nil
|
||||
}
|
||||
|
||||
return controldisplay.GetTemplateExportFormatter(arg, true)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,14 +20,12 @@ type FormatterMap map[string]Formatter
|
||||
var outputFormatters FormatterMap = FormatterMap{
|
||||
constants.CheckOutputFormatNone: &NullFormatter{},
|
||||
constants.CheckOutputFormatCSV: &CSVFormatter{},
|
||||
constants.CheckOutputFormatJSON: &JSONFormatter{},
|
||||
constants.CheckOutputFormatText: &TextFormatter{},
|
||||
constants.CheckOutputFormatBrief: &TextFormatter{},
|
||||
}
|
||||
|
||||
var exportFormatters FormatterMap = FormatterMap{
|
||||
constants.CheckOutputFormatCSV: &CSVFormatter{},
|
||||
constants.CheckOutputFormatJSON: &JSONFormatter{},
|
||||
constants.CheckOutputFormatCSV: &CSVFormatter{},
|
||||
}
|
||||
|
||||
type CheckExportTarget struct {
|
||||
@@ -80,7 +78,7 @@ func (j *NullFormatter) FileExtension() string {
|
||||
}
|
||||
|
||||
func templateFuncs() template.FuncMap {
|
||||
useFromSprigMap := []string{"upper", "toJson", "quote", "dict", "add", "now"}
|
||||
useFromSprigMap := []string{"upper", "toJson", "quote", "dict", "add", "now", "toPrettyJson"}
|
||||
|
||||
var funcs template.FuncMap = template.FuncMap{}
|
||||
sprigMap := sprig.TxtFuncMap()
|
||||
|
||||
3
control/controldisplay/templates/json/output.tmpl
Normal file
3
control/controldisplay/templates/json/output.tmpl
Normal file
@@ -0,0 +1,3 @@
|
||||
{{ define "output" -}}
|
||||
{{ toPrettyJson .Data.Root }}
|
||||
{{ end }}
|
||||
@@ -4,255 +4,255 @@
|
||||
"description": "",
|
||||
"tags": {},
|
||||
"summary": {
|
||||
"status": {
|
||||
"alarm": 5,
|
||||
"ok": 10,
|
||||
"info": 3,
|
||||
"skip": 1,
|
||||
"error": 2
|
||||
}
|
||||
"status": {
|
||||
"alarm": 5,
|
||||
"ok": 10,
|
||||
"info": 3,
|
||||
"skip": 1,
|
||||
"error": 2
|
||||
}
|
||||
},
|
||||
"groups": [],
|
||||
"controls": [
|
||||
{
|
||||
"results": [
|
||||
{
|
||||
"reason": "Resource has some error",
|
||||
"resource": "steampipe",
|
||||
"status": "error",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "16"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource has some error",
|
||||
"resource": "steampipe",
|
||||
"status": "error",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "17"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "11"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "12"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "13"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "14"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "15"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Information",
|
||||
"resource": "steampipe",
|
||||
"status": "info",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "19"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Information",
|
||||
"resource": "steampipe",
|
||||
"status": "info",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "20"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Information",
|
||||
"resource": "steampipe",
|
||||
"status": "info",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "21"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "1"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "2"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "3"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "4"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "5"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "7"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "8"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "9"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "10"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource is skipped",
|
||||
"resource": "steampipe",
|
||||
"status": "skip",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "18"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"control_id": "control.sample_control_mixed_results_1",
|
||||
"description": "Sample control that returns 10 OK, 5 ALARM, 2 ERROR, 1 SKIP and 3 INFO",
|
||||
"severity": "high",
|
||||
"tags": {},
|
||||
"title": "Sample control with all possible statuses(severity=high)"
|
||||
}
|
||||
{
|
||||
"results": [
|
||||
{
|
||||
"reason": "Resource has some error",
|
||||
"resource": "steampipe",
|
||||
"status": "error",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "16"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource has some error",
|
||||
"resource": "steampipe",
|
||||
"status": "error",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "17"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "11"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "12"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "13"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "14"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource does not satisfy condition",
|
||||
"resource": "steampipe",
|
||||
"status": "alarm",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "15"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Information",
|
||||
"resource": "steampipe",
|
||||
"status": "info",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "19"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Information",
|
||||
"resource": "steampipe",
|
||||
"status": "info",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "20"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Information",
|
||||
"resource": "steampipe",
|
||||
"status": "info",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "21"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "1"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "2"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "3"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "4"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "5"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "7"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "8"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "9"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource satisfies condition",
|
||||
"resource": "steampipe",
|
||||
"status": "ok",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "10"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Resource is skipped",
|
||||
"resource": "steampipe",
|
||||
"status": "skip",
|
||||
"dimensions": [
|
||||
{
|
||||
"key": "id",
|
||||
"value": "18"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"control_id": "control.sample_control_mixed_results_1",
|
||||
"description": "Sample control that returns 10 OK, 5 ALARM, 2 ERROR, 1 SKIP and 3 INFO",
|
||||
"severity": "high",
|
||||
"tags": {},
|
||||
"title": "Sample control with all possible statuses(severity=high)"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,9 +80,9 @@ load "$LIB_BATS_SUPPORT/load.bash"
|
||||
|
||||
@test "steampipe check - export json" {
|
||||
cd $CONTROL_RENDERING_TEST_MOD
|
||||
run steampipe check control.sample_control_mixed_results_1 --export=json --progress=false
|
||||
assert_equal "$(cat *.json)" "$(cat $TEST_DATA_DIR/expected_check_json.json)"
|
||||
rm -f *.json
|
||||
run steampipe check control.sample_control_mixed_results_1 --export test.json --progress=false
|
||||
assert_equal "$(cat test.json)" "$(cat $TEST_DATA_DIR/expected_check_json.json)"
|
||||
rm -f test.json
|
||||
cd -
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user