Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_vpn_site - split create and update function to fix lifecycle - ignore changes #26163

Merged
merged 3 commits into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading