Skip to content

Commit f567d37

Browse files
One more fix for grafana_cloud_stack_service_account (#1426)
* One more fix for `grafana_cloud_stack_service_account` Closes #1425 I didn't fix all of it in #1424, unfortunately. Now, I have a test though! * Remove trailing whiteline
1 parent 0194058 commit f567d37

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

internal/resources/cloud/resource_cloud_stack_service_account_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,53 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
6060
})
6161
}
6262

63+
// Tests that the ID change from 2.13.0 to latest works
64+
// Remove on next major release
65+
func TestAccGrafanaServiceAccountFromCloud_MigrateFrom213(t *testing.T) {
66+
testutils.CheckCloudAPITestsEnabled(t)
67+
68+
var stack gcom.FormattedApiInstance
69+
prefix := "tfsa213test"
70+
slug := GetRandomStackName(prefix)
71+
72+
check := resource.ComposeTestCheckFunc(
73+
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
74+
testAccGrafanaAuthCheckServiceAccounts(&stack, []string{"management-sa"}),
75+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "name", "management-sa"),
76+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "role", "Admin"),
77+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "true"),
78+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account_token.management_token", "name", "management-sa-token"),
79+
resource.TestCheckNoResourceAttr("grafana_cloud_stack_service_account_token.management_token", "expiration"),
80+
resource.TestCheckResourceAttrSet("grafana_cloud_stack_service_account_token.management_token", "key"),
81+
)
82+
83+
resource.ParallelTest(t, resource.TestCase{
84+
PreCheck: func() {
85+
testAccDeleteExistingStacks(t, prefix)
86+
},
87+
CheckDestroy: testAccStackCheckDestroy(&stack),
88+
Steps: []resource.TestStep{
89+
// Apply with 2.13.0 provider
90+
{
91+
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true),
92+
ExternalProviders: map[string]resource.ExternalProvider{
93+
"grafana": {
94+
VersionConstraint: "=2.13.0",
95+
Source: "grafana/grafana",
96+
},
97+
},
98+
Check: check,
99+
},
100+
// Apply with latest provider
101+
{
102+
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true),
103+
Check: check,
104+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
105+
},
106+
},
107+
})
108+
}
109+
63110
func testAccGrafanaServiceAccountFromCloud(name, slug string, disabled bool) string {
64111
return testAccStackConfigBasic(name, slug, "description") + fmt.Sprintf(`
65112
resource "grafana_cloud_stack_service_account" "management" {

internal/resources/cloud/resource_cloud_stack_service_account_token.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cloud
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"strconv"
78

@@ -83,11 +84,10 @@ func stackServiceAccountTokenCreate(ctx context.Context, d *schema.ResourceData,
8384
}
8485
defer cleanup()
8586

86-
split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string))
87+
serviceAccountID, err := getStackServiceAccountID(d.Get("service_account_id").(string))
8788
if err != nil {
8889
return diag.FromErr(err)
8990
}
90-
serviceAccountID := split[1].(int64)
9191

9292
name := d.Get("name").(string)
9393
ttl := d.Get("seconds_to_live").(int)
@@ -123,11 +123,10 @@ func stackServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, c
123123
}
124124

125125
func stackServiceAccountTokenReadWithClient(c *goapi.GrafanaHTTPAPI, d *schema.ResourceData) diag.Diagnostics {
126-
split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string))
126+
serviceAccountID, err := getStackServiceAccountID(d.Get("service_account_id").(string))
127127
if err != nil {
128128
return diag.FromErr(err)
129129
}
130-
serviceAccountID := split[1].(int64)
131130

132131
response, err := c.ServiceAccounts.ListTokens(serviceAccountID)
133132
if err != nil {
@@ -170,11 +169,10 @@ func stackServiceAccountTokenDelete(ctx context.Context, d *schema.ResourceData,
170169
}
171170
defer cleanup()
172171

173-
split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string))
172+
serviceAccountID, err := getStackServiceAccountID(d.Get("service_account_id").(string))
174173
if err != nil {
175174
return diag.FromErr(err)
176175
}
177-
serviceAccountID := split[1].(int64)
178176

179177
id, err := strconv.ParseInt(d.Id(), 10, 32)
180178
if err != nil {
@@ -188,3 +186,18 @@ func stackServiceAccountTokenDelete(ctx context.Context, d *schema.ResourceData,
188186

189187
return nil
190188
}
189+
190+
func getStackServiceAccountID(id string) (int64, error) {
191+
split, splitErr := resourceStackServiceAccountID.Split(id)
192+
if splitErr != nil {
193+
// ID used to be just the service account ID.
194+
// Even though that's an incomplete ID for imports, we need to handle it for backwards compatibility
195+
// TODO: Remove on next major version
196+
serviceAccountID, parseErr := strconv.ParseInt(id, 10, 64)
197+
if parseErr != nil {
198+
return 0, fmt.Errorf("failed to parse ID (%s) as stackSlug:serviceAccountID: %v and failed to parse as serviceAccountID: %v", id, splitErr, parseErr)
199+
}
200+
return serviceAccountID, nil
201+
}
202+
return split[1].(int64), nil
203+
}

0 commit comments

Comments
 (0)