-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from terraform-providers/subnets
Refactoring `azurerm_subnet` + New Data Source: `azurerm_subnet`
- Loading branch information
Showing
8 changed files
with
622 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.