Skip to content

Commit

Permalink
Merge pull request #411 from terraform-providers/subnets
Browse files Browse the repository at this point in the history
Refactoring `azurerm_subnet` + New Data Source: `azurerm_subnet`
  • Loading branch information
tombuildsstuff authored Oct 10, 2017
2 parents afd244c + a9a0017 commit eb93737
Show file tree
Hide file tree
Showing 8 changed files with 622 additions and 113 deletions.
102 changes: 102 additions & 0 deletions azurerm/data_source_subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmSubnet() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmSubnetRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"virtual_network_name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": {
Type: schema.TypeString,
Required: true,
},

"address_prefix": {
Type: schema.TypeString,
Computed: true,
},

"network_security_group_id": {
Type: schema.TypeString,
Computed: true,
},

"route_table_id": {
Type: schema.TypeString,
Computed: true,
},

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

func dataSourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).subnetClient

name := d.Get("name").(string)
virtualNetworkName := d.Get("virtual_network_name").(string)
resourceGroupName := d.Get("resource_group_name").(string)

resp, err := client.Get(resourceGroupName, virtualNetworkName, name, "")
if err != nil {
return fmt.Errorf("Error reading Subnet: %+v", err)
}

d.SetId(*resp.ID)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Subnet %q: %+v", name, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroupName)
d.Set("virtual_network_name", virtualNetworkName)

if props := resp.SubnetPropertiesFormat; props != nil {
d.Set("address_prefix", props.AddressPrefix)

if props.NetworkSecurityGroup != nil {
d.Set("network_security_group_id", props.NetworkSecurityGroup.ID)
} else {
d.Set("network_security_group_id", "")
}

if props.RouteTable != nil {
d.Set("route_table_id", props.RouteTable.ID)
} else {
d.Set("route_table_id", "")
}

ips := flattenSubnetIPConfigurations(props.IPConfigurations)
if err := d.Set("ip_configurations", ips); err != nil {
return err
}
}

return nil
}
198 changes: 198 additions & 0 deletions azurerm/data_source_subnet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMSubnet_basic(t *testing.T) {
resourceName := "data.azurerm_subnet.test"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSubnet_basic(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "name"),
resource.TestCheckResourceAttrSet(resourceName, "resource_group_name"),
resource.TestCheckResourceAttrSet(resourceName, "virtual_network_name"),
resource.TestCheckResourceAttrSet(resourceName, "address_prefix"),
resource.TestCheckResourceAttr(resourceName, "network_security_group_id", ""),
resource.TestCheckResourceAttr(resourceName, "route_table_id", ""),
),
},
},
})
}

func TestAccDataSourceAzureRMSubnet_networkSecurityGroup(t *testing.T) {
dataSourceName := "data.azurerm_subnet.test"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSubnet_networkSecurityGroup(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "virtual_network_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "address_prefix"),
resource.TestCheckResourceAttrSet(dataSourceName, "network_security_group_id"),
resource.TestCheckResourceAttr(dataSourceName, "route_table_id", ""),
),
},
},
})
}

func TestAccDataSourceAzureRMSubnet_routeTable(t *testing.T) {
dataSourceName := "data.azurerm_subnet.test"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSubnet_routeTable(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "virtual_network_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "address_prefix"),
resource.TestCheckResourceAttr(dataSourceName, "network_security_group_id", ""),
resource.TestCheckResourceAttrSet(dataSourceName, "route_table_id"),
),
},
},
})
}

func testAccDataSourceAzureRMSubnet_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctest%d-rg"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctest%d-vn"
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 = "acctest%d-private"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.0.0/24"
}
data "azurerm_subnet" "test" {
name = "${azurerm_subnet.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
}
`, rInt, location, rInt, rInt)
}

func testAccDataSourceAzureRMSubnet_networkSecurityGroup(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctest%d-rg"
location = "%s"
}
resource "azurerm_network_security_group" "test" {
name = "acctestnsg%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
security_rule {
name = "test123"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_virtual_network" "test" {
name = "acctest%d-vn"
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 = "acctest%d-private"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.0.0/24"
network_security_group_id = "${azurerm_network_security_group.test.id}"
}
data "azurerm_subnet" "test" {
name = "${azurerm_subnet.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
}
`, rInt, location, rInt, rInt, rInt)
}

func testAccDataSourceAzureRMSubnet_routeTable(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_route_table" "test" {
name = "acctest-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
route {
name = "acctest-%d"
address_prefix = "10.100.0.0/14"
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "10.10.1.1"
}
}
resource "azurerm_virtual_network" "test" {
name = "acctestvirtnet%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 = "acctestsubnet%d"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
route_table_id = "${azurerm_route_table.test.id}"
}
data "azurerm_subnet" "test" {
name = "${azurerm_subnet.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
}
`, rInt, location, rInt, rInt, rInt, rInt)
}
23 changes: 23 additions & 0 deletions azurerm/import_arm_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,26 @@ func TestAccAzureRMSubnet_importWithRouteTable(t *testing.T) {
},
})
}

func TestAccAzureRMSubnet_importWithNetworkSecurityGroup(t *testing.T) {
resourceName := "azurerm_subnet.test"

ri := acctest.RandInt()
config := testAccAzureRMSubnet_networkSecurityGroup(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSubnetDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
},

Expand Down
Loading

0 comments on commit eb93737

Please sign in to comment.