Skip to content

Commit

Permalink
azurerm_virtual_network_gateway - force new when ip_configuration
Browse files Browse the repository at this point in the history
overwrite or removing existing ones
  • Loading branch information
ziyeqf committed Oct 29, 2024
1 parent e2798fc commit 6ec9feb
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
48 changes: 48 additions & 0 deletions internal/services/network/virtual_network_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package network

import (
"bytes"
"context"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -47,6 +48,53 @@ func resourceVirtualNetworkGateway() *pluginsdk.Resource {
},

Schema: resourceVirtualNetworkGatewaySchema(),

CustomizeDiff: pluginsdk.ForceNewIfChange("ip_configuration", func(ctx context.Context, old, n, meta interface{}) bool {
if oldArray, ok := old.([]interface{}); ok {
if newArray, ok := n.([]interface{}); ok {
if len(newArray) < len(oldArray) {
return true
}

// map[name]map[propertyName]value
newIpConfigs := make(map[string]map[string]string, 0)
for _, newItem := range newArray {
newConf := newItem.(map[string]interface{})
if name, ok := newConf["name"]; ok {
nameStr := name.(string)
newIpConfigs[nameStr] = make(map[string]string, 0)
for prop, value := range newConf {
newIpConfigs[nameStr][prop] = value.(string)
}
}
}

// All old items shall be kept in the new configuration.
for _, oldItem := range oldArray {
oldConf := oldItem.(map[string]interface{})
if name, ok := oldConf["name"]; ok {
nameStr := name.(string)
if newConf, ok := newIpConfigs[nameStr]; ok {
if len(oldConf) != len(newConf) {
return true
}
for prop, value := range oldConf {
if newValue, ok := newConf[prop]; ok {
oldValue := value.(string)
if newValue != oldValue {
return true
}
}
}
} else {
return true
}
}
}
}
}
return false
}),
}
}

Expand Down
155 changes: 155 additions & 0 deletions internal/services/network/virtual_network_gateway_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,34 @@ func TestAccVirtualNetworkGateway_updateWithNatRule(t *testing.T) {
})
}

func TestAccVirtualNetworkGateway_ipConfiguration(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_virtual_network_gateway", "test")
r := VirtualNetworkGatewayResource{}

// Add new ip_configuration shall not force new, but removing or overwrite should.
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.ipConfiguration(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.ipConfigurationUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.ipConfiguration(data),
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
})
}

func (t VirtualNetworkGatewayResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := virtualnetworkgateways.ParseVirtualNetworkGatewayID(state.ID)
if err != nil {
Expand Down Expand Up @@ -1878,3 +1906,130 @@ resource "azurerm_virtual_network_gateway_nat_rule" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, tag, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (VirtualNetworkGatewayResource) ipConfiguration(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctestvn-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
address_space = ["10.0.0.0/16"]
}
resource "azurerm_subnet" "test" {
name = "GatewaySubnet"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_public_ip" "test" {
name = "acctestpip-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Static"
sku = "Standard"
zones = ["1", "2", "3"]
}
resource "azurerm_public_ip" "test2" {
name = "acctestpip2-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Static"
sku = "Standard"
zones = ["1", "2", "3"]
}
resource "azurerm_virtual_network_gateway" "test" {
name = "acctestvng-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
type = "Vpn"
vpn_type = "RouteBased"
sku = "VpnGw2AZ"
ip_configuration {
public_ip_address_id = azurerm_public_ip.test.id
subnet_id = azurerm_subnet.test.id
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (VirtualNetworkGatewayResource) ipConfigurationUpdate(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctestvn-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
address_space = ["10.0.0.0/16"]
}
resource "azurerm_subnet" "test" {
name = "GatewaySubnet"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_public_ip" "test" {
name = "acctestpip-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Static"
sku = "Standard"
zones = ["1", "2", "3"]
}
resource "azurerm_public_ip" "test2" {
name = "acctestpip2-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Static"
sku = "Standard"
zones = ["1", "2", "3"]
}
resource "azurerm_virtual_network_gateway" "test" {
name = "acctestvng-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
type = "Vpn"
vpn_type = "RouteBased"
sku = "VpnGw2AZ"
ip_configuration {
name = "default"
public_ip_address_id = azurerm_public_ip.test2.id
subnet_id = azurerm_subnet.test.id
}
ip_configuration {
public_ip_address_id = azurerm_public_ip.test.id
subnet_id = azurerm_subnet.test.id
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

0 comments on commit 6ec9feb

Please sign in to comment.