Skip to content

Commit 6b3396f

Browse files
Fix grafana_cloud_stack_api_key panic (#848)
* 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
1 parent 7d0dabe commit 6b3396f

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

internal/resources/cloud/resource_cloud_stack_api_key.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package cloud
33
import (
44
"context"
55
"strconv"
6+
"strings"
67
"time"
78

89
gapi "github.com/grafana/grafana-api-golang-client"
910
"github.com/grafana/terraform-provider-grafana/internal/common"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1315
)
@@ -77,14 +79,25 @@ func resourceStackAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m in
7779
defer cleanup()
7880

7981
request := gapi.CreateAPIKeyRequest{Name: name, Role: role, SecondsToLive: int64(ttl)}
80-
response, err := c.CreateAPIKey(request)
82+
err = resource.RetryContext(ctx, 2*time.Minute, func() *resource.RetryError {
83+
response, err := c.CreateAPIKey(request)
84+
85+
if err != nil {
86+
if strings.Contains(err.Error(), "Your instance is loading, and will be ready shortly.") {
87+
return resource.RetryableError(err)
88+
}
89+
return resource.NonRetryableError(err)
90+
}
91+
92+
d.SetId(strconv.FormatInt(response.ID, 10))
93+
d.Set("key", response.Key)
94+
return nil
95+
})
96+
8197
if err != nil {
8298
return diag.FromErr(err)
8399
}
84100

85-
d.SetId(strconv.FormatInt(response.ID, 10))
86-
d.Set("key", response.Key)
87-
88101
// Fill the true resource's state after a create by performing a read
89102
return resourceStackAPIKeyRead(ctx, d, m)
90103
}
@@ -147,5 +160,5 @@ func resourceStackAPIKeyDelete(ctx context.Context, d *schema.ResourceData, m in
147160

148161
func getClientForAPIKeyManagement(d *schema.ResourceData, m interface{}) (c *gapi.Client, cleanup func() error, err error) {
149162
cloudClient := m.(*common.Client).GrafanaCloudAPI
150-
return cloudClient.CreateTemporaryStackGrafanaClient(d.Get("stack-slug").(string), "terraform-temp-", 60*time.Second)
163+
return cloudClient.CreateTemporaryStackGrafanaClient(d.Get("stack_slug").(string), "terraform-temp-", 60*time.Second)
151164
}

internal/resources/cloud/resource_cloud_stack_api_key_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ func TestAccGrafanaAuthKeyFromCloud(t *testing.T) {
3232
Config: testAccGrafanaAuthKeyFromCloud(slug, slug),
3333
Check: resource.ComposeTestCheckFunc(
3434
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
35-
resource.TestCheckResourceAttrSet("grafana_api_key.foo", "key"),
36-
resource.TestCheckResourceAttr("grafana_api_key.foo", "name", "management-key"),
37-
resource.TestCheckResourceAttr("grafana_api_key.foo", "role", "Admin"),
38-
resource.TestCheckNoResourceAttr("grafana_api_key.foo", "expiration"),
35+
resource.TestCheckResourceAttrSet("grafana_cloud_stack_api_key.management", "key"),
36+
resource.TestCheckResourceAttr("grafana_cloud_stack_api_key.management", "name", "management-key"),
37+
resource.TestCheckResourceAttr("grafana_cloud_stack_api_key.management", "role", "Admin"),
38+
resource.TestCheckNoResourceAttr("grafana_cloud_stack_api_key.management", "expiration"),
3939
),
4040
},
4141
{
@@ -48,7 +48,7 @@ func TestAccGrafanaAuthKeyFromCloud(t *testing.T) {
4848

4949
func testAccGrafanaAuthKeyFromCloud(name, slug string) string {
5050
return testAccStackConfigBasic(name, slug) + `
51-
resource "grafana_api_key" "management" {
51+
resource "grafana_cloud_stack_api_key" "management" {
5252
stack_slug = grafana_cloud_stack.test.slug
5353
name = "management-key"
5454
role = "Admin"

0 commit comments

Comments
 (0)