From 9839901168766cee49530819358a4316a4f241ef Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 23 May 2024 12:07:27 +0800 Subject: [PATCH 1/2] azurerm_virtual_network_peering - update to use go-azure-sdk --- internal/services/network/client/client.go | 5 - .../virtual_network_peering_resource.go | 113 +++++++++--------- .../virtual_network_peering_resource_test.go | 15 +-- 3 files changed, 67 insertions(+), 66 deletions(-) diff --git a/internal/services/network/client/client.go b/internal/services/network/client/client.go index 5e863dee392e..9e0135c43c85 100644 --- a/internal/services/network/client/client.go +++ b/internal/services/network/client/client.go @@ -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 } @@ -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) @@ -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, diff --git a/internal/services/network/virtual_network_peering_resource.go b/internal/services/network/virtual_network_peering_resource.go index 57f46592f9ae..a824437d7d4f 100644 --- a/internal/services/network/virtual_network_peering_resource.go +++ b/internal/services/network/virtual_network_peering_resource.go @@ -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" @@ -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 }), @@ -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)), }, }, } @@ -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, @@ -173,11 +172,11 @@ 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 } @@ -185,34 +184,38 @@ func resourceVirtualNetworkPeeringUpdate(d *pluginsdk.ResourceData, meta interfa locks.ByID(virtualNetworkPeeringResourceType) defer locks.UnlockByID(virtualNetworkPeeringResourceType) - existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name) + existing, err := client.Get(ctx, pointer.From(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", pointer.From(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) + future, err := client.CreateOrUpdate(ctx, pointer.From(id), pointer.From(existing.Model), virtualnetworkpeerings.CreateOrUpdateOperationOptions{SyncRemoteAddressSpace: pointer.To(virtualnetworkpeerings.SyncRemoteAddressSpaceTrue)}) if err != nil { return fmt.Errorf("updating %s: %+v", *id, err) } - if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { + if err := future.Poller.PollUntilDone(ctx); err != nil { return fmt.Errorf("waiting for the update of %s: %+v", *id, err) } @@ -220,54 +223,56 @@ func resourceVirtualNetworkPeeringUpdate(d *pluginsdk.ResourceData, meta interfa } 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, pointer.From(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 fmt.Errorf("parsing %q as a Virtual Network ID: %+v", *network.Id, 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 } @@ -275,12 +280,12 @@ func resourceVirtualNetworkPeeringDelete(d *pluginsdk.ResourceData, meta interfa locks.ByID(virtualNetworkPeeringResourceType) defer locks.UnlockByID(virtualNetworkPeeringResourceType) - future, err := client.Delete(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name) + future, err := client.Delete(ctx, pointer.From(id)) if err != nil { return fmt.Errorf("deleting %s: %+v", *id, err) } - if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { + if err = future.Poller.PollUntilDone(ctx); err != nil { return fmt.Errorf("waiting for deletion of %s: %+v", *id, err) } diff --git a/internal/services/network/virtual_network_peering_resource_test.go b/internal/services/network/virtual_network_peering_resource_test.go index f98e2eea28b3..ab9550b2a3ee 100644 --- a/internal/services/network/virtual_network_peering_resource_test.go +++ b/internal/services/network/virtual_network_peering_resource_test.go @@ -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" ) @@ -122,30 +123,30 @@ 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, pointer.From(id)) if err != nil { return nil, fmt.Errorf("reading %s: %+v", *id, err) } - return utils.Bool(resp.ID != nil), nil + return utils.Bool(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) + future, err := client.Network.VirtualNetworkPeerings.Delete(ctx, pointer.From(id)) if err != nil { return nil, fmt.Errorf("deleting on virtual network peering: %+v", err) } - if err = future.WaitForCompletionRef(ctx, client.Network.VnetPeeringsClient.Client); err != nil { + if err = future.Poller.PollUntilDone(ctx); err != nil { return nil, fmt.Errorf("waiting for deletion of %s: %+v", *id, err) } From 6ca958eac57890f30832c2b076a432c5ab79ca6a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Mon, 27 May 2024 14:36:35 +0800 Subject: [PATCH 2/2] update code per comments --- .../virtual_network_peering_resource.go | 21 ++++++------------- .../virtual_network_peering_resource_test.go | 11 +++------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/internal/services/network/virtual_network_peering_resource.go b/internal/services/network/virtual_network_peering_resource.go index a824437d7d4f..6b9b27646b9b 100644 --- a/internal/services/network/virtual_network_peering_resource.go +++ b/internal/services/network/virtual_network_peering_resource.go @@ -184,13 +184,13 @@ func resourceVirtualNetworkPeeringUpdate(d *pluginsdk.ResourceData, meta interfa locks.ByID(virtualNetworkPeeringResourceType) defer locks.UnlockByID(virtualNetworkPeeringResourceType) - existing, err := client.Get(ctx, pointer.From(id)) + 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", pointer.From(id)) + return fmt.Errorf("retrieving %s: `properties` was nil", *id) } if d.HasChange("allow_forwarded_traffic") { @@ -211,13 +211,9 @@ func resourceVirtualNetworkPeeringUpdate(d *pluginsdk.ResourceData, meta interfa } } - future, err := client.CreateOrUpdate(ctx, pointer.From(id), pointer.From(existing.Model), virtualnetworkpeerings.CreateOrUpdateOperationOptions{SyncRemoteAddressSpace: pointer.To(virtualnetworkpeerings.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.Poller.PollUntilDone(ctx); err != nil { - return fmt.Errorf("waiting for the update of %s: %+v", *id, err) - } return resourceVirtualNetworkPeeringRead(d, meta) } @@ -232,7 +228,7 @@ func resourceVirtualNetworkPeeringRead(d *pluginsdk.ResourceData, meta interface return err } - resp, err := client.Get(ctx, pointer.From(id)) + resp, err := client.Get(ctx, *id) if err != nil { if response.WasNotFound(resp.HttpResponse) { d.SetId("") @@ -256,7 +252,7 @@ func resourceVirtualNetworkPeeringRead(d *pluginsdk.ResourceData, meta interface 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) + return err } remoteVirtualNetworkId = parsed.ID() } @@ -280,14 +276,9 @@ func resourceVirtualNetworkPeeringDelete(d *pluginsdk.ResourceData, meta interfa locks.ByID(virtualNetworkPeeringResourceType) defer locks.UnlockByID(virtualNetworkPeeringResourceType) - future, err := client.Delete(ctx, pointer.From(id)) - if err != nil { + if err := client.DeleteThenPoll(ctx, *id); err != nil { return fmt.Errorf("deleting %s: %+v", *id, err) } - if err = future.Poller.PollUntilDone(ctx); err != nil { - return fmt.Errorf("waiting for deletion of %s: %+v", *id, err) - } - return err } diff --git a/internal/services/network/virtual_network_peering_resource_test.go b/internal/services/network/virtual_network_peering_resource_test.go index ab9550b2a3ee..0d15de730df4 100644 --- a/internal/services/network/virtual_network_peering_resource_test.go +++ b/internal/services/network/virtual_network_peering_resource_test.go @@ -127,12 +127,12 @@ func (r VirtualNetworkPeeringResource) Exists(ctx context.Context, clients *clie if err != nil { return nil, err } - resp, err := clients.Network.VirtualNetworkPeerings.Get(ctx, pointer.From(id)) + resp, err := clients.Network.VirtualNetworkPeerings.Get(ctx, *id) if err != nil { return nil, fmt.Errorf("reading %s: %+v", *id, err) } - return utils.Bool(resp.Model != nil), nil + return pointer.To(resp.Model != nil), nil } func (r VirtualNetworkPeeringResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { @@ -141,15 +141,10 @@ func (r VirtualNetworkPeeringResource) Destroy(ctx context.Context, client *clie return nil, err } - future, err := client.Network.VirtualNetworkPeerings.Delete(ctx, pointer.From(id)) - 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.Poller.PollUntilDone(ctx); err != nil { - return nil, fmt.Errorf("waiting for deletion of %s: %+v", *id, err) - } - return utils.Bool(true), nil }