From 1819303e6574af158ee7d47a8d4ae05d7b22546e Mon Sep 17 00:00:00 2001 From: Abhijeet Gaiha Date: Tue, 6 Jun 2017 17:18:37 +0530 Subject: [PATCH] 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. --- .../azurerm/data_source_arm_public_ip.go | 86 +++++++++++++++++++ .../azurerm/data_source_arm_public_ip_test.go | 65 ++++++++++++++ builtin/providers/azurerm/provider.go | 1 + .../azurerm/d/public_ip.html.markdown | 62 +++++++++++++ website/source/layouts/azurerm.erb | 3 + 5 files changed, 217 insertions(+) create mode 100644 builtin/providers/azurerm/data_source_arm_public_ip.go create mode 100644 builtin/providers/azurerm/data_source_arm_public_ip_test.go create mode 100644 website/source/docs/providers/azurerm/d/public_ip.html.markdown diff --git a/builtin/providers/azurerm/data_source_arm_public_ip.go b/builtin/providers/azurerm/data_source_arm_public_ip.go new file mode 100644 index 000000000..bc71f9c48 --- /dev/null +++ b/builtin/providers/azurerm/data_source_arm_public_ip.go @@ -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 +} diff --git a/builtin/providers/azurerm/data_source_arm_public_ip_test.go b/builtin/providers/azurerm/data_source_arm_public_ip_test.go new file mode 100644 index 000000000..f9956b602 --- /dev/null +++ b/builtin/providers/azurerm/data_source_arm_public_ip_test.go @@ -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) +} diff --git a/builtin/providers/azurerm/provider.go b/builtin/providers/azurerm/provider.go index 220b34d99..f690ea369 100644 --- a/builtin/providers/azurerm/provider.go +++ b/builtin/providers/azurerm/provider.go @@ -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{ diff --git a/website/source/docs/providers/azurerm/d/public_ip.html.markdown b/website/source/docs/providers/azurerm/d/public_ip.html.markdown new file mode 100644 index 000000000..168c16cc2 --- /dev/null +++ b/website/source/docs/providers/azurerm/d/public_ip.html.markdown @@ -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. \ No newline at end of file diff --git a/website/source/layouts/azurerm.erb b/website/source/layouts/azurerm.erb index d80cefbb7..db032a31d 100644 --- a/website/source/layouts/azurerm.erb +++ b/website/source/layouts/azurerm.erb @@ -17,6 +17,9 @@ > azurerm_client_config + > + azurerm_public_ip +