2016-01-08 01:20:49 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-21 21:59:30 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-01-08 01:20:49 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceAzureRMPublicIpAllocation_validation(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Value string
|
|
|
|
ErrCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Value: "Random",
|
|
|
|
ErrCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "Static",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "Dynamic",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "STATIC",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "static",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
_, errors := validatePublicIpAllocation(tc.Value, "azurerm_public_ip")
|
|
|
|
|
|
|
|
if len(errors) != tc.ErrCount {
|
|
|
|
t.Fatalf("Expected the Azure RM Public IP allocation to trigger a validation error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceAzureRMPublicIpDomainNameLabel_validation(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Value string
|
|
|
|
ErrCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Value: "tEsting123",
|
|
|
|
ErrCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "testing123!",
|
|
|
|
ErrCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "testing123-",
|
|
|
|
ErrCount: 1,
|
|
|
|
},
|
|
|
|
{
|
2016-01-21 21:59:30 +01:00
|
|
|
Value: acctest.RandString(80),
|
2016-01-08 01:20:49 +01:00
|
|
|
ErrCount: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
_, errors := validatePublicIpDomainNameLabel(tc.Value, "azurerm_public_ip")
|
|
|
|
|
|
|
|
if len(errors) != tc.ErrCount {
|
|
|
|
t.Fatalf("Expected the Azure RM Public IP Domain Name Label to trigger a validation error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMPublicIpStatic_basic(t *testing.T) {
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri)
|
|
|
|
|
2016-01-08 01:20:49 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMPublicIpDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: config,
|
2016-01-08 01:20:49 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func TestAccAzureRMPublicIpStatic_disappears(t *testing.T) {
|
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMPublicIpDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
testCheckAzureRMPublicIpDisappears("azurerm_public_ip.test"),
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-18 15:05:14 +02:00
|
|
|
func TestAccAzureRMPublicIpStatic_idleTimeout(t *testing.T) {
|
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_idleTimeout, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMPublicIpDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_public_ip.test",
|
|
|
|
"idle_timeout_in_minutes",
|
|
|
|
"30",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-18 21:29:26 +01:00
|
|
|
func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) {
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTags, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTagsUpdate, ri, ri)
|
|
|
|
|
2016-01-18 21:29:26 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMPublicIpDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: preConfig,
|
2016-01-18 21:29:26 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_public_ip.test", "tags.%", "2"),
|
2016-01-18 21:29:26 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_public_ip.test", "tags.environment", "Production"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_public_ip.test", "tags.cost_center", "MSFT"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: postConfig,
|
2016-01-18 21:29:26 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_public_ip.test", "tags.%", "1"),
|
2016-01-18 21:29:26 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_public_ip.test", "tags.environment", "staging"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:20:49 +01:00
|
|
|
func TestAccAzureRMPublicIpStatic_update(t *testing.T) {
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_update, ri, ri)
|
|
|
|
|
2016-01-08 01:20:49 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMPublicIpDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: preConfig,
|
2016-01-08 01:20:49 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: postConfig,
|
2016-01-08 01:20:49 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_public_ip.test", "domain_name_label", "mylabel01"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) {
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMVPublicIpDynamic_basic, ri, ri)
|
|
|
|
|
2016-01-08 01:20:49 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMPublicIpDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: config,
|
2016-01-08 01:20:49 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMPublicIpExists(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
availSetName := 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 public ip: %s", availSetName)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).publicIPClient
|
|
|
|
|
2016-01-08 21:23:54 +01:00
|
|
|
resp, err := conn.Get(resourceGroup, availSetName, "")
|
2016-01-08 01:20:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on publicIPClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func testCheckAzureRMPublicIpDisappears(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
publicIpName := 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 public ip: %s", publicIpName)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).publicIPClient
|
|
|
|
|
|
|
|
_, err := conn.Delete(resourceGroup, publicIpName, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Delete on publicIPClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:20:49 +01:00
|
|
|
func testCheckAzureRMPublicIpDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).publicIPClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_public_ip" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
|
2016-01-08 21:23:54 +01:00
|
|
|
resp, err := conn.Get(resourceGroup, name, "")
|
2016-01-08 01:20:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Public IP still exists:\n%#v", resp.Properties)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMVPublicIpStatic_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrg-%d"
|
2016-01-08 01:20:49 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestpublicip-%d"
|
2016-01-08 01:20:49 +01:00
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMVPublicIpStatic_update = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrg-%d"
|
2016-01-08 01:20:49 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestpublicip-%d"
|
2016-01-08 01:20:49 +01:00
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
domain_name_label = "mylabel01"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-08-18 15:05:14 +02:00
|
|
|
var testAccAzureRMVPublicIpStatic_idleTimeout = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestrg-%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
|
|
name = "acctestpublicip-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
idle_timeout_in_minutes = 30
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-01-08 01:20:49 +01:00
|
|
|
var testAccAzureRMVPublicIpDynamic_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrg-%d"
|
2016-01-08 01:20:49 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestpublicip-%d"
|
2016-01-08 01:20:49 +01:00
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "dynamic"
|
|
|
|
}
|
|
|
|
`
|
2016-01-18 21:29:26 +01:00
|
|
|
|
|
|
|
var testAccAzureRMVPublicIpStatic_withTags = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrg-%d"
|
2016-01-18 21:29:26 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestpublicip-%d"
|
2016-01-18 21:29:26 +01:00
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "Production"
|
|
|
|
cost_center = "MSFT"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMVPublicIpStatic_withTagsUpdate = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrg-%d"
|
2016-01-18 21:29:26 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestpublicip-%d"
|
2016-01-18 21:29:26 +01:00
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|