@@ -1226,7 +1226,7 @@ func ResourceInstanceServerDelete(ctx context.Context, d *schema.ResourceData, m
12261226 Zone : zone ,
12271227 IP : zonal .ExpandID (ipID ).ID ,
12281228 Server : & instanceSDK.NullableStringValue {Null : true },
1229- })
1229+ }, scw . WithContext ( ctx ) )
12301230 if err != nil {
12311231 log .Print ("[WARN] Failed to detach eip of server" )
12321232 }
@@ -1237,18 +1237,27 @@ func ResourceInstanceServerDelete(ctx context.Context, d *schema.ResourceData, m
12371237 Zone : zone ,
12381238 PlacementGroup : & instanceSDK.NullableStringValue {Null : true },
12391239 ServerID : id ,
1240- })
1240+ }, scw . WithContext ( ctx ) )
12411241 if err != nil {
12421242 log .Print ("[WARN] Failed remove server from instanceSDK group" )
12431243 }
12441244 }
1245- // reach stopped state
1246- err = reachState (ctx , api , zone , id , instanceSDK .ServerStateStopped )
1247- if httperrors .Is404 (err ) {
1248- return nil
1245+
1246+ // reach running state (mandatory for termination)
1247+ err = reachState (ctx , api , zone , id , instanceSDK .ServerStateRunning )
1248+ if err != nil && ! httperrors .Is404 (err ) {
1249+ return diag .FromErr (err )
12491250 }
12501251
1251- if err != nil {
1252+ timeout := d .Timeout (schema .TimeoutDelete )
1253+
1254+ err = api .ServerActionAndWait (& instanceSDK.ServerActionAndWaitRequest {
1255+ Zone : zone ,
1256+ ServerID : id ,
1257+ Action : instanceSDK .ServerActionTerminate ,
1258+ Timeout : & timeout ,
1259+ }, scw .WithContext (ctx ))
1260+ if err != nil && ! httperrors .Is404 (err ) {
12521261 return diag .FromErr (err )
12531262 }
12541263
@@ -1270,24 +1279,6 @@ func ResourceInstanceServerDelete(ctx context.Context, d *schema.ResourceData, m
12701279 }
12711280 }
12721281
1273- _ , err = waitForServer (ctx , api .API , zone , id , d .Timeout (schema .TimeoutDelete ))
1274- if err != nil && ! httperrors .Is404 (err ) {
1275- return diag .FromErr (err )
1276- }
1277-
1278- err = api .DeleteServer (& instanceSDK.DeleteServerRequest {
1279- Zone : zone ,
1280- ServerID : id ,
1281- }, scw .WithContext (ctx ))
1282- if err != nil && ! httperrors .Is404 (err ) {
1283- return diag .FromErr (err )
1284- }
1285-
1286- _ , err = waitForServer (ctx , api .API , zone , id , d .Timeout (schema .TimeoutDelete ))
1287- if err != nil && ! httperrors .Is404 (err ) {
1288- return diag .FromErr (err )
1289- }
1290-
12911282 // Related to https://github.com/hashicorp/terraform-plugin-sdk/issues/142
12921283 _ , rootVolumeAttributeSet := d .GetOk ("root_volume" )
12931284 if d .Get ("root_volume.0.delete_on_termination" ).(bool ) || ! rootVolumeAttributeSet {
0 commit comments