|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "net/url"
|
| 7 | + "strconv" |
7 | 8 | "time"
|
8 | 9 |
|
9 | 10 | "github.com/grafana/grafana-com-public-clients/go/gcom"
|
@@ -100,18 +101,31 @@ func createStackServiceAccount(ctx context.Context, d *schema.ResourceData, clou
|
100 | 101 | }
|
101 | 102 |
|
102 | 103 | func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
|
103 |
| - split, err := resourceStackServiceAccountID.Split(d.Id()) |
104 |
| - if err != nil { |
105 |
| - return diag.FromErr(err) |
| 104 | + var stackSlug string |
| 105 | + var serviceAccountID int64 |
| 106 | + split, splitErr := resourceStackServiceAccountID.Split(d.Id()) |
| 107 | + if splitErr != nil { |
| 108 | + // ID used to be just the service account ID. |
| 109 | + // Even though that's an incomplete ID for imports, we need to handle it for backwards compatibility |
| 110 | + // TODO: Remove on next major version |
| 111 | + stackSlug = d.Get("stack_slug").(string) |
| 112 | + var parseErr error |
| 113 | + if serviceAccountID, parseErr = strconv.ParseInt(d.Id(), 10, 64); parseErr != nil { |
| 114 | + return diag.Errorf("failed to parse ID (%s) as stackSlug:serviceAccountID: %v and failed to parse as serviceAccountID: %v", d.Id(), splitErr, parseErr) |
| 115 | + } |
| 116 | + } else { |
| 117 | + stackSlug, serviceAccountID = split[0].(string), split[1].(int64) |
106 | 118 | }
|
107 |
| - stackSlug, serviceAccountID := split[0].(string), split[1].(int64) |
108 | 119 |
|
109 | 120 | client, cleanup, err := CreateTemporaryStackGrafanaClient(ctx, cloudClient, stackSlug, "terraform-temp-")
|
110 | 121 | if err != nil {
|
111 | 122 | return diag.FromErr(err)
|
112 | 123 | }
|
113 | 124 | defer cleanup()
|
114 | 125 |
|
| 126 | + d.Set("stack_slug", stackSlug) |
| 127 | + d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID)) |
| 128 | + |
115 | 129 | return readStackServiceAccountWithClient(client, d, serviceAccountID)
|
116 | 130 | }
|
117 | 131 |
|
@@ -162,6 +176,8 @@ func updateStackServiceAccount(ctx context.Context, d *schema.ResourceData, clou
|
162 | 176 | if _, err := client.ServiceAccounts.UpdateServiceAccount(updateRequest); err != nil {
|
163 | 177 | return diag.FromErr(err)
|
164 | 178 | }
|
| 179 | + d.Set("stack_slug", stackSlug) |
| 180 | + d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID)) |
165 | 181 |
|
166 | 182 | return readStackServiceAccountWithClient(client, d, serviceAccountID)
|
167 | 183 | }
|
|
0 commit comments