Skip to content

Commit 67768b3

Browse files
authored
Update k6 test schedules to support cron expressions (#2393)
1 parent 2e10afe commit 67768b3

File tree

14 files changed

+375
-24
lines changed

14 files changed

+375
-24
lines changed

docs/data-sources/k6_schedule.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ output "complete_schedule_info" {
6363
next_run = data.grafana_k6_schedule.from_load_test.next_run
6464
created_by = data.grafana_k6_schedule.from_load_test.created_by
6565
recurrence_rule = data.grafana_k6_schedule.from_load_test.recurrence_rule
66+
cron = data.grafana_k6_schedule.from_load_test.cron
6667
}
6768
}
6869
```
@@ -77,12 +78,22 @@ output "complete_schedule_info" {
7778
### Read-Only
7879

7980
- `created_by` (String) The email of the user who created the schedule.
81+
- `cron` (Block, Read-only) The cron schedule to trigger the test periodically. If null, the test will run only once on the 'starts' date. (see [below for nested schema](#nestedblock--cron))
8082
- `deactivated` (Boolean) Whether the schedule is deactivated.
8183
- `id` (String) Numeric identifier of the schedule.
8284
- `next_run` (String) The next scheduled execution time.
8385
- `recurrence_rule` (Block, Read-only) The schedule recurrence settings. If null, the test will run only once on the starts date. (see [below for nested schema](#nestedblock--recurrence_rule))
8486
- `starts` (String) The start time for the schedule (RFC3339 format).
8587

88+
<a id="nestedblock--cron"></a>
89+
### Nested Schema for `cron`
90+
91+
Read-Only:
92+
93+
- `schedule` (String) A cron expression with exactly 5 entries, or an alias. The allowed aliases are: @yearly, @annually, @monthly, @weekly, @daily, @hourly.
94+
- `timezone` (String) The timezone of the cron expression. For example, 'UTC' or 'Europe/London'.
95+
96+
8697
<a id="nestedblock--recurrence_rule"></a>
8798
### Nested Schema for `recurrence_rule`
8899

docs/data-sources/k6_schedules.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ resource "grafana_k6_load_test" "schedules_load_test_2" {
4545
]
4646
}
4747
48+
resource "grafana_k6_load_test" "schedules_load_test_3" {
49+
project_id = grafana_k6_project.schedules_project.id
50+
name = "Terraform Test Load Test for Schedules (3)"
51+
script = <<-EOT
52+
export default function() {
53+
console.log('Hello from k6 schedules test!');
54+
}
55+
EOT
56+
57+
depends_on = [
58+
grafana_k6_project.schedules_project,
59+
]
60+
}
61+
62+
4863
4964
resource "grafana_k6_schedule" "test_schedule_1" {
5065
load_test_id = grafana_k6_load_test.schedules_load_test.id
@@ -74,11 +89,25 @@ resource "grafana_k6_schedule" "test_schedule_2" {
7489
]
7590
}
7691
92+
resource "grafana_k6_schedule" "test_schedule_3" {
93+
load_test_id = grafana_k6_load_test.schedules_load_test_3.id
94+
starts = "2023-12-26T14:00:00Z"
95+
cron {
96+
schedule = "0 10 1 12 6"
97+
timezone = "UTC"
98+
}
99+
100+
depends_on = [
101+
grafana_k6_load_test.schedules_load_test_3,
102+
]
103+
}
104+
77105
data "grafana_k6_schedules" "from_load_test_id" {
78106
79107
depends_on = [
80108
grafana_k6_schedule.test_schedule_1,
81109
grafana_k6_schedule.test_schedule_2,
110+
grafana_k6_schedule.test_schedule_3,
82111
]
83112
}
84113
```
@@ -97,13 +126,23 @@ data "grafana_k6_schedules" "from_load_test_id" {
97126
Read-Only:
98127

99128
- `created_by` (String)
129+
- `cron` (Object) (see [below for nested schema](#nestedobjatt--schedules--cron))
100130
- `deactivated` (Boolean)
101131
- `id` (String)
102132
- `load_test_id` (String)
103133
- `next_run` (String)
104134
- `recurrence_rule` (Object) (see [below for nested schema](#nestedobjatt--schedules--recurrence_rule))
105135
- `starts` (String)
106136

137+
<a id="nestedobjatt--schedules--cron"></a>
138+
### Nested Schema for `schedules.cron`
139+
140+
Read-Only:
141+
142+
- `schedule` (String)
143+
- `timezone` (String)
144+
145+
107146
<a id="nestedobjatt--schedules--recurrence_rule"></a>
108147
### Nested Schema for `schedules.recurrence_rule`
109148

docs/resources/k6_schedule.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ resource "grafana_k6_load_test" "scheduled_test" {
3131
]
3232
}
3333
34+
resource "grafana_k6_schedule" "cron_monthly" {
35+
load_test_id = grafana_k6_load_test.scheduled_test.id
36+
starts = "2024-12-25T10:00:00Z"
37+
cron {
38+
schedule = "0 10 1 * *"
39+
timezone = "UTC"
40+
}
41+
}
42+
43+
3444
resource "grafana_k6_schedule" "daily" {
3545
load_test_id = grafana_k6_load_test.scheduled_test.id
3646
starts = "2024-12-25T10:00:00Z"
@@ -79,7 +89,8 @@ resource "grafana_k6_schedule" "one_time" {
7989

8090
### Optional
8191

82-
- `recurrence_rule` (Block, Optional) The schedule recurrence settings. If not specified, the test will run only once on the 'starts' date. (see [below for nested schema](#nestedblock--recurrence_rule))
92+
- `cron` (Block, Optional) The cron schedule to trigger the test periodically. If not specified, the test will run only once on the 'starts' date. Only one of `recurrence_rule` and `cron` can be set. (see [below for nested schema](#nestedblock--cron))
93+
- `recurrence_rule` (Block, Optional) The schedule recurrence settings. If not specified, the test will run only once on the 'starts' date. Only one of `recurrence_rule` and `cron` can be set. (see [below for nested schema](#nestedblock--recurrence_rule))
8394

8495
### Read-Only
8596

@@ -88,6 +99,15 @@ resource "grafana_k6_schedule" "one_time" {
8899
- `id` (String) Numeric identifier of the schedule.
89100
- `next_run` (String) The next scheduled execution time.
90101

102+
<a id="nestedblock--cron"></a>
103+
### Nested Schema for `cron`
104+
105+
Optional:
106+
107+
- `schedule` (String) A cron expression with exactly 5 entries, or an alias. The allowed aliases are: @yearly, @annually, @monthly, @weekly, @daily, @hourly.
108+
- `timezone` (String) The timezone of the cron expression. For example, 'UTC' or 'Europe/London'.
109+
110+
91111
<a id="nestedblock--recurrence_rule"></a>
92112
### Nested Schema for `recurrence_rule`
93113

examples/data-sources/grafana_k6_schedule/data-source.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ output "complete_schedule_info" {
4848
next_run = data.grafana_k6_schedule.from_load_test.next_run
4949
created_by = data.grafana_k6_schedule.from_load_test.created_by
5050
recurrence_rule = data.grafana_k6_schedule.from_load_test.recurrence_rule
51+
cron = data.grafana_k6_schedule.from_load_test.cron
5152
}
5253
}

examples/data-sources/grafana_k6_schedules/data-source.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ resource "grafana_k6_load_test" "schedules_load_test_2" {
3030
]
3131
}
3232

33+
resource "grafana_k6_load_test" "schedules_load_test_3" {
34+
project_id = grafana_k6_project.schedules_project.id
35+
name = "Terraform Test Load Test for Schedules (3)"
36+
script = <<-EOT
37+
export default function() {
38+
console.log('Hello from k6 schedules test!');
39+
}
40+
EOT
41+
42+
depends_on = [
43+
grafana_k6_project.schedules_project,
44+
]
45+
}
46+
47+
3348

3449
resource "grafana_k6_schedule" "test_schedule_1" {
3550
load_test_id = grafana_k6_load_test.schedules_load_test.id
@@ -59,10 +74,24 @@ resource "grafana_k6_schedule" "test_schedule_2" {
5974
]
6075
}
6176

77+
resource "grafana_k6_schedule" "test_schedule_3" {
78+
load_test_id = grafana_k6_load_test.schedules_load_test_3.id
79+
starts = "2023-12-26T14:00:00Z"
80+
cron {
81+
schedule = "0 10 1 12 6"
82+
timezone = "UTC"
83+
}
84+
85+
depends_on = [
86+
grafana_k6_load_test.schedules_load_test_3,
87+
]
88+
}
89+
6290
data "grafana_k6_schedules" "from_load_test_id" {
6391

6492
depends_on = [
6593
grafana_k6_schedule.test_schedule_1,
6694
grafana_k6_schedule.test_schedule_2,
95+
grafana_k6_schedule.test_schedule_3,
6796
]
6897
}

examples/resources/grafana_k6_schedule/resource.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ resource "grafana_k6_load_test" "scheduled_test" {
1616
]
1717
}
1818

19+
resource "grafana_k6_schedule" "cron_monthly" {
20+
load_test_id = grafana_k6_load_test.scheduled_test.id
21+
starts = "2024-12-25T10:00:00Z"
22+
cron {
23+
schedule = "0 10 1 * *"
24+
timezone = "UTC"
25+
}
26+
}
27+
28+
1929
resource "grafana_k6_schedule" "daily" {
2030
load_test_id = grafana_k6_load_test.scheduled_test.id
2131
starts = "2024-12-25T10:00:00Z"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/grafana/grafana/apps/dashboard v0.0.0-20250424064802-2fbb2d6f5d27
2020
github.com/grafana/grafana/apps/playlist v0.0.0-20250424064802-2fbb2d6f5d27
2121
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250901080157-a0280d701b28
22-
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250715154343-32edc34ec1db
22+
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20251022100644-dd6cfbb68f85
2323
github.com/grafana/machine-learning-go-client v0.8.2
2424
github.com/grafana/river v0.3.0
2525
github.com/grafana/slo-openapi-client/go/slo v0.0.0-20250218172929-ab9cae090da6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ github.com/grafana/grafana/apps/playlist v0.0.0-20250424064802-2fbb2d6f5d27 h1:a
195195
github.com/grafana/grafana/apps/playlist v0.0.0-20250424064802-2fbb2d6f5d27/go.mod h1:9U44mptAJW8bkvgPgCxsnki58/nz3wKPgDayeyeFWJs=
196196
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250901080157-a0280d701b28 h1:PgMfX4OPENz/iXmtDDIW9+poZY4UD0hhmXm7flVclDo=
197197
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250901080157-a0280d701b28/go.mod h1:av5N0Naq+8VV9MLF7zAkihy/mVq5UbS2EvRSJukDHlY=
198-
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250715154343-32edc34ec1db h1:GZGkcFrQF09j8rZfxcIRql7liBf1U7pdmqSAseGintg=
199-
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250715154343-32edc34ec1db/go.mod h1:RBPBP7qIR/K6qzQEQYESVhp/XJspiBTOyBEBCbPXrvI=
198+
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20251022100644-dd6cfbb68f85 h1:GmbPOHOiw/oa3sfGpKSsf6p5R1Ko/c6aYrZvTRfMbUY=
199+
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20251022100644-dd6cfbb68f85/go.mod h1:RBPBP7qIR/K6qzQEQYESVhp/XJspiBTOyBEBCbPXrvI=
200200
github.com/grafana/machine-learning-go-client v0.8.2 h1:TvU4e+Kgg4GhwBNYTMjBUNq4tbhcxe0L8w1eo/UfV2M=
201201
github.com/grafana/machine-learning-go-client v0.8.2/go.mod h1:GQKDn10CZqG11l1Qtc6BZ5V6e54fSv5Vi8wskWn3BWs=
202202
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=

internal/resources/k6/data_source_k6_schedule.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type scheduleDataSourceModel struct {
3636
LoadTestID types.String `tfsdk:"load_test_id"`
3737
Starts types.String `tfsdk:"starts"`
3838
RecurrenceRule *recurrenceRuleModel `tfsdk:"recurrence_rule"`
39+
Cron *cronScheduleModel `tfsdk:"cron"`
3940
Deactivated types.Bool `tfsdk:"deactivated"`
4041
NextRun types.String `tfsdk:"next_run"`
4142
CreatedBy types.String `tfsdk:"created_by"`
@@ -108,6 +109,19 @@ func (d *scheduleDataSource) Schema(_ context.Context, _ datasource.SchemaReques
108109
},
109110
},
110111
},
112+
"cron": schema.SingleNestedBlock{
113+
Description: "The cron schedule to trigger the test periodically. If null, the test will run only once on the 'starts' date.",
114+
Attributes: map[string]schema.Attribute{
115+
"schedule": schema.StringAttribute{
116+
Description: "A cron expression with exactly 5 entries, or an alias. The allowed aliases are: @yearly, @annually, @monthly, @weekly, @daily, @hourly.",
117+
Computed: true,
118+
},
119+
"timezone": schema.StringAttribute{
120+
Description: "The timezone of the cron expression. For example, 'UTC' or 'Europe/London'.",
121+
Computed: true,
122+
},
123+
},
124+
},
111125
},
112126
}
113127
}
@@ -172,7 +186,7 @@ func populateScheduleDataSourceModel(schedule *k6.ScheduleApiModel, model *sched
172186
}
173187

174188
// Extract recurrence rule details
175-
if recurrenceRule, ok := schedule.GetRecurrenceRuleOk(); ok {
189+
if recurrenceRule, ok := schedule.GetRecurrenceRuleOk(); ok && recurrenceRule != nil {
176190
model.RecurrenceRule = &recurrenceRuleModel{
177191
Frequency: types.StringValue(string(recurrenceRule.GetFrequency())),
178192
}
@@ -206,4 +220,13 @@ func populateScheduleDataSourceModel(schedule *k6.ScheduleApiModel, model *sched
206220
} else {
207221
model.RecurrenceRule = nil
208222
}
223+
224+
if cronSchedule, ok := schedule.GetCronOk(); ok && cronSchedule != nil {
225+
model.Cron = &cronScheduleModel{
226+
Schedule: types.StringValue(cronSchedule.GetSchedule()),
227+
Timezone: types.StringValue(cronSchedule.GetTimeZone()),
228+
}
229+
} else {
230+
model.Cron = nil
231+
}
209232
}

internal/resources/k6/data_source_k6_schedule_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ func TestAccDataSourceK6Schedule_basic(t *testing.T) {
6060
// until and next_run are optional and may be null
6161
),
6262
},
63+
{
64+
// Replance recurrence_rule with cron
65+
Config: testutils.TestAccExampleWithReplace(t, "data-sources/grafana_k6_schedule/data-source.tf", map[string]string{
66+
"Terraform Schedule Test Project": projectName,
67+
"Terraform Test Load Test for Schedule": loadTestName,
68+
`
69+
recurrence_rule {
70+
frequency = "MONTHLY"
71+
interval = 12
72+
count = 100
73+
}`: `
74+
cron {
75+
schedule = "0 10 1 12 6"
76+
timezone = "UTC"
77+
}`,
78+
}),
79+
Check: resource.ComposeTestCheckFunc(
80+
scheduleCheckExists.exists("grafana_k6_schedule.test_schedule", &schedule),
81+
// Basic attributes
82+
resource.TestCheckResourceAttrSet("data.grafana_k6_schedule.from_load_test", "id"),
83+
resource.TestCheckResourceAttrWith("data.grafana_k6_schedule.from_load_test", "id", checkScheduleIDMatch),
84+
resource.TestCheckResourceAttrWith("data.grafana_k6_schedule.from_load_test", "load_test_id", checkLoadTestIDMatch),
85+
// Schedule configuration attributes
86+
resource.TestCheckResourceAttr("data.grafana_k6_schedule.from_load_test", "starts", "2024-12-25T10:00:00Z"),
87+
resource.TestCheckResourceAttr("data.grafana_k6_schedule.from_load_test", "cron.schedule", "0 10 1 12 6"),
88+
resource.TestCheckResourceAttr("data.grafana_k6_schedule.from_load_test", "cron.timezone", "UTC"),
89+
// Optional attributes that should be set
90+
resource.TestCheckResourceAttr("data.grafana_k6_schedule.from_load_test", "deactivated", "false"),
91+
resource.TestCheckResourceAttrSet("data.grafana_k6_schedule.from_load_test", "created_by"),
92+
resource.TestCheckResourceAttrSet("data.grafana_k6_schedule.from_load_test", "next_run"),
93+
),
94+
},
6395
},
6496
})
6597
}

0 commit comments

Comments
 (0)