2015-12-09 02:25:05 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-21 17:06:51 +02:00
|
|
|
"net/http"
|
2015-12-09 02:25:05 +01:00
|
|
|
"testing"
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-12-09 02:25:05 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureRMResourceGroup_basic(t *testing.T) {
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMResourceGroup_basic, ri)
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMResourceGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: config,
|
2015-12-09 02:25:05 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func TestAccAzureRMResourceGroup_disappears(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMResourceGroup_basic, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMResourceGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"),
|
|
|
|
testCheckAzureRMResourceGroupDisappears("azurerm_resource_group.test"),
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-18 19:35:41 +01:00
|
|
|
func TestAccAzureRMResourceGroup_withTags(t *testing.T) {
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMResourceGroup_withTags, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMResourceGroup_withTagsUpdated, ri)
|
|
|
|
|
2016-01-18 19:35:41 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMResourceGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: preConfig,
|
2016-01-18 19:35:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_resource_group.test", "tags.%", "2"),
|
2016-01-18 19:35:41 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_resource_group.test", "tags.environment", "Production"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_resource_group.test", "tags.cost_center", "MSFT"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: postConfig,
|
2016-01-18 19:35:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_resource_group.test", "tags.%", "1"),
|
2016-01-18 19:35:41 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_resource_group.test", "tags.environment", "staging"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
func testCheckAzureRMResourceGroupExists(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceGroup := rs.Primary.Attributes["name"]
|
|
|
|
|
|
|
|
// Ensure resource group exists in API
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient
|
|
|
|
|
|
|
|
resp, err := conn.Get(resourceGroup)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on resourceGroupClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: Virtual Network %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func testCheckAzureRMResourceGroupDisappears(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceGroup := rs.Primary.Attributes["name"]
|
|
|
|
|
|
|
|
// Ensure resource group exists in API
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient
|
|
|
|
|
|
|
|
_, err := conn.Delete(resourceGroup, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Delete on resourceGroupClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
func testCheckAzureRMResourceGroupDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_resource_group" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceGroup := rs.Primary.ID
|
|
|
|
|
|
|
|
resp, err := conn.Get(resourceGroup)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Resource Group still exists:\n%#v", resp.Properties)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMResourceGroup_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG-%d"
|
2015-12-09 02:25:05 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
`
|
2016-01-18 19:35:41 +01:00
|
|
|
|
|
|
|
var testAccAzureRMResourceGroup_withTags = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG-%d"
|
2016-01-18 19:35:41 +01:00
|
|
|
location = "West US"
|
|
|
|
|
|
|
|
tags {
|
2016-02-18 23:50:43 +01:00
|
|
|
environment = "Production"
|
|
|
|
cost_center = "MSFT"
|
2016-01-18 19:35:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMResourceGroup_withTagsUpdated = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG-%d"
|
2016-01-18 19:35:41 +01:00
|
|
|
location = "West US"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|