provider/azurerm: Fix azurerm_public_ip

```
HTTP_PROXY=http://localhost:8888 make testacc TEST=./builtin/providers/azurerm TESTARGS="-run TestAccAzureRMPublicIpStatic"
==> Checking that code complies with gofmt requirements...
/Users/James/Code/go/bin/stringer
go generate $(go list ./... | grep -v /vendor/)
2016/06/01 17:09:54 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMPublicIpStatic -timeout 120m
=== RUN   TestAccAzureRMPublicIpStatic_basic
--- PASS: TestAccAzureRMPublicIpStatic_basic (101.00s)
=== RUN   TestAccAzureRMPublicIpStatic_withTags
--- PASS: TestAccAzureRMPublicIpStatic_withTags (125.13s)
=== RUN   TestAccAzureRMPublicIpStatic_update
--- PASS: TestAccAzureRMPublicIpStatic_update (128.66s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/azurerm	354.802s
```
This commit is contained in:
James Nugent 2016-06-01 17:18:50 -05:00
parent 876d0269df
commit 3831553c72
3 changed files with 547 additions and 564 deletions

View File

@ -52,7 +52,7 @@ func Provider() terraform.ResourceProvider {
//"azurerm_network_interface": resourceArmNetworkInterface(),
//"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
//"azurerm_network_security_rule": resourceArmNetworkSecurityRule(),
//"azurerm_public_ip": resourceArmPublicIp(),
"azurerm_public_ip": resourceArmPublicIp(),
//"azurerm_route": resourceArmRoute(),
//"azurerm_route_table": resourceArmRouteTable(),
//"azurerm_storage_account": resourceArmStorageAccount(),

View File

@ -1,251 +1,234 @@
package azurerm
//import (
// "fmt"
// "log"
// "net/http"
// "regexp"
// "strings"
// "time"
//
// "github.com/Azure/azure-sdk-for-go/arm/network"
// "github.com/hashicorp/terraform/helper/resource"
// "github.com/hashicorp/terraform/helper/schema"
//)
//
//func resourceArmPublicIp() *schema.Resource {
// return &schema.Resource{
// Create: resourceArmPublicIpCreate,
// Read: resourceArmPublicIpRead,
// Update: resourceArmPublicIpCreate,
// Delete: resourceArmPublicIpDelete,
//
// Schema: map[string]*schema.Schema{
// "name": &schema.Schema{
// Type: schema.TypeString,
// Required: true,
// ForceNew: true,
// },
//
// "location": &schema.Schema{
// Type: schema.TypeString,
// Required: true,
// ForceNew: true,
// StateFunc: azureRMNormalizeLocation,
// },
//
// "resource_group_name": &schema.Schema{
// Type: schema.TypeString,
// Required: true,
// ForceNew: true,
// },
//
// "public_ip_address_allocation": &schema.Schema{
// Type: schema.TypeString,
// Required: true,
// ValidateFunc: validatePublicIpAllocation,
// },
//
// "idle_timeout_in_minutes": &schema.Schema{
// Type: schema.TypeInt,
// Optional: true,
// ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// value := v.(int)
// if value < 4 || value > 30 {
// errors = append(errors, fmt.Errorf(
// "The idle timeout must be between 4 and 30 minutes"))
// }
// return
// },
// },
//
// "domain_name_label": &schema.Schema{
// Type: schema.TypeString,
// Optional: true,
// ValidateFunc: validatePublicIpDomainNameLabel,
// },
//
// "reverse_fqdn": &schema.Schema{
// Type: schema.TypeString,
// Optional: true,
// },
//
// "fqdn": &schema.Schema{
// Type: schema.TypeString,
// Computed: true,
// },
//
// "ip_address": &schema.Schema{
// Type: schema.TypeString,
// Computed: true,
// },
//
// "tags": tagsSchema(),
// },
// }
//}
//
//func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error {
// client := meta.(*ArmClient)
// publicIPClient := client.publicIPClient
//
// log.Printf("[INFO] preparing arguments for Azure ARM Public IP creation.")
//
// name := d.Get("name").(string)
// location := d.Get("location").(string)
// resGroup := d.Get("resource_group_name").(string)
// tags := d.Get("tags").(map[string]interface{})
//
// properties := network.PublicIPAddressPropertiesFormat{
// PublicIPAllocationMethod: network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string)),
// }
//
// dnl, hasDnl := d.GetOk("domain_name_label")
// rfqdn, hasRfqdn := d.GetOk("reverse_fqdn")
//
// if hasDnl || hasRfqdn {
// dnsSettings := network.PublicIPAddressDNSSettings{}
//
// if hasRfqdn {
// reverse_fqdn := rfqdn.(string)
// dnsSettings.ReverseFqdn = &reverse_fqdn
// }
//
// if hasDnl {
// domain_name_label := dnl.(string)
// dnsSettings.DomainNameLabel = &domain_name_label
//
// }
//
// properties.DNSSettings = &dnsSettings
// }
//
// if v, ok := d.GetOk("idle_timeout_in_minutes"); ok {
// idle_timeout := v.(int)
// properties.IdleTimeoutInMinutes = &idle_timeout
// }
//
// publicIp := network.PublicIPAddress{
// Name: &name,
// Location: &location,
// Properties: &properties,
// Tags: expandTags(tags),
// }
//
// resp, err := publicIPClient.CreateOrUpdate(resGroup, name, publicIp)
// if err != nil {
// return err
// }
//
// d.SetId(*resp.ID)
//
// log.Printf("[DEBUG] Waiting for Public IP (%s) to become available", name)
// stateConf := &resource.StateChangeConf{
// Pending: []string{"Accepted", "Updating"},
// Target: []string{"Succeeded"},
// Refresh: publicIPStateRefreshFunc(client, resGroup, name),
// Timeout: 10 * time.Minute,
// }
// if _, err := stateConf.WaitForState(); err != nil {
// return fmt.Errorf("Error waiting for Public IP (%s) to become available: %s", name, err)
// }
//
// return resourceArmPublicIpRead(d, meta)
//}
//
//func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error {
// publicIPClient := meta.(*ArmClient).publicIPClient
//
// id, err := parseAzureResourceID(d.Id())
// if err != nil {
// return err
// }
// resGroup := id.ResourceGroup
// name := id.Path["publicIPAddresses"]
//
// resp, err := publicIPClient.Get(resGroup, name, "")
// if resp.StatusCode == http.StatusNotFound {
// d.SetId("")
// return nil
// }
// if err != nil {
// return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err)
// }
//
// if resp.Properties.DNSSettings != nil && resp.Properties.DNSSettings.Fqdn != nil && *resp.Properties.DNSSettings.Fqdn != "" {
// d.Set("fqdn", resp.Properties.DNSSettings.Fqdn)
// }
//
// if resp.Properties.IPAddress != nil && *resp.Properties.IPAddress != "" {
// d.Set("ip_address", resp.Properties.IPAddress)
// }
//
// flattenAndSetTags(d, resp.Tags)
//
// return nil
//}
//
//func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error {
// publicIPClient := meta.(*ArmClient).publicIPClient
//
// id, err := parseAzureResourceID(d.Id())
// if err != nil {
// return err
// }
// resGroup := id.ResourceGroup
// name := id.Path["publicIPAddresses"]
//
// _, err = publicIPClient.Delete(resGroup, name)
//
// return err
//}
//
//func publicIPStateRefreshFunc(client *ArmClient, resourceGroupName string, publicIpName string) resource.StateRefreshFunc {
// return func() (interface{}, string, error) {
// res, err := client.publicIPClient.Get(resourceGroupName, publicIpName, "")
// if err != nil {
// return nil, "", fmt.Errorf("Error issuing read request in publicIPStateRefreshFunc to Azure ARM for public ip '%s' (RG: '%s'): %s", publicIpName, resourceGroupName, err)
// }
//
// return res, *res.Properties.ProvisioningState, nil
// }
//}
//
//func validatePublicIpAllocation(v interface{}, k string) (ws []string, errors []error) {
// value := strings.ToLower(v.(string))
// allocations := map[string]bool{
// "static": true,
// "dynamic": true,
// }
//
// if !allocations[value] {
// errors = append(errors, fmt.Errorf("Public IP Allocation can only be Static of Dynamic"))
// }
// return
//}
//
//func validatePublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) {
// value := v.(string)
// if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) {
// errors = append(errors, fmt.Errorf(
// "only alphanumeric characters and hyphens allowed in %q: %q",
// k, value))
// }
//
// if len(value) > 61 {
// errors = append(errors, fmt.Errorf(
// "%q cannot be longer than 61 characters: %q", k, value))
// }
//
// if len(value) == 0 {
// errors = append(errors, fmt.Errorf(
// "%q cannot be an empty string: %q", k, value))
// }
// if regexp.MustCompile(`-$`).MatchString(value) {
// errors = append(errors, fmt.Errorf(
// "%q cannot end with a hyphen: %q", k, value))
// }
//
// return
//
//}
import (
"fmt"
"log"
"net/http"
"regexp"
"strings"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmPublicIp() *schema.Resource {
return &schema.Resource{
Create: resourceArmPublicIpCreate,
Read: resourceArmPublicIpRead,
Update: resourceArmPublicIpCreate,
Delete: resourceArmPublicIpDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: azureRMNormalizeLocation,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_ip_address_allocation": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validatePublicIpAllocation,
},
"idle_timeout_in_minutes": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 4 || value > 30 {
errors = append(errors, fmt.Errorf(
"The idle timeout must be between 4 and 30 minutes"))
}
return
},
},
"domain_name_label": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validatePublicIpDomainNameLabel,
},
"reverse_fqdn": {
Type: schema.TypeString,
Optional: true,
},
"fqdn": {
Type: schema.TypeString,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
publicIPClient := client.publicIPClient
log.Printf("[INFO] preparing arguments for Azure ARM Public IP creation.")
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
properties := network.PublicIPAddressPropertiesFormat{
PublicIPAllocationMethod: network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string)),
}
dnl, hasDnl := d.GetOk("domain_name_label")
rfqdn, hasRfqdn := d.GetOk("reverse_fqdn")
if hasDnl || hasRfqdn {
dnsSettings := network.PublicIPAddressDNSSettings{}
if hasRfqdn {
reverse_fqdn := rfqdn.(string)
dnsSettings.ReverseFqdn = &reverse_fqdn
}
if hasDnl {
domain_name_label := dnl.(string)
dnsSettings.DomainNameLabel = &domain_name_label
}
properties.DNSSettings = &dnsSettings
}
if v, ok := d.GetOk("idle_timeout_in_minutes"); ok {
idle_timeout := v.(int32)
properties.IdleTimeoutInMinutes = &idle_timeout
}
publicIp := network.PublicIPAddress{
Name: &name,
Location: &location,
Properties: &properties,
Tags: expandTags(tags),
}
_, err := publicIPClient.CreateOrUpdate(resGroup, name, publicIp, make(chan struct{}))
if err != nil {
return err
}
read, err := publicIPClient.Get(resGroup, name, "")
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Public IP %s (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmPublicIpRead(d, meta)
}
func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error {
publicIPClient := meta.(*ArmClient).publicIPClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["publicIPAddresses"]
resp, err := publicIPClient.Get(resGroup, name, "")
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err)
}
if resp.Properties.DNSSettings != nil && resp.Properties.DNSSettings.Fqdn != nil && *resp.Properties.DNSSettings.Fqdn != "" {
d.Set("fqdn", resp.Properties.DNSSettings.Fqdn)
}
if resp.Properties.IPAddress != nil && *resp.Properties.IPAddress != "" {
d.Set("ip_address", resp.Properties.IPAddress)
}
flattenAndSetTags(d, resp.Tags)
return nil
}
func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error {
publicIPClient := meta.(*ArmClient).publicIPClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["publicIPAddresses"]
_, err = publicIPClient.Delete(resGroup, name, make(chan struct{}))
return err
}
func validatePublicIpAllocation(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))
allocations := map[string]bool{
"static": true,
"dynamic": true,
}
if !allocations[value] {
errors = append(errors, fmt.Errorf("Public IP Allocation can only be Static of Dynamic"))
}
return
}
func validatePublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only alphanumeric characters and hyphens allowed in %q: %q",
k, value))
}
if len(value) > 61 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 61 characters: %q", k, value))
}
if len(value) == 0 {
errors = append(errors, fmt.Errorf(
"%q cannot be an empty string: %q", k, value))
}
if regexp.MustCompile(`-$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot end with a hyphen: %q", k, value))
}
return
}

View File

@ -1,316 +1,316 @@
package azurerm
//import (
// "fmt"
// "net/http"
// "testing"
//
// "github.com/hashicorp/terraform/helper/acctest"
// "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,
// },
// {
// Value: acctest.RandString(80),
// 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) {
//
// 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"),
// ),
// },
// },
// })
//}
//
//func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) {
//
// ri := acctest.RandInt()
// preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTags, ri, ri)
// postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTagsUpdate, ri, ri)
//
// resource.Test(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
// Providers: testAccProviders,
// CheckDestroy: testCheckAzureRMPublicIpDestroy,
// Steps: []resource.TestStep{
// resource.TestStep{
// Config: preConfig,
// Check: resource.ComposeTestCheckFunc(
// testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
// resource.TestCheckResourceAttr(
// "azurerm_public_ip.test", "tags.#", "2"),
// resource.TestCheckResourceAttr(
// "azurerm_public_ip.test", "tags.environment", "Production"),
// resource.TestCheckResourceAttr(
// "azurerm_public_ip.test", "tags.cost_center", "MSFT"),
// ),
// },
//
// resource.TestStep{
// Config: postConfig,
// Check: resource.ComposeTestCheckFunc(
// testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
// resource.TestCheckResourceAttr(
// "azurerm_public_ip.test", "tags.#", "1"),
// resource.TestCheckResourceAttr(
// "azurerm_public_ip.test", "tags.environment", "staging"),
// ),
// },
// },
// })
//}
//
//func TestAccAzureRMPublicIpStatic_update(t *testing.T) {
//
// ri := acctest.RandInt()
// preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri)
// postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_update, ri, ri)
//
// resource.Test(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
// Providers: testAccProviders,
// CheckDestroy: testCheckAzureRMPublicIpDestroy,
// Steps: []resource.TestStep{
// resource.TestStep{
// Config: preConfig,
// Check: resource.ComposeTestCheckFunc(
// testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
// ),
// },
//
// resource.TestStep{
// Config: postConfig,
// Check: resource.ComposeTestCheckFunc(
// testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
// resource.TestCheckResourceAttr(
// "azurerm_public_ip.test", "domain_name_label", "mylabel01"),
// ),
// },
// },
// })
//}
//
//func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) {
//
// ri := acctest.RandInt()
// config := fmt.Sprintf(testAccAzureRMVPublicIpDynamic_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"),
// ),
// },
// },
// })
//}
//
//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
//
// resp, err := conn.Get(resourceGroup, availSetName, "")
// 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
// }
//}
//
//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"]
//
// resp, err := conn.Get(resourceGroup, name, "")
//
// 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" {
// 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"
//}
//`
//
//var testAccAzureRMVPublicIpStatic_update = `
//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"
// domain_name_label = "mylabel01"
//}
//`
//
//var testAccAzureRMVPublicIpDynamic_basic = `
//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 = "dynamic"
//}
//`
//
//var testAccAzureRMVPublicIpStatic_withTags = `
//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"
//
// tags {
// environment = "Production"
// cost_center = "MSFT"
// }
//}
//`
//
//var testAccAzureRMVPublicIpStatic_withTagsUpdate = `
//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"
//
// tags {
// environment = "staging"
// }
//}
//`
import (
"fmt"
"net/http"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"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,
},
{
Value: acctest.RandString(80),
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) {
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"),
),
},
},
})
}
func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTags, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTagsUpdate, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPublicIpDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.#", "2"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.environment", "Production"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.cost_center", "MSFT"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.#", "1"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.environment", "staging"),
),
},
},
})
}
func TestAccAzureRMPublicIpStatic_update(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_update, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPublicIpDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "domain_name_label", "mylabel01"),
),
},
},
})
}
func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMVPublicIpDynamic_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"),
),
},
},
})
}
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
resp, err := conn.Get(resourceGroup, availSetName, "")
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
}
}
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"]
resp, err := conn.Get(resourceGroup, name, "")
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" {
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"
}
`
var testAccAzureRMVPublicIpStatic_update = `
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"
domain_name_label = "mylabel01"
}
`
var testAccAzureRMVPublicIpDynamic_basic = `
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 = "dynamic"
}
`
var testAccAzureRMVPublicIpStatic_withTags = `
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"
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMVPublicIpStatic_withTagsUpdate = `
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"
tags {
environment = "staging"
}
}
`