Skip to content

Commit 232561e

Browse files
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
1 parent 20d1c5f commit 232561e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

internal/resources/cloud/resource_cloud_stack_service_account.go

Lines changed: 20 additions & 4 deletions
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

Lines changed: 5 additions & 0 deletions
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{}),

0 commit comments

Comments
 (0)