Skip to content

Commit 5880613

Browse files
Bhuvisha03bhuvisha-n
authored andcommitted
fix : Space quota resource destruction bug fix
Fixed space quota deletion logic: -Terraform-managed space assignments are now removed before deleting the quota. -Deletion fails if non-Terraform-managed space assignments still exist. Skipped non-Terraform managed space assignments during deletion to avoid unintended changes. Refactored repeated code in the resource_security_group_space_bindings.go .
1 parent bdc6503 commit 5880613

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

internal/provider/resource_security_group_space_bindings.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@ func (rs *SecurityGroupSpacesResource) Update(ctx context.Context, req resource.
248248
diags = plan.mapSecurityGroupSpacesValuestoType(ctx, runningSpaces, stagingSpaces)
249249
resp.Diagnostics.Append(diags...)
250250

251-
diags = plan.mapSecurityGroupSpacesValuestoType(ctx, runningSpaces, stagingSpaces)
252-
resp.Diagnostics.Append(diags...)
253-
254251
tflog.Trace(ctx, "updated a security group spaces resource")
255252
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
256253
}

internal/provider/resource_space_quota.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (r *spaceQuotaResource) Metadata(_ context.Context, req resource.MetadataRe
3838

3939
func (r *spaceQuotaResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
4040
resp.Schema = schema.Schema{
41-
MarkdownDescription: "Provides a Cloud Foundry resource to manage space quota definitions.",
41+
MarkdownDescription: "Provides a Cloud Foundry resource to manage space quota definitions. Only spaces assigned via Terraform are managed. On deletion, Terraform removes these assignments before deleting the quota. Non-Terraform managed assignments are not removed, which may cause deletion to fail.",
4242
Attributes: map[string]schema.Attribute{
4343
"name": schema.StringAttribute{
4444
MarkdownDescription: "The name you use to identify the quota or plan in Cloud Foundry",
@@ -191,6 +191,9 @@ func (r *spaceQuotaResource) Read(ctx context.Context, req resource.ReadRequest,
191191
if resp.Diagnostics.HasError() {
192192
return
193193
}
194+
diags = spacesQuotaType.mapSpaceQuotaAssignmentValuestoType(ctx, spaceQuotaTypeState.Spaces)
195+
resp.Diagnostics.Append(diags...)
196+
194197
diags = resp.State.Set(ctx, spacesQuotaType)
195198
resp.Diagnostics.Append(diags...)
196199
if resp.Diagnostics.HasError() {
@@ -252,6 +255,10 @@ func (r *spaceQuotaResource) Update(ctx context.Context, req resource.UpdateRequ
252255
if resp.Diagnostics.HasError() {
253256
return
254257
}
258+
259+
diags = spacesQuotaType.mapSpaceQuotaAssignmentValuestoType(ctx, spaceQuotaTypePlan.Spaces)
260+
resp.Diagnostics.Append(diags...)
261+
255262
diags = resp.State.Set(ctx, spacesQuotaType)
256263
resp.Diagnostics.Append(diags...)
257264
if resp.Diagnostics.HasError() {
@@ -266,6 +273,22 @@ func (r *spaceQuotaResource) Delete(ctx context.Context, req resource.DeleteRequ
266273
if resp.Diagnostics.HasError() {
267274
return
268275
}
276+
277+
// Iterate through all Terraform-managed spaces that are currently assigned to this space quota
278+
var assignedSpaces []string
279+
diags = spaceQuotaType.Spaces.ElementsAs(ctx, &assignedSpaces, false)
280+
resp.Diagnostics.Append(diags...)
281+
282+
for _, spaceId := range assignedSpaces {
283+
err := r.cfClient.SpaceQuotas.Remove(ctx, spaceQuotaType.ID.ValueString(), spaceId)
284+
if err != nil {
285+
resp.Diagnostics.AddError(
286+
"Unable to remove space quota from the space ",
287+
fmt.Sprintf("Request failed with %s", err.Error()),
288+
)
289+
return
290+
}
291+
}
269292
jobID, err := r.cfClient.SpaceQuotas.Delete(ctx, spaceQuotaType.ID.ValueString())
270293
if err != nil {
271294
resp.Diagnostics.AddError(

internal/provider/types_space_quota.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
cfv3resource "github.com/cloudfoundry/go-cfclient/v3/resource"
88
"github.com/hashicorp/terraform-plugin-framework/diag"
99
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/samber/lo"
1011
)
1112

1213
type spaceQuotaType struct {
@@ -130,3 +131,22 @@ func mapSpaceQuotasValuesToType(spaceQuotas []*cfv3resource.SpaceQuota) ([]space
130131

131132
return spaceQuotasList, diagnostics
132133
}
134+
135+
func (plan *spaceQuotaType) mapSpaceQuotaAssignmentValuestoType(ctx context.Context, stateSpacesSet types.Set) diag.Diagnostics {
136+
var diags, diagnostics diag.Diagnostics
137+
var liveSpaces, stateSpaces []string
138+
diags = plan.Spaces.ElementsAs(ctx, &liveSpaces, false)
139+
diagnostics.Append(diags...)
140+
diags = stateSpacesSet.ElementsAs(ctx, &stateSpaces, false)
141+
diagnostics.Append(diags...)
142+
143+
terraformHandledSpaces := lo.Intersect(liveSpaces, stateSpaces)
144+
145+
if len(terraformHandledSpaces) == 0 {
146+
plan.Spaces = types.SetNull(types.StringType)
147+
} else {
148+
plan.Spaces, diags = types.SetValueFrom(ctx, types.StringType, terraformHandledSpaces)
149+
diagnostics.Append(diags...)
150+
}
151+
return diagnostics
152+
}

0 commit comments

Comments
 (0)