Skip to content

Commit 57615fc

Browse files
Synthetic Monitoring: Restrict invalid labels (#576)
* Synthetic Monitoring: Restrict invalid labels Restricting label names according to the prometheus data model: https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels For the value, I found that commas and empty values are disallowed by the SM API Fixes #541 * Extract banned chars and label name regex * Use Validate function from SM package * Remove value validation. Will be fixed in the API * remove unused var
1 parent a68ea6e commit 57615fc

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

grafana/resource_synthetic_monitoring_probe.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"strings"
1010

11+
"github.com/hashicorp/go-cty/cty"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314

@@ -80,6 +81,16 @@ Grafana Synthetic Monitoring Agent.
8081
Elem: &schema.Schema{
8182
Type: schema.TypeString,
8283
},
84+
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
85+
for k, vInt := range i.(map[string]interface{}) {
86+
v := vInt.(string)
87+
lbl := sm.Label{Name: k, Value: v}
88+
if err := lbl.Validate(); err != nil {
89+
return diag.Errorf(`invalid label "%s=%s": %s`, k, v, err)
90+
}
91+
}
92+
return nil
93+
},
8394
},
8495
"public": {
8596
Description: "Public probes are run by Grafana Labs and can be used by all users. Only Grafana Labs managed public probes will be set to `true`.",

grafana/resource_synthetic_monitoring_probe_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package grafana
22

33
import (
44
"context"
5+
"fmt"
6+
"regexp"
57
"strconv"
68
"testing"
79

@@ -77,3 +79,50 @@ func TestAccResourceSyntheticMonitoringProbe_recreate(t *testing.T) {
7779
},
7880
})
7981
}
82+
83+
func TestAccResourceSyntheticMonitoringProbe_InvalidLabels(t *testing.T) {
84+
CheckCloudInstanceTestsEnabled(t)
85+
86+
var steps []resource.TestStep
87+
for _, tc := range []struct {
88+
cfg string
89+
err string
90+
}{
91+
{
92+
cfg: testSyntheticMonitoringProbeLabel("", "any"),
93+
err: `invalid label "=any": invalid label name`,
94+
},
95+
{
96+
cfg: testSyntheticMonitoringProbeLabel("bad-label", "any"),
97+
err: `invalid label "bad-label=any": invalid label name`,
98+
},
99+
{
100+
cfg: testSyntheticMonitoringProbeLabel("thisisempty", ""),
101+
err: `invalid label "thisisempty=": invalid label value`,
102+
},
103+
} {
104+
steps = append(steps, resource.TestStep{
105+
Config: tc.cfg,
106+
ExpectError: regexp.MustCompile(tc.err),
107+
})
108+
}
109+
110+
resource.Test(t, resource.TestCase{
111+
ProviderFactories: testAccProviderFactories,
112+
Steps: steps,
113+
})
114+
}
115+
116+
func testSyntheticMonitoringProbeLabel(name, value string) string {
117+
return fmt.Sprintf(`
118+
resource "grafana_synthetic_monitoring_probe" "main" {
119+
name = "Everest"
120+
latitude = 27.98606
121+
longitude = 86.92262
122+
region = "APAC"
123+
labels = {
124+
"%s" = "%s"
125+
}
126+
}
127+
`, name, value)
128+
}

0 commit comments

Comments
 (0)