Skip to content

Commit

Permalink
One more fix for grafana_cloud_stack_service_account (#1426)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
julienduchesne authored Mar 19, 2024
1 parent 0194058 commit f567d37
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,53 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
})
}

// Tests that the ID change from 2.13.0 to latest works
// Remove on next major release
func TestAccGrafanaServiceAccountFromCloud_MigrateFrom213(t *testing.T) {
testutils.CheckCloudAPITestsEnabled(t)

var stack gcom.FormattedApiInstance
prefix := "tfsa213test"
slug := GetRandomStackName(prefix)

check := resource.ComposeTestCheckFunc(
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
testAccGrafanaAuthCheckServiceAccounts(&stack, []string{"management-sa"}),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "name", "management-sa"),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "role", "Admin"),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "true"),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account_token.management_token", "name", "management-sa-token"),
resource.TestCheckNoResourceAttr("grafana_cloud_stack_service_account_token.management_token", "expiration"),
resource.TestCheckResourceAttrSet("grafana_cloud_stack_service_account_token.management_token", "key"),
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccDeleteExistingStacks(t, prefix)
},
CheckDestroy: testAccStackCheckDestroy(&stack),
Steps: []resource.TestStep{
// Apply with 2.13.0 provider
{
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true),
ExternalProviders: map[string]resource.ExternalProvider{
"grafana": {
VersionConstraint: "=2.13.0",
Source: "grafana/grafana",
},
},
Check: check,
},
// Apply with latest provider
{
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true),
Check: check,
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
},
},
})
}

func testAccGrafanaServiceAccountFromCloud(name, slug string, disabled bool) string {
return testAccStackConfigBasic(name, slug, "description") + fmt.Sprintf(`
resource "grafana_cloud_stack_service_account" "management" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloud

import (
"context"
"fmt"
"log"
"strconv"

Expand Down Expand Up @@ -83,11 +84,10 @@ func stackServiceAccountTokenCreate(ctx context.Context, d *schema.ResourceData,
}
defer cleanup()

split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string))
serviceAccountID, err := getStackServiceAccountID(d.Get("service_account_id").(string))
if err != nil {
return diag.FromErr(err)
}
serviceAccountID := split[1].(int64)

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

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

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

split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string))
serviceAccountID, err := getStackServiceAccountID(d.Get("service_account_id").(string))
if err != nil {
return diag.FromErr(err)
}
serviceAccountID := split[1].(int64)

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

return nil
}

func getStackServiceAccountID(id string) (int64, error) {
split, splitErr := resourceStackServiceAccountID.Split(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
serviceAccountID, parseErr := strconv.ParseInt(id, 10, 64)
if parseErr != nil {
return 0, fmt.Errorf("failed to parse ID (%s) as stackSlug:serviceAccountID: %v and failed to parse as serviceAccountID: %v", id, splitErr, parseErr)
}
return serviceAccountID, nil
}
return split[1].(int64), nil
}

0 comments on commit f567d37

Please sign in to comment.