2015-12-08 19:25:05 -06:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/core/http"
|
2016-01-28 15:00:43 -06:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-12-08 19:25:05 -06:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureRMVirtualNetwork_basic(t *testing.T) {
|
2016-01-28 15:00:43 -06:00
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMVirtualNetwork_basic, ri, ri)
|
|
|
|
|
2015-12-08 19:25:05 -06:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMVirtualNetworkDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 15:00:43 -06:00
|
|
|
Config: config,
|
2015-12-08 19:25:05 -06:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMVirtualNetworkExists("azurerm_virtual_network.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-18 12:35:41 -06:00
|
|
|
func TestAccAzureRMVirtualNetwork_withTags(t *testing.T) {
|
2016-01-28 15:00:43 -06:00
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMVirtualNetwork_withTags, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMVirtualNetwork_withTagsUpdated, ri, ri)
|
|
|
|
|
2016-01-18 12:35:41 -06:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMVirtualNetworkDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 15:00:43 -06:00
|
|
|
Config: preConfig,
|
2016-01-18 12:35:41 -06:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMVirtualNetworkExists("azurerm_virtual_network.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_virtual_network.test", "tags.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_virtual_network.test", "tags.environment", "Production"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_virtual_network.test", "tags.cost_center", "MSFT"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-01-28 15:00:43 -06:00
|
|
|
Config: postConfig,
|
2016-01-18 12:35:41 -06:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMVirtualNetworkExists("azurerm_virtual_network.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_virtual_network.test", "tags.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_virtual_network.test", "tags.environment", "staging"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:25:05 -06:00
|
|
|
func testCheckAzureRMVirtualNetworkExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
virtualNetworkName := rs.Primary.Attributes["name"]
|
|
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
if !hasResourceGroup {
|
|
|
|
return fmt.Errorf("Bad: no resource group found in state for virtual network: %s", virtualNetworkName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure resource group/virtual network combination exists in API
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).vnetClient
|
|
|
|
|
2016-01-08 14:23:54 -06:00
|
|
|
resp, err := conn.Get(resourceGroup, virtualNetworkName, "")
|
2015-12-08 19:25:05 -06:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on vnetClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: Virtual Network %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMVirtualNetworkDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).vnetClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_virtual_network" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
|
2016-01-08 14:23:54 -06:00
|
|
|
resp, err := conn.Get(resourceGroup, name, "")
|
2015-12-08 19:25:05 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Virtual Network sitll exists:\n%#v", resp.Properties)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMVirtualNetwork_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 15:00:43 -06:00
|
|
|
name = "acctestrg-%d"
|
2015-12-08 19:25:05 -06:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_virtual_network" "test" {
|
2016-01-28 15:00:43 -06:00
|
|
|
name = "acctestvirtnet%d"
|
2015-12-08 19:25:05 -06:00
|
|
|
address_space = ["10.0.0.0/16"]
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
subnet {
|
|
|
|
name = "subnet1"
|
|
|
|
address_prefix = "10.0.1.0/24"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2016-01-18 12:35:41 -06:00
|
|
|
|
|
|
|
var testAccAzureRMVirtualNetwork_withTags = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 15:00:43 -06:00
|
|
|
name = "acctestrg-%d"
|
2016-01-18 12:35:41 -06:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_virtual_network" "test" {
|
2016-01-28 15:00:43 -06:00
|
|
|
name = "acctestvirtnet%d"
|
2016-01-18 12:35:41 -06:00
|
|
|
address_space = ["10.0.0.0/16"]
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
subnet {
|
|
|
|
name = "subnet1"
|
|
|
|
address_prefix = "10.0.1.0/24"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "Production"
|
|
|
|
cost_center = "MSFT"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMVirtualNetwork_withTagsUpdated = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 15:00:43 -06:00
|
|
|
name = "acctestrg-%d"
|
2016-01-18 12:35:41 -06:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_virtual_network" "test" {
|
2016-01-28 15:00:43 -06:00
|
|
|
name = "acctestvirtnet%d"
|
2016-01-18 12:35:41 -06:00
|
|
|
address_space = ["10.0.0.0/16"]
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
subnet {
|
|
|
|
name = "subnet1"
|
|
|
|
address_prefix = "10.0.1.0/24"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|