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

r/virtual_network_peering: splitting the create/update #21009

Merged
merged 2 commits into from
Mar 17, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
r/virtual_network_peering: splitting the Create and Update method
  • Loading branch information
tombuildsstuff committed Mar 17, 2023

Verified

This commit was signed with the committer’s verified signature.
cosmicBboy Niels Bantilan
commit 53b475584f3de9f1672c540a671de78a8ae26870
79 changes: 62 additions & 17 deletions internal/services/network/virtual_network_peering_resource.go
Original file line number Diff line number Diff line change
@@ -2,17 +2,17 @@ package network

import (
"fmt"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
@@ -23,9 +23,9 @@ const virtualNetworkPeeringResourceType = "azurerm_virtual_network_peering"

func resourceVirtualNetworkPeering() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceVirtualNetworkPeeringCreateUpdate,
Create: resourceVirtualNetworkPeeringCreate,
Read: resourceVirtualNetworkPeeringRead,
Update: resourceVirtualNetworkPeeringCreateUpdate,
Update: resourceVirtualNetworkPeeringUpdate,
Delete: resourceVirtualNetworkPeeringDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.VirtualNetworkPeeringID(id)
@@ -96,26 +96,24 @@ func resourceVirtualNetworkPeering() *pluginsdk.Resource {
}
}

func resourceVirtualNetworkPeeringCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
func resourceVirtualNetworkPeeringCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.VnetPeeringsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewVirtualNetworkPeeringID(subscriptionId, d.Get("resource_group_name").(string), d.Get("virtual_network_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_virtual_network_peering", id.ID())
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_virtual_network_peering", id.ID())
}

peer := network.VirtualNetworkPeering{
VirtualNetworkPeeringPropertiesFormat: &network.VirtualNetworkPeeringPropertiesFormat{
AllowVirtualNetworkAccess: pointer.To(d.Get("allow_virtual_network_access").(bool)),
@@ -171,6 +169,53 @@ func resourceVirtualNetworkPeeringCreateUpdate(d *pluginsdk.ResourceData, meta i
return resourceVirtualNetworkPeeringRead(d, meta)
}

func resourceVirtualNetworkPeeringUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.VnetPeeringsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.VirtualNetworkPeeringID(d.Id())
if err != nil {
return err
}

locks.ByID(virtualNetworkPeeringResourceType)
defer locks.UnlockByID(virtualNetworkPeeringResourceType)

existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if d.HasChange("allow_forwarded_traffic") {
existing.VirtualNetworkPeeringPropertiesFormat.AllowForwardedTraffic = pointer.To(d.Get("allow_forwarded_traffic").(bool))
}
if d.HasChange("allow_gateway_transit") {
existing.VirtualNetworkPeeringPropertiesFormat.AllowGatewayTransit = pointer.To(d.Get("allow_gateway_transit").(bool))
}
if d.HasChange("allow_virtual_network_access") {
existing.VirtualNetworkPeeringPropertiesFormat.AllowVirtualNetworkAccess = pointer.To(d.Get("allow_virtual_network_access").(bool))
}
if d.HasChange("use_remote_gateways") {
existing.VirtualNetworkPeeringPropertiesFormat.UseRemoteGateways = pointer.To(d.Get("use_remote_gateways").(bool))
}
if d.HasChange("remote_virtual_network_id") {
existing.VirtualNetworkPeeringPropertiesFormat.RemoteVirtualNetwork = &network.SubResource{
ID: pointer.To(d.Get("remote_virtual_network_id").(string)),
}
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name, existing, network.SyncRemoteAddressSpaceTrue)
if err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the update of %s: %+v", *id, err)
}

return resourceVirtualNetworkPeeringRead(d, meta)
}

func resourceVirtualNetworkPeeringRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.VnetPeeringsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)