Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-yechenwei authored and manicminer committed Apr 17, 2020
1 parent 21923af commit f896372
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
48 changes: 38 additions & 10 deletions azurerm/internal/services/compute/resource_arm_shared_image.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package compute

import (
"context"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down Expand Up @@ -261,23 +262,50 @@ func resourceArmSharedImageDelete(d *schema.ResourceData, meta interface{}) erro

future, err := client.Delete(ctx, resourceGroup, galleryName, name)
if err != nil {
// deleted outside of Terraform
if response.WasNotFound(future.Response()) {
return nil
}

return fmt.Errorf("Error deleting Shared Image %q (Gallery %q / Resource Group %q): %+v", name, galleryName, resourceGroup, err)
return fmt.Errorf("deleting Shared Image %q (Gallery %q / Resource Group %q): %+v", name, galleryName, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error waiting for the deletion of Shared Image %q (Gallery %q / Resource Group %q): %+v", name, galleryName, resourceGroup, err)
}
return fmt.Errorf("failed to wait for deleting Shared Image %q (Gallery %q / Resource Group %q): %+v", name, galleryName, resourceGroup, err)
}

log.Printf("[DEBUG] Waiting for Shared Image %q (Gallery %q / Resource Group %q) to be eventually deleted", name, galleryName, resourceGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{"Exists"},
Target: []string{"NotFound"},
Refresh: sharedImageDeleteStateRefreshFunc(ctx, client, resourceGroup, name, galleryName),
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 10,
Timeout: d.Timeout(schema.TimeoutDelete),
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("failed to wait for Shared Image %q (Gallery %q / Resource Group %q) to be deleted: %+v", name, galleryName, resourceGroup, err)
}

return nil
}

func sharedImageDeleteStateRefreshFunc(ctx context.Context, client *compute.GalleryImagesClient, resourceGroupName string, imageName string, galleryName string) resource.StateRefreshFunc {
// The resource Shared Image depends on the resource Shared Image Gallery.
// Although the delete API returns 404 which means the Shared Image resource has been deleted.
// Then it tries to immediately delete Shared Image Gallery but it still throws error `Can not delete resource before nested resources are deleted.`
// In this case we're going to try triggering the Deletion again, in-case it didn't work prior to this attempt.
// For more details, see related Bug: https://github.com/Azure/azure-sdk-for-go/issues/8314
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroupName, galleryName, imageName)
if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return "NotFound", "NotFound", nil
}

return nil, "", fmt.Errorf("failed to poll to check if the Shared Image has been deleted: %+v", err)
}

return res, "Exists", nil
}
}

func expandGalleryImageIdentifier(d *schema.ResourceData) *compute.GalleryImageIdentifier {
vs := d.Get("identifier").([]interface{})
v := vs[0].(map[string]interface{})
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/shared_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ The following arguments are supported:

---

* `hyper_v_generation` - (Optional) The hypervisor generation of the virtual machine. Possible values are `V1` or `V2`. Defaults to `V1`. Changing this forces a new resource to be created.

* `description` - (Optional) A description of this Shared Image.

* `eula` - (Optional) The End User Licence Agreement for the Shared Image.

* `hyper_v_generation` - (Optional) The HyperVGenerationType of the Virtual Machine created from the image. Possible values are `V1` and `V2`. Defaults to `V1`. Changing this forces a new resource to be created.

* `privacy_statement_uri` - (Optional) The URI containing the Privacy Statement associated with this Shared Image.

* `release_note_uri` - (Optional) The URI containing the Release Notes associated with this Shared Image.
Expand Down

0 comments on commit f896372

Please sign in to comment.