Skip to content

Commit

Permalink
Fix grafana_cloud_service_account read (#1424)
Browse files Browse the repository at this point in the history
* Fix `grafana_cloud_service_account` read
Regression from #1412
In that PR, I noticed that the ID format for the stack service account is incomplete
It contained only the SA ID, and without the stack slug, it didn't really allow for imports
I fixed it, but now read operations from the old ID format didn't work anymore

To fix this, we can support the old ID format (just the SA ID) until the next major version

This is unfortunately not testable with the acceptance test framework, but I tested it manually

* Fix SM http check test that now randomly fails
Probably new validation in the API
  • Loading branch information
julienduchesne authored Mar 18, 2024
1 parent 20d1c5f commit a0b2f87
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/resources/synthetic_monitoring_check.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ EOS
]
fail_if_body_matches_regexp = [
"*bad stuff*",
".*bad stuff.*",
]
fail_if_body_not_matches_regexp = [
"*good stuff*",
".*good stuff.*",
]
fail_if_header_matches_regexp {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ EOS
]

fail_if_body_matches_regexp = [
"*bad stuff*",
".*bad stuff.*",
]

fail_if_body_not_matches_regexp = [
"*good stuff*",
".*good stuff.*",
]

fail_if_header_matches_regexp {
Expand Down
24 changes: 20 additions & 4 deletions internal/resources/cloud/resource_cloud_stack_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/url"
"strconv"
"time"

"github.com/grafana/grafana-com-public-clients/go/gcom"
Expand Down Expand Up @@ -100,18 +101,31 @@ func createStackServiceAccount(ctx context.Context, d *schema.ResourceData, clou
}

func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
split, err := resourceStackServiceAccountID.Split(d.Id())
if err != nil {
return diag.FromErr(err)
var stackSlug string
var serviceAccountID int64
split, splitErr := resourceStackServiceAccountID.Split(d.Id())
if splitErr != nil {
// ID used to be just the service account ID.
// Even though that's an incomplete ID for imports, we need to handle it for backwards compatibility
// TODO: Remove on next major version
stackSlug = d.Get("stack_slug").(string)
var parseErr error
if serviceAccountID, parseErr = strconv.ParseInt(d.Id(), 10, 64); parseErr != nil {
return diag.Errorf("failed to parse ID (%s) as stackSlug:serviceAccountID: %v and failed to parse as serviceAccountID: %v", d.Id(), splitErr, parseErr)
}
} else {
stackSlug, serviceAccountID = split[0].(string), split[1].(int64)
}
stackSlug, serviceAccountID := split[0].(string), split[1].(int64)

client, cleanup, err := CreateTemporaryStackGrafanaClient(ctx, cloudClient, stackSlug, "terraform-temp-")
if err != nil {
return diag.FromErr(err)
}
defer cleanup()

d.Set("stack_slug", stackSlug)
d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID))

return readStackServiceAccountWithClient(client, d, serviceAccountID)
}

Expand Down Expand Up @@ -162,6 +176,8 @@ func updateStackServiceAccount(ctx context.Context, d *schema.ResourceData, clou
if _, err := client.ServiceAccounts.UpdateServiceAccount(updateRequest); err != nil {
return diag.FromErr(err)
}
d.Set("stack_slug", stackSlug)
d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID))

return readStackServiceAccountWithClient(client, d, serviceAccountID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "false"),
),
},
{
ImportState: true,
ResourceName: "grafana_cloud_stack_service_account.management",
ImportStateVerify: true,
},
{
Config: testAccStackConfigBasic(slug, slug, "description"),
Check: testAccGrafanaAuthCheckServiceAccounts(&stack, []string{}),
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/syntheticmonitoring/resource_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func TestAccResourceCheck_http(t *testing.T) {
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.0", "HTTP/1.0"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.1", "HTTP/1.1"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.2", "HTTP/2.0"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_matches_regexp.0", "*bad stuff*"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_not_matches_regexp.0", "*good stuff*"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_matches_regexp.0", ".*bad stuff.*"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_not_matches_regexp.0", ".*good stuff.*"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.header", "Content-Type"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.regexp", "application/soap*"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.allow_missing", "true"),
Expand Down

0 comments on commit a0b2f87

Please sign in to comment.