Skip to content

Commit 3385c87

Browse files
authored
Convert k6 resource IDs to string (#2204)
1 parent 3ed93a5 commit 3385c87

19 files changed

+635
-186
lines changed

docs/data-sources/k6_load_test.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ data "grafana_k6_load_test" "from_id" {
3737

3838
### Required
3939

40-
- `id` (Number) Numeric identifier of the load test.
40+
- `id` (String) Numeric identifier of the load test.
4141

4242
### Read-Only
4343

44-
- `baseline_test_run_id` (Number) Identifier of a baseline test run used for results comparison.
44+
- `baseline_test_run_id` (String) Identifier of a baseline test run used for results comparison.
4545
- `created` (String) The date when the load test was created.
4646
- `name` (String) Human-friendly identifier of the load test.
47-
- `project_id` (Number) The identifier of the project this load test belongs to.
47+
- `project_id` (String) The identifier of the project this load test belongs to.
4848
- `script` (String) The k6 test script content.
4949
- `updated` (String) The date when the load test was last updated.

docs/data-sources/k6_load_tests.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,26 @@ data "grafana_k6_load_tests" "filter_by_name" {
6969

7070
### Required
7171

72-
- `project_id` (Number) The identifier of the project the load tests belong to.
72+
- `project_id` (String) The identifier of the project the load tests belong to.
7373

7474
### Optional
7575

7676
- `name` (String) Human-friendly identifier of the load test.
7777

7878
### Read-Only
7979

80-
- `id` (Number) The identifier of the project the load tests belong to. This is set to the same as the project_id.
80+
- `id` (String) The identifier of the project the load tests belong to. This is set to the same as the project_id.
8181
- `load_tests` (List of Object) (see [below for nested schema](#nestedatt--load_tests))
8282

8383
<a id="nestedatt--load_tests"></a>
8484
### Nested Schema for `load_tests`
8585

8686
Read-Only:
8787

88-
- `baseline_test_run_id` (Number)
88+
- `baseline_test_run_id` (String)
8989
- `created` (String)
90-
- `id` (Number)
90+
- `id` (String)
9191
- `name` (String)
92-
- `project_id` (Number)
92+
- `project_id` (String)
9393
- `script` (String)
9494
- `updated` (String)

docs/data-sources/k6_project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ data "grafana_k6_project" "from_id" {
3030

3131
### Required
3232

33-
- `id` (Number) Numeric identifier of the project.
33+
- `id` (String) Numeric identifier of the project.
3434

3535
### Read-Only
3636

docs/data-sources/k6_project_limits.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ data "grafana_k6_project_limits" "from_project_id" {
2727

2828
### Required
2929

30-
- `project_id` (Number) The identifier of the project to get limits for.
30+
- `project_id` (String) The identifier of the project to get limits for.
3131

3232
### Read-Only
3333

3434
- `duration_max_per_test` (Number) Maximum duration of a test in seconds.
35-
- `id` (Number) The identifier of the project limits. This is set to the same as the project_id.
35+
- `id` (String) The identifier of the project limits. This is set to the same as the project_id.
3636
- `vu_browser_max_per_test` (Number) Maximum number of concurrent browser virtual users (VUs) used in one test.
3737
- `vu_max_per_test` (Number) Maximum number of concurrent virtual users (VUs) used in one test.
3838
- `vuh_max_per_month` (Number) Maximum amount of virtual user hours (VU/h) used per one calendar month.

docs/data-sources/k6_projects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Read-Only:
4545

4646
- `created` (String)
4747
- `grafana_folder_uid` (String)
48-
- `id` (Number)
48+
- `id` (String)
4949
- `is_default` (Boolean)
5050
- `name` (String)
5151
- `updated` (String)

docs/resources/k6_load_test.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ resource "grafana_k6_load_test" "test_load_test" {
3434
### Required
3535

3636
- `name` (String) Human-friendly identifier of the load test.
37-
- `project_id` (Number) The identifier of the project this load test belongs to.
37+
- `project_id` (String) The identifier of the project this load test belongs to.
3838
- `script` (String) The k6 test script content. Can be provided inline or via the `file()` function.
3939

4040
### Optional
4141

42-
- `baseline_test_run_id` (Number) Identifier of a baseline test run used for results comparison.
42+
- `baseline_test_run_id` (String) Identifier of a baseline test run used for results comparison.
4343

4444
### Read-Only
4545

4646
- `created` (String) The date when the load test was created.
47-
- `id` (Number) Numeric identifier of the load test.
47+
- `id` (String) Numeric identifier of the load test.
4848
- `updated` (String) The date when the load test was last updated.
4949

5050
## Import

docs/resources/k6_project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ resource "grafana_k6_project" "test_project" {
2929

3030
- `created` (String) The date when the project was created.
3131
- `grafana_folder_uid` (String) The Grafana folder uid.
32-
- `id` (Number) Numeric identifier of the project.
32+
- `id` (String) Numeric identifier of the project.
3333
- `is_default` (Boolean) Use this project as default for running tests when no explicit project identifier is provided.
3434
- `updated` (String) The date when the project was last updated.
3535

docs/resources/k6_project_limits.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resource "grafana_k6_project_limits" "test_limits" {
3131

3232
### Required
3333

34-
- `project_id` (Number) The identifier of the project to manage limits for.
34+
- `project_id` (String) The identifier of the project to manage limits for.
3535

3636
### Optional
3737

@@ -42,7 +42,7 @@ resource "grafana_k6_project_limits" "test_limits" {
4242

4343
### Read-Only
4444

45-
- `id` (Number) The identifier of the project limits. This is the same as the project_id.
45+
- `id` (String) The identifier of the project limits. This is the same as the project_id.
4646

4747
## Import
4848

internal/resources/k6/data_source_k6_loadtest.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func dataSourceLoadTest() *common.DataSource {
3333

3434
// loadTestDataSourceModel maps the data source schema data.
3535
type loadTestDataSourceModel struct {
36-
ID types.Int32 `tfsdk:"id"`
37-
ProjectID types.Int32 `tfsdk:"project_id"`
36+
ID types.String `tfsdk:"id"`
37+
ProjectID types.String `tfsdk:"project_id"`
3838
Name types.String `tfsdk:"name"`
3939
Script types.String `tfsdk:"script"`
40-
BaselineTestRunID types.Int32 `tfsdk:"baseline_test_run_id"`
40+
BaselineTestRunID types.String `tfsdk:"baseline_test_run_id"`
4141
Created types.String `tfsdk:"created"`
4242
Updated types.String `tfsdk:"updated"`
4343
}
@@ -57,11 +57,11 @@ func (d *loadTestDataSource) Schema(_ context.Context, _ datasource.SchemaReques
5757
resp.Schema = schema.Schema{
5858
Description: "Retrieves a k6 load test.",
5959
Attributes: map[string]schema.Attribute{
60-
"id": schema.Int32Attribute{
60+
"id": schema.StringAttribute{
6161
Description: "Numeric identifier of the load test.",
6262
Required: true,
6363
},
64-
"project_id": schema.Int32Attribute{
64+
"project_id": schema.StringAttribute{
6565
Description: "The identifier of the project this load test belongs to.",
6666
Computed: true,
6767
},
@@ -73,7 +73,7 @@ func (d *loadTestDataSource) Schema(_ context.Context, _ datasource.SchemaReques
7373
Description: "The k6 test script content.",
7474
Computed: true,
7575
},
76-
"baseline_test_run_id": schema.Int32Attribute{
76+
"baseline_test_run_id": schema.StringAttribute{
7777
Description: "Identifier of a baseline test run used for results comparison.",
7878
Computed: true,
7979
},
@@ -98,35 +98,45 @@ func (d *loadTestDataSource) Read(ctx context.Context, req datasource.ReadReques
9898
return
9999
}
100100

101+
intID, err := strconv.ParseInt(state.ID.ValueString(), 10, 32)
102+
if err != nil {
103+
resp.Diagnostics.AddError(
104+
"Error parsing load test ID",
105+
"Could not parse load test ID '"+state.ID.ValueString()+"': "+err.Error(),
106+
)
107+
return
108+
}
109+
loadTestID := int32(intID)
110+
101111
// Retrieve the load test attributes
102112
ctx = context.WithValue(ctx, k6.ContextAccessToken, d.config.Token)
103-
k6Req := d.client.LoadTestsAPI.LoadTestsRetrieve(ctx, state.ID.ValueInt32()).
113+
k6Req := d.client.LoadTestsAPI.LoadTestsRetrieve(ctx, loadTestID).
104114
XStackId(d.config.StackID)
105115

106116
lt, _, err := k6Req.Execute()
107117
if err != nil {
108118
resp.Diagnostics.AddError(
109119
"Error reading k6 load test",
110-
"Could not read k6 load test with id "+strconv.Itoa(int(state.ID.ValueInt32()))+": "+err.Error(),
120+
"Could not read k6 load test with id "+state.ID.ValueString()+": "+err.Error(),
111121
)
112122
return
113123
}
114124

115125
// Retrieve the load test script content
116-
scriptReq := d.client.LoadTestsAPI.LoadTestsScriptRetrieve(ctx, state.ID.ValueInt32()).
126+
scriptReq := d.client.LoadTestsAPI.LoadTestsScriptRetrieve(ctx, loadTestID).
117127
XStackId(d.config.StackID)
118128

119129
script, _, err := scriptReq.Execute()
120130
if err != nil {
121131
resp.Diagnostics.AddError(
122132
"Error reading k6 load test script",
123-
"Could not read k6 load test script with id "+strconv.Itoa(int(state.ID.ValueInt32()))+": "+err.Error(),
133+
"Could not read k6 load test script with id "+state.ID.ValueString()+": "+err.Error(),
124134
)
125135
return
126136
}
127137

128138
state.Name = types.StringValue(lt.GetName())
129-
state.ProjectID = types.Int32Value(lt.GetProjectId())
139+
state.ProjectID = types.StringValue(strconv.Itoa(int(lt.GetProjectId())))
130140
state.BaselineTestRunID = handleBaselineTestRunID(lt.GetBaselineTestRunId())
131141
state.Script = types.StringValue(script)
132142
state.Created = types.StringValue(lt.GetCreated().Format(time.RFC3339Nano))
@@ -136,10 +146,10 @@ func (d *loadTestDataSource) Read(ctx context.Context, req datasource.ReadReques
136146
resp.Diagnostics.Append(diags...)
137147
}
138148

139-
func handleBaselineTestRunID(baselineTestRunID int32) types.Int32 {
149+
func handleBaselineTestRunID(baselineTestRunID int32) types.String {
140150
if baselineTestRunID == 0 {
141151
// If the API returned 0, set it as null
142-
return types.Int32Null()
152+
return types.StringNull()
143153
}
144-
return types.Int32Value(baselineTestRunID)
154+
return types.StringValue(strconv.Itoa(int(baselineTestRunID)))
145155
}

internal/resources/k6/data_source_k6_loadtests.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func dataSourceLoadTests() *common.DataSource {
3737

3838
// loadTestsDataSourceModel maps the data source schema data.
3939
type loadTestsDataSourceModel struct {
40-
ID types.Int32 `tfsdk:"id"`
41-
ProjectID types.Int32 `tfsdk:"project_id"`
40+
ID types.String `tfsdk:"id"`
41+
ProjectID types.String `tfsdk:"project_id"`
4242
Name types.String `tfsdk:"name"`
4343
LoadTests []loadTestDataSourceModel `tfsdk:"load_tests"`
4444
}
@@ -58,12 +58,12 @@ func (d *loadTestsDataSource) Schema(_ context.Context, _ datasource.SchemaReque
5858
resp.Schema = schema.Schema{
5959
Description: "Retrieves all k6 load tests that belong to a project.",
6060
Attributes: map[string]schema.Attribute{
61-
"id": schema.Int32Attribute{
61+
"id": schema.StringAttribute{
6262
Description: "The identifier of the project the load tests belong to. " +
6363
"This is set to the same as the project_id.",
6464
Computed: true,
6565
},
66-
"project_id": schema.Int32Attribute{
66+
"project_id": schema.StringAttribute{
6767
Description: "The identifier of the project the load tests belong to.",
6868
Required: true,
6969
},
@@ -75,11 +75,11 @@ func (d *loadTestsDataSource) Schema(_ context.Context, _ datasource.SchemaReque
7575
Computed: true,
7676
ElementType: types.ObjectType{
7777
AttrTypes: map[string]attr.Type{
78-
"id": types.Int32Type,
78+
"id": types.StringType,
7979
"name": types.StringType,
80-
"project_id": types.Int32Type,
80+
"project_id": types.StringType,
8181
"script": types.StringType,
82-
"baseline_test_run_id": types.Int32Type,
82+
"baseline_test_run_id": types.StringType,
8383
"created": types.StringType,
8484
"updated": types.StringType,
8585
},
@@ -101,16 +101,26 @@ func (d *loadTestsDataSource) Read(ctx context.Context, req datasource.ReadReque
101101
// Set the ID to match the project_id
102102
state.ID = state.ProjectID
103103

104+
intID, err := strconv.ParseInt(state.ID.ValueString(), 10, 32)
105+
if err != nil {
106+
resp.Diagnostics.AddError(
107+
"Error parsing project ID",
108+
"Could not parse project ID '"+state.ID.ValueString()+"': "+err.Error(),
109+
)
110+
return
111+
}
112+
projectID := int32(intID)
113+
104114
// Retrieve the project's load tests
105115
ctx = context.WithValue(ctx, k6.ContextAccessToken, d.config.Token)
106-
k6Req := d.client.LoadTestsAPI.ProjectsLoadTestsRetrieve(ctx, state.ProjectID.ValueInt32()).
116+
k6Req := d.client.LoadTestsAPI.ProjectsLoadTestsRetrieve(ctx, projectID).
107117
XStackId(d.config.StackID)
108118

109119
lts, _, err := k6Req.Execute()
110120
if err != nil {
111121
resp.Diagnostics.AddError(
112122
"Error reading k6 load tests",
113-
"Could not read k6 load tests with project id "+strconv.Itoa(int(state.ProjectID.ValueInt32()))+": "+err.Error(),
123+
"Could not read k6 load tests with project id "+state.ID.ValueString()+": "+err.Error(),
114124
)
115125
return
116126
}
@@ -142,9 +152,9 @@ func (d *loadTestsDataSource) Read(ctx context.Context, req datasource.ReadReque
142152
}
143153

144154
ltState := loadTestDataSourceModel{
145-
ID: types.Int32Value(lt.GetId()),
155+
ID: types.StringValue(strconv.Itoa(int(lt.GetId()))),
146156
Name: types.StringValue(lt.GetName()),
147-
ProjectID: types.Int32Value(lt.GetProjectId()),
157+
ProjectID: types.StringValue(strconv.Itoa(int(lt.GetProjectId()))),
148158
BaselineTestRunID: handleBaselineTestRunID(lt.GetBaselineTestRunId()),
149159
Script: types.StringValue(script),
150160
Created: types.StringValue(lt.GetCreated().Format(time.RFC3339Nano)),

0 commit comments

Comments
 (0)