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

azurerm_virtual_network_peering - update to use go-azure-sdk package #26065

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type Client struct {
VnetGatewayNatRuleClient *network.VirtualNetworkGatewayNatRulesClient
VnetGatewayClient *network.VirtualNetworkGatewaysClient
VnetClient *network.VirtualNetworksClient
VnetPeeringsClient *network.VirtualNetworkPeeringsClient
VirtualWanClient *network.VirtualWansClient
VirtualHubClient *network.VirtualHubsClient
}
Expand Down Expand Up @@ -118,9 +117,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
VnetClient := network.NewVirtualNetworksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VnetClient.Client, o.ResourceManagerAuthorizer)

VnetPeeringsClient := network.NewVirtualNetworkPeeringsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VnetPeeringsClient.Client, o.ResourceManagerAuthorizer)

PublicIPsClient := network.NewPublicIPAddressesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&PublicIPsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -219,7 +215,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
VnetGatewayNatRuleClient: &VnetGatewayNatRuleClient,
VnetGatewayClient: &VnetGatewayClient,
VnetClient: &VnetClient,
VnetPeeringsClient: &VnetPeeringsClient,
VirtualWanClient: &VirtualWanClient,
VirtualHubClient: &VirtualHubClient,
PrivateDnsZoneGroupClient: &PrivateDnsZoneGroupClient,
Expand Down
118 changes: 57 additions & 61 deletions internal/services/network/virtual_network_peering_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/virtualnetworkpeerings"
"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/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/network/2022-07-01/network"
)

const virtualNetworkPeeringResourceType = "azurerm_virtual_network_peering"
Expand All @@ -31,7 +30,7 @@ func resourceVirtualNetworkPeering() *pluginsdk.Resource {
Update: resourceVirtualNetworkPeeringUpdate,
Delete: resourceVirtualNetworkPeeringDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.VirtualNetworkPeeringID(id)
_, err := virtualnetworkpeerings.ParseVirtualNetworkPeeringID(id)
return err
}),

Expand Down Expand Up @@ -100,31 +99,31 @@ func resourceVirtualNetworkPeering() *pluginsdk.Resource {
}

func resourceVirtualNetworkPeeringCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.VnetPeeringsClient
client := meta.(*clients.Client).Network.VirtualNetworkPeerings
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
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))
existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
id := virtualnetworkpeerings.NewVirtualNetworkPeeringID(subscriptionId, d.Get("resource_group_name").(string), d.Get("virtual_network_name").(string), d.Get("name").(string))
existing, err := client.Get(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

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

peer := network.VirtualNetworkPeering{
VirtualNetworkPeeringPropertiesFormat: &network.VirtualNetworkPeeringPropertiesFormat{
peer := virtualnetworkpeerings.VirtualNetworkPeering{
Properties: &virtualnetworkpeerings.VirtualNetworkPeeringPropertiesFormat{
AllowVirtualNetworkAccess: pointer.To(d.Get("allow_virtual_network_access").(bool)),
AllowForwardedTraffic: pointer.To(d.Get("allow_forwarded_traffic").(bool)),
AllowGatewayTransit: pointer.To(d.Get("allow_gateway_transit").(bool)),
UseRemoteGateways: pointer.To(d.Get("use_remote_gateways").(bool)),
RemoteVirtualNetwork: &network.SubResource{
ID: pointer.To(d.Get("remote_virtual_network_id").(string)),
RemoteVirtualNetwork: &virtualnetworkpeerings.SubResource{
Id: pointer.To(d.Get("remote_virtual_network_id").(string)),
},
},
}
Expand All @@ -140,25 +139,25 @@ func resourceVirtualNetworkPeeringCreate(d *pluginsdk.ResourceData, meta interfa
Pending: []string{"Pending"},
Target: []string{"Created"},
Refresh: func() (interface{}, string, error) {
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name, peer, network.SyncRemoteAddressSpaceTrue)
future, err := client.CreateOrUpdate(ctx, id, peer, virtualnetworkpeerings.CreateOrUpdateOperationOptions{SyncRemoteAddressSpace: pointer.To(virtualnetworkpeerings.SyncRemoteAddressSpaceTrue)})
if err != nil {
if utils.ResponseErrorIsRetryable(err) {
return future.Response(), "Pending", err
return future.HttpResponse, "Pending", err
} else {
if resp := future.Response(); resp != nil && response.WasBadRequest(resp) && strings.Contains(err.Error(), "ReferencedResourceNotProvisioned") {
if resp := future.HttpResponse; resp != nil && response.WasBadRequest(resp) && 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 future.Response(), "Pending", err
return future.HttpResponse, "Pending", err
}
}

return future.Response(), "", err
return future.HttpResponse, "", err
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return future.Response(), "", err
if err = future.Poller.PollUntilDone(ctx); err != nil {
return future.HttpResponse, "", err
}

return future.Response(), "Created", nil
return future.HttpResponse, "Created", nil
},
Timeout: time.Until(deadline),
Delay: 15 * time.Second,
Expand All @@ -173,116 +172,113 @@ func resourceVirtualNetworkPeeringCreate(d *pluginsdk.ResourceData, meta interfa
}

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

id, err := parse.VirtualNetworkPeeringID(d.Id())
id, err := virtualnetworkpeerings.ParseVirtualNetworkPeeringID(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)
existing, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if existing.Model == nil || existing.Model.Properties == nil {
return fmt.Errorf("retrieving %s: `properties` was nil", *id)
}

if d.HasChange("allow_forwarded_traffic") {
existing.VirtualNetworkPeeringPropertiesFormat.AllowForwardedTraffic = pointer.To(d.Get("allow_forwarded_traffic").(bool))
existing.Model.Properties.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))
existing.Model.Properties.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))
existing.Model.Properties.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))
existing.Model.Properties.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)),
existing.Model.Properties.RemoteVirtualNetwork = &virtualnetworkpeerings.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 {
if err := client.CreateOrUpdateThenPoll(ctx, *id, *existing.Model, virtualnetworkpeerings.CreateOrUpdateOperationOptions{SyncRemoteAddressSpace: pointer.To(virtualnetworkpeerings.SyncRemoteAddressSpaceTrue)}); 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
client := meta.(*clients.Client).Network.VirtualNetworkPeerings
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
resp, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("name", id.VirtualNetworkPeeringName)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("virtual_network_name", id.VirtualNetworkName)

if peer := resp.VirtualNetworkPeeringPropertiesFormat; peer != nil {
d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess)
d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic)
d.Set("allow_gateway_transit", peer.AllowGatewayTransit)
d.Set("use_remote_gateways", peer.UseRemoteGateways)

