provider/azurerm: Adding data source support for Public IP (#15110)

* Added support for public IP data source. Tested manually.

* WIP: Update to implementation, basic test added.

* WIP: Updates to implementation, basic test added.

* WIP: Added support for idle timeout

* Completed implementation and basic test

* Added documentation.

* Updated the example so it makes a little more sense.
This commit is contained in:
Abhijeet Gaiha 2017-06-06 17:18:37 +05:30 committed by Paul Stack
parent 4019335f39
commit 1819303e65
5 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,86 @@
package azurerm
import (
"fmt"
"net/http"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceArmPublicIP() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPublicIPRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
},
"domain_name_label": {
Type: schema.TypeString,
Computed: true,
},
"idle_timeout_in_minutes": {
Type: schema.TypeInt,
Computed: true,
},
"fqdn": {
Type: schema.TypeString,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func dataSourceArmPublicIPRead(d *schema.ResourceData, meta interface{}) error {
publicIPClient := meta.(*ArmClient).publicIPClient
resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
resp, err := publicIPClient.Get(resGroup, name, "")
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
}
return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err)
}
d.SetId(*resp.ID)
if resp.PublicIPAddressPropertiesFormat.DNSSettings != nil {
if resp.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != nil && *resp.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != "" {
d.Set("fqdn", resp.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn)
}
if resp.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != nil && *resp.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != "" {
d.Set("domain_name_label", resp.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel)
}
}
if resp.PublicIPAddressPropertiesFormat.IPAddress != nil && *resp.PublicIPAddressPropertiesFormat.IPAddress != "" {
d.Set("ip_address", resp.PublicIPAddressPropertiesFormat.IPAddress)
}
if resp.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes != nil {
d.Set("idle_timeout_in_minutes", *resp.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes)
}
flattenAndSetTags(d, resp.Tags)
return nil
}

View File

@ -0,0 +1,65 @@
package azurerm
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceAzureRMPublicIP_basic(t *testing.T) {
ri := acctest.RandInt()
name := fmt.Sprintf("acctestpublicip-%d", ri)
resourceGroupName := fmt.Sprintf("acctestRG-%d", ri)
config := testAccDatSourceAzureRMPublicIPBasic(name, resourceGroupName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPublicIpDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.azurerm_public_ip.test", "name", name),
resource.TestCheckResourceAttr("data.azurerm_public_ip.test", "resource_group_name", resourceGroupName),
resource.TestCheckResourceAttr("data.azurerm_public_ip.test", "domain_name_label", "mylabel01"),
resource.TestCheckResourceAttr("data.azurerm_public_ip.test", "idle_timeout_in_minutes", "30"),
resource.TestCheckResourceAttrSet("data.azurerm_public_ip.test", "fqdn"),
resource.TestCheckResourceAttrSet("data.azurerm_public_ip.test", "ip_address"),
resource.TestCheckResourceAttr("data.azurerm_public_ip.test", "tags.%", "1"),
resource.TestCheckResourceAttr("data.azurerm_public_ip.test", "tags.environment", "test"),
),
},
},
})
}
func testAccDatSourceAzureRMPublicIPBasic(name string, resourceGroupName string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "%s"
location = "West US"
}
resource "azurerm_public_ip" "test" {
name = "%s"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
public_ip_address_allocation = "static"
domain_name_label = "mylabel01"
idle_timeout_in_minutes = 30
tags {
environment = "test"
}
}
data "azurerm_public_ip" "test" {
name = "${azurerm_public_ip.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, resourceGroupName, name)
}

View File

@ -63,6 +63,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_public_ip": dataSourceArmPublicIP(),
},
ResourcesMap: map[string]*schema.Resource{

View File

@ -0,0 +1,62 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_public_ip"
sidebar_current: "docs-azurerm-datasource-public-ip"
description: |-
Get information about the specified public IP address.
---
# azurerm\_public\_ip
Use this data source to access the properties of an existing Azure Public IP Address.
## Example Usage
```hcl
data "azurerm_public_ip" "datasourceip" {
name = "testPublicIp"
resource_group_name = "acctestRG"
}
resource "azurerm_virtual_network" "helloterraformnetwork" {
name = "acctvn"
address_space = ["10.0.0.0/16"]
location = "West US 2"
resource_group_name = "acctestRG"
}
resource "azurerm_subnet" "helloterraformsubnet" {
name = "acctsub"
resource_group_name = "acctestRG"
virtual_network_name = "${azurerm_virtual_network.helloterraformnetwork.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "helloterraformnic" {
name = "tfni"
location = "West US 2"
resource_group_name = "acctestRG"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.helloterraformsubnet.id}"
private_ip_address_allocation = "static"
private_ip_address = "10.0.2.5"
public_ip_address_id = "${data.azurerm_public_ip.datasourceip.id}"
}
}
```
## Argument Reference
* `name` - (Required) Specifies the name of the public IP address.
* `resource_group_name` - (Required) Specifies the name of the resource group.
## Attributes Reference
* `domain_name_label` - The label for the Domain Name.
* `idle_timeout_in_minutes` - Specifies the timeout for the TCP idle connection.
* `fqdn` - Fully qualified domain name of the A DNS record associated with the public IP. This is the concatenation of the domainNameLabel and the regionalized DNS zone.
* `ip_address` - The IP address value that was allocated.
* `tags` - A mapping of tags to assigned to the resource.

View File

@ -17,6 +17,9 @@
<li<%= sidebar_current("docs-azurerm-datasource-client-config") %>>
<a href="/docs/providers/azurerm/d/client_config.html">azurerm_client_config</a>
</li>
<li<%= sidebar_current("docs-azurerm-datasource-public-ip") %>>
<a href="/docs/providers/azurerm/d/public_ip.html">azurerm_public_ip</a>
</li>
</ul>
</li>