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

One more fix for grafana_cloud_stack_service_account #1426

Merged
merged 2 commits into from
Mar 19, 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
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
}
Loading