From 079adce42f62f5da95740eec88f486bc4fa00b18 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Fri, 31 May 2024 14:02:20 -0700 Subject: [PATCH] `azurerm_vpn_site` - split create and update function to fix lifecycle - ignore changes (#26163) --- .../services/network/vpn_site_resource.go | 75 ++++++++++++++++--- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/internal/services/network/vpn_site_resource.go b/internal/services/network/vpn_site_resource.go index fe9f48f15b6e..d5de90205344 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,58 @@ func resourceVpnSiteRead(d *pluginsdk.ResourceData, meta interface{}) error { return nil } +func resourceVpnSiteUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Network.VirtualWANs + ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + 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) + } + + 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("updating %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)