Skip to content

Commit

Permalink
azurerm_vpn_site - split create and update function to fix lifecycl…
Browse files Browse the repository at this point in the history
…e - ignore changes (#26163)
  • Loading branch information
mbfrahry authored May 31, 2024
1 parent 7bd124f commit 079adce
Showing 1 changed file with 63 additions and 12 deletions.
75 changes: 63 additions & 12 deletions internal/services/network/vpn_site_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 079adce

Please sign in to comment.