Skip to content

Commit

Permalink
Fix workspace delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ismirlia committed Nov 17, 2023
1 parent 2a48c49 commit 154487f
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions ibm/service/power/resource_ibm_pi_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ func ResourceIBMPIWorkspace() *schema.Resource {
Importer: &schema.ResourceImporter{},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -68,7 +67,7 @@ func resourceIBMPIWorkspaceCreate(ctx context.Context, d *schema.ResourceData, m

// No need for cloudInstanceID because we are creating a workspace
client := st.NewIBMPIWorkspacesClient(ctx, sess, "")
controller, err := client.Create(name, datacenter, resourceGroup, plan)
controller, _, err := client.Create(name, datacenter, resourceGroup, plan)

Check failure on line 70 in ibm/service/power/resource_ibm_pi_workspace.go

View workflow job for this annotation

GitHub Actions / Build

client.Create undefined (type *instance.IBMPIWorkspacesClient has no field or method Create)
if err != nil {
log.Printf("[DEBUG] create workspace failed %v", err)
return diag.FromErr(err)
Expand All @@ -83,6 +82,31 @@ func resourceIBMPIWorkspaceCreate(ctx context.Context, d *schema.ResourceData, m
return resourceIBMPIWorkspaceRead(ctx, d, meta)
}

func waitForResourceInstanceCreate(ctx context.Context, client *st.IBMPIWorkspacesClient, id string, timeout time.Duration) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"in progress", "inactive", "provisioning"},
Target: []string{"active"},
Refresh: isIBMPIWorkspaceCreateRefreshFunc(client, id),
Delay: 10 * time.Second,
MinTimeout: 1 * time.Minute,
Timeout: timeout,
}
return stateConf.WaitForStateContext(ctx)
}

func isIBMPIWorkspaceCreateRefreshFunc(client *st.IBMPIWorkspacesClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
controller, _, err := client.GetRC(id)

Check failure on line 99 in ibm/service/power/resource_ibm_pi_workspace.go

View workflow job for this annotation

GitHub Actions / Build

client.GetRC undefined (type *instance.IBMPIWorkspacesClient has no field or method GetRC)
if err != nil {
return nil, "", err
}
if *controller.State == "failed" {
return controller, *controller.State, fmt.Errorf("[ERROR] The resource instance %s failed to create", id)
}
return controller, *controller.State, nil
}
}

func resourceIBMPIWorkspaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// session
sess, err := meta.(conns.ClientSession).IBMPISession()
Expand All @@ -92,12 +116,11 @@ func resourceIBMPIWorkspaceRead(ctx context.Context, d *schema.ResourceData, met

cloudInstanceID := d.Id()
client := st.NewIBMPIWorkspacesClient(ctx, sess, cloudInstanceID)

wsData, err := client.Get(cloudInstanceID)
controller, _, err := client.GetRC(cloudInstanceID)

Check failure on line 119 in ibm/service/power/resource_ibm_pi_workspace.go

View workflow job for this annotation

GitHub Actions / Build

client.GetRC undefined (type *instance.IBMPIWorkspacesClient has no field or method GetRC)
if err != nil {
return diag.FromErr(err)
}
d.Set(PIWorkspaceName, wsData.Name)
d.Set(PIWorkspaceName, controller.Name)

return nil
}
Expand All @@ -110,35 +133,47 @@ func resourceIBMPIWorkspaceDelete(ctx context.Context, d *schema.ResourceData, m

cloudInstanceID := d.Id()
client := st.NewIBMPIWorkspacesClient(ctx, sess, cloudInstanceID)
err = client.Delete(cloudInstanceID)
response, err := client.Delete(cloudInstanceID)

Check failure on line 136 in ibm/service/power/resource_ibm_pi_workspace.go

View workflow job for this annotation

GitHub Actions / Build

client.Delete undefined (type *instance.IBMPIWorkspacesClient has no field or method Delete)
if err != nil && response != nil && response.StatusCode == 410 {
return nil
}
_, err = waitForResourceInstanceDelete(ctx, client, cloudInstanceID, d.Timeout(schema.TimeoutDelete))
if err != nil {
return diag.FromErr(err)
}
d.SetId("")

return nil
}

func waitForResourceInstanceCreate(ctx context.Context, client *st.IBMPIWorkspacesClient, id string, timeout time.Duration) (interface{}, error) {
func waitForResourceInstanceDelete(ctx context.Context, client *st.IBMPIWorkspacesClient, id string, timeout time.Duration) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"provisioning", "in progress", "inactive"},
Target: []string{"active"},
Refresh: isIBMPIWorkspaceRefreshFunc(client, id),
Timeout: timeout,
Pending: []string{"in progress", "inactive", "active"},
Target: []string{"removed", "pending_reclamation"},
Refresh: isIBMPIResourceDeleteRefreshFunc(client, id),
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
MinTimeout: 1 * time.Second,
Timeout: timeout,
}
return stateConf.WaitForStateContext(ctx)
}

func isIBMPIWorkspaceRefreshFunc(client *st.IBMPIWorkspacesClient, id string) resource.StateRefreshFunc {
func isIBMPIResourceDeleteRefreshFunc(client *st.IBMPIWorkspacesClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
ws, err := client.GetRC(id)
controller, response, err := client.GetRC(id)

Check failure on line 163 in ibm/service/power/resource_ibm_pi_workspace.go

View workflow job for this annotation

GitHub Actions / Build

client.GetRC undefined (type *instance.IBMPIWorkspacesClient has no field or method GetRC)
if err != nil {
if response != nil && response.StatusCode == 404 {
return controller, "active", nil
}
return nil, "", err
}
if *ws.State == "failed" {
return ws, *ws.State, fmt.Errorf("[ERROR] The resource instance %s failed to provisioned", id)
if controller == nil {
return controller, "removed", nil
} else {
if *controller.State == "failed" {
return controller, *controller.State, fmt.Errorf("[ERROR] The resource instance %s failed to delete", id)
}
return controller, *controller.State, nil
}
return ws, *ws.State, nil
}
}

0 comments on commit 154487f

Please sign in to comment.