Skip to content

Commit

Permalink
Fix grafana_cloud_stack_api_key panic (#848)
Browse files Browse the repository at this point in the history
* Fix `grafana_cloud_stack_api_key` panic
The resource wasn't even tested, because the tests were still using `grafana_api_key`

* Add retry on cloud API key creation. The instance may be offline
  • Loading branch information
julienduchesne authored Mar 10, 2023
1 parent 7d0dabe commit 6b3396f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
23 changes: 18 additions & 5 deletions internal/resources/cloud/resource_cloud_stack_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package cloud
import (
"context"
"strconv"
"strings"
"time"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -77,14 +79,25 @@ func resourceStackAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m in
defer cleanup()

request := gapi.CreateAPIKeyRequest{Name: name, Role: role, SecondsToLive: int64(ttl)}
response, err := c.CreateAPIKey(request)
err = resource.RetryContext(ctx, 2*time.Minute, func() *resource.RetryError {
response, err := c.CreateAPIKey(request)

if err != nil {
if strings.Contains(err.Error(), "Your instance is loading, and will be ready shortly.") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}

d.SetId(strconv.FormatInt(response.ID, 10))
d.Set("key", response.Key)
return nil
})

if err != nil {
return diag.FromErr(err)
}

d.SetId(strconv.FormatInt(response.ID, 10))
d.Set("key", response.Key)

// Fill the true resource's state after a create by performing a read
return resourceStackAPIKeyRead(ctx, d, m)
}
Expand Down Expand Up @@ -147,5 +160,5 @@ func resourceStackAPIKeyDelete(ctx context.Context, d *schema.ResourceData, m in

func getClientForAPIKeyManagement(d *schema.ResourceData, m interface{}) (c *gapi.Client, cleanup func() error, err error) {
cloudClient := m.(*common.Client).GrafanaCloudAPI
return cloudClient.CreateTemporaryStackGrafanaClient(d.Get("stack-slug").(string), "terraform-temp-", 60*time.Second)
return cloudClient.CreateTemporaryStackGrafanaClient(d.Get("stack_slug").(string), "terraform-temp-", 60*time.Second)
}
10 changes: 5 additions & 5 deletions internal/resources/cloud/resource_cloud_stack_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func TestAccGrafanaAuthKeyFromCloud(t *testing.T) {
Config: testAccGrafanaAuthKeyFromCloud(slug, slug),
Check: resource.ComposeTestCheckFunc(
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
resource.TestCheckResourceAttrSet("grafana_api_key.foo", "key"),
resource.TestCheckResourceAttr("grafana_api_key.foo", "name", "management-key"),
resource.TestCheckResourceAttr("grafana_api_key.foo", "role", "Admin"),
resource.TestCheckNoResourceAttr("grafana_api_key.foo", "expiration"),
resource.TestCheckResourceAttrSet("grafana_cloud_stack_api_key.management", "key"),
resource.TestCheckResourceAttr("grafana_cloud_stack_api_key.management", "name", "management-key"),
resource.TestCheckResourceAttr("grafana_cloud_stack_api_key.management", "role", "Admin"),
resource.TestCheckNoResourceAttr("grafana_cloud_stack_api_key.management", "expiration"),
),
},
{
Expand All @@ -48,7 +48,7 @@ func TestAccGrafanaAuthKeyFromCloud(t *testing.T) {

func testAccGrafanaAuthKeyFromCloud(name, slug string) string {
return testAccStackConfigBasic(name, slug) + `
resource "grafana_api_key" "management" {
resource "grafana_cloud_stack_api_key" "management" {
stack_slug = grafana_cloud_stack.test.slug
name = "management-key"
role = "Admin"
Expand Down

0 comments on commit 6b3396f

Please sign in to comment.