From 24fbb85aee33eaa3ec8db8c6fb56af415e159077 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Tue, 4 Jun 2024 10:45:51 -0700 Subject: [PATCH] `azurerm_vpn_server_configuration_policy_group` - split create and update function to fix lifecycle - ignore (#26207) --- ...ver_configuration_policy_group_resource.go | 70 +++++++++++++++---- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/internal/services/network/vpn_server_configuration_policy_group_resource.go b/internal/services/network/vpn_server_configuration_policy_group_resource.go index 17044dc27d9a..b94b193460f5 100644 --- a/internal/services/network/vpn_server_configuration_policy_group_resource.go +++ b/internal/services/network/vpn_server_configuration_policy_group_resource.go @@ -22,9 +22,9 @@ import ( func resourceVPNServerConfigurationPolicyGroup() *pluginsdk.Resource { return &pluginsdk.Resource{ - Create: resourceVPNServerConfigurationPolicyGroupCreateUpdate, + Create: resourceVPNServerConfigurationPolicyGroupCreate, Read: resourceVPNServerConfigurationPolicyGroupRead, - Update: resourceVPNServerConfigurationPolicyGroupCreateUpdate, + Update: resourceVPNServerConfigurationPolicyGroupUpdate, Delete: resourceVPNServerConfigurationPolicyGroupDelete, Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { @@ -101,10 +101,10 @@ func resourceVPNServerConfigurationPolicyGroup() *pluginsdk.Resource { } } -func resourceVPNServerConfigurationPolicyGroupCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { +func resourceVPNServerConfigurationPolicyGroupCreate(d *pluginsdk.ResourceData, meta interface{}) error { subscriptionId := meta.(*clients.Client).Account.SubscriptionId client := meta.(*clients.Client).Network.VirtualWANs - ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() vpnServerConfigurationId, err := virtualwans.ParseVpnServerConfigurationID(d.Get("vpn_server_configuration_id").(string)) @@ -117,17 +117,15 @@ func resourceVPNServerConfigurationPolicyGroupCreateUpdate(d *pluginsdk.Resource id := virtualwans.NewConfigurationPolicyGroupID(subscriptionId, vpnServerConfigurationId.ResourceGroupName, vpnServerConfigurationId.VpnServerConfigurationName, d.Get("name").(string)) - if d.IsNewResource() { - existing, err := client.ConfigurationPolicyGroupsGet(ctx, id) - if err != nil { - if !response.WasNotFound(existing.HttpResponse) { - return fmt.Errorf("checking for existing %s: %+v", id, err) - } - } + existing, err := client.ConfigurationPolicyGroupsGet(ctx, id) + if err != nil { if !response.WasNotFound(existing.HttpResponse) { - return tf.ImportAsExistsError("azurerm_vpn_server_configuration_policy_group", id.ID()) + return fmt.Errorf("checking for existing %s: %+v", id, err) } } + if !response.WasNotFound(existing.HttpResponse) { + return tf.ImportAsExistsError("azurerm_vpn_server_configuration_policy_group", id.ID()) + } payload := virtualwans.VpnServerConfigurationPolicyGroup{ Properties: &virtualwans.VpnServerConfigurationPolicyGroupProperties{ @@ -138,7 +136,7 @@ func resourceVPNServerConfigurationPolicyGroupCreateUpdate(d *pluginsdk.Resource } if err := client.ConfigurationPolicyGroupsCreateOrUpdateThenPoll(ctx, id, payload); err != nil { - return fmt.Errorf("creating/updating %s: %+v", id, err) + return fmt.Errorf("creating %s: %+v", id, err) } d.SetId(id.ID()) @@ -184,6 +182,52 @@ func resourceVPNServerConfigurationPolicyGroupRead(d *pluginsdk.ResourceData, me return nil } +func resourceVPNServerConfigurationPolicyGroupUpdate(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.ParseConfigurationPolicyGroupID(d.Id()) + if err != nil { + return err + } + + vpnServerConfigurationId := virtualwans.NewVpnServerConfigurationID(id.SubscriptionId, id.ResourceGroupName, id.VpnServerConfigurationName) + + locks.ByID(vpnServerConfigurationId.ID()) + defer locks.UnlockByID(vpnServerConfigurationId.ID()) + + existing, err := client.ConfigurationPolicyGroupsGet(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("policy") { + payload.Properties.PolicyMembers = expandVPNServerConfigurationPolicyGroupPolicyMembers(d.Get("policy").(*pluginsdk.Set).List()) + } + + if d.HasChange("priority") { + payload.Properties.Priority = pointer.To(int64(d.Get("priority").(int))) + } + + if err := client.ConfigurationPolicyGroupsCreateOrUpdateThenPoll(ctx, *id, *payload); err != nil { + return fmt.Errorf("updating %s: %+v", id, err) + } + + d.SetId(id.ID()) + return resourceVPNServerConfigurationPolicyGroupRead(d, meta) +} + func resourceVPNServerConfigurationPolicyGroupDelete(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VirtualWANs ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)