-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4340 from midacts/data_source_public_ip_prefix
Adding data source 'azurerm_public_ip_prefix'
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmPublicIpPrefix() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmPublicIpPrefixRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"location": azure.SchemaLocationForDataSource(), | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"sku": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"prefix_length": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"ip_prefix": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"zones": azure.SchemaZonesComputed(), | ||
|
||
"tags": tags.SchemaDataSource(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmPublicIpPrefixRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).network.PublicIPPrefixesClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
resp, err := client.Get(ctx, resourceGroup, name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error: Public IP prefix %q was not found in Resource Group %q", name, resourceGroup) | ||
} | ||
return fmt.Errorf("Error retrieving Public IP Prefix %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
d.Set("zones", resp.Zones) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
if sku := resp.Sku; sku != nil { | ||
d.Set("sku", string(sku.Name)) | ||
} | ||
if props := resp.PublicIPPrefixPropertiesFormat; props != nil { | ||
d.Set("prefix_length", props.PrefixLength) | ||
d.Set("ip_prefix", props.IPPrefix) | ||
} | ||
return tags.FlattenAndSet(d, resp.Tags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
) | ||
|
||
func TestAccDataSourceAzureRMPublicIPPrefix_basic(t *testing.T) { | ||
ri := tf.AccRandTimeInt() | ||
name := fmt.Sprintf("acctestpublicipprefix-%d", ri) | ||
resourceGroupName := fmt.Sprintf("acctestRG-%d", ri) | ||
location := testLocation() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAzureRMPublicIPPrefixBasic(name, resourceGroupName, location), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.azurerm_public_ip_prefix.test", "name", name), | ||
resource.TestCheckResourceAttr("data.azurerm_public_ip_prefix.test", "resource_group_name", resourceGroupName), | ||
resource.TestCheckResourceAttr("data.azurerm_public_ip_prefix.test", "location", location), | ||
resource.TestCheckResourceAttr("data.azurerm_public_ip_prefix.test", "sku", "Standard"), | ||
resource.TestCheckResourceAttr("data.azurerm_public_ip_prefix.test", "prefix_length", "31"), | ||
resource.TestCheckResourceAttrSet("data.azurerm_public_ip_prefix.test", "ip_prefix"), | ||
resource.TestCheckResourceAttr("data.azurerm_public_ip_prefix.test", "tags.%", "1"), | ||
resource.TestCheckResourceAttr("data.azurerm_public_ip_prefix.test", "tags.env", "test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureRMPublicIPPrefixBasic(name string, resourceGroupName string, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "%s" | ||
location = "%s" | ||
tags = { | ||
env = "test" | ||
} | ||
} | ||
resource "azurerm_public_ip_prefix" "test" { | ||
name = "%s" | ||
location = azurerm_resource_group.test.location | ||
resource_group_name = azurerm_resource_group.test.name | ||
sku = "Standard" | ||
prefix_length = 31 | ||
tags = { | ||
env = "test" | ||
} | ||
} | ||
data "azurerm_public_ip_prefix" "test" { | ||
name = azurerm_public_ip_prefix.test.name | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
`, resourceGroupName, location, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_public_ip_prefix" | ||
sidebar_current: "docs-azurerm-datasource-public-ip-prefix-x" | ||
description: |- | ||
Gets information about an existing Public IP Prefix. | ||
--- | ||
|
||
# Data Source: azurerm_public_ip_prefix | ||
|
||
Use this data source to access information about an existing Public IP Prefix. | ||
|
||
## Example Usage (reference an existing) | ||
|
||
```hcl | ||
data "azurerm_public_ip_prefix" "test" { | ||
name = "name_of_public_ip" | ||
resource_group_name = "name_of_resource_group" | ||
} | ||
output "public_ip_prefix" { | ||
value = "${data.azurerm_public_ip_prefix.test.ip_prefix}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `name` - (Required) Specifies the name of the public IP prefix. | ||
* `resource_group_name` - (Required) Specifies the name of the resource group. | ||
|
||
## Attributes Reference | ||
|
||
* `name` - The name of the Public IP prefix resource. | ||
* `resource_group_name` - The name of the resource group in which to create the public IP. | ||
* `location` - The supported Azure location where the resource exists. | ||
* `sku` - The SKU of the Public IP Prefix. | ||
* `prefix_length` - The number of bits of the prefix. | ||
* `tags` - A mapping of tags to assigned to the resource. |