Skip to content

Commit

Permalink
r/virtual_network_peering: splitting the Create and Update method
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Mar 17, 2023
1 parent 42721b5 commit 3ff1873
Showing 1 changed file with 58 additions and 13 deletions.
71 changes: 58 additions & 13 deletions internal/services/network/virtual_network_peering_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3ff1873

Please sign in to comment.