Skip to content

Commit

Permalink
virtual_network_gateway_connection: add traffic_selector_policy block…
Browse files Browse the repository at this point in the history
… and tests (#6586)

Co-authored-by: Carlo <[email protected]>
  • Loading branch information
carloVentrella and cventr-agilelab authored Jul 13, 2020
1 parent a5da7c0 commit 9806f0d
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ func TestAccAzureRMVirtualNetworkGatewayConnection_ipsecpolicy(t *testing.T) {
})
}

func TestAccAzureRMVirtualNetworkGatewayConnection_trafficSelectorPolicy(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_virtual_network_gateway_connection", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMVirtualNetworkGatewayConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMVirtualNetworkGatewayConnection_trafficselectorpolicy(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualNetworkGatewayConnectionExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "traffic_selector_policy.0.local_address_cidrs.0", "10.66.18.0/24"),
resource.TestCheckResourceAttr(data.ResourceName, "traffic_selector_policy.0.local_address_cidrs.1", "10.66.17.0/24"),
resource.TestCheckResourceAttr(data.ResourceName, "traffic_selector_policy.0.remote_address_cidrs.0", "10.1.1.0/24"),
),
},
},
})
}

func TestAccAzureRMVirtualNetworkGatewayConnection_connectionprotocol(t *testing.T) {
expectedConnectionProtocol := "IKEv1"
data := acceptance.BuildTestData(t, "azurerm_virtual_network_gateway_connection", "test")
Expand Down Expand Up @@ -674,3 +695,95 @@ resource "azurerm_virtual_network_gateway_connection" "test" {
}
`, data.RandomInteger, data.Locations.Primary)
}

func testAccAzureRMVirtualNetworkGatewayConnection_trafficselectorpolicy(data acceptance.TestData) string {
return fmt.Sprintf(`
variable "random" {
default = "%d"
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-${var.random}"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctestvn-${var.random}"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
address_space = ["10.66.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_prefix = "10.66.1.0/24"
}
resource "azurerm_public_ip" "test" {
name = "acctest-${var.random}"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Dynamic"
}
resource "azurerm_virtual_network_gateway" "test" {
name = "acctest-${var.random}"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
type = "Vpn"
vpn_type = "RouteBased"
sku = "VpnGw1"
ip_configuration {
name = "vnetGatewayConfig"
public_ip_address_id = azurerm_public_ip.test.id
private_ip_address_allocation = "Dynamic"
subnet_id = azurerm_subnet.test.id
}
}
resource "azurerm_local_network_gateway" "test" {
name = "acctest"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
gateway_address = "168.62.225.23"
address_space = ["10.1.1.0/24"]
}
resource "azurerm_virtual_network_gateway_connection" "test" {
name = "acctest-${var.random}"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
type = "IPsec"
virtual_network_gateway_id = azurerm_virtual_network_gateway.test.id
local_network_gateway_id = azurerm_local_network_gateway.test.id
use_policy_based_traffic_selectors = true
routing_weight = 20
ipsec_policy {
dh_group = "DHGroup14"
ike_encryption = "AES256"
ike_integrity = "SHA256"
ipsec_encryption = "AES256"
ipsec_integrity = "SHA256"
pfs_group = "PFS2048"
sa_datasize = 102400000
sa_lifetime = 27000
}
shared_key = "4-v3ry-53cr37-1p53c-5h4r3d-k3y"
traffic_selector_policy {
local_address_cidrs = ["10.66.18.0/24", "10.66.17.0/24"]
remote_address_cidrs = ["10.1.1.0/24"]
}
}
`, data.RandomInteger, data.Locations.Primary)
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ func dataSourceArmVirtualNetworkGatewayConnection() *schema.Resource {
Computed: true,
},

"traffic_selector_policy": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"local_address_cidrs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"remote_address_cidrs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},

"ipsec_policy": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -216,6 +240,11 @@ func dataSourceArmVirtualNetworkGatewayConnectionRead(d *schema.ResourceData, me
if err := d.Set("ipsec_policy", ipsecPoliciesSettingsFlat); err != nil {
return fmt.Errorf("Error setting `ipsec_policy`: %+v", err)
}

trafficSelectorsPolicyFlat := flattenArmVirtualNetworkGatewayConnectionDataSourcePolicyTrafficSelectors(gwc.TrafficSelectorPolicies)
if err := d.Set("traffic_selector_policy", trafficSelectorsPolicyFlat); err != nil {
return fmt.Errorf("Error setting `traffic_selector_policy`: %+v", err)
}
}

return nil
Expand Down Expand Up @@ -249,3 +278,18 @@ func flattenArmVirtualNetworkGatewayConnectionDataSourceIpsecPolicies(ipsecPolic

return schemaIpsecPolicies
}

func flattenArmVirtualNetworkGatewayConnectionDataSourcePolicyTrafficSelectors(trafficSelectorPolicies *[]network.TrafficSelectorPolicy) []interface{} {
schemaTrafficSelectorPolicies := make([]interface{}, 0)

if trafficSelectorPolicies != nil {
for _, trafficSelectorPolicy := range *trafficSelectorPolicies {
schemaTrafficSelectorPolicies = append(schemaTrafficSelectorPolicies, map[string]interface{}{
"local_address_cidrs": utils.FlattenStringSlice(trafficSelectorPolicy.LocalAddressRanges),
"remote_address_cidrs": utils.FlattenStringSlice(trafficSelectorPolicy.RemoteAddressRanges),
})
}
}

return schemaTrafficSelectorPolicies
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,30 @@ func resourceArmVirtualNetworkGatewayConnection() *schema.Resource {
}, false),
},

"traffic_selector_policy": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"local_address_cidrs": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"remote_address_cidrs": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},

"ipsec_policy": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -394,6 +418,11 @@ func resourceArmVirtualNetworkGatewayConnectionRead(d *schema.ResourceData, meta
}
}

trafficSelectorPolicies := flattenArmVirtualNetworkGatewayConnectionTrafficSelectorPolicies(conn.TrafficSelectorPolicies)
if err := d.Set("traffic_selector_policy", trafficSelectorPolicies); err != nil {
return fmt.Errorf("Error setting `traffic_selector_policy`: %+v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down Expand Up @@ -504,6 +533,10 @@ func getArmVirtualNetworkGatewayConnectionProperties(d *schema.ResourceData) (*n
props.ConnectionProtocol = network.VirtualNetworkGatewayConnectionProtocol(connectionProtocol)
}

if v, ok := d.GetOk("traffic_selector_policy"); ok {
props.TrafficSelectorPolicies = expandArmVirtualNetworkGatewayConnectionTrafficSelectorPolicies(v.([]interface{}))
}

if v, ok := d.GetOk("ipsec_policy"); ok {
props.IpsecPolicies = expandArmVirtualNetworkGatewayConnectionIpsecPolicies(v.([]interface{}))
}
Expand Down Expand Up @@ -587,6 +620,25 @@ func expandArmVirtualNetworkGatewayConnectionIpsecPolicies(schemaIpsecPolicies [
return &ipsecPolicies
}

func expandArmVirtualNetworkGatewayConnectionTrafficSelectorPolicies(schemaTrafficSelectorPolicies []interface{}) *[]network.TrafficSelectorPolicy {
trafficSelectorPolicies := make([]network.TrafficSelectorPolicy, 0, len(schemaTrafficSelectorPolicies))

for _, d := range schemaTrafficSelectorPolicies {
schemaTrafficSelectorPolicy := d.(map[string]interface{})
trafficSelectorPolicy := &network.TrafficSelectorPolicy{}
if localAddressRanges, ok := schemaTrafficSelectorPolicy["local_address_cidrs"].([]interface{}); ok {
trafficSelectorPolicy.LocalAddressRanges = utils.ExpandStringSlice(localAddressRanges)
}
if remoteAddressRanges, ok := schemaTrafficSelectorPolicy["remote_address_cidrs"].([]interface{}); ok {
trafficSelectorPolicy.RemoteAddressRanges = utils.ExpandStringSlice(remoteAddressRanges)
}

trafficSelectorPolicies = append(trafficSelectorPolicies, *trafficSelectorPolicy)
}

return &trafficSelectorPolicies
}

func flattenArmVirtualNetworkGatewayConnectionIpsecPolicies(ipsecPolicies *[]network.IpsecPolicy) []interface{} {
schemaIpsecPolicies := make([]interface{}, 0)

Expand Down Expand Up @@ -615,3 +667,18 @@ func flattenArmVirtualNetworkGatewayConnectionIpsecPolicies(ipsecPolicies *[]net

return schemaIpsecPolicies
}

func flattenArmVirtualNetworkGatewayConnectionTrafficSelectorPolicies(trafficSelectorPolicies *[]network.TrafficSelectorPolicy) []interface{} {
schemaTrafficSelectorPolicies := make([]interface{}, 0)

if trafficSelectorPolicies != nil {
for _, trafficSelectorPolicy := range *trafficSelectorPolicies {
schemaTrafficSelectorPolicies = append(schemaTrafficSelectorPolicies, map[string]interface{}{
"local_address_cidrs": utils.FlattenStringSlice(trafficSelectorPolicy.LocalAddressRanges),
"remote_address_cidrs": utils.FlattenStringSlice(trafficSelectorPolicy.RemoteAddressRanges),
})
}
}

return schemaTrafficSelectorPolicies
}
10 changes: 10 additions & 0 deletions website/docs/d/virtual_network_gateway_connection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ output "virtual_network_gateway_connection_id" {
Only a single policy can be defined for a connection. For details on
custom policies refer to [the relevant section in the Azure documentation](https://docs.microsoft.com/en-us/azure/vpn-gateway/vpn-gateway-ipsecikepolicy-rm-powershell).

* `traffic_selector_policy` A `traffic_selector_policy` which allows to specify traffic selector policy proposal to be used in a virtual network gateway connection.
Only one block can be defined for a connection.
For details about traffic selectors refer to [the relevant section in the Azure documentation](https://docs.microsoft.com/en-us/azure/vpn-gateway/vpn-gateway-connect-multiple-policybased-rm-ps).

* `tags` - A mapping of tags to assign to the resource.

The `ipsec_policy` block supports:
Expand Down Expand Up @@ -102,6 +106,12 @@ The `ipsec_policy` block supports:
* `sa_lifetime` - The IPSec SA lifetime in seconds. Must be at least
`300` seconds.

The `traffic_selector_policy` block supports:

* `local_address_cidrs` - List of local CIDRs.

* `remote_address_cidrs` - List of remote CIDRs.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ The following arguments are supported:
Only a single policy can be defined for a connection. For details on
custom policies refer to [the relevant section in the Azure documentation](https://docs.microsoft.com/en-us/azure/vpn-gateway/vpn-gateway-ipsecikepolicy-rm-powershell).

* `traffic_selector_policy` A `traffic_selector_policy` which allows to specify traffic selector policy proposal to be used in a virtual network gateway connection.
Only one block can be defined for a connection.
For details about traffic selectors refer to [the relevant section in the Azure documentation](https://docs.microsoft.com/en-us/azure/vpn-gateway/vpn-gateway-connect-multiple-policybased-rm-ps).

* `tags` - (Optional) A mapping of tags to assign to the resource.

The `ipsec_policy` block supports:
Expand Down

0 comments on commit 9806f0d

Please sign in to comment.