Skip to content

Commit 5f1d172

Browse files
sleungcysleungcy-sap
andauthored
fix service_instance resource failing to update synchronously (#551)
Co-authored-by: sleungcy-sap <[email protected]>
1 parent f5d5eaf commit 5f1d172

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

cloudfoundry/resource_cf_service_instance.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func resourceServiceInstanceUpdate(ctx context.Context, d *schema.ResourceData,
345345
}
346346

347347
serviceInstanceUpdate := resources.ServiceInstance{
348-
Name: name,
348+
Name: name,
349349
}
350350
// Only add in the request body what has changed, because some services don't support updating multiple attributes at the same time
351351
if d.HasChange("service_plan") {
@@ -367,38 +367,42 @@ func resourceServiceInstanceUpdate(ctx context.Context, d *schema.ResourceData,
367367
return diag.FromErr(err)
368368
}
369369

370-
// Poll the state of the async job
371-
err = common.PollingWithTimeout(func() (bool, error) {
372-
job, _, err := session.ClientV3.GetJob(jobURL)
373-
if err != nil {
374-
return true, err
375-
}
376-
377-
// Stop polling and return error if job failed
378-
if job.State == constant.JobFailed {
379-
return true, fmt.Errorf(
380-
"Instance %s failed %s, reason: %+v",
381-
name,
382-
space,
383-
job.Errors(),
384-
)
385-
}
386-
// If job completed, check if the service instance exists
387-
if job.State == constant.JobComplete {
388-
si, _, _, err := session.ClientV3.GetServiceInstanceByNameAndSpace(name, space)
370+
// jobURL is empty if no update is required or if the updates were done synchronously
371+
if jobURL != "" {
372+
// Poll the state of the async job
373+
err = common.PollingWithTimeout(func() (bool, error) {
374+
job, _, err := session.ClientV3.GetJob(jobURL)
389375
if err != nil {
390376
return true, err
391377
}
392-
d.SetId(si.GUID)
393-
return true, nil
394-
}
395378

396-
// Last operation initial or inprogress or job not completed, continue polling
397-
return false, nil
398-
}, 5*time.Second, d.Timeout(schema.TimeoutUpdate))
399-
if err != nil {
400-
return diag.FromErr(err)
379+
// Stop polling and return error if job failed
380+
if job.State == constant.JobFailed {
381+
return true, fmt.Errorf(
382+
"Instance %s failed %s, reason: %+v",
383+
name,
384+
space,
385+
job.Errors(),
386+
)
387+
}
388+
// If job completed, check if the service instance exists
389+
if job.State == constant.JobComplete {
390+
si, _, _, err := session.ClientV3.GetServiceInstanceByNameAndSpace(name, space)
391+
if err != nil {
392+
return true, err
393+
}
394+
d.SetId(si.GUID)
395+
return true, nil
396+
}
397+
398+
// Last operation initial or inprogress or job not completed, continue polling
399+
return false, nil
400+
}, 5*time.Second, d.Timeout(schema.TimeoutUpdate))
401+
if err != nil {
402+
return diag.FromErr(err)
403+
}
401404
}
405+
402406
// We succeeded, disable partial mode
403407
d.Partial(false)
404408
return nil

0 commit comments

Comments
 (0)