Skip to content

Commit f5e7335

Browse files
I've fixed an issue in the elasticstack_kibana_slo resource where the slo_id field wasn't being properly checked. Previously, if you provided an ID that was too short, too long, or had invalid characters, it could cause errors when communicating with the API.
Here's what I've done: 1. I've updated the definition for `slo_id` in `internal/kibana/slo.go`: * The description now clearly states the ID must be between 8 and 48 characters. * I've added a check to ensure the `slo_id` meets this length requirement and only contains letters, numbers, hyphens, and underscores. 2. I've also added a new automated check in `internal/kibana/slo_test.go`. This makes sure that the system now correctly rejects `slo_id` values that are: * Too short * Too long * Contain invalid characters And that it provides you with the right error message in these cases.
1 parent f04902a commit f5e7335

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

internal/kibana/slo.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kibana
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67

78
"github.com/elastic/terraform-provider-elasticstack/generated/slo"
89
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
@@ -81,11 +82,15 @@ func getSchema() map[string]*schema.Schema {
8182

8283
return map[string]*schema.Schema{
8384
"slo_id": {
84-
Description: "An ID (8 and 36 characters). If omitted, a UUIDv1 will be generated server-side.",
85+
Description: "An ID (8 and 48 characters). If omitted, a UUIDv1 will be generated server-side.",
8586
Type: schema.TypeString,
8687
Optional: true,
8788
Computed: true,
8889
ForceNew: true,
90+
ValidateFunc: validation.StringMatch(
91+
regexp.MustCompile("^[a-zA-Z0-9_-]{8,48}$"),
92+
"slo_id must be between 8 and 48 characters and contain only letters, numbers, hyphens, and underscores",
93+
),
8994
},
9095
"name": {
9196
Description: "The name of the SLO.",

internal/kibana/slo_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,105 @@ import (
2121
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2222
)
2323

24+
const testAccResourceKibanaSloInvalidIdTooShort = `
25+
provider "elasticstack" {
26+
elasticsearch {}
27+
kibana {}
28+
}
29+
30+
resource "elasticstack_kibana_slo" "test_slo_short_id" {
31+
slo_id = "short"
32+
name = "Test SLO Too Short"
33+
description = "Test description for SLO with too short ID"
34+
kql_custom_indicator {
35+
index = "logs-*"
36+
good = "response_time < 100"
37+
total = "*"
38+
}
39+
time_window {
40+
duration = "7d"
41+
type = "rolling"
42+
}
43+
budgeting_method = "occurrences"
44+
objective {
45+
target = 0.99
46+
}
47+
}
48+
`
49+
50+
const testAccResourceKibanaSloInvalidIdTooLong = `
51+
provider "elasticstack" {
52+
elasticsearch {}
53+
kibana {}
54+
}
55+
56+
resource "elasticstack_kibana_slo" "test_slo_long_id" {
57+
slo_id = "thisidistoolongandoverfortyeightcharacterslonggggg12345" // 53 chars
58+
name = "Test SLO Too Long"
59+
description = "Test description for SLO with too long ID"
60+
kql_custom_indicator {
61+
index = "logs-*"
62+
good = "response_time < 200"
63+
total = "*"
64+
}
65+
time_window {
66+
duration = "14d"
67+
type = "rolling"
68+
}
69+
budgeting_method = "occurrences"
70+
objective {
71+
target = 0.995
72+
}
73+
}
74+
`
75+
76+
const testAccResourceKibanaSloInvalidIdChars = `
77+
provider "elasticstack" {
78+
elasticsearch {}
79+
kibana {}
80+
}
81+
82+
resource "elasticstack_kibana_slo" "test_slo_invalid_chars_id" {
83+
slo_id = "invalid!id@#"
84+
name = "Test SLO Invalid Chars"
85+
description = "Test description for SLO with invalid characters in ID"
86+
kql_custom_indicator {
87+
index = "logs-*"
88+
good = "response_time < 300"
89+
total = "*"
90+
}
91+
time_window {
92+
duration = "30d"
93+
type = "rolling"
94+
}
95+
budgeting_method = "occurrences"
96+
objective {
97+
target = 0.999
98+
}
99+
}
100+
`
101+
102+
func TestAccResourceKibanaSlo_slo_id_validation(t *testing.T) {
103+
resource.Test(t, resource.TestCase{
104+
PreCheck: func() { acctest.PreCheck(t) },
105+
ProtoV6ProviderFactories: acctest.Providers,
106+
Steps: []resource.TestStep{
107+
{
108+
Config: testAccResourceKibanaSloInvalidIdTooShort,
109+
ExpectError: regexp.MustCompile("slo_id must be between 8 and 48 characters and contain only letters, numbers, hyphens, and underscores"),
110+
},
111+
{
112+
Config: testAccResourceKibanaSloInvalidIdTooLong,
113+
ExpectError: regexp.MustCompile("slo_id must be between 8 and 48 characters and contain only letters, numbers, hyphens, and underscores"),
114+
},
115+
{
116+
Config: testAccResourceKibanaSloInvalidIdChars,
117+
ExpectError: regexp.MustCompile("slo_id must be between 8 and 48 characters and contain only letters, numbers, hyphens, and underscores"),
118+
},
119+
},
120+
})
121+
}
122+
24123
func TestAccResourceSlo(t *testing.T) {
25124
// This test exposes a bug in Kibana present in 8.11.x
26125
slo8_9Constraints, err := version.NewConstraint(">=8.9.0,!=8.11.0,!=8.11.1,!=8.11.2,!=8.11.3,!=8.11.4")

0 commit comments

Comments
 (0)