/
hiera5.go
152 lines (116 loc) · 3.03 KB
/
hiera5.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package hiera5
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"github.com/spf13/cast"
"github.com/chriskuchin/terraform-provider-hiera5/hiera5/helper"
)
type override func(h *hiera5) *hiera5
type hiera5 struct {
Config string
Scope map[string]interface{}
Merge string
}
func WithScopeOverride(scope map[string]interface{}) override {
return func(h *hiera5) *hiera5 {
if scope == nil {
return h
}
return &hiera5{
Config: h.Config,
Scope: scope,
Merge: h.Merge,
}
}
}
func handleOverrides(h *hiera5, opts ...override) *hiera5 {
override := h
for _, opt := range opts {
override = opt(override)
}
return override
}
func newHiera5(config string, scope map[string]interface{}, merge string) hiera5 {
return hiera5{
Config: config,
Scope: scope,
Merge: merge,
}
}
func (h *hiera5) lookup(ctx context.Context, key string, valueType string) ([]byte, error) {
out, err := helper.Lookup(ctx, h.Config, h.Merge, key, valueType, h.Scope)
if err == nil && string(out) == "" {
return out, fmt.Errorf("key '%s' not found", key)
}
if !json.Valid(out) {
return out, fmt.Errorf("key '%s''s lookup returned invalid JSON: '%s'", key, out)
}
return out, err
}
func (h *hiera5) array(ctx context.Context, key string, opts ...override) ([]interface{}, error) {
var (
f interface{}
e []interface{}
)
out, err := handleOverrides(h, opts...).lookup(ctx, key, "Array")
if err != nil {
return nil, err
}
_ = json.Unmarshal(out, &f)
if _, ok := f.([]interface{}); ok {
for _, v := range f.([]interface{}) {
e = append(e, cast.ToString(v))
}
} else {
return nil, fmt.Errorf("key '%s' does not return a valid array", key)
}
return e, nil
}
func (h *hiera5) hash(ctx context.Context, key string, opts ...override) (map[string]interface{}, error) {
var f interface{}
e := make(map[string]interface{})
out, err := handleOverrides(h, opts...).lookup(ctx, key, "Hash")
if err != nil {
return nil, err
}
_ = json.Unmarshal(out, &f)
if _, ok := f.(map[string]interface{}); ok {
for k, v := range f.(map[string]interface{}) {
e[k] = cast.ToString(v)
}
} else {
return nil, fmt.Errorf("key '%s' does not return a valid hash", key)
}
return e, nil
}
func (h *hiera5) value(ctx context.Context, key string, opts ...override) (string, error) {
var f interface{}
out, err := handleOverrides(h, opts...).lookup(ctx, key, "")
if err != nil {
return "", err
}
_ = json.Unmarshal(out, &f)
return cast.ToString(f), nil
}
func (h *hiera5) bool(ctx context.Context, key string, opts ...override) (bool, error) {
var f interface{}
out, err := handleOverrides(h, opts...).lookup(ctx, key, "")
if err != nil {
return false, err
}
_ = json.Unmarshal(out, &f)
return cast.ToBool(f), nil
}
func (h *hiera5) json(ctx context.Context, key string, opts ...override) (string, error) {
var b bytes.Buffer
out, err := handleOverrides(h, opts...).lookup(ctx, key, "")
if err != nil {
return "", err
}
_ = json.Compact(&b, out)
out, _ = io.ReadAll(io.Reader(&b))
return string(out), nil
}