Skip to content

Commit d64e5b1

Browse files
authored
fix: Read rollout options from Bindplane (#191)
1 parent a2e731b commit d64e5b1

File tree

7 files changed

+55
-311
lines changed

7 files changed

+55
-311
lines changed

example/destination_loki.tf

Lines changed: 0 additions & 79 deletions
This file was deleted.

internal/parameter/parameter.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
package parameter
1717

1818
import (
19-
"encoding/json"
2019
"fmt"
2120

21+
jsoniter "github.com/json-iterator/go"
2222
"github.com/observiq/bindplane-op-enterprise/model"
2323
)
2424

2525
// StringToParameter unmarshals serialized json parameters
2626
// to a list of Bindplane parameters.
2727
func StringToParameter(s string) ([]model.Parameter, error) {
2828
parameters := []model.Parameter{}
29-
if err := json.Unmarshal([]byte(s), &parameters); err != nil {
29+
if err := jsoniter.Unmarshal([]byte(s), &parameters); err != nil {
3030
return nil, fmt.Errorf("failed to unmarshal parameters '%s': %w", s, err)
3131
}
3232

@@ -44,7 +44,7 @@ func ParametersToString(p []model.Parameter) (string, error) {
4444
return "", nil
4545
}
4646

47-
paramBytes, err := json.Marshal(p)
47+
paramBytes, err := jsoniter.Marshal(p)
4848
if err != nil {
4949
return "", fmt.Errorf("failed to marshal parameters: %w", err)
5050
}
@@ -70,13 +70,26 @@ func validateParameters(parameters []model.Parameter) error {
7070

7171
// validateParametersForConditions validates parameters for malformed condition UI blocks
7272
func validateParametersForConditions(param model.Parameter, paramIndex int) error {
73-
conditionBytes, err := json.Marshal(param.Value)
73+
// Bindplane accepts condition values as either:
74+
// - a string OTTL expression (including ""), or
75+
// - an object containing ottl/ui fields.
76+
//
77+
// Terraform configs commonly set condition to "" to mean "no condition". Treat string
78+
// and nil values as valid and skip UI validation.
79+
if param.Value == nil {
80+
return nil
81+
}
82+
if _, ok := param.Value.(string); ok {
83+
return nil
84+
}
85+
86+
conditionBytes, err := jsoniter.Marshal(param.Value)
7487
if err != nil {
7588
return fmt.Errorf("parameter %d: failed to marshal condition: %w", paramIndex, err)
7689
}
7790

7891
var condition Condition
79-
if err := json.Unmarshal(conditionBytes, &condition); err != nil {
92+
if err := jsoniter.Unmarshal(conditionBytes, &condition); err != nil {
8093
return fmt.Errorf("parameter %d: malformed condition structure: %w", paramIndex, err)
8194
}
8295

internal/parameter/parameter_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package parameter
1919

2020
import (
21+
"encoding/json"
2122
"strings"
2223
"testing"
2324

@@ -268,7 +269,22 @@ func TestParametersToSring(t *testing.T) {
268269
require.ErrorContains(t, err, tc.expectErr)
269270
return
270271
}
271-
require.Equal(t, expect, output)
272+
273+
// Empty string is a valid output for "no parameters" and is not JSON.
274+
if expect == "" {
275+
require.Equal(t, "", output)
276+
return
277+
}
278+
279+
// JSON object key ordering is not stable (and differs between json implementations).
280+
// Compare semantically instead of via string equality.
281+
var expectedParams []model.Parameter
282+
require.NoError(t, json.Unmarshal([]byte(expect), &expectedParams))
283+
284+
var actualParams []model.Parameter
285+
require.NoError(t, json.Unmarshal([]byte(output), &actualParams))
286+
287+
require.Equal(t, expectedParams, actualParams)
272288
})
273289
}
274290
}
@@ -279,6 +295,20 @@ func TestStringToParameter_WithConditions(t *testing.T) {
279295
input string
280296
errMsg string
281297
}{
298+
{
299+
"empty-condition-string",
300+
`[
301+
{
302+
"name": "telemetry_types",
303+
"value": ["Logs"]
304+
},
305+
{
306+
"name": "condition",
307+
"value": ""
308+
}
309+
]`,
310+
"",
311+
},
282312
{
283313
"malformed-condition",
284314
`[

test/integration/destinations.tf

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,3 @@
1-
resource "bindplane_destination" "loki" {
2-
rollout = true
3-
name = "example-loki"
4-
type = "loki"
5-
parameters_json = jsonencode(
6-
[
7-
{
8-
"name" : "endpoint",
9-
"value" : "https://loki.corp.net:3100/loki/api/v1/push"
10-
},
11-
{
12-
"name" : "headers",
13-
"value" : {
14-
"token" : "xxx-xxx-xxx"
15-
}
16-
},
17-
{
18-
"name" : "configure_tls",
19-
"value" : true
20-
},
21-
{
22-
"name" : "insecure_skip_verify",
23-
"value" : false
24-
},
25-
{
26-
"name" : "ca_file",
27-
"value" : "/opt/tls/ca.crt"
28-
},
29-
{
30-
"name" : "mutual_tls",
31-
"value" : true
32-
},
33-
{
34-
"name" : "cert_file",
35-
"value" : "/opt/tls/client.crt"
36-
},
37-
{
38-
"name" : "key_file",
39-
"value" : "/opt/tls/client.key"
40-
},
41-
{
42-
"name" : "retry_on_failure_enabled",
43-
"value" : true
44-
},
45-
{
46-
"name" : "retry_on_failure_initial_interval",
47-
"value" : 5
48-
},
49-
{
50-
"name" : "retry_on_failure_max_interval",
51-
"value" : 30
52-
},
53-
{
54-
"name" : "retry_on_failure_max_elapsed_time",
55-
"value" : 300
56-
},
57-
{
58-
"name" : "sending_queue_enabled",
59-
"value" : true
60-
},
61-
{
62-
"name" : "sending_queue_num_consumers",
63-
"value" : 10
64-
},
65-
{
66-
"name" : "sending_queue_queue_size",
67-
"value" : 5000
68-
},
69-
{
70-
"name" : "persistent_queue_enabled",
71-
"value" : true
72-
},
73-
{
74-
"name" : "persistent_queue_directory",
75-
"value" : "$OIQ_OTEL_COLLECTOR_HOME/storage"
76-
}
77-
]
78-
)
79-
}
80-
811
resource "bindplane_destination" "google" {
822
rollout = true
833
name = "example-google"

test/integration/main.tf

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -599,24 +599,6 @@ resource "bindplane_connector" "fluent_router" {
599599
},
600600
"id": "parser"
601601
},
602-
{
603-
"condition": {
604-
"ottl": "(attributes[\"env\"] == \"dev\")",
605-
"ottlContext": "resource",
606-
"ui": {
607-
"operator": "",
608-
"statements": [
609-
{
610-
"key": "env",
611-
"match": "resource",
612-
"operator": "Equals",
613-
"value": "dev"
614-
}
615-
]
616-
}
617-
},
618-
"id": "loki"
619-
},
620602
]
621603
}
622604
]
@@ -747,7 +729,7 @@ resource "bindplane_configuration_v2" "configuration" {
747729
components = [
748730
"destinations/${bindplane_destination.datadog.id}",
749731
"destinations/${bindplane_destination.google.id}",
750-
"destinations/${bindplane_destination.loki.id}"
732+
"destinations/${bindplane_destination.google.id}"
751733
]
752734
}
753735
route {
@@ -756,7 +738,7 @@ resource "bindplane_configuration_v2" "configuration" {
756738
components = [
757739
"destinations/${bindplane_destination.datadog.id}",
758740
"destinations/${bindplane_destination.google.id}",
759-
"destinations/${bindplane_destination.loki.id}"
741+
"destinations/${bindplane_destination.google.id}"
760742
]
761743
}
762744
}
@@ -782,7 +764,7 @@ resource "bindplane_configuration_v2" "configuration" {
782764
route_id = "fallback"
783765
telemetry_type = "logs"
784766
components = [
785-
"destinations/${bindplane_destination.loki.id}"
767+
"destinations/${bindplane_destination.google.id}"
786768
]
787769
}
788770
}
@@ -797,13 +779,6 @@ resource "bindplane_configuration_v2" "configuration" {
797779
"processors/parser"
798780
]
799781
}
800-
route {
801-
route_id = "loki"
802-
telemetry_type = "logs"
803-
components = [
804-
"destinations/${bindplane_destination.loki.id}"
805-
]
806-
}
807782
}
808783

809784
destination {
@@ -820,11 +795,6 @@ resource "bindplane_configuration_v2" "configuration" {
820795
name = bindplane_destination.datadog.name
821796
}
822797

823-
destination {
824-
route_id = bindplane_destination.loki.id
825-
name = bindplane_destination.loki.name
826-
}
827-
828798
extensions = [
829799
bindplane_extension.pprof.name
830800
]

0 commit comments

Comments
 (0)