Skip to content

Commit

Permalink
azurerm_public_ip_prefix: Update prefix_length validation to acce…
Browse files Browse the repository at this point in the history
…pt all valid IPv4 address ranges (hashicorp#6589)

* Update prefix length validation

* Updated documentation for new ranges

* Add test and change upper limit to 31

* Update error msgs to be go compliant

* Add note to test case about subscription config
  • Loading branch information
WodansSon authored and neil-yechenwei committed Apr 24, 2020
1 parent b0d2780 commit 1e55c70
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func resourceArmPublicIpPrefix() *schema.Resource {
Optional: true,
Default: 28,
ForceNew: true,
ValidateFunc: validation.IntBetween(28, 31),
ValidateFunc: validation.IntBetween(0, 31),
},

"ip_prefix": {
Expand Down Expand Up @@ -104,11 +104,11 @@ func resourceArmPublicIpPrefixCreateUpdate(d *schema.ResourceData, meta interfac

future, err := client.CreateOrUpdate(ctx, resGroup, name, publicIpPrefix)
if err != nil {
return fmt.Errorf("Error Creating/Updating Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("creating/Updating Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("waiting for completion of Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

read, err := client.Get(ctx, resGroup, name, "")
Expand Down Expand Up @@ -143,7 +143,7 @@ func resourceArmPublicIpPrefixRead(d *schema.ResourceData, meta interface{}) err
return nil
}

return fmt.Errorf("Error making Read request on Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("making Read request on Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

d.Set("name", resp.Name)
Expand Down Expand Up @@ -179,11 +179,11 @@ func resourceArmPublicIpPrefixDelete(d *schema.ResourceData, meta interface{}) e

future, err := client.Delete(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error deleting Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("deleting Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("waiting for deletion of Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestAccAzureRMPublicIpPrefix_basic(t *testing.T) {
})
}

func TestAccAzureRMPublicIpPrefix_prefixLength(t *testing.T) {
func TestAccAzureRMPublicIpPrefix_prefixLength31(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_public_ip_prefix", "test")

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -127,7 +127,7 @@ func TestAccAzureRMPublicIpPrefix_prefixLength(t *testing.T) {
CheckDestroy: testCheckAzureRMPublicIPPrefixDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMPublicIPPrefix_prefixLength(data),
Config: testAccAzureRMPublicIPPrefix_prefixLength31(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIPPrefixExists(data.ResourceName),
resource.TestCheckResourceAttrSet(data.ResourceName, "ip_prefix"),
Expand All @@ -139,6 +139,29 @@ func TestAccAzureRMPublicIpPrefix_prefixLength(t *testing.T) {
})
}

func TestAccAzureRMPublicIpPrefix_prefixLength24(t *testing.T) {
// NOTE: This test will fail unless the subscription is updated
// to accept a minimum PrefixLength of 24
data := acceptance.BuildTestData(t, "azurerm_public_ip_prefix", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMPublicIPPrefixDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMPublicIPPrefix_prefixLength24(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIPPrefixExists(data.ResourceName),
resource.TestCheckResourceAttrSet(data.ResourceName, "ip_prefix"),
resource.TestCheckResourceAttr(data.ResourceName, "prefix_length", "24"),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMPublicIpPrefix_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_public_ip_prefix", "test")

Expand Down Expand Up @@ -254,7 +277,7 @@ resource "azurerm_public_ip_prefix" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMPublicIPPrefix_prefixLength(data acceptance.TestData) string {
func testAccAzureRMPublicIPPrefix_prefixLength31(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand All @@ -274,3 +297,24 @@ resource "azurerm_public_ip_prefix" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMPublicIPPrefix_prefixLength24(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_public_ip_prefix" "test" {
name = "acctestpublicipprefix-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
prefix_length = 24
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
2 changes: 1 addition & 1 deletion website/docs/r/public_ip_prefix.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The following arguments are supported:

-> **Note**: Public IP Prefix can only be created with Standard SKUs at this time.

* `prefix_length` - (Optional) Specifies the number of bits of the prefix. The value can be set between 28 (16 addresses) and 31 (2 addresses). Changing this forces a new resource to be created.
* `prefix_length` - (Optional) Specifies the number of bits of the prefix. The value can be set between 0 (4,294,967,296 addresses) and 31 (2 addresses). Defaults to `28`(16 addresses). Changing this forces a new resource to be created.

-> **Please Note**: There may be Public IP address limits on the subscription . [More information available here](https://docs.microsoft.com/en-us/azure/azure-subscription-service-limits?toc=%2fazure%2fvirtual-network%2ftoc.json#publicip-address)

Expand Down

0 comments on commit 1e55c70

Please sign in to comment.