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_network_interface - adding support for attaching to Application Security Groups #911

Merged
merged 2 commits into from
Mar 1, 2018
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
21 changes: 21 additions & 0 deletions azurerm/import_arm_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ func TestAccAzureRMNetworkInterface_importPublicIP(t *testing.T) {
},
})
}

func TestAccAzureRMNetworkInterface_importApplicationSecurityGroup(t *testing.T) {
resourceName := "azurerm_network_interface.test"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNetworkInterface_applicationSecurityGroup(rInt, testLocation()),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
36 changes: 35 additions & 1 deletion azurerm/resource_arm_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ func resourceArmNetworkInterface() *schema.Resource {
Set: schema.HashString,
},

"application_security_group_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"primary": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -329,7 +337,10 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
}

if iface.IPConfigurations != nil {
d.Set("ip_configuration", flattenNetworkInterfaceIPConfigurations(iface.IPConfigurations))
configs := flattenNetworkInterfaceIPConfigurations(iface.IPConfigurations)
if err := d.Set("ip_configuration", configs); err != nil {
return fmt.Errorf("Error setting `ip_configuration`: %+v", err)
}
}

if iface.VirtualMachine != nil {
Expand Down Expand Up @@ -480,6 +491,14 @@ func flattenNetworkInterfaceIPConfigurations(ipConfigs *[]network.InterfaceIPCon
}
niIPConfig["load_balancer_inbound_nat_rules_ids"] = schema.NewSet(schema.HashString, rules)

securityGroups := make([]interface{}, 0)
if sgs := props.ApplicationSecurityGroups; sgs != nil {
for _, sg := range *sgs {
securityGroups = append(securityGroups, *sg.ID)
}
}
niIPConfig["application_security_group_ids"] = schema.NewSet(schema.HashString, securityGroups)

result = append(result, niIPConfig)
}
return result
Expand Down Expand Up @@ -566,6 +585,21 @@ func expandAzureRmNetworkInterfaceIpConfigurations(d *schema.ResourceData) ([]ne
properties.LoadBalancerInboundNatRules = &natRules
}

if v, ok := data["application_security_group_ids"]; ok {
var securityGroups []network.ApplicationSecurityGroup
rules := v.(*schema.Set).List()
for _, r := range rules {
groupId := r.(string)
group := network.ApplicationSecurityGroup{
ID: &groupId,
}

securityGroups = append(securityGroups, group)
}

properties.ApplicationSecurityGroups = &securityGroups
}

name := data["name"].(string)
ipConfig := network.InterfaceIPConfiguration{
Name: &name,
Expand Down
61 changes: 61 additions & 0 deletions azurerm/resource_arm_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,25 @@ func TestAccAzureRMNetworkInterface_bug7986(t *testing.T) {
})
}

func TestAccAzureRMNetworkInterface_applicationSecurityGroups(t *testing.T) {
resourceName := "azurerm_network_interface.test"
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNetworkInterface_applicationSecurityGroup(rInt, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkInterfaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "ip_configuration.0.application_security_group_ids.#", "1"),
),
},
},
})
}

func testCheckAzureRMNetworkInterfaceExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
Expand Down Expand Up @@ -1012,3 +1031,45 @@ resource "azurerm_network_interface" "test" {

`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMNetworkInterface_applicationSecurityGroup(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctest-rg-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctestvn-%d"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
name = "testsubnet"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_application_security_group" "test" {
name = "acctest-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_network_interface" "test" {
name = "acctestnic-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.test.id}"
private_ip_address_allocation = "dynamic"
application_security_group_ids = ["${azurerm_application_security_group.test.id}"]
}
}
`, rInt, location, rInt, rInt, rInt)
}
4 changes: 4 additions & 0 deletions website/docs/r/network_interface.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ The `ip_configuration` block supports:

* `load_balancer_inbound_nat_rules_ids` - (Optional) List of Load Balancer Inbound Nat Rules IDs involving this NIC

* `application_security_group_ids` - (Optional) List of Application Security Group IDs which should be attached to this NIC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 this wants a Preview disclaimer here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

-> **Note:** Application Security Groups are currently in Public Preview on an opt-in basis. [More information, including how you can register for the Preview, and which regions Application Security Groups are available in are available here](https://docs.microsoft.com/en-us/azure/virtual-network/create-network-security-group-preview)

* `primary` - (Optional) Is this the Primary Network Interface? If set to `true` this should be the first `ip_configuration` in the array.

## Attributes Reference
Expand Down