Skip to content

Commit d340716

Browse files
authored
Add delete protection (#2201)
1 parent 6a7415e commit d340716

File tree

8 files changed

+29
-22
lines changed

8 files changed

+29
-22
lines changed

docs/data-sources/cloud_stack.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ available at “https://<stack_slug>.grafana.net".
4141
- `alertmanager_url` (String) Base URL of the Alertmanager instance configured for this stack.
4242
- `alertmanager_user_id` (Number) User ID of the Alertmanager instance configured for this stack.
4343
- `cluster_slug` (String) Slug of the cluster where this stack resides.
44+
- `delete_protection` (Boolean) Whether to enable delete protection for the stack, preventing accidental deletion.
4445
- `description` (String) Description of stack.
4546
- `fleet_management_name` (String) Name of the Fleet Management instance configured for this stack.
4647
- `fleet_management_private_connectivity_info_private_dns` (String) Private DNS for Fleet Management when using AWS PrivateLink (only for AWS stacks)

docs/resources/cloud_stack.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ resource "grafana_cloud_stack" "test" {
3939

4040
### Optional
4141

42+
- `delete_protection` (Boolean) Whether to enable delete protection for the stack, preventing accidental deletion. Defaults to `true`.
4243
- `description` (String) Description of stack.
4344
- `labels` (Map of String) A map of labels to assign to the stack. Label keys and values must match the following regexp: "^[a-zA-Z0-9/\\-.]+$" and stacks cannot have more than 10 labels.
4445
- `region_slug` (String) Region slug to assign to this stack. Changing region will destroy the existing stack and create a new one in the desired region. Use the region list API to get the list of available regions: https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#list-regions.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/grafana/authlib/claims v0.0.0-20250120084028-e3328c576437
1313
github.com/grafana/fleet-management-api v1.0.0
1414
github.com/grafana/grafana-app-sdk v0.35.2-0.20250408075831-c2a87bde0849
15-
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250225152211-076f0759931d
15+
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb
1616
github.com/grafana/grafana-openapi-client-go v0.0.0-20250516123951-83fcd32d7bbe
1717
github.com/grafana/grafana/apps/dashboard v0.0.0-20250424064802-2fbb2d6f5d27
1818
github.com/grafana/grafana/apps/playlist v0.0.0-20250424064802-2fbb2d6f5d27

go.sum

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1
168168
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
169169
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
170170
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
171-
github.com/grafana/amixr-api-go-client v0.0.23 h1:9It5KYc79J8znWBriOezF723+1cyB/PWiS/4/RpaoVI=
172-
github.com/grafana/amixr-api-go-client v0.0.23/go.mod h1:ihgLhTVimmjASuZ06y/mQxPcYH3toAIuUVGK6flHsMU=
173171
github.com/grafana/amixr-api-go-client v0.0.24 h1:Yvj8Ir02e3GTcetd+qHmajrLC690YJxK8lppEUkrsyA=
174172
github.com/grafana/amixr-api-go-client v0.0.24/go.mod h1:ihgLhTVimmjASuZ06y/mQxPcYH3toAIuUVGK6flHsMU=
175173
github.com/grafana/authlib/claims v0.0.0-20250120084028-e3328c576437 h1:OlwbIVFcYgMjnQhpbZwRPVNrvZKTodvPMqwb8yEqVW0=
@@ -180,12 +178,8 @@ github.com/grafana/grafana-app-sdk v0.35.2-0.20250408075831-c2a87bde0849 h1:fzoG
180178
github.com/grafana/grafana-app-sdk v0.35.2-0.20250408075831-c2a87bde0849/go.mod h1:hQFtfn65wd/zTcQ/ftwOpRTr/B37MfIzPZD8jGoAx5E=
181179
github.com/grafana/grafana-app-sdk/logging v0.35.1 h1:taVpl+RoixTYl0JBJGhH+fPVmwA9wvdwdzJTZsv9buM=
182180
github.com/grafana/grafana-app-sdk/logging v0.35.1/go.mod h1:Y/bvbDhBiV/tkIle9RW49pgfSPIPSON8Q4qjx3pyqDk=
183-
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250214150112-a52892176c26 h1:7NMB6/x0CcfH/zKQ5D+3Ffb2DbYMJBx0QdJ1GGdw8z4=
184-
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250214150112-a52892176c26/go.mod h1:sYWkB3NhyirQJfy3wtNQ29UYjoHbRlJlYhqN1jNsC5g=
185-
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250225152211-076f0759931d h1:CS04cUrE9ke/S5sbaH01gW4JcEy3joLKMJO5Vu3T2/4=
186-
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250225152211-076f0759931d/go.mod h1:sYWkB3NhyirQJfy3wtNQ29UYjoHbRlJlYhqN1jNsC5g=
187-
github.com/grafana/grafana-openapi-client-go v0.0.0-20250424142317-beadd3136e10 h1:RznghhbjMUEvGJD0p9AOCjnVOTq0MiINDt98BscNcdw=
188-
github.com/grafana/grafana-openapi-client-go v0.0.0-20250424142317-beadd3136e10/go.mod h1:hiZnMmXc9KXNUlvkV2BKFsiWuIFF/fF4wGgYWEjBitI=
181+
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb h1:rmYEnCXHNQbRsuzc5jCX5qkBqFF37c5RCHlyqAAPJZo=
182+
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb/go.mod h1:sYWkB3NhyirQJfy3wtNQ29UYjoHbRlJlYhqN1jNsC5g=
189183
github.com/grafana/grafana-openapi-client-go v0.0.0-20250516123951-83fcd32d7bbe h1:OdLLQKwEBVVhe9wHAncGp+Ff1N5aDl7erDIBdO9xBmA=
190184
github.com/grafana/grafana-openapi-client-go v0.0.0-20250516123951-83fcd32d7bbe/go.mod h1:hiZnMmXc9KXNUlvkV2BKFsiWuIFF/fF4wGgYWEjBitI=
191185
github.com/grafana/grafana-plugin-sdk-go v0.275.0 h1:icGmZG91lVqIo79w/pSki6N44d3IjOjTfsfQPfu4THU=

internal/resources/cloud/resource_cloud_access_policy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func flattenCloudAccessPolicyConditions(condition *gcom.AuthAccessPolicyConditio
336336
return result
337337
}
338338

339-
func expandCloudAccessPolicyConditions(condition []interface{}) *gcom.PostAccessPoliciesRequestConditions {
339+
func expandCloudAccessPolicyConditions(condition []interface{}) gcom.NullablePostAccessPoliciesRequestConditions {
340340
var result gcom.PostAccessPoliciesRequestConditions
341341

342342
for _, c := range condition {
@@ -346,7 +346,7 @@ func expandCloudAccessPolicyConditions(condition []interface{}) *gcom.PostAccess
346346
}
347347
}
348348

349-
return &result
349+
return *gcom.NewNullablePostAccessPoliciesRequestConditions(&result)
350350
}
351351

352352
func expandCloudAccessPolicyRealm(realm []interface{}) []gcom.PostAccessPoliciesRequestRealmsInner {

internal/resources/cloud/resource_cloud_stack.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ Required access policy scopes:
155155
return nil, nil
156156
},
157157
},
158+
"delete_protection": {
159+
Type: schema.TypeBool,
160+
Optional: true,
161+
Default: true,
162+
Description: "Whether to enable delete protection for the stack, preventing accidental deletion.",
163+
},
158164

159165
"grafanas_ip_allow_list_cname": ipAllowListCNAMEDescription("the grafana instance"),
160166

@@ -279,12 +285,13 @@ func listStacks(ctx context.Context, client *gcom.APIClient, data *ListerData) (
279285

280286
func createStack(ctx context.Context, d *schema.ResourceData, client *gcom.APIClient) diag.Diagnostics {
281287
stack := gcom.PostInstancesRequest{
282-
Name: d.Get("name").(string),
283-
Slug: common.Ref(d.Get("slug").(string)),
284-
Url: common.Ref(d.Get("url").(string)),
285-
Region: common.Ref(d.Get("region_slug").(string)),
286-
Description: common.Ref(d.Get("description").(string)),
287-
Labels: common.Ref(common.UnpackMap[string](d.Get("labels"))),
288+
Name: d.Get("name").(string),
289+
Slug: common.Ref(d.Get("slug").(string)),
290+
Url: common.Ref(d.Get("url").(string)),
291+
Region: common.Ref(d.Get("region_slug").(string)),
292+
Description: common.Ref(d.Get("description").(string)),
293+
Labels: common.Ref(common.UnpackMap[string](d.Get("labels"))),
294+
DeleteProtection: common.Ref(d.Get("delete_protection").(bool)),
288295
}
289296

290297
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
@@ -344,11 +351,12 @@ func updateStack(ctx context.Context, d *schema.ResourceData, client *gcom.APICl
344351
}
345352

346353
stack := gcom.PostInstanceRequest{
347-
Name: common.Ref(d.Get("name").(string)),
348-
Slug: common.Ref(d.Get("slug").(string)),
349-
Description: common.Ref(d.Get("description").(string)),
350-
Url: &url,
351-
Labels: common.Ref(common.UnpackMap[string](d.Get("labels"))),
354+
Name: common.Ref(d.Get("name").(string)),
355+
Slug: common.Ref(d.Get("slug").(string)),
356+
Description: common.Ref(d.Get("description").(string)),
357+
Url: &url,
358+
Labels: common.Ref(common.UnpackMap[string](d.Get("labels"))),
359+
DeleteProtection: common.Ref(d.Get("delete_protection").(bool)),
352360
}
353361
req := client.InstancesAPI.PostInstance(ctx, id.(string)).PostInstanceRequest(stack).XRequestId(ClientRequestID())
354362
_, _, err = req.Execute()
@@ -436,6 +444,7 @@ func flattenStack(d *schema.ResourceData, stack *gcom.FormattedApiInstance, conn
436444
d.Set("cluster_slug", stack.ClusterSlug)
437445
d.Set("description", stack.Description)
438446
d.Set("labels", stack.Labels)
447+
d.Set("delete_protection", stack.DeleteProtection)
439448

440449
d.Set("org_id", stack.OrgId)
441450
d.Set("org_slug", stack.OrgSlug)

internal/resources/cloud/resource_cloud_stack_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func TestResourceStack_Basic(t *testing.T) {
8282
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "pdc_gateway_private_connectivity_info_private_dns"),
8383
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "pdc_gateway_private_connectivity_info_service_name"),
8484
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "oncall_api_url"),
85+
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "delete_protection"),
8586
)
8687

8788
resource.ParallelTest(t, resource.TestCase{

pkg/provider/configure_clients.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ func createCloudClient(client *common.Client, providerConfig ProviderConfig) err
267267
openAPIConfig.Scheme = parsedURL.Scheme
268268
openAPIConfig.HTTPClient = getRetryClient(providerConfig)
269269
openAPIConfig.DefaultHeader["Authorization"] = "Bearer " + providerConfig.CloudAccessPolicyToken.ValueString()
270+
openAPIConfig.UserAgent = providerConfig.UserAgent.String()
270271
httpHeaders, err := getHTTPHeadersMap(providerConfig)
271272
if err != nil {
272273
return err

0 commit comments

Comments
 (0)