forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_vault.go
70 lines (59 loc) · 1.36 KB
/
datasource_vault.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
package data
import (
"net/url"
"strings"
"github.com/pkg/errors"
"github.com/hairyhenderson/gomplate/vault"
)
func parseVaultParams(sourceURL *url.URL, args []string) (params map[string]interface{}, p string, err error) {
p = sourceURL.Path
params = make(map[string]interface{})
for key, val := range sourceURL.Query() {
params[key] = strings.Join(val, " ")
}
if len(args) == 1 {
parsed, err := url.Parse(args[0])
if err != nil {
return nil, "", err
}
if parsed.Path != "" {
p = p + "/" + parsed.Path
}
for key, val := range parsed.Query() {
params[key] = strings.Join(val, " ")
}
}
return params, p, nil
}
func readVault(source *Source, args ...string) (data []byte, err error) {
if source.vc == nil {
source.vc, err = vault.New(source.URL)
if err != nil {
return nil, err
}
err = source.vc.Login()
if err != nil {
return nil, err
}
}
params, p, err := parseVaultParams(source.URL, args)
if err != nil {
return nil, err
}
source.mediaType = jsonMimetype
if len(params) > 0 {
data, err = source.vc.Write(p, params)
} else if strings.HasSuffix(p, "/") {
source.mediaType = jsonArrayMimetype
data, err = source.vc.List(p)
} else {
data, err = source.vc.Read(p)
}
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, errors.Errorf("no value found for path %s", p)
}
return data, nil
}