Skip to content

Commit 932119e

Browse files
committed
resolve validation func
1 parent e81c057 commit 932119e

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

internal/component/route.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package component
33
import (
44
"fmt"
55
"strings"
6+
7+
"github.com/observiq/bindplane-op-enterprise/model"
68
)
79

810
const (
@@ -38,10 +40,10 @@ func ValidateRouteType(routeType string) error {
3840
}
3941

4042
// ValidateRouteComponents returns an error if the route components are invalid
41-
func ValidateRouteComponents(components []string) []error {
43+
func ValidateRouteComponents(components []model.ComponentPath) []error {
4244
var errs []error
4345
for _, c := range components {
44-
prefix := strings.Split(c, "/")[0]
46+
prefix := strings.Split(string(c), "/")[0]
4547
switch prefix {
4648
case "destinations", "processors", "connectors":
4749
continue

internal/component/route_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8+
"github.com/observiq/bindplane-op-enterprise/model"
89
"github.com/stretchr/testify/require"
910
)
1011

@@ -35,11 +36,11 @@ func TestValidateRouteType(t *testing.T) {
3536

3637
func TestValidateRouteComponents(t *testing.T) {
3738
cases := []struct {
38-
components []string
39+
components []model.ComponentPath
3940
expected []error
4041
}{
41-
{[]string{"destinations", "processors", "connectors"}, nil},
42-
{[]string{"destinations", "processors", "invalid"}, []error{fmt.Errorf("invalid route component: invalid")}},
42+
{[]model.ComponentPath{"destinations", "processors", "connectors"}, nil},
43+
{[]model.ComponentPath{"destinations", "processors", "invalid"}, []error{fmt.Errorf("invalid route component: invalid")}},
4344
}
4445

4546
for _, c := range cases {

provider/resource_configuration_v2.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func resourceConfigurationV2() *schema.Resource {
104104
Schema: map[string]*schema.Schema{
105105
"telemetry_type": {
106106
Type: schema.TypeString,
107-
Required: true,
107+
Optional: true,
108108
ForceNew: false,
109109
Description: "The telemetry type to route. Valid route types include 'logs', 'metrics', or 'traces' 'logs+metrics', 'logs+traces', 'metrics+traces', 'logs+metrics+traces'.",
110110
ValidateFunc: func(val any, _ string) (warns []string, errs []error) {
@@ -114,25 +114,14 @@ func resourceConfigurationV2() *schema.Resource {
114114
}
115115
return
116116
},
117+
Default: component.RouteTypeLogsMetricsTraces,
117118
},
118119
"components": {
119120
Type: schema.TypeList,
120121
Required: true,
121122
ForceNew: false,
122123
Elem: &schema.Schema{Type: schema.TypeString},
123124
Description: "List of component names to route.",
124-
ValidateFunc: func(val any, _ string) (warns []string, errs []error) {
125-
rawComponents := val.([]any)
126-
components := []string{}
127-
for _, c := range rawComponents {
128-
components = append(components, c.(string))
129-
}
130-
validationErrors := component.ValidateRouteComponents(components)
131-
if len(validationErrors) > 0 {
132-
errs = append(errs, validationErrors...)
133-
}
134-
return
135-
},
136125
},
137126
},
138127
},
@@ -310,13 +299,16 @@ func resourceConfigurationV2Create(d *schema.ResourceData, meta any) error {
310299
routes := &model.Routes{}
311300
if rawRoutes := sourcesRaw["route"].([]any); v != nil {
312301
for _, r := range rawRoutes {
313-
telemetryType := r.(map[string]any)["telemetry_type"].(string)
314302
rawComponents := r.(map[string]any)["components"].([]any)
315303
components := []model.ComponentPath{}
316304
for _, c := range rawComponents {
317305
components = append(components, model.ComponentPath(c.(string)))
318306
}
307+
if err := component.ValidateRouteComponents(components); err != nil {
308+
return fmt.Errorf("validate route components: %v", err)
309+
}
319310

311+
telemetryType := r.(map[string]any)["telemetry_type"].(string)
320312
switch telemetryType {
321313
case "logs":
322314
routes.Logs = append(routes.Logs, model.Route{

test/local/main.tf

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,28 +293,27 @@ resource "bindplane_configuration_v2" "configuration" {
293293

294294
source {
295295
name = bindplane_source.otlp.name
296+
297+
// Route all telemetry types to datadog
296298
route {
297-
telemetry_type = "logs"
298299
components = [
299300
"destinations/${bindplane_destination.datadog.id}"
300301
]
301302
}
303+
304+
// route logs to loki
302305
route {
303-
telemetry_type = "metrics"
306+
telemetry_type = "logs"
304307
components = [
305-
"destinations/${bindplane_destination.datadog.id}"
308+
"destinations/${bindplane_destination.loki.id}"
306309
]
307310
}
311+
312+
// route traces to google
308313
route {
309314
telemetry_type = "traces"
310315
components = [
311-
"destinations/${bindplane_destination.datadog.id}"
312-
]
313-
}
314-
route {
315-
telemetry_type = "logs"
316-
components = [
317-
"destinations/${bindplane_destination.loki.id}"
316+
"destinations/${bindplane_destination.google.id}"
318317
]
319318
}
320319
}
@@ -326,27 +325,34 @@ resource "bindplane_configuration_v2" "configuration" {
326325
]
327326

328327
route {
329-
telemetry_type = "logs"
330328
components = [
331329
"destinations/${bindplane_destination.custom.id}"
332330
]
333331
}
334332

335333
route {
336-
telemetry_type = "logs"
337334
components = [
338335
"destinations/${bindplane_destination.google.id}"
339336
]
340337
}
341338

342339
route {
343-
telemetry_type = "logs"
344340
components = [
345341
"destinations/${bindplane_destination.loki.id}"
346342
]
347343
}
348344
}
349345

346+
source {
347+
name = bindplane_source.host.name
348+
route {
349+
telemetry_type = "metrics"
350+
components = [
351+
"destinations/${bindplane_destination.google.id}"
352+
]
353+
}
354+
}
355+
350356
destination {
351357
route_id = bindplane_destination.custom.id
352358
name = bindplane_destination.custom.name

0 commit comments

Comments
 (0)