forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
constructor.go
203 lines (181 loc) · 7.32 KB
/
constructor.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 2014 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package input
import (
"bytes"
"encoding/json"
"sort"
"strings"
"github.com/Jeffail/benthos/lib/input/reader"
"github.com/Jeffail/benthos/lib/pipeline"
"github.com/Jeffail/benthos/lib/processor"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
// typeSpec is a constructor and a usage description for each input type.
type typeSpec struct {
brokerConstructor func(
conf Config,
mgr types.Manager,
log log.Modular,
stats metrics.Type,
pipelineConstructors ...pipeline.ConstructorFunc,
) (Type, error)
constructor func(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error)
description string
}
var constructors = map[string]typeSpec{}
//------------------------------------------------------------------------------
// Config is the all encompassing configuration struct for all input types. Note
// that some configs are empty structs, as the type has no optional values but
// we want to list it as an option.
type Config struct {
Type string `json:"type" yaml:"type"`
AmazonS3 reader.AmazonS3Config `json:"amazon_s3" yaml:"amazon_s3"`
AmazonSQS reader.AmazonSQSConfig `json:"amazon_sqs" yaml:"amazon_sqs"`
AMQP reader.AMQPConfig `json:"amqp" yaml:"amqp"`
Dynamic DynamicConfig `json:"dynamic" yaml:"dynamic"`
FanIn FanInConfig `json:"fan_in" yaml:"fan_in"`
File FileConfig `json:"file" yaml:"file"`
HTTPClient HTTPClientConfig `json:"http_client" yaml:"http_client"`
HTTPServer HTTPServerConfig `json:"http_server" yaml:"http_server"`
Kafka reader.KafkaConfig `json:"kafka" yaml:"kafka"`
KafkaBalanced reader.KafkaBalancedConfig `json:"kafka_balanced" yaml:"kafka_balanced"`
NATS reader.NATSConfig `json:"nats" yaml:"nats"`
NATSStream reader.NATSStreamConfig `json:"nats_stream" yaml:"nats_stream"`
NSQ reader.NSQConfig `json:"nsq" yaml:"nsq"`
RedisList reader.RedisListConfig `json:"redis_list" yaml:"redis_list"`
RedisPubSub reader.RedisPubSubConfig `json:"redis_pubsub" yaml:"redis_pubsub"`
ScaleProto reader.ScaleProtoConfig `json:"scalability_protocols" yaml:"scalability_protocols"`
STDIN STDINConfig `json:"stdin" yaml:"stdin"`
ZMQ4 *ZMQ4Config `json:"zmq4,omitempty" yaml:"zmq4,omitempty"`
Processors []processor.Config `json:"processors" yaml:"processors"`
}
// NewConfig returns a configuration struct fully populated with default values.
func NewConfig() Config {
return Config{
Type: "stdin",
AmazonS3: reader.NewAmazonS3Config(),
AmazonSQS: reader.NewAmazonSQSConfig(),
AMQP: reader.NewAMQPConfig(),
Dynamic: NewDynamicConfig(),
FanIn: NewFanInConfig(),
File: NewFileConfig(),
HTTPClient: NewHTTPClientConfig(),
HTTPServer: NewHTTPServerConfig(),
Kafka: reader.NewKafkaConfig(),
KafkaBalanced: reader.NewKafkaBalancedConfig(),
NATS: reader.NewNATSConfig(),
NATSStream: reader.NewNATSStreamConfig(),
NSQ: reader.NewNSQConfig(),
RedisList: reader.NewRedisListConfig(),
RedisPubSub: reader.NewRedisPubSubConfig(),
ScaleProto: reader.NewScaleProtoConfig(),
STDIN: NewSTDINConfig(),
ZMQ4: NewZMQ4Config(),
Processors: []processor.Config{processor.NewConfig()},
}
}
//------------------------------------------------------------------------------
// UnmarshalJSON ensures that when parsing configs that are in a map or slice
// the default values are still applied.
func (c *Config) UnmarshalJSON(bytes []byte) error {
type confAlias Config
aliased := confAlias(NewConfig())
if err := json.Unmarshal(bytes, &aliased); err != nil {
return err
}
*c = Config(aliased)
return nil
}
// UnmarshalYAML ensures that when parsing configs that are in a map or slice
// the default values are still applied.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type confAlias Config
aliased := confAlias(NewConfig())
if err := unmarshal(&aliased); err != nil {
return err
}
*c = Config(aliased)
return nil
}
//------------------------------------------------------------------------------
// Descriptions returns a formatted string of descriptions for each type.
func Descriptions() string {
// Order our input types alphabetically
names := []string{}
for name := range constructors {
names = append(names, name)
}
sort.Strings(names)
buf := bytes.Buffer{}
buf.WriteString("INPUTS\n")
buf.WriteString(strings.Repeat("=", 6))
buf.WriteString("\n\n")
buf.WriteString("This document has been generated with `benthos --list-inputs`.")
buf.WriteString("\n\n")
// Append each description
for i, name := range names {
buf.WriteString("## ")
buf.WriteString("`" + name + "`")
buf.WriteString("\n")
buf.WriteString(constructors[name].description)
if i != (len(names) - 1) {
buf.WriteString("\n\n")
}
}
return buf.String()
}
// New creates an input type based on an input configuration.
func New(
conf Config,
mgr types.Manager,
log log.Modular,
stats metrics.Type,
pipelines ...pipeline.ConstructorFunc,
) (Type, error) {
if len(conf.Processors) > 0 {
pipelines = append([]pipeline.ConstructorFunc{func() (pipeline.Type, error) {
processors := make([]processor.Type, len(conf.Processors))
for i, procConf := range conf.Processors {
var err error
processors[i], err = processor.New(procConf, log.NewModule("."+conf.Type), stats)
if err != nil {
return nil, err
}
}
return pipeline.NewProcessor(log, stats, processors...), nil
}}, pipelines...)
}
if c, ok := constructors[conf.Type]; ok {
if c.brokerConstructor != nil {
return c.brokerConstructor(conf, mgr, log, stats, pipelines...)
}
input, err := c.constructor(conf, mgr, log, stats)
for err != nil {
return nil, err
}
return WrapWithPipelines(input, pipelines...)
}
return nil, types.ErrInvalidInputType
}
//------------------------------------------------------------------------------