77 "context"
88 "fmt"
99 "log"
10+ "strings"
1011 "time"
1112
1213 "github.com/IBM-Cloud/power-go-client/clients/instance"
@@ -15,6 +16,7 @@ import (
1516 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1617 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
1718 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
19+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1820 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1921 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2022)
@@ -139,10 +141,41 @@ func resourceIBMPIPlacementGroupDelete(ctx context.Context, d *schema.ResourceDa
139141 cloudInstanceID := parts [0 ]
140142 client := instance .NewIBMPIPlacementGroupClient (ctx , sess , cloudInstanceID )
141143 err = client .Delete (parts [1 ])
144+ if err != nil {
145+ return diag .FromErr (err )
146+ }
142147
148+ _ , err = isWaitForPIPlacementGroupDeleted (ctx , client , parts [1 ], d .Timeout (schema .TimeoutDelete ))
143149 if err != nil {
144150 return diag .FromErr (err )
145151 }
152+
146153 d .SetId ("" )
147154 return nil
148155}
156+
157+ func isWaitForPIPlacementGroupDeleted (ctx context.Context , client * instance.IBMPIPlacementGroupClient , id string , timeout time.Duration ) (interface {}, error ) {
158+ log .Printf ("Waiting for placement group (%s) to be deleted." , id )
159+
160+ stateConf := & retry.StateChangeConf {
161+ Pending : []string {State_Deleting },
162+ Target : []string {State_NotFound },
163+ Refresh : isPIPlacementGroupDeleteRefreshFunc (client , id ),
164+ Delay : 10 * time .Second ,
165+ MinTimeout : 30 * time .Second ,
166+ Timeout : timeout ,
167+ }
168+
169+ return stateConf .WaitForStateContext (ctx )
170+ }
171+
172+ func isPIPlacementGroupDeleteRefreshFunc (client * instance.IBMPIPlacementGroupClient , id string ) retry.StateRefreshFunc {
173+ return func () (interface {}, string , error ) {
174+ pg , err := client .Get (id )
175+ if err != nil && strings .Contains (err .Error (), NotFound ) {
176+ log .Printf ("The power placement group does not exist" )
177+ return pg , State_NotFound , nil
178+ }
179+ return pg , State_Deleting , nil
180+ }
181+ }
0 commit comments