Skip to content

Commit

Permalink
Merge pull request #3392 from tiwood/retry-vnet-peering
Browse files Browse the repository at this point in the history
Retry support for azurerm_virtual_network_peering
  • Loading branch information
tombuildsstuff authored May 7, 2019
2 parents b7d97ef + f3be1e7 commit 4da4aa7
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions azurerm/resource_arm_virtual_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package azurerm
import (
"fmt"
"log"
"strings"
"sync"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -104,13 +107,8 @@ func resourceArmVirtualNetworkPeeringCreateUpdate(d *schema.ResourceData, meta i
peerMutex.Lock()
defer peerMutex.Unlock()

future, err := client.CreateOrUpdate(ctx, resGroup, vnetName, name, peer)
if err != nil {
return fmt.Errorf("Error Creating/Updating Virtual Network Peering %q (Network %q / Resource Group %q): %+v", name, vnetName, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Virtual Network Peering %q (Network %q / Resource Group %q): %+v", name, vnetName, resGroup, err)
if err := resource.Retry(300*time.Second, retryVnetPeeringsClientCreateUpdate(resGroup, vnetName, name, peer, meta)); err != nil {
return err
}

read, err := client.Get(ctx, resGroup, vnetName, name)
Expand Down Expand Up @@ -206,3 +204,28 @@ func getVirtualNetworkPeeringProperties(d *schema.ResourceData) *network.Virtual
},
}
}

func retryVnetPeeringsClientCreateUpdate(resGroup string, vnetName string, name string, peer network.VirtualNetworkPeering, meta interface{}) func() *resource.RetryError {
return func() *resource.RetryError {
vnetPeeringsClient := meta.(*ArmClient).vnetPeeringsClient
ctx := meta.(*ArmClient).StopContext

future, err := vnetPeeringsClient.CreateOrUpdate(ctx, resGroup, vnetName, name, peer)
if err != nil {
if utils.ResponseErrorIsRetryable(err) {
return resource.RetryableError(err)
} else if future.Response().StatusCode == 400 && strings.Contains(err.Error(), "ReferencedResourceNotProvisioned") {
// Resource is not yet ready, this may be the case if the Vnet was just created or another peering was just initiated.
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

if err = future.WaitForCompletionRef(ctx, vnetPeeringsClient.Client); err != nil {
return resource.NonRetryableError(err)
}

return nil
}
}

0 comments on commit 4da4aa7

Please sign in to comment.