Skip to content

Commit 7bd9ba7

Browse files
Cloud: Optimize Cloud SA operations (#1356)
Currently, the `cloud_stack_(service_account|api_key)` resources create a temporary service account for all operations (The cloud API doesn't proxy the full CRUD) However, for operations like Create and Update, two SAs are created because it's customary to read after write in TF This PR changes the logic so that we only one temporary key is created
1 parent e639869 commit 7bd9ba7

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

internal/resources/cloud/resource_cloud_stack_api_key.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
goapi "github.com/grafana/grafana-openapi-client-go/client"
910
"github.com/grafana/grafana-openapi-client-go/client/api_keys"
1011
"github.com/grafana/grafana-openapi-client-go/models"
1112
"github.com/grafana/terraform-provider-grafana/internal/common"
@@ -105,7 +106,7 @@ func resourceStackAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m in
105106
}
106107

107108
// Fill the true resource's state after a create by performing a read
108-
return resourceStackAPIKeyRead(ctx, d, m)
109+
return resourceStackAPIKeyReadWithClient(c, d)
109110
}
110111

111112
func resourceStackAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
@@ -116,6 +117,10 @@ func resourceStackAPIKeyRead(ctx context.Context, d *schema.ResourceData, m inte
116117
}
117118
defer cleanup()
118119

120+
return resourceStackAPIKeyReadWithClient(c, d)
121+
}
122+
123+
func resourceStackAPIKeyReadWithClient(c *goapi.GrafanaHTTPAPI, d *schema.ResourceData) diag.Diagnostics {
119124
response, err := c.APIKeys.GetAPIkeys(api_keys.NewGetAPIkeysParams().WithIncludeExpired(common.Ref((true))))
120125
if err != nil {
121126
return diag.FromErr(err)

internal/resources/cloud/resource_cloud_stack_service_account.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func createStackServiceAccount(ctx context.Context, d *schema.ResourceData, meta
8585
sa := resp.Payload
8686

8787
d.SetId(strconv.FormatInt(sa.ID, 10))
88-
return readStackServiceAccount(ctx, d, meta)
88+
return readStackServiceAccountWithClient(client, d)
8989
}
9090

9191
func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
@@ -96,6 +96,10 @@ func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, meta i
9696
}
9797
defer cleanup()
9898

99+
return readStackServiceAccountWithClient(client, d)
100+
}
101+
102+
func readStackServiceAccountWithClient(client *goapi.GrafanaHTTPAPI, d *schema.ResourceData) diag.Diagnostics {
99103
id, err := strconv.ParseInt(d.Id(), 10, 64)
100104
if err != nil {
101105
return diag.FromErr(err)
@@ -148,7 +152,7 @@ func updateStackServiceAccount(ctx context.Context, d *schema.ResourceData, meta
148152
return diag.FromErr(err)
149153
}
150154

151-
return readStackServiceAccount(ctx, d, meta)
155+
return readStackServiceAccountWithClient(client, d)
152156
}
153157

154158
func deleteStackServiceAccount(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

internal/resources/cloud/resource_cloud_stack_service_account_token.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"strconv"
77

8+
goapi "github.com/grafana/grafana-openapi-client-go/client"
89
"github.com/grafana/grafana-openapi-client-go/client/service_accounts"
910
"github.com/grafana/grafana-openapi-client-go/models"
1011
"github.com/grafana/terraform-provider-grafana/internal/common"
@@ -98,7 +99,7 @@ func stackServiceAccountTokenCreate(ctx context.Context, d *schema.ResourceData,
9899
}
99100

100101
// Fill the true resource's state by performing a read
101-
return stackServiceAccountTokenRead(ctx, d, m)
102+
return stackServiceAccountTokenReadWithClient(c, d)
102103
}
103104

104105
func stackServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
@@ -109,6 +110,10 @@ func stackServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, m
109110
}
110111
defer cleanup()
111112

113+
return stackServiceAccountTokenReadWithClient(c, d)
114+
}
115+
116+
func stackServiceAccountTokenReadWithClient(c *goapi.GrafanaHTTPAPI, d *schema.ResourceData) diag.Diagnostics {
112117
serviceAccountID, err := strconv.ParseInt(d.Get("service_account_id").(string), 10, 64)
113118
if err != nil {
114119
return diag.FromErr(err)

0 commit comments

Comments
 (0)