diff --git a/azurerm/import_arm_public_ip_test.go b/azurerm/import_arm_public_ip_test.go deleted file mode 100644 index 629bd531e2a3..000000000000 --- a/azurerm/import_arm_public_ip_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package azurerm - -import ( - "fmt" - "os" - "regexp" - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" -) - -func TestAccAzureRMPublicIpStatic_importBasic(t *testing.T) { - resourceName := "azurerm_public_ip.test" - - ri := acctest.RandInt() - config := testAccAzureRMPublicIPStatic_basic(ri, testLocation()) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMPublicIpDestroy, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccAzureRMPublicIpStatic_importBasic_withZone(t *testing.T) { - resourceName := "azurerm_public_ip.test" - - ri := acctest.RandInt() - config := testAccAzureRMPublicIPStatic_basic_withZone(ri, testLocation()) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMPublicIpDestroy, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccAzureRMPublicIpStatic_importBasic_withDNSLabel(t *testing.T) { - resourceName := "azurerm_public_ip.test" - - ri := acctest.RandInt() - dnl := fmt.Sprintf("acctestdnl-%d", ri) - config := testAccAzureRMPublicIPStatic_basic_withDNSLabel(ri, testLocation(), dnl) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMPublicIpDestroy, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccAzureRMPublicIpStatic_importIdError(t *testing.T) { - resourceName := "azurerm_public_ip.test" - - ri := acctest.RandInt() - config := testAccAzureRMPublicIPStatic_basic(ri, testLocation()) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMPublicIpDestroy, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateId: fmt.Sprintf("/subscriptions/%s/resourceGroups/acctestRG-%d/providers/Microsoft.Network/publicIPAdresses/acctestpublicip-%d", os.Getenv("ARM_SUBSCRIPTION_ID"), ri, ri), - ExpectError: regexp.MustCompile("Error parsing supplied resource id."), - }, - }, - }) -} diff --git a/azurerm/resource_arm_public_ip.go b/azurerm/resource_arm_public_ip.go index 8663880a353e..bcf06ff00581 100644 --- a/azurerm/resource_arm_public_ip.go +++ b/azurerm/resource_arm_public_ip.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" "log" "regexp" "strings" @@ -35,9 +36,10 @@ func resourceArmPublicIp() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.NoZeroValues, }, "location": locationSchema(), @@ -46,33 +48,34 @@ func resourceArmPublicIp() *schema.Resource { "zones": singleZonesSchema(), - //should this perhaps be allocation_method? + //should this perhaps be allocation_method? (yes i think so) "public_ip_address_allocation": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + DiffSuppressFunc: suppress.CaseDifference, + StateFunc: ignoreCaseStateFunc, ValidateFunc: validation.StringInSlice([]string{ string(network.Static), string(network.Dynamic), }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, - StateFunc: ignoreCaseStateFunc, }, "sku": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - Default: string(network.PublicIPAddressSkuNameBasic), + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: string(network.PublicIPAddressSkuNameBasic), + DiffSuppressFunc: suppress.CaseDifference, ValidateFunc: validation.StringInSlice([]string{ string(network.PublicIPAddressSkuNameBasic), string(network.PublicIPAddressSkuNameStandard), }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, "idle_timeout_in_minutes": { Type: schema.TypeInt, Optional: true, + Default: 4, ValidateFunc: validation.IntBetween(4, 30), }, @@ -117,6 +120,7 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error { tags := d.Get("tags").(map[string]interface{}) zones := expandZones(d.Get("zones").([]interface{})) + idleTimeout := d.Get("idle_timeout_in_minutes").(int) ipAllocationMethod := network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string)) if strings.ToLower(string(sku.Name)) == "standard" { @@ -125,8 +129,16 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error { } } - properties := network.PublicIPAddressPropertiesFormat{ - PublicIPAllocationMethod: ipAllocationMethod, + publicIp := network.PublicIPAddress{ + Name: &name, + Location: &location, + Sku: &sku, + PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{ + PublicIPAllocationMethod: ipAllocationMethod, + IdleTimeoutInMinutes: utils.Int32(int32(idleTimeout)), + }, + Tags: expandTags(tags), + Zones: zones, } dnl, dnlOk := d.GetOk("domain_name_label") @@ -145,20 +157,7 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error { dnsSettings.DomainNameLabel = &domainNameLabel } - properties.DNSSettings = &dnsSettings - } - - if v, ok := d.GetOk("idle_timeout_in_minutes"); ok { - properties.IdleTimeoutInMinutes = utils.Int32(int32(v.(int))) - } - - publicIp := network.PublicIPAddress{ - Name: &name, - Location: &location, - Sku: &sku, - PublicIPAddressPropertiesFormat: &properties, - Tags: expandTags(tags), - Zones: zones, + publicIp.PublicIPAddressPropertiesFormat.DNSSettings = &dnsSettings } future, err := client.CreateOrUpdate(ctx, resGroup, name, publicIp) @@ -166,8 +165,7 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error Creating/Updating Public IP %q (Resource Group %q): %+v", name, resGroup, err) } - err = future.WaitForCompletionRef(ctx, client.Client) - if err != nil { + if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { return fmt.Errorf("Error waiting for completion of Public IP %q (Resource Group %q): %+v", name, resGroup, err) } @@ -236,6 +234,10 @@ func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error { } else { d.Set("ip_address", "") } + + if idleTimeout := props.IdleTimeoutInMinutes; idleTimeout != nil { + d.Set("idle_timeout_in_minutes", idleTimeout) + } } flattenAndSetTags(d, resp.Tags) @@ -259,8 +261,7 @@ func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error deleting Public IP %q (Resource Group %q): %+v", name, resGroup, err) } - err = future.WaitForCompletionRef(ctx, client.Client) - if err != nil { + if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { return fmt.Errorf("Error waiting for deletion of Public IP %q (Resource Group %q): %+v", name, resGroup, err) } diff --git a/azurerm/resource_arm_public_ip_test.go b/azurerm/resource_arm_public_ip_test.go index 72bb578004a9..6fc922833be2 100644 --- a/azurerm/resource_arm_public_ip_test.go +++ b/azurerm/resource_arm_public_ip_test.go @@ -3,6 +3,8 @@ package azurerm import ( "fmt" "net/http" + "os" + "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -60,6 +62,11 @@ func TestAccAzureRMPublicIpStatic_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "public_ip_address_allocation", "static"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -84,11 +91,17 @@ func TestAccAzureRMPublicIpStatic_basic_withDNSLabel(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "domain_name_label", dnl), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } func TestAccAzureRMPublicIpStatic_standard(t *testing.T) { + resourceName := "azurerm_public_ip.test" ri := acctest.RandInt() config := testAccAzureRMPublicIPStatic_standard(ri, testLocation()) @@ -100,9 +113,14 @@ func TestAccAzureRMPublicIpStatic_standard(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), + testCheckAzureRMPublicIpExists(resourceName), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -146,6 +164,11 @@ func TestAccAzureRMPublicIpStatic_idleTimeout(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "idle_timeout_in_minutes", "30"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -208,11 +231,17 @@ func TestAccAzureRMPublicIpStatic_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "domain_name_label", fmt.Sprintf("acctest-%d", ri)), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) { + resourceName := "azurerm_public_ip.test" ri := acctest.RandInt() config := testAccAzureRMPublicIPDynamic_basic(ri, testLocation()) @@ -224,9 +253,38 @@ func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), + testCheckAzureRMPublicIpExists(resourceName), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMPublicIpStatic_importIdError(t *testing.T) { + resourceName := "azurerm_public_ip.test" + + ri := acctest.RandInt() + config := testAccAzureRMPublicIPStatic_basic(ri, testLocation()) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateId: fmt.Sprintf("/subscriptions/%s/resourceGroups/acctestRG-%d/providers/Microsoft.Network/publicIPAdresses/acctestpublicip-%d", os.Getenv("ARM_SUBSCRIPTION_ID"), ri, ri), + ExpectError: regexp.MustCompile("Error parsing supplied resource id."), + }, }, }) }