From e11ceb555bcf5ee266f2654c99164f274e4e36ba Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Wed, 21 Feb 2024 09:47:24 -0500 Subject: [PATCH] Add OTLP endpoint to cloud stacks Closes https://github.com/grafana/terraform-provider-grafana/issues/1358 --- docs/data-sources/cloud_stack.md | 1 + docs/resources/cloud_stack.md | 1 + go.mod | 2 +- go.sum | 4 ++-- .../cloud/data_source_cloud_organization.go | 2 +- .../cloud/data_source_cloud_stack.go | 17 ++------------ .../resources/cloud/resource_cloud_stack.go | 22 ++++++++++++++++--- .../cloud/resource_cloud_stack_test.go | 1 + 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/docs/data-sources/cloud_stack.md b/docs/data-sources/cloud_stack.md index 57c768b10..8bf80e421 100644 --- a/docs/data-sources/cloud_stack.md +++ b/docs/data-sources/cloud_stack.md @@ -53,6 +53,7 @@ available at “https://.grafana.net". - `org_id` (Number) Organization id to assign to this stack. - `org_name` (String) Organization name to assign to this stack. - `org_slug` (String) Organization slug to assign to this stack. +- `otlp_url` (String) Base URL of the OTLP instance configured for this stack. See https://grafana.com/docs/grafana-cloud/send-data/otlp/send-data-otlp/ for docs on how to use this. - `profiles_name` (String) - `profiles_status` (String) - `profiles_url` (String) diff --git a/docs/resources/cloud_stack.md b/docs/resources/cloud_stack.md index daa1832d9..88ee9b8f5 100644 --- a/docs/resources/cloud_stack.md +++ b/docs/resources/cloud_stack.md @@ -56,6 +56,7 @@ available at “https://.grafana.net". - `org_id` (Number) Organization id to assign to this stack. - `org_name` (String) Organization name to assign to this stack. - `org_slug` (String) Organization slug to assign to this stack. +- `otlp_url` (String) Base URL of the OTLP instance configured for this stack. See https://grafana.com/docs/grafana-cloud/send-data/otlp/send-data-otlp/ for docs on how to use this. - `profiles_name` (String) - `profiles_status` (String) - `profiles_url` (String) diff --git a/go.mod b/go.mod index ce5c4ccb6..72d46ddae 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/go-openapi/runtime v0.27.1 github.com/go-openapi/strfmt v0.22.0 github.com/grafana/amixr-api-go-client v0.0.11 - github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240205204245-e327bebd7c02 + github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240220151716-042876eff9fb github.com/grafana/grafana-openapi-client-go v0.0.0-20240131162504-9263d72bd697 github.com/grafana/machine-learning-go-client v0.5.0 github.com/grafana/slo-openapi-client/go v0.0.0-20240112175006-de02e75b9d73 diff --git a/go.sum b/go.sum index c9d14172a..ae4bd8ac3 100644 --- a/go.sum +++ b/go.sum @@ -98,8 +98,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grafana/amixr-api-go-client v0.0.11 h1:jlE+5t0tRuCtjbpM81j70Dr2J4eCySuWyNGdfLMGdhE= github.com/grafana/amixr-api-go-client v0.0.11/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE= -github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240205204245-e327bebd7c02 h1:TvPNghzCF1/DC+PGi2fccwayVS/8wCB1X6E4etuosao= -github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240205204245-e327bebd7c02/go.mod h1:6sYY1qgwYfSDNQhKQA0tar8Oc38cIGfyqwejhxoOsPs= +github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240220151716-042876eff9fb h1:9lMBTa0DTo6/sluXCOA6UO/xxKPq8jWIID57KkoKQj0= +github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240220151716-042876eff9fb/go.mod h1:6sYY1qgwYfSDNQhKQA0tar8Oc38cIGfyqwejhxoOsPs= github.com/grafana/grafana-openapi-client-go v0.0.0-20240131162504-9263d72bd697 h1:pqQoKCVWXVtPwgCjFcCtuWiElkSNv1a97uUjiYEUl0k= github.com/grafana/grafana-openapi-client-go v0.0.0-20240131162504-9263d72bd697/go.mod h1:EapKj5Z1OKDssvRofVwD8XEuJtZUaysLWPFFM2QoK0Q= github.com/grafana/machine-learning-go-client v0.5.0 h1:Q1K+MPSy8vfMm2jsk3WQ7O77cGr2fM5hxwtPSoPc5NU= diff --git a/internal/resources/cloud/data_source_cloud_organization.go b/internal/resources/cloud/data_source_cloud_organization.go index a8b2e12fc..c1ca0b45b 100644 --- a/internal/resources/cloud/data_source_cloud_organization.go +++ b/internal/resources/cloud/data_source_cloud_organization.go @@ -55,7 +55,7 @@ func DataSourceOrganizationRead(ctx context.Context, d *schema.ResourceData, met return apiError(err) } - id = strconv.FormatInt(int64(*org.Id), 10) + id = strconv.FormatInt(int64(org.Id), 10) d.SetId(id) d.Set("id", id) d.Set("name", org.Name) diff --git a/internal/resources/cloud/data_source_cloud_stack.go b/internal/resources/cloud/data_source_cloud_stack.go index 08a9ffdd6..91305c2b9 100644 --- a/internal/resources/cloud/data_source_cloud_stack.go +++ b/internal/resources/cloud/data_source_cloud_stack.go @@ -32,19 +32,6 @@ available at “https://.grafana.net".`, } func DataSourceStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(*common.Client).GrafanaCloudAPI - - slug := d.Get("slug").(string) - - req := client.InstancesAPI.GetInstance(ctx, slug) - stack, _, err := req.Execute() - if err != nil { - return apiError(err) - } - - if err := FlattenStack(d, stack); err != nil { - return diag.FromErr(err) - } - - return nil + d.SetId(d.Get("slug").(string)) + return ReadStack(ctx, d, meta) } diff --git a/internal/resources/cloud/resource_cloud_stack.go b/internal/resources/cloud/resource_cloud_stack.go index e1dfd5080..2f988dbb1 100644 --- a/internal/resources/cloud/resource_cloud_stack.go +++ b/internal/resources/cloud/resource_cloud_stack.go @@ -143,6 +143,9 @@ available at “https://.grafana.net".`, "graphite_name": common.ComputedString(), "graphite_url": common.ComputedString(), "graphite_status": common.ComputedString(), + + // OTLP + "otlp_url": common.ComputedStringWithDescription("Base URL of the OTLP instance configured for this stack. See https://grafana.com/docs/grafana-cloud/send-data/otlp/send-data-otlp/ for docs on how to use this."), }, CustomizeDiff: customdiff.All( customdiff.ComputedIf("url", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool { @@ -263,17 +266,26 @@ func ReadStack(ctx context.Context, d *schema.ResourceData, meta interface{}) di return nil } - if err := FlattenStack(d, stack); err != nil { + connectionsReq := client.InstancesAPI.GetConnections(ctx, d.Id()) + connections, _, err := connectionsReq.Execute() + if err != nil { + return apiError(err) + } + + if err := FlattenStack(d, stack, connections); err != nil { return diag.FromErr(err) } // Always set the wait attribute to true after creation // It no longer matters and this will prevent drift if the stack was imported - d.Set("wait_for_readiness", true) + // The "if" condition is here to allow using the same Read function for the data source + if v, ok := d.GetOk("wait_for_readiness"); ok && !v.(bool) { + d.Set("wait_for_readiness", true) + } return nil } -func FlattenStack(d *schema.ResourceData, stack *gcom.FormattedApiInstance) error { +func FlattenStack(d *schema.ResourceData, stack *gcom.FormattedApiInstance, connections *gcom.FormattedApiInstanceConnections) error { id := strconv.FormatInt(int64(stack.Id), 10) d.SetId(id) d.Set("name", stack.Name) @@ -327,6 +339,10 @@ func FlattenStack(d *schema.ResourceData, stack *gcom.FormattedApiInstance) erro d.Set("graphite_url", stack.HmInstanceGraphiteUrl) d.Set("graphite_status", stack.HmInstanceGraphiteStatus) + if otlpURL := connections.OtlpHttpUrl; otlpURL.IsSet() { + d.Set("otlp_url", otlpURL.Get()) + } + return nil } diff --git a/internal/resources/cloud/resource_cloud_stack_test.go b/internal/resources/cloud/resource_cloud_stack_test.go index 5e3f28929..b4195bb5e 100644 --- a/internal/resources/cloud/resource_cloud_stack_test.go +++ b/internal/resources/cloud/resource_cloud_stack_test.go @@ -45,6 +45,7 @@ func TestResourceStack_Basic(t *testing.T) { resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "profiles_name"), resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "profiles_url"), resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "profiles_status"), + resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "otlp_url"), ) resource.ParallelTest(t, resource.TestCase{