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_virtual_network_gateway - update the resource with existing Nat Rules #25690

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions internal/services/network/virtual_network_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ func resourceVirtualNetworkGatewayCreateUpdate(d *pluginsdk.ResourceData, meta i

id := parse.NewVirtualNetworkGatewayID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

var existingVNetGateway network.VirtualNetworkGateway
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
Expand All @@ -668,12 +669,18 @@ func resourceVirtualNetworkGatewayCreateUpdate(d *pluginsdk.ResourceData, meta i
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_virtual_network_gateway", id.ID())
}
} else {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
return err
}
existingVNetGateway = existing
}

location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

properties, err := getVirtualNetworkGatewayProperties(id, d)
properties, err := getVirtualNetworkGatewayProperties(id, d, existingVNetGateway)
if err != nil {
return err
}
Expand Down Expand Up @@ -802,7 +809,7 @@ func resourceVirtualNetworkGatewayDelete(d *pluginsdk.ResourceData, meta interfa
return nil
}

func getVirtualNetworkGatewayProperties(id parse.VirtualNetworkGatewayId, d *pluginsdk.ResourceData) (*network.VirtualNetworkGatewayPropertiesFormat, error) {
func getVirtualNetworkGatewayProperties(id parse.VirtualNetworkGatewayId, d *pluginsdk.ResourceData, existingVNetGateway network.VirtualNetworkGateway) (*network.VirtualNetworkGatewayPropertiesFormat, error) {
gatewayType := network.VirtualNetworkGatewayType(d.Get("type").(string))
vpnType := network.VpnType(d.Get("vpn_type").(string))
enableBgp := d.Get("enable_bgp").(bool)
Expand Down Expand Up @@ -857,6 +864,10 @@ func getVirtualNetworkGatewayProperties(id parse.VirtualNetworkGatewayId, d *plu
props.BgpSettings = bgpSettings
}

if existingVNetGateway.VirtualNetworkGatewayPropertiesFormat != nil && existingVNetGateway.VirtualNetworkGatewayPropertiesFormat.NatRules != nil {
props.NatRules = existingVNetGateway.VirtualNetworkGatewayPropertiesFormat.NatRules
}

// Sku validation for policy-based VPN gateways
if props.GatewayType == network.VirtualNetworkGatewayTypeVpn && props.VpnType == network.VpnTypePolicyBased {
if ok, err := evaluateSchemaValidateFunc(string(props.Sku.Name), "sku", validateVirtualNetworkGatewayPolicyBasedVpnSku()); !ok {
Expand Down
108 changes: 108 additions & 0 deletions internal/services/network/virtual_network_gateway_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,28 @@ func TestAccVirtualNetworkGateway_updateTagsWithBgpSettings(t *testing.T) {
})
}

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.updateWithNatRule(data, "Test1"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.updateWithNatRule(data, "Test2"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t VirtualNetworkGatewayResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
gatewayName := state.Attributes["name"]
resourceGroup := state.Attributes["resource_group_name"]
Expand Down Expand Up @@ -1763,3 +1785,89 @@ resource "azurerm_virtual_network_gateway" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (VirtualNetworkGatewayResource) updateWithNatRule(data acceptance.TestData, tag string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_client_config" "current" {}

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.1.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.1.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"
}

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"
enable_bgp = false
active_active = false
private_ip_address_enabled = false
sku = "VpnGw2"
generation = "Generation2"

ip_configuration {
name = "default"
public_ip_address_id = azurerm_public_ip.test.id
private_ip_address_allocation = "Dynamic"
subnet_id = azurerm_subnet.test.id
}

tags = {
env = "%s"
}
}

data "azurerm_virtual_network_gateway" "test" {
name = azurerm_virtual_network_gateway.test.name
resource_group_name = azurerm_virtual_network_gateway.test.resource_group_name
}

resource "azurerm_virtual_network_gateway_nat_rule" "test" {
name = "acctestvngnatrule-%d"
resource_group_name = azurerm_resource_group.test.name
virtual_network_gateway_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/acctestRG-%d/providers/Microsoft.Network/virtualNetworkGateways/acctestvng-%d"
mode = "EgressSnat"
type = "Dynamic"
ip_configuration_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/acctestRG-%d/providers/Microsoft.Network/virtualNetworkGateways/acctestvng-%d/ipConfigurations/default"

external_mapping {
address_space = "10.1.0.0/26"
}

internal_mapping {
address_space = "10.2.0.0/26"
}

depends_on = [data.azurerm_virtual_network_gateway.test]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, tag, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}
Loading