Skip to content

Commit

Permalink
resolve validation func
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirianni committed Feb 8, 2025
1 parent db48fb1 commit 73343c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
6 changes: 4 additions & 2 deletions internal/component/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package component
import (
"fmt"
"strings"

"github.com/observiq/bindplane-op-enterprise/model"
)

const (
Expand Down Expand Up @@ -38,10 +40,10 @@ func ValidateRouteType(routeType string) error {
}

// ValidateRouteComponents returns an error if the route components are invalid
func ValidateRouteComponents(components []string) []error {
func ValidateRouteComponents(components []model.ComponentPath) []error {
var errs []error
for _, c := range components {
prefix := strings.Split(c, "/")[0]
prefix := strings.Split(string(c), "/")[0]
switch prefix {
case "destinations", "processors", "connectors":
continue
Expand Down
7 changes: 4 additions & 3 deletions internal/component/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

"github.com/observiq/bindplane-op-enterprise/model"
"github.com/stretchr/testify/require"
)

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

func TestValidateRouteComponents(t *testing.T) {
cases := []struct {
components []string
components []model.ComponentPath
expected []error
}{
{[]string{"destinations", "processors", "connectors"}, nil},
{[]string{"destinations", "processors", "invalid"}, []error{fmt.Errorf("invalid route component: invalid")}},
{[]model.ComponentPath{"destinations", "processors", "connectors"}, nil},
{[]model.ComponentPath{"destinations", "processors", "invalid"}, []error{fmt.Errorf("invalid route component: invalid")}},
}

for _, c := range cases {
Expand Down
20 changes: 6 additions & 14 deletions provider/resource_configuration_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func resourceConfigurationV2() *schema.Resource {
Schema: map[string]*schema.Schema{
"telemetry_type": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: false,
Description: "The telemetry type to route. Valid route types include 'logs', 'metrics', or 'traces' 'logs+metrics', 'logs+traces', 'metrics+traces', 'logs+metrics+traces'.",
ValidateFunc: func(val any, _ string) (warns []string, errs []error) {
Expand All @@ -114,25 +114,14 @@ func resourceConfigurationV2() *schema.Resource {
}
return
},
Default: component.RouteTypeLogsMetricsTraces,
},
"components": {
Type: schema.TypeList,
Required: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of component names to route.",
ValidateFunc: func(val any, _ string) (warns []string, errs []error) {
rawComponents := val.([]any)
components := []string{}
for _, c := range rawComponents {
components = append(components, c.(string))
}
validationErrors := component.ValidateRouteComponents(components)
if len(validationErrors) > 0 {
errs = append(errs, validationErrors...)
}
return
},
},
},
},
Expand Down Expand Up @@ -310,13 +299,16 @@ func resourceConfigurationV2Create(d *schema.ResourceData, meta any) error {
routes := &model.Routes{}
if rawRoutes := sourcesRaw["route"].([]any); v != nil {
for _, r := range rawRoutes {
telemetryType := r.(map[string]any)["telemetry_type"].(string)
rawComponents := r.(map[string]any)["components"].([]any)
components := []model.ComponentPath{}
for _, c := range rawComponents {
components = append(components, model.ComponentPath(c.(string)))
}
if err := component.ValidateRouteComponents(components); err != nil {
return fmt.Errorf("validate route components: %v", err)
}

telemetryType := r.(map[string]any)["telemetry_type"].(string)
switch telemetryType {
case "logs":
routes.Logs = append(routes.Logs, model.Route{
Expand Down
32 changes: 19 additions & 13 deletions test/local/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -293,28 +293,27 @@ resource "bindplane_configuration_v2" "configuration" {

source {
name = bindplane_source.otlp.name

// Route all telemetry types to datadog
route {
telemetry_type = "logs"
components = [
"destinations/${bindplane_destination.datadog.id}"
]
}

// route logs to loki
route {
telemetry_type = "metrics"
telemetry_type = "logs"
components = [
"destinations/${bindplane_destination.datadog.id}"
"destinations/${bindplane_destination.loki.id}"
]
}

// route traces to google
route {
telemetry_type = "traces"
components = [
"destinations/${bindplane_destination.datadog.id}"
]
}
route {
telemetry_type = "logs"
components = [
"destinations/${bindplane_destination.loki.id}"
"destinations/${bindplane_destination.google.id}"
]
}
}
Expand All @@ -326,27 +325,34 @@ resource "bindplane_configuration_v2" "configuration" {
]

route {
telemetry_type = "logs"
components = [
"destinations/${bindplane_destination.custom.id}"
]
}

route {
telemetry_type = "logs"
components = [
"destinations/${bindplane_destination.google.id}"
]
}

route {
telemetry_type = "logs"
components = [
"destinations/${bindplane_destination.loki.id}"
]
}
}

source {
name = bindplane_source.host.name
route {
telemetry_type = "metrics"
components = [
"destinations/${bindplane_destination.google.id}"
]
}
}

destination {
route_id = bindplane_destination.custom.id
name = bindplane_destination.custom.name
Expand Down

0 comments on commit 73343c3

Please sign in to comment.