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_firewall: allow multiple ip_configuration blocks #4639

Merged
merged 11 commits into from
Nov 15, 2019
4 changes: 1 addition & 3 deletions azurerm/data_source_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func dataSourceArmFirewall() *schema.Resource {
"ip_configuration": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -95,8 +94,7 @@ func dataSourceArmFirewallRead(d *schema.ResourceData, meta interface{}) error {
}

if props := read.AzureFirewallPropertiesFormat; props != nil {
ipConfigs := flattenArmFirewallIPConfigurations(props.IPConfigurations)
if err := d.Set("ip_configuration", ipConfigs); err != nil {
if err := d.Set("ip_configuration", flattenArmFirewallIPConfigurations(props.IPConfigurations)); err != nil {
return fmt.Errorf("Error setting `ip_configuration`: %+v", err)
}
houkms marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
72 changes: 36 additions & 36 deletions azurerm/resource_arm_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"regexp"
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
Expand Down Expand Up @@ -52,7 +53,6 @@ func resourceArmFirewall() *schema.Resource {
"ip_configuration": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -62,24 +62,22 @@ func resourceArmFirewall() *schema.Resource {
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
houkms marked this conversation as resolved.
Show resolved Hide resolved
ForceNew: true,
ValidateFunc: validateAzureFirewallSubnetName,
},
"internal_public_ip_address_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: azure.ValidateResourceID,
Deprecated: "This field has been deprecated. Use `public_ip_address_id` instead.",
ConflictsWith: []string{"ip_configuration.0.public_ip_address_id"},
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: azure.ValidateResourceID,
Deprecated: "This field has been deprecated. Use `public_ip_address_id` instead.",
houkms marked this conversation as resolved.
Show resolved Hide resolved
},
houkms marked this conversation as resolved.
Show resolved Hide resolved
"public_ip_address_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: azure.ValidateResourceID,
ConflictsWith: []string{"ip_configuration.0.internal_public_ip_address_id"},
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: azure.ValidateResourceID,
},
houkms marked this conversation as resolved.
Show resolved Hide resolved
"private_ip_address": {
Type: schema.TypeString,
Expand Down Expand Up @@ -211,8 +209,7 @@ func resourceArmFirewallRead(d *schema.ResourceData, meta interface{}) error {
}

if props := read.AzureFirewallPropertiesFormat; props != nil {
ipConfigs := flattenArmFirewallIPConfigurations(props.IPConfigurations)
if err := d.Set("ip_configuration", ipConfigs); err != nil {
if err := d.Set("ip_configuration", flattenArmFirewallIPConfigurations(props.IPConfigurations)); err != nil {
return fmt.Errorf("Error setting `ip_configuration`: %+v", err)
}
houkms marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -297,7 +294,7 @@ func expandArmFirewallIPConfigurations(d *schema.ResourceData) (*[]network.Azure
subnetNamesToLock := make([]string, 0)
virtualNetworkNamesToLock := make([]string, 0)

for _, configRaw := range configs {
for index, configRaw := range configs {
houkms marked this conversation as resolved.
Show resolved Hide resolved
data := configRaw.(map[string]interface{})
name := data["name"].(string)
subnetId := data["subnet_id"].(string)
Expand All @@ -308,36 +305,39 @@ func expandArmFirewallIPConfigurations(d *schema.ResourceData) (*[]network.Azure
}

if !exist || pubID == "" {
return nil, nil, nil, fmt.Errorf("one of `ip_configuration.0.internal_public_ip_address_id` or `ip_configuration.0.public_ip_address_id` must be set")
}

subnetID, err := azure.ParseAzureResourceID(subnetId)
if err != nil {
return nil, nil, nil, err
}

subnetName := subnetID.Path["subnets"]
virtualNetworkName := subnetID.Path["virtualNetworks"]

if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}

if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
return nil, nil, nil, fmt.Errorf("one of `ip_configuration.%s.internal_public_ip_address_id` or `ip_configuration.%s.public_ip_address_id` must be set", strconv.Itoa(index), strconv.Itoa(index))
houkms marked this conversation as resolved.
Show resolved Hide resolved
}

ipConfig := network.AzureFirewallIPConfiguration{
Name: utils.String(name),
AzureFirewallIPConfigurationPropertiesFormat: &network.AzureFirewallIPConfigurationPropertiesFormat{
Subnet: &network.SubResource{
ID: utils.String(subnetId),
},
PublicIPAddress: &network.SubResource{
ID: utils.String(pubID),
},
},
}

if subnetId != "" {
subnetID, err := azure.ParseAzureResourceID(subnetId)
if err != nil {
return nil, nil, nil, err
}

subnetName := subnetID.Path["subnets"]
virtualNetworkName := subnetID.Path["virtualNetworks"]

if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}

if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
}

ipConfig.AzureFirewallIPConfigurationPropertiesFormat.Subnet = &network.SubResource{
ID: utils.String(subnetId),
}
}
ipConfigs = append(ipConfigs, ipConfig)
}
return &ipConfigs, &subnetNamesToLock, &virtualNetworkNamesToLock, nil
Expand Down