-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathresource_cloud_stack_service_account.go
212 lines (182 loc) · 6.6 KB
/
resource_cloud_stack_service_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cloud
import (
"context"
"fmt"
"net/url"
"strconv"
"time"
"github.com/grafana/grafana-com-public-clients/go/gcom"
goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
var (
resourceStackServiceAccountID = common.NewResourceID(
common.StringIDField("stackSlug"),
common.IntIDField("serviceAccountID"),
)
)
func resourceStackServiceAccount() *common.Resource {
schema := &schema.Resource{
Description: `
Manages service accounts of a Grafana Cloud stack using the Cloud API
This can be used to bootstrap a management service account for a new stack
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/service-accounts/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/serviceaccount/#service-account-api)
Required access policy scopes:
* stacks:read
* stack-service-accounts:write
`,
CreateContext: withClient[schema.CreateContextFunc](createStackServiceAccount),
ReadContext: withClient[schema.ReadContextFunc](readStackServiceAccount),
DeleteContext: withClient[schema.DeleteContextFunc](deleteStackServiceAccount),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"stack_slug": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the service account.",
ForceNew: true,
},
"role": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"Viewer", "Editor", "Admin", "None"}, false),
Description: "The basic role of the service account in the organization.",
ForceNew: true, // The grafana API does not support updating the service account
},
"is_disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "The disabled status for the service account.",
ForceNew: true, // The grafana API does not support updating the service account
},
},
}
return common.NewLegacySDKResource(
"grafana_cloud_stack_service_account",
resourceStackServiceAccountID,
schema,
)
}
func createStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
if err := waitForStackReadinessFromSlug(ctx, 5*time.Minute, d.Get("stack_slug").(string), cloudClient); err != nil {
return err
}
stackSlug := d.Get("stack_slug").(string)
req := gcom.PostInstanceServiceAccountsRequest{
Name: d.Get("name").(string),
Role: d.Get("role").(string),
IsDisabled: common.Ref(d.Get("is_disabled").(bool)),
}
resp, _, err := cloudClient.InstancesAPI.PostInstanceServiceAccounts(ctx, stackSlug).
PostInstanceServiceAccountsRequest(req).
XRequestId(ClientRequestID()).
Execute()
if err != nil {
return diag.FromErr(err)
}
d.SetId(resourceStackServiceAccountID.Make(stackSlug, resp.Id))
return readStackServiceAccount(ctx, d, cloudClient)
}
func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
var stackSlug string
var serviceAccountID int64
split, splitErr := resourceStackServiceAccountID.Split(d.Id())
if splitErr != nil {
stackSlug = d.Get("stack_slug").(string)
var parseErr error
if serviceAccountID, parseErr = strconv.ParseInt(d.Id(), 10, 64); parseErr != nil {
return diag.Errorf("failed to parse ID (%s) as stackSlug:serviceAccountID: %v and failed to parse as serviceAccountID: %v", d.Id(), splitErr, parseErr)
}
} else {
stackSlug, serviceAccountID = split[0].(string), split[1].(int64)
}
if err := waitForStackReadinessFromSlug(ctx, 5*time.Minute, stackSlug, cloudClient); err != nil {
return err
}
resp, httpResp, err := cloudClient.InstancesAPI.GetInstanceServiceAccount(ctx, stackSlug, strconv.FormatInt(serviceAccountID, 10)).Execute()
if httpResp != nil && httpResp.StatusCode == 404 {
d.SetId("")
return nil
}
if err != nil {
return diag.FromErr(err)
}
d.Set("stack_slug", stackSlug)
d.Set("name", resp.Name)
d.Set("role", resp.Role)
d.Set("is_disabled", resp.IsDisabled)
d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID))
return nil
}
func deleteStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
if err := waitForStackReadinessFromSlug(ctx, 5*time.Minute, d.Get("stack_slug").(string), cloudClient); err != nil {
return err
}
split, err := resourceStackServiceAccountID.Split(d.Id())
if err != nil {
return diag.FromErr(err)
}
stackSlug, serviceAccountID := split[0].(string), split[1].(int64)
_, err = cloudClient.InstancesAPI.DeleteInstanceServiceAccount(ctx, stackSlug, strconv.FormatInt(serviceAccountID, 10)).
XRequestId(ClientRequestID()).
Execute()
return diag.FromErr(err)
}
func CreateTemporaryStackGrafanaClient(ctx context.Context, cloudClient *gcom.APIClient, stackSlug, tempSaPrefix string) (*goapi.GrafanaHTTPAPI, func() error, error) {
stack, _, err := cloudClient.InstancesAPI.GetInstance(ctx, stackSlug).Execute()
if err != nil {
return nil, nil, err
}
name := fmt.Sprintf("%s%d", tempSaPrefix, time.Now().UnixNano())
req := gcom.PostInstanceServiceAccountsRequest{
Name: name,
Role: "Admin",
}
sa, _, err := cloudClient.InstancesAPI.PostInstanceServiceAccounts(ctx, stackSlug).
PostInstanceServiceAccountsRequest(req).
XRequestId(ClientRequestID()).
Execute()
if err != nil {
return nil, nil, err
}
tokenRequest := gcom.PostInstanceServiceAccountTokensRequest{
Name: name,
SecondsToLive: common.Ref(int32(60)),
}
token, _, err := cloudClient.InstancesAPI.PostInstanceServiceAccountTokens(ctx, stackSlug, fmt.Sprintf("%d", int(*sa.Id))).
PostInstanceServiceAccountTokensRequest(tokenRequest).
XRequestId(ClientRequestID()).
Execute()
if err != nil {
return nil, nil, err
}
stackURLParsed, err := url.Parse(stack.Url)
if err != nil {
return nil, nil, err
}
client := goapi.NewHTTPClientWithConfig(nil, &goapi.TransportConfig{
Host: stackURLParsed.Host,
Schemes: []string{stackURLParsed.Scheme},
BasePath: "api",
APIKey: *token.Key,
NumRetries: 5,
RetryTimeout: 10 * time.Second,
})
cleanup := func() error {
_, err = client.ServiceAccounts.DeleteServiceAccount(*sa.Id)
return err
}
return client, cleanup, nil
}