Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry support for azurerm_virtual_network_peering #3392

Merged
merged 1 commit into from
May 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}