Skip to content

Commit bbc18eb

Browse files
julienduchesneBjörn Ingesonbjornin
authored
grafana_cloud_stack with proper prometheus urls (#489)
* grafana_cloud_stack with proper prometheus urls Create prometheus_remote_endpoint and prometheus_remote_write_endpoint using url.Parse instead of path.Join as the latter render an invalid url, for example http:/test.com * Return error from FlattenStack * Apply suggestions from code review Co-authored-by: Julien Duchesne <[email protected]> * Beautify syntax * Fix function name * Add tests Co-authored-by: Björn Ingeson <[email protected]> Co-authored-by: Björn Ingeson <[email protected]>
1 parent f3d96fa commit bbc18eb

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

grafana/data_source_cloud_stack.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ available at “https://<stack_slug>.grafana.net".`,
3232
func datasourceCloudStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
3333
client := meta.(*client).gcloudapi
3434

35-
var diags diag.Diagnostics
36-
3735
slug := d.Get("slug").(string)
3836

3937
stack, err := client.StackBySlug(slug)
4038
if err != nil {
4139
return diag.FromErr(err)
4240
}
4341

44-
FlattenStack(d, stack)
42+
if err := FlattenStack(d, stack); err != nil {
43+
return diag.FromErr(err)
44+
}
4545

46-
return diags
46+
return nil
4747
}

grafana/data_source_cloud_stack_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func TestAccDatasourceCloudStack_Basic(t *testing.T) {
3030
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "id"),
3131
resource.TestCheckResourceAttr("data.grafana_cloud_stack.test", "name", resourceName),
3232
resource.TestCheckResourceAttr("data.grafana_cloud_stack.test", "slug", resourceName),
33+
resource.TestCheckResourceAttr("data.grafana_cloud_stack.test", "prometheus_remote_endpoint", "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom"),
34+
resource.TestCheckResourceAttr("data.grafana_cloud_stack.test", "prometheus_remote_write_endpoint", "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push"),
3335
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "prometheus_url"),
3436
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "prometheus_user_id"),
3537
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "alertmanager_user_id"),

grafana/resource_cloud_stack.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"log"
88
"net/http"
9-
"path"
9+
"net/url"
1010
"regexp"
1111
"strconv"
1212
"strings"
@@ -266,15 +266,17 @@ func ReadStack(ctx context.Context, d *schema.ResourceData, meta interface{}) di
266266
return nil
267267
}
268268

269-
FlattenStack(d, stack)
269+
if err := FlattenStack(d, stack); err != nil {
270+
return diag.FromErr(err)
271+
}
270272
// Always set the wait attribute to true after creation
271273
// It no longer matters and this will prevent drift if the stack was imported
272274
d.Set("wait_for_readiness", true)
273275

274276
return nil
275277
}
276278

277-
func FlattenStack(d *schema.ResourceData, stack gapi.Stack) {
279+
func FlattenStack(d *schema.ResourceData, stack gapi.Stack) error {
278280
id := strconv.FormatInt(stack.ID, 10)
279281

280282
d.SetId(id)
@@ -292,8 +294,16 @@ func FlattenStack(d *schema.ResourceData, stack gapi.Stack) {
292294
d.Set("prometheus_user_id", stack.HmInstancePromID)
293295
d.Set("prometheus_url", stack.HmInstancePromURL)
294296
d.Set("prometheus_name", stack.HmInstancePromName)
295-
d.Set("prometheus_remote_endpoint", path.Join(stack.HmInstancePromURL, "api/prom"))
296-
d.Set("prometheus_remote_write_endpoint", path.Join(stack.HmInstancePromURL, "api/prom/push"))
297+
reURL, err := appendPath(stack.HmInstancePromURL, "/api/prom")
298+
d.Set("prometheus_remote_endpoint", reURL)
299+
if err != nil {
300+
return err
301+
}
302+
rweURL, err := appendPath(stack.HmInstancePromURL, "/api/prom/push")
303+
if err != nil {
304+
return err
305+
}
306+
d.Set("prometheus_remote_write_endpoint", rweURL)
297307
d.Set("prometheus_status", stack.HmInstancePromStatus)
298308

299309
d.Set("logs_user_id", stack.HlInstanceID)
@@ -305,6 +315,21 @@ func FlattenStack(d *schema.ResourceData, stack gapi.Stack) {
305315
d.Set("alertmanager_name", stack.AmInstanceName)
306316
d.Set("alertmanager_url", stack.AmInstanceURL)
307317
d.Set("alertmanager_status", stack.AmInstanceStatus)
318+
319+
return nil
320+
}
321+
322+
// Append path to baseurl
323+
func appendPath(baseUrl, path string) (string, error) {
324+
bu, err := url.Parse(baseUrl)
325+
if err != nil {
326+
return "", err
327+
}
328+
u, err := bu.Parse(path)
329+
if err != nil {
330+
return "", err
331+
}
332+
return u.String(), nil
308333
}
309334

310335
// waitForStackReadiness retries until the stack is ready, verified by querying the Grafana URL

grafana/resource_cloud_stack_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func TestResourceCloudStack_Basic(t *testing.T) {
3838
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "name", resourceName),
3939
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "slug", resourceName),
4040
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "status", "active"),
41+
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "prometheus_remote_endpoint", "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom"),
42+
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "prometheus_remote_write_endpoint", "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push"),
4143
),
4244
},
4345
{

0 commit comments

Comments
 (0)