Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grafana_cloud_service_account read #1424

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Loading