Skip to content

Commit 63fb775

Browse files
deepakibmshkantare
authored andcommitted
adding wait before delete option for instance deletion
1 parent efa5de7 commit 63fb775

File tree

1 file changed

+59
-43
lines changed

1 file changed

+59
-43
lines changed

ibm/resource_ibm_is_instance.go

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const (
5858
isInstanceMemory = "memory"
5959
isInstanceStatus = "status"
6060

61+
isEnableCleanDelete = "wait_before_delete"
6162
isInstanceProvisioning = "provisioning"
6263
isInstanceProvisioningDone = "done"
6364
isInstanceAvailable = "available"
@@ -153,6 +154,13 @@ func resourceIBMISInstance() *schema.Resource {
153154
Description: "list of tags for the instance",
154155
},
155156

157+
isEnableCleanDelete: {
158+
Type: schema.TypeBool,
159+
Optional: true,
160+
Default: true,
161+
Description: "Enables stopping of instance before deleting and waits till deletion is complete",
162+
},
163+
156164
isInstanceVolumeAttachments: {
157165
Type: schema.TypeList,
158166
Computed: true,
@@ -1958,6 +1966,7 @@ func instanceDelete(d *schema.ResourceData, meta interface{}, id string) error {
19581966
return err
19591967
}
19601968

1969+
cleanDelete := d.Get(isEnableCleanDelete).(bool)
19611970
getinsOptions := &vpcv1.GetInstanceOptions{
19621971
ID: &id,
19631972
}
@@ -1969,48 +1978,53 @@ func instanceDelete(d *schema.ResourceData, meta interface{}, id string) error {
19691978
}
19701979
return fmt.Errorf("Error Getting Instance (%s): %s\n%s", id, err, response)
19711980
}
1972-
actiontype := "stop"
1973-
createinsactoptions := &vpcv1.CreateInstanceActionOptions{
1974-
InstanceID: &id,
1975-
Type: &actiontype,
1976-
}
1977-
_, response, err = instanceC.CreateInstanceAction(createinsactoptions)
1978-
if err != nil {
1979-
if response != nil && response.StatusCode == 404 {
1980-
return nil
1981-
}
1982-
return fmt.Errorf("Error Creating Instance Action: %s\n%s", err, response)
1983-
}
1984-
_, err = isWaitForInstanceActionStop(instanceC, d.Timeout(schema.TimeoutDelete), id, d)
1985-
if err != nil {
1986-
return err
1987-
}
1988-
listvolattoptions := &vpcv1.ListInstanceVolumeAttachmentsOptions{
1989-
InstanceID: &id,
1990-
}
1991-
vols, response, err := instanceC.ListInstanceVolumeAttachments(listvolattoptions)
1992-
if err != nil {
1993-
return fmt.Errorf("Error Listing volume attachments to the instance: %s\n%s", err, response)
1994-
}
1981+
19951982
bootvolid := ""
1996-
for _, vol := range vols.VolumeAttachments {
1997-
if *vol.Type == "data" {
1998-
delvolattoptions := &vpcv1.DeleteInstanceVolumeAttachmentOptions{
1999-
InstanceID: &id,
2000-
ID: vol.ID,
1983+
1984+
if cleanDelete {
1985+
actiontype := "stop"
1986+
createinsactoptions := &vpcv1.CreateInstanceActionOptions{
1987+
InstanceID: &id,
1988+
Type: &actiontype,
1989+
}
1990+
_, response, err = instanceC.CreateInstanceAction(createinsactoptions)
1991+
if err != nil {
1992+
if response != nil && response.StatusCode == 404 {
1993+
return nil
20011994
}
2002-
_, err := instanceC.DeleteInstanceVolumeAttachment(delvolattoptions)
2003-
if err != nil {
2004-
return fmt.Errorf("Error while removing volume Attachment %q for instance %s: %q", *vol.ID, d.Id(), err)
1995+
return fmt.Errorf("Error Creating Instance Action: %s\n%s", err, response)
1996+
}
1997+
_, err = isWaitForInstanceActionStop(instanceC, d.Timeout(schema.TimeoutDelete), id, d)
1998+
if err != nil {
1999+
return err
2000+
}
2001+
listvolattoptions := &vpcv1.ListInstanceVolumeAttachmentsOptions{
2002+
InstanceID: &id,
2003+
}
2004+
vols, response, err := instanceC.ListInstanceVolumeAttachments(listvolattoptions)
2005+
if err != nil {
2006+
return fmt.Errorf("Error Listing volume attachments to the instance: %s\n%s", err, response)
2007+
}
2008+
2009+
for _, vol := range vols.VolumeAttachments {
2010+
if *vol.Type == "data" {
2011+
delvolattoptions := &vpcv1.DeleteInstanceVolumeAttachmentOptions{
2012+
InstanceID: &id,
2013+
ID: vol.ID,
2014+
}
2015+
_, err := instanceC.DeleteInstanceVolumeAttachment(delvolattoptions)
2016+
if err != nil {
2017+
return fmt.Errorf("Error while removing volume Attachment %q for instance %s: %q", *vol.ID, d.Id(), err)
2018+
}
2019+
_, err = isWaitForInstanceVolumeDetached(instanceC, d, d.Id(), *vol.ID)
2020+
if err != nil {
2021+
return err
2022+
}
20052023
}
2006-
_, err = isWaitForInstanceVolumeDetached(instanceC, d, d.Id(), *vol.ID)
2007-
if err != nil {
2008-
return err
2024+
if *vol.Type == "boot" {
2025+
bootvolid = *vol.Volume.ID
20092026
}
20102027
}
2011-
if *vol.Type == "boot" {
2012-
bootvolid = *vol.Volume.ID
2013-
}
20142028
}
20152029
deleteinstanceOptions := &vpcv1.DeleteInstanceOptions{
20162030
ID: &id,
@@ -2019,15 +2033,17 @@ func instanceDelete(d *schema.ResourceData, meta interface{}, id string) error {
20192033
if err != nil {
20202034
return err
20212035
}
2022-
_, err = isWaitForInstanceDelete(instanceC, d, d.Id())
2023-
if err != nil {
2024-
return err
2025-
}
2026-
if _, ok := d.GetOk(isInstanceBootVolume); ok {
2027-
_, err = isWaitForVolumeDeleted(instanceC, bootvolid, d.Timeout(schema.TimeoutDelete))
2036+
if cleanDelete {
2037+
_, err = isWaitForInstanceDelete(instanceC, d, d.Id())
20282038
if err != nil {
20292039
return err
20302040
}
2041+
if _, ok := d.GetOk(isInstanceBootVolume); ok {
2042+
_, err = isWaitForVolumeDeleted(instanceC, bootvolid, d.Timeout(schema.TimeoutDelete))
2043+
if err != nil {
2044+
return err
2045+
}
2046+
}
20312047
}
20322048
return nil
20332049
}

0 commit comments

Comments
 (0)