From aa28e0225188ecca8251bc1f8210d05c777d0c87 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 30 May 2024 15:54:53 -0700 Subject: [PATCH 1/3] azurerm_vpn_site - split create/update --- .../services/network/vpn_site_resource.go | 73 ++++++++++++++++--- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/internal/services/network/vpn_site_resource.go b/internal/services/network/vpn_site_resource.go index 641787be3673..1e35a57ff773 100644 --- a/internal/services/network/vpn_site_resource.go +++ b/internal/services/network/vpn_site_resource.go @@ -25,9 +25,9 @@ import ( func resourceVpnSite() *pluginsdk.Resource { return &pluginsdk.Resource{ - Create: resourceVpnSiteCreateUpdate, + Create: resourceVpnSiteCreate, Read: resourceVpnSiteRead, - Update: resourceVpnSiteCreateUpdate, + Update: resourceVpnSiteUpdate, Delete: resourceVpnSiteDelete, Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { @@ -183,26 +183,25 @@ func resourceVpnSite() *pluginsdk.Resource { } } -func resourceVpnSiteCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { +func resourceVpnSiteCreate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VirtualWANs 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 := virtualwans.NewVpnSiteID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) - if d.IsNewResource() { - resp, err := client.VpnSitesGet(ctx, id) - if err != nil { - if !response.WasNotFound(resp.HttpResponse) { - return fmt.Errorf("checking for existing %s: %+v", id, err) - } - } + resp, err := client.VpnSitesGet(ctx, id) + if err != nil { if !response.WasNotFound(resp.HttpResponse) { - return tf.ImportAsExistsError("azurerm_vpn_site", id.ID()) + return fmt.Errorf("checking for existing %s: %+v", id, err) } } + if !response.WasNotFound(resp.HttpResponse) { + return tf.ImportAsExistsError("azurerm_vpn_site", id.ID()) + } + payload := virtualwans.VpnSite{ Location: pointer.To(location.Normalize(d.Get("location").(string))), Properties: &virtualwans.VpnSiteProperties{ @@ -290,6 +289,56 @@ func resourceVpnSiteRead(d *pluginsdk.ResourceData, meta interface{}) error { return nil } +func resourceVpnSiteUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Network.VirtualWANs + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + id := virtualwans.NewVpnSiteID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) + existing, err := client.VpnSitesGet(ctx, id) + if err != nil { + return fmt.Errorf("retrieving %s: %+v", id, err) + } + + if existing.Model == nil { + return fmt.Errorf("retrieving %s: `model` was nil", id) + } + + if existing.Model.Properties == nil { + return fmt.Errorf("retrieving %s: `properties` was nil", id) + } + + payload := existing.Model + + if d.HasChange("address_cidrs") { + payload.Properties.AddressSpace = expandVpnSiteAddressSpace(d.Get("address_cidrs").(*pluginsdk.Set).List()) + } + + if d.HasChange("device_vendor") || d.HasChange("device_model") { + payload.Properties.DeviceProperties = expandVpnSiteDeviceProperties(d.Get("device_vendor").(string), d.Get("device_model").(string)) + } + + if d.HasChange("link") { + payload.Properties.VpnSiteLinks = expandVpnSiteLinks(d.Get("link").([]interface{})) + } + + if d.HasChange("o365_policy") { + payload.Properties.O365Policy = expandVpnSiteO365Policy(d.Get("o365_policy").([]interface{})) + } + + if d.HasChange("tags") { + payload.Tags = tags.Expand(d.Get("tags").(map[string]interface{})) + } + + if err := client.VpnSitesCreateOrUpdateThenPoll(ctx, id, *payload); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) + } + + d.SetId(id.ID()) + return resourceVpnSiteRead(d, meta) +} + func resourceVpnSiteDelete(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VirtualWANs ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) From 6788e3c6980f27081c54b88d80d737d026245788 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 30 May 2024 15:58:11 -0700 Subject: [PATCH 2/3] update comment --- internal/services/network/vpn_site_resource.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/services/network/vpn_site_resource.go b/internal/services/network/vpn_site_resource.go index 1e35a57ff773..5a27fe3e61e4 100644 --- a/internal/services/network/vpn_site_resource.go +++ b/internal/services/network/vpn_site_resource.go @@ -292,7 +292,7 @@ func resourceVpnSiteRead(d *pluginsdk.ResourceData, meta interface{}) error { func resourceVpnSiteUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VirtualWANs subscriptionId := meta.(*clients.Client).Account.SubscriptionId - ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) defer cancel() id := virtualwans.NewVpnSiteID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) @@ -332,7 +332,7 @@ func resourceVpnSiteUpdate(d *pluginsdk.ResourceData, meta interface{}) error { } if err := client.VpnSitesCreateOrUpdateThenPoll(ctx, id, *payload); err != nil { - return fmt.Errorf("creating %s: %+v", id, err) + return fmt.Errorf("updating %s: %+v", id, err) } d.SetId(id.ID()) From 093ce79885cf2f6479c541471e995c707e98935f Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 31 May 2024 11:27:18 -0700 Subject: [PATCH 3/3] Address review --- internal/services/network/vpn_site_resource.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/services/network/vpn_site_resource.go b/internal/services/network/vpn_site_resource.go index 5a27fe3e61e4..5f72d7cb89a0 100644 --- a/internal/services/network/vpn_site_resource.go +++ b/internal/services/network/vpn_site_resource.go @@ -291,12 +291,14 @@ func resourceVpnSiteRead(d *pluginsdk.ResourceData, meta interface{}) error { func resourceVpnSiteUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VirtualWANs - subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - id := virtualwans.NewVpnSiteID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) - existing, err := client.VpnSitesGet(ctx, id) + id, err := virtualwans.ParseVpnSiteID(d.Id()) + if err != nil { + return err + } + existing, err := client.VpnSitesGet(ctx, *id) if err != nil { return fmt.Errorf("retrieving %s: %+v", id, err) } @@ -331,7 +333,7 @@ func resourceVpnSiteUpdate(d *pluginsdk.ResourceData, meta interface{}) error { payload.Tags = tags.Expand(d.Get("tags").(map[string]interface{})) } - if err := client.VpnSitesCreateOrUpdateThenPoll(ctx, id, *payload); err != nil { + if err := client.VpnSitesCreateOrUpdateThenPoll(ctx, *id, *payload); err != nil { return fmt.Errorf("updating %s: %+v", id, err) }