Skip to content

Commit

Permalink
azurerm_vpn_server_configuration_policy_group - split create and up…
Browse files Browse the repository at this point in the history
…date function to fix lifecycle - ignore (#26207)
  • Loading branch information
mbfrahry authored Jun 4, 2024
1 parent 228d566 commit 24fbb85
Showing 1 changed file with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand All @@ -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{
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 24fbb85

Please sign in to comment.