Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding data source 'azurerm_public_ip_prefix' #4340

Merged
merged 3 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions azurerm/data_source_public_ip_prefix.go
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)
}
66 changes: 66 additions & 0 deletions azurerm/data_source_public_ip_prefix_test.go
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)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_proximity_placement_group": dataSourceArmProximityPlacementGroup(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_public_ips": dataSourceArmPublicIPs(),
"azurerm_public_ip_prefix": dataSourceArmPublicIpPrefix(),
"azurerm_recovery_services_vault": dataSourceArmRecoveryServicesVault(),
"azurerm_recovery_services_protection_policy_vm": dataSourceArmRecoveryServicesProtectionPolicyVm(),
"azurerm_redis_cache": dataSourceArmRedisCache(),
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@
<a href="/docs/providers/azurerm/d/policy_definition.html">azurerm_policy_definition</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/public_ip_prefix.html">azurerm_public_ip_prefix</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/public_ip.html">azurerm_public_ip</a>
</li>
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/public_ip_prefix.html.markdown
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
midacts marked this conversation as resolved.
Show resolved Hide resolved

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.