remoteVirtualNetworkId := ""
if network := peer.RemoteVirtualNetwork; network != nil {
parsed, err := commonids.ParseVirtualNetworkIDInsensitively(*network.ID)
if err != nil {
return fmt.Errorf("parsing %q as a Virtual Network ID: %+v", *network.ID, err)
if model := resp.Model; model != nil {
if peer := model.Properties; peer != nil {
d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess)
d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic)
d.Set("allow_gateway_transit", peer.AllowGatewayTransit)
d.Set("use_remote_gateways", peer.UseRemoteGateways)

remoteVirtualNetworkId := ""
if network := peer.RemoteVirtualNetwork; network != nil {
parsed, err := commonids.ParseVirtualNetworkIDInsensitively(*network.Id)
if err != nil {
return err
}
remoteVirtualNetworkId = parsed.ID()
}
remoteVirtualNetworkId = parsed.ID()
d.Set("remote_virtual_network_id", remoteVirtualNetworkId)
}
d.Set("remote_virtual_network_id", remoteVirtualNetworkId)
}

return nil
}

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

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

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

future, err := client.Delete(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
if err != nil {
if err := client.DeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/virtualnetworkpeerings"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -122,33 +123,28 @@ func TestAccVirtualNetworkPeering_update(t *testing.T) {
}

func (r VirtualNetworkPeeringResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.VirtualNetworkPeeringID(state.ID)
id, err := virtualnetworkpeerings.ParseVirtualNetworkPeeringID(state.ID)
if err != nil {
return nil, err
}
resp, err := clients.Network.VnetPeeringsClient.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
resp, err := clients.Network.VirtualNetworkPeerings.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("reading %s: %+v", *id, err)
}

return utils.Bool(resp.ID != nil), nil
return pointer.To(resp.Model != nil), nil
}

func (r VirtualNetworkPeeringResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.VirtualNetworkPeeringID(state.ID)
id, err := virtualnetworkpeerings.ParseVirtualNetworkPeeringID(state.ID)
if err != nil {
return nil, err
}

future, err := client.Network.VnetPeeringsClient.Delete(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name)
if err != nil {
if err := client.Network.VirtualNetworkPeerings.DeleteThenPoll(ctx, *id); err != nil {
return nil, fmt.Errorf("deleting on virtual network peering: %+v", err)
}

if err = future.WaitForCompletionRef(ctx, client.Network.VnetPeeringsClient.Client); err != nil {
return nil, fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

return utils.Bool(true), nil
}

Expand Down
Loading