Skip to content

Commit 122fb83

Browse files
committed
fix: add ReadyForDeletionWaitHandler to avoid error when deleting a network area with projects in deletion state
1 parent 01fb599 commit 122fb83

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

stackit/internal/services/iaas/networkarea/resource.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"net/http"
77
"strings"
88

9+
"github.com/stackitcloud/stackit-sdk-go/services/resourcemanager"
910
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
11+
resourcemanagerUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/resourcemanager/utils"
1012

1113
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1214
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
@@ -73,7 +75,8 @@ func NewNetworkAreaResource() resource.Resource {
7375

7476
// networkResource is the resource implementation.
7577
type networkAreaResource struct {
76-
client *iaas.APIClient
78+
client *iaas.APIClient
79+
resourceManagerClient *resourcemanager.APIClient
7780
}
7881

7982
// Metadata returns the resource type name.
@@ -93,6 +96,11 @@ func (r *networkAreaResource) Configure(ctx context.Context, req resource.Config
9396
return
9497
}
9598
r.client = apiClient
99+
resourceManagerClient := resourcemanagerUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
100+
if resp.Diagnostics.HasError() {
101+
return
102+
}
103+
r.resourceManagerClient = resourceManagerClient
96104
tflog.Info(ctx, "IaaS client configured")
97105
}
98106

@@ -411,14 +419,9 @@ func (r *networkAreaResource) Delete(ctx context.Context, req resource.DeleteReq
411419
ctx = tflog.SetField(ctx, "organization_id", organizationId)
412420
ctx = tflog.SetField(ctx, "network_area_id", networkAreaId)
413421

414-
projects, err := r.client.ListNetworkAreaProjects(ctx, organizationId, networkAreaId).Execute()
422+
_, err := wait.ReadyForNetworkAreaDeletionWaitHandler(ctx, r.client, r.resourceManagerClient, organizationId, networkAreaId).WaitWithContext(ctx)
415423
if err != nil {
416-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network area", fmt.Sprintf("Calling API to get the list of projects: %v", err))
417-
return
418-
}
419-
420-
if projects != nil && len(*projects.Items) > 0 {
421-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network area", fmt.Sprintln("You still have projects attached to the network area. Please delete or remove them from the network area before deleting the network area."))
424+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network area", fmt.Sprintf("Network area ready for deletion waiting: %v", err))
422425
return
423426
}
424427

0 commit comments

Comments
 (0)