-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconfig.go
More file actions
145 lines (123 loc) · 3.45 KB
/
config.go
File metadata and controls
145 lines (123 loc) · 3.45 KB
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
package plugin
import (
"encoding/json"
"fmt"
"strings"
"github.com/observiq/stanza/errors"
"github.com/observiq/stanza/operator"
"github.com/observiq/stanza/operator/helper"
"github.com/observiq/stanza/pipeline"
yaml "gopkg.in/yaml.v2"
)
// Enforce that Config implements operator.Builder
var _ operator.Builder = (*Config)(nil)
// Config is the config values for the plugin
type Config struct {
helper.WriterConfig
Plugin *Plugin `json:"-" yaml:"-"`
Parameters map[string]interface{} `json:",squash" yaml:",squash"`
}
// Build implements operator.MultiBuilder
func (c *Config) Build(bc operator.BuildContext) ([]operator.Operator, error) {
if bc.PluginDepth > 10 {
return nil, errors.NewError("reached max plugin depth", "ensure that there are no recursive dependencies in plugins")
}
params := c.getRenderParams(bc)
pipelineConfigBytes, err := c.Plugin.Render(params)
if err != nil {
return nil, err
}
var pipelineConfig struct {
Pipeline pipeline.Config
}
if err := yaml.Unmarshal(pipelineConfigBytes, &pipelineConfig); err != nil {
return nil, err
}
nbc := bc.WithSubNamespace(c.ID()).WithIncrementedDepth()
return pipelineConfig.Pipeline.BuildOperators(nbc)
}
func (c *Config) getRenderParams(bc operator.BuildContext) map[string]interface{} {
// Copy the parameters to avoid mutating them
params := map[string]interface{}{}
for k, v := range c.Parameters {
params[k] = v
}
// Add ID and output to params
params["input"] = bc.PrependNamespace(c.ID())
params["id"] = c.ID()
params["output"] = c.yamlOutputs(bc)
return params
}
func (c *Config) yamlOutputs(bc operator.BuildContext) string {
outputIDs := c.OutputIDs
if len(outputIDs) == 0 {
outputIDs = bc.DefaultOutputIDs
}
namespacedOutputs := make([]string, 0, len(outputIDs))
for _, outputID := range outputIDs {
namespacedOutputs = append(namespacedOutputs, bc.PrependNamespace(outputID))
}
return fmt.Sprintf("[%s]", strings.Join(namespacedOutputs, ","))
}
// UnmarshalJSON unmarshals JSON
func (c *Config) UnmarshalJSON(raw []byte) error {
var m map[string]interface{}
if err := json.Unmarshal(raw, &m); err != nil {
return err
}
return c.unmarshalMap(m)
}
// UnmarshalYAML unmarshals YAML
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
var m map[string]interface{}
if err := unmarshal(&m); err != nil {
return err
}
return c.unmarshalMap(m)
}
func (c *Config) unmarshalMap(m map[string]interface{}) error {
if id, ok := m["id"]; ok {
if idString, ok := id.(string); ok {
c.OperatorID = idString
delete(m, "id")
}
}
if t, ok := m["type"]; ok {
if typeString, ok := t.(string); ok {
c.OperatorType = typeString
delete(m, "type")
} else {
return fmt.Errorf("invalid type %T for operator type", t)
}
} else {
return fmt.Errorf("missing required field 'type'")
}
if output, ok := m["output"]; ok {
outputIDs, err := helper.NewOutputIDsFromInterface(output)
if err != nil {
return err
}
c.OutputIDs = outputIDs
delete(m, "output")
}
c.Parameters = m
return nil
}
// MarshalYAML marshals YAML
func (c Config) MarshalYAML() (interface{}, error) {
return c.toMap(), nil
}
// MarshalJSON marshals JSON
func (c Config) MarshalJSON() ([]byte, error) {
return json.Marshal(c.toMap())
}
func (c Config) toMap() map[string]interface{} {
m := make(map[string]interface{})
for k, v := range c.Parameters {
m[k] = v
}
m["id"] = c.ID()
m["type"] = c.Type()
m["output"] = c.OutputIDs
return m
}