Skip to content

Commit a0b2f87

Browse files
Fix grafana_cloud_service_account read (#1424)
* 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
1 parent 20d1c5f commit a0b2f87

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

docs/resources/synthetic_monitoring_check.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ EOS
203203
]
204204
205205
fail_if_body_matches_regexp = [
206-
"*bad stuff*",
206+
".*bad stuff.*",
207207
]
208208
209209
fail_if_body_not_matches_regexp = [
210-
"*good stuff*",
210+
".*good stuff.*",
211211
]
212212
213213
fail_if_header_matches_regexp {

examples/resources/grafana_synthetic_monitoring_check/http_complex.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ EOS
7777
]
7878

7979
fail_if_body_matches_regexp = [
80-
"*bad stuff*",
80+
".*bad stuff.*",
8181
]
8282

8383
fail_if_body_not_matches_regexp = [
84-
"*good stuff*",
84+
".*good stuff.*",
8585
]
8686

8787
fail_if_header_matches_regexp {

internal/resources/cloud/resource_cloud_stack_service_account.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/url"
7+
"strconv"
78
"time"
89

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

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

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

126+
d.Set("stack_slug", stackSlug)
127+
d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID))
128+
115129
return readStackServiceAccountWithClient(client, d, serviceAccountID)
116130
}
117131

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

166182
return readStackServiceAccountWithClient(client, d, serviceAccountID)
167183
}

internal/resources/cloud/resource_cloud_stack_service_account_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
4747
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "false"),
4848
),
4949
},
50+
{
51+
ImportState: true,
52+
ResourceName: "grafana_cloud_stack_service_account.management",
53+
ImportStateVerify: true,
54+
},
5055
{
5156
Config: testAccStackConfigBasic(slug, slug, "description"),
5257
Check: testAccGrafanaAuthCheckServiceAccounts(&stack, []string{}),

internal/resources/syntheticmonitoring/resource_check_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func TestAccResourceCheck_http(t *testing.T) {
126126
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.0", "HTTP/1.0"),
127127
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.1", "HTTP/1.1"),
128128
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.2", "HTTP/2.0"),
129-
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_matches_regexp.0", "*bad stuff*"),
130-
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_not_matches_regexp.0", "*good stuff*"),
129+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_matches_regexp.0", ".*bad stuff.*"),
130+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_not_matches_regexp.0", ".*good stuff.*"),
131131
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.header", "Content-Type"),
132132
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.regexp", "application/soap*"),
133133
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.allow_missing", "true"),

0 commit comments

Comments
 (0)