Skip to content

Commit 7618841

Browse files
authored
Make k6 API URL configurable in the plugin installation (#2424)
1 parent be773b1 commit 7618841

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

docs/resources/k6_installation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ resource "grafana_k6_project" "my_k6_project" {
108108
- `grafana_user` (String) The user to use for the installation.
109109
- `stack_id` (String) The identifier of the stack to install k6 on.
110110

111+
### Optional
112+
113+
- `k6_api_url` (String) The Grafana Cloud k6 API url.
114+
111115
### Read-Only
112116

113117
- `id` (String) The ID of this resource.

internal/resources/cloud/resource_k6_installation.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cloud
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"net/http"
78
"strconv"
89

@@ -36,7 +37,7 @@ Required access policy scopes:
3637
* stack-service-accounts:write
3738
`,
3839
CreateContext: withClient[schema.CreateContextFunc](resourceK6InstallationCreate),
39-
ReadContext: resourceK6InstallationRead,
40+
ReadContext: withClient[schema.ReadContextFunc](resourceK6InstallationRead),
4041
DeleteContext: resourceK6InstallationDelete,
4142

4243
Schema: map[string]*schema.Schema{
@@ -66,6 +67,13 @@ Required access policy scopes:
6667
ForceNew: true,
6768
Description: "The user to use for the installation.",
6869
},
70+
"k6_api_url": {
71+
Type: schema.TypeString,
72+
Optional: true,
73+
Computed: true,
74+
ForceNew: true,
75+
Description: "The Grafana Cloud k6 API url.",
76+
},
6977
"k6_access_token": {
7078
Type: schema.TypeString,
7179
Sensitive: true,
@@ -89,7 +97,9 @@ Required access policy scopes:
8997
}
9098

9199
func resourceK6InstallationCreate(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
92-
const url = "https://api.k6.io/v3/account/grafana-app/start"
100+
k6ApiURL := getk6ApiURL(d)
101+
102+
url := fmt.Sprintf("%s/v3/account/grafana-app/start", k6ApiURL)
93103
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, nil)
94104
if err != nil {
95105
return diag.FromErr(err)
@@ -119,6 +129,7 @@ func resourceK6InstallationCreate(ctx context.Context, d *schema.ResourceData, c
119129
req.Header.Set("X-Grafana-Key", cloudAccessPolicyToken)
120130
req.Header.Set("X-Grafana-Service-Token", grafanaServiceAccountToken)
121131
req.Header.Set("X-Grafana-User", grafanaUser)
132+
req.Header.Set("User-Agent", cloudClient.GetConfig().UserAgent)
122133

123134
resp, err := cloudClient.GetConfig().HTTPClient.Do(req)
124135
if err != nil {
@@ -140,6 +151,10 @@ func resourceK6InstallationCreate(ctx context.Context, d *schema.ResourceData, c
140151

141152
d.SetId(installationRes.OrganizationID)
142153

154+
if err := d.Set("k6_api_url", k6ApiURL); err != nil {
155+
return diag.FromErr(err)
156+
}
157+
143158
if err := d.Set("k6_access_token", installationRes.V3GrafanaToken); err != nil {
144159
return diag.FromErr(err)
145160
}
@@ -148,20 +163,30 @@ func resourceK6InstallationCreate(ctx context.Context, d *schema.ResourceData, c
148163
return diag.FromErr(err)
149164
}
150165

151-
return resourceK6InstallationRead(ctx, d, nil)
166+
return resourceK6InstallationRead(ctx, d, cloudClient)
152167
}
153168

154169
// Management of the installation is a one-off operation. The state cannot be updated through a read operation.
155170
// This read function will only invalidate the state (forcing recreation) if the installation has been deleted.
156-
func resourceK6InstallationRead(ctx context.Context, d *schema.ResourceData, _ any) diag.Diagnostics {
171+
func resourceK6InstallationRead(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
157172
var stackID int32
158173
if intStackID, err := strconv.Atoi(d.Get("stack_id").(string)); err != nil {
159174
return diag.Errorf("could not convert stack_id to integer: %s", err.Error())
160175
} else if stackID, err = common.ToInt32(intStackID); err != nil {
161176
return diag.Errorf("could not convert stack_id to int32: %s", err.Error())
162177
}
163178

164-
tempClient := k6.NewAPIClient(k6.NewConfiguration())
179+
k6ApiURL := getk6ApiURL(d)
180+
181+
k6Cfg := k6.NewConfiguration()
182+
k6Cfg.Servers = []k6.ServerConfiguration{
183+
{URL: k6ApiURL},
184+
}
185+
k6Cfg.UserAgent = cloudClient.GetConfig().UserAgent
186+
k6Cfg.HTTPClient = cloudClient.GetConfig().HTTPClient
187+
188+
tempClient := k6.NewAPIClient(k6Cfg)
189+
165190
ctx = context.WithValue(ctx, k6.ContextAccessToken, d.Get("k6_access_token").(string))
166191
if _, _, err := tempClient.ProjectsAPI.ProjectsList(ctx).XStackId(stackID).Execute(); err != nil {
167192
return common.WarnMissing("k6 installation", d)
@@ -174,3 +199,11 @@ func resourceK6InstallationDelete(_ context.Context, _ *schema.ResourceData, _ a
174199
// To be implemented, not supported yet
175200
return nil
176201
}
202+
203+
func getk6ApiURL(d *schema.ResourceData) string {
204+
k6APIURL, ok := d.Get("k6_api_url").(string)
205+
if !ok || len(k6APIURL) == 0 {
206+
k6APIURL = "https://api.k6.io"
207+
}
208+
return k6APIURL
209+
}

0 commit comments

Comments
 (0)