@@ -3,18 +3,19 @@ package intake_test
33import (
44 "context"
55 _ "embed"
6- "errors"
76 "fmt"
87 "maps"
9- "net/http "
8+ "strings "
109 "testing"
1110
1211 "github.com/hashicorp/terraform-plugin-testing/config"
1312 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
1413 "github.com/hashicorp/terraform-plugin-testing/terraform"
1514 sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
16- "github.com/stackitcloud/stackit-sdk-go/core/oapierror "
15+ "github.com/stackitcloud/stackit-sdk-go/core/utils "
1716 "github.com/stackitcloud/stackit-sdk-go/services/intake"
17+ "github.com/stackitcloud/stackit-sdk-go/services/intake/wait"
18+ "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1819 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
1920)
2021
@@ -181,53 +182,57 @@ func TestAccIntakeRunnerMax(t *testing.T) {
181182
182183// testAccCheckIntakeRunnerDestroy act as independent auditor to verify destroy operation
183184func testAccCheckIntakeRunnerDestroy (s * terraform.State ) error {
184- // Create own raw API client
185185 ctx := context .Background ()
186186 var client * intake.APIClient
187187 var err error
188188
189- effectiveRegion := testutil .Region
190- if effectiveRegion == "" {
191- effectiveRegion = "eu01"
192- }
193-
194189 if testutil .IntakeCustomEndpoint == "" {
195- client , err = intake .NewAPIClient (sdkConfig . WithRegion ( effectiveRegion ) )
190+ client , err = intake .NewAPIClient ()
196191 } else {
197192 client , err = intake .NewAPIClient (
198193 sdkConfig .WithEndpoint (testutil .IntakeCustomEndpoint ),
199- sdkConfig .WithRegion (effectiveRegion ),
200194 )
201195 }
202196 if err != nil {
203197 return fmt .Errorf ("creating client: %w" , err )
204198 }
205199
206- // Loop through resources that should have been deleted
200+ instancesToDestroy := [] string {}
207201 for _ , rs := range s .RootModule ().Resources {
208202 if rs .Type != "stackit_intake_runner" {
209203 continue
210204 }
205+ // Intake internal ID: "[project_id],[region],[runner_id]"
206+ runnerId := strings .Split (rs .Primary .ID , core .Separator )[2 ]
207+ instancesToDestroy = append (instancesToDestroy , runnerId )
208+ }
211209
212- pID := rs .Primary .Attributes ["project_id" ]
213- reg := rs .Primary .Attributes ["region" ]
214- rID := rs .Primary .Attributes ["runner_id" ]
215-
216- // If it still exists, destroy operation was unsuccessful
217- _ , err := client .GetIntakeRunner (ctx , pID , reg , rID ).Execute ()
218- if err == nil {
219- // Delete to prevent orphaned instances
220- errDel := client .DeleteIntakeRunner (ctx , pID , reg , rID ).Execute ()
221- if errDel != nil {
222- return fmt .Errorf ("resource leaked and manual cleanup failed: %w" , errDel )
223- }
210+ // List all resources in the project/region to see what's left
211+ instancesResp , err := client .ListIntakeRunners (ctx , testutil .ProjectId , testutil .Region ).Execute ()
212+ if err != nil {
213+ return fmt .Errorf ("getting instancesResp: %w" , err )
214+ }
224215
225- return fmt .Errorf ("intake runner %s still exists in region %s" , rID , reg )
216+ // If the API returns a list of runners, check if our deleted ones are still there
217+ items := * instancesResp .IntakeRunners
218+ for i := range items {
219+ if items [i ].Id == nil {
220+ continue
226221 }
227222
228- var oapiErr * oapierror.GenericOpenAPIError
229- if ! errors .As (err , & oapiErr ) || oapiErr .StatusCode != http .StatusNotFound {
230- return fmt .Errorf ("unexpected error checking destruction: %w" , err )
223+ // If a runner we thought we deleted is found in the list
224+ if utils .Contains (instancesToDestroy , * items [i ].Id ) {
225+ // Attempt a final delete and wait, just like Postgres
226+ err := client .DeleteIntakeRunner (ctx , testutil .ProjectId , testutil .Region , * items [i ].Id ).Execute ()
227+ if err != nil {
228+ return fmt .Errorf ("deleting runner %s during CheckDestroy: %w" , * items [i ].Id , err )
229+ }
230+
231+ // Using the wait handler for destruction verification
232+ _ , err = wait .DeleteIntakeRunnerWaitHandler (ctx , client , testutil .ProjectId , testutil .Region , * items [i ].Id ).WaitWithContext (ctx )
233+ if err != nil {
234+ return fmt .Errorf ("deleting runner %s during CheckDestroy: waiting for deletion %w" , * items [i ].Id , err )
235+ }
231236 }
232237 }
233238 return nil
0 commit comments