-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingestor.go
133 lines (110 loc) · 2.81 KB
/
ingestor.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"encoding/json"
"fmt"
"github.com/clbanning/mxj"
"github.com/ghodss/yaml"
"io/ioutil"
"os"
"reflect"
)
type InfraPayload struct {
Name string `json:"name"`
ProtocolVersion string `json:"protocol_version"`
IntegrationVersion string `json:"integration_version"`
Inventory map[string]interface{} `json:"inventory"`
}
type FileDef struct {
Inventory string
Path string
Type string
}
func check(e error) {
if e != nil {
fmt.Printf("err: %v\n", e)
panic(e)
}
}
func sanitizeLeaf(leaf interface{}) string {
marshalled, err := json.Marshal(leaf)
check(err)
return string(marshalled)
}
func sanitizeMap(jdata interface{}) interface{} {
jmap := reflect.ValueOf(jdata)
sanitized := make(map[string]interface{})
for _, key := range jmap.MapKeys() {
jval := jmap.MapIndex(key)
sanitized[key.String()] = sanitizeLeaf(jval.Elem().Interface())
}
return sanitized
}
//Uses slice index as map key
func sanitizeSlice(jdata interface{}) interface{} {
jslice := reflect.ValueOf(jdata)
sanitized := make(map[int]interface{}, jslice.Len())
for i := 0; i < jslice.Len(); i++ {
sanitized[i] = sanitizeLeaf(jslice.Index(i).Interface())
}
return sanitized
}
func extractConfs(fileDef FileDef) interface{} {
fdata, err := ioutil.ReadFile(fileDef.Path)
check(err)
var invdata []byte
switch ftype := fileDef.Type; ftype {
case "text":
//No structure - return raw text
sanitized := make(map[string]string, 1)
sanitized["data"] = string(fdata)
return sanitized
case "yaml":
invdata, err = yaml.YAMLToJSON(fdata)
check(err)
case "json":
invdata = fdata
case "xml":
xdata, err := mxj.NewMapXml(fdata)
check(err)
invdata, err = xdata.Json()
check(err)
//TODO
//case "properties":
default:
fmt.Printf("Unsupported file adapter type %s:", ftype)
}
var jdata interface{}
err = json.Unmarshal(invdata, &jdata)
check(err)
// Encode any leaf maps as scalars to adhere to infra sdk spec
var sanitized interface{}
switch reflect.TypeOf(jdata).Kind() {
case reflect.Map:
sanitized = sanitizeMap(jdata)
case reflect.Slice:
sanitized = sanitizeSlice(jdata)
default:
sanitized = sanitizeLeaf(jdata)
}
return sanitized
}
func main() {
conFile := os.Getenv("NR_INGEST_CONF")
confData, err := ioutil.ReadFile(conFile)
check(err)
confs := []FileDef{}
err = yaml.Unmarshal([]byte(confData), &confs)
check(err)
var payload InfraPayload
payload.Name = "haus.chris.nringest"
payload.ProtocolVersion = "1"
payload.IntegrationVersion = "1.0.0"
payload.Inventory = make(map[string]interface{})
for _, fileDef := range confs {
confs := extractConfs(fileDef)
payload.Inventory[fileDef.Inventory] = confs
}
payloadJson, err := json.Marshal(payload)
check(err)
fmt.Printf(string(payloadJson))
}