Skip to content

Commit e81c057

Browse files
committed
input validation
1 parent d4785b2 commit e81c057

File tree

3 files changed

+137
-7
lines changed

3 files changed

+137
-7
lines changed

internal/component/route.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package component
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
const (
9+
// Logs route type
10+
RouteTypeLogs = "logs"
11+
12+
// Metrics route type
13+
RouteTypeMetrics = "metrics"
14+
15+
// Traces route type
16+
RouteTypeTraces = "traces"
17+
18+
// Logs + Metrics route type
19+
RouteTypeLogsMetrics = "logs+metrics"
20+
21+
// Logs + Traces route type
22+
RouteTypeLogsTraces = "logs+traces"
23+
24+
// Metrics + Traces route type
25+
RouteTypeMetricsTraces = "metrics+traces"
26+
27+
// Logs + Metrics + Traces route type
28+
RouteTypeLogsMetricsTraces = "logs+metrics+traces"
29+
)
30+
31+
// ValidateRouteType returns an error if the route type is invalid
32+
func ValidateRouteType(routeType string) error {
33+
switch routeType {
34+
case RouteTypeLogs, RouteTypeMetrics, RouteTypeTraces, RouteTypeLogsMetrics, RouteTypeLogsTraces, RouteTypeMetricsTraces, RouteTypeLogsMetricsTraces:
35+
return nil
36+
}
37+
return fmt.Errorf("invalid route type: %s", routeType)
38+
}
39+
40+
// ValidateRouteComponents returns an error if the route components are invalid
41+
func ValidateRouteComponents(components []string) []error {
42+
var errs []error
43+
for _, c := range components {
44+
prefix := strings.Split(c, "/")[0]
45+
switch prefix {
46+
case "destinations", "processors", "connectors":
47+
continue
48+
default:
49+
errs = append(errs, fmt.Errorf("invalid route component: %s", c))
50+
}
51+
}
52+
return errs
53+
}

internal/component/route_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package component
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestValidateRouteType(t *testing.T) {
12+
cases := []struct {
13+
routeType string
14+
expected error
15+
}{
16+
{"logs", nil},
17+
{"metrics", nil},
18+
{"traces", nil},
19+
{"logs+metrics", nil},
20+
{"logs+traces", nil},
21+
{"metrics+traces", nil},
22+
{"logs+metrics+traces", nil},
23+
{"invalid", errors.New("invalid route type: invalid")},
24+
}
25+
26+
for _, c := range cases {
27+
err := ValidateRouteType(c.routeType)
28+
if c.expected != nil {
29+
require.Error(t, err)
30+
} else {
31+
require.NoError(t, err)
32+
}
33+
}
34+
}
35+
36+
func TestValidateRouteComponents(t *testing.T) {
37+
cases := []struct {
38+
components []string
39+
expected []error
40+
}{
41+
{[]string{"destinations", "processors", "connectors"}, nil},
42+
{[]string{"destinations", "processors", "invalid"}, []error{fmt.Errorf("invalid route component: invalid")}},
43+
}
44+
45+
for _, c := range cases {
46+
errs := ValidateRouteComponents(c.components)
47+
if len(c.expected) == 0 {
48+
require.Equal(t, c.expected, errs)
49+
}
50+
require.Len(t, errs, len(c.expected))
51+
}
52+
}

provider/resource_configuration_v2.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/observiq/bindplane-op-enterprise/model"
2525
"github.com/observiq/terraform-provider-bindplane/client"
26+
"github.com/observiq/terraform-provider-bindplane/internal/component"
2627
"github.com/observiq/terraform-provider-bindplane/internal/configuration"
2728
"github.com/observiq/terraform-provider-bindplane/internal/maputil"
2829
"github.com/observiq/terraform-provider-bindplane/internal/resource"
@@ -101,19 +102,15 @@ func resourceConfigurationV2() *schema.Resource {
101102
ForceNew: false,
102103
Elem: &schema.Resource{
103104
Schema: map[string]*schema.Schema{
104-
// TODO(jsirianni): Could be plural with
105-
// list or array. Provider would handle combining
106-
// them into string.
107105
"telemetry_type": {
108106
Type: schema.TypeString,
109107
Required: true,
110108
ForceNew: false,
111-
Description: "The telemetry type to route, such as 'logs', 'metrics', or 'traces'.",
109+
Description: "The telemetry type to route. Valid route types include 'logs', 'metrics', or 'traces' 'logs+metrics', 'logs+traces', 'metrics+traces', 'logs+metrics+traces'.",
112110
ValidateFunc: func(val any, _ string) (warns []string, errs []error) {
113-
// Ensure one of logs, metrics, traces
114111
telemetryType := val.(string)
115-
if telemetryType != "logs" && telemetryType != "metrics" && telemetryType != "traces" {
116-
errs = append(errs, errors.New("telemetry_type must be one of 'logs', 'metrics', or 'traces'"))
112+
if err := component.ValidateRouteType(telemetryType); err != nil {
113+
errs = append(errs, err)
117114
}
118115
return
119116
},
@@ -124,6 +121,18 @@ func resourceConfigurationV2() *schema.Resource {
124121
ForceNew: false,
125122
Elem: &schema.Schema{Type: schema.TypeString},
126123
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+
},
127136
},
128137
},
129138
},
@@ -321,6 +330,22 @@ func resourceConfigurationV2Create(d *schema.ResourceData, meta any) error {
321330
routes.Traces = append(routes.Traces, model.Route{
322331
Components: components,
323332
})
333+
case "logs+metrics":
334+
routes.LogsMetrics = append(routes.LogsMetrics, model.Route{
335+
Components: components,
336+
})
337+
case "logs+traces":
338+
routes.LogsTraces = append(routes.LogsTraces, model.Route{
339+
Components: components,
340+
})
341+
case "metrics+traces":
342+
routes.MetricsTraces = append(routes.MetricsTraces, model.Route{
343+
Components: components,
344+
})
345+
case "logs+metrics+traces":
346+
routes.LogsMetricsTraces = append(routes.LogsMetricsTraces, model.Route{
347+
Components: components,
348+
})
324349
}
325350
}
326351
}

0 commit comments

Comments
 (0)