From 2d005bbb44e642abe06659298cb626d7bb38b0f9 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Sun, 3 Sep 2017 18:48:25 +0100 Subject: [PATCH 01/15] add public_ip_ids data source --- azurerm/data_source_arm_public_ip_ids.go | 60 ++++++ azurerm/data_source_arm_public_ip_ids_test.go | 171 ++++++++++++++++++ azurerm/provider.go | 1 + 3 files changed, 232 insertions(+) create mode 100644 azurerm/data_source_arm_public_ip_ids.go create mode 100644 azurerm/data_source_arm_public_ip_ids_test.go diff --git a/azurerm/data_source_arm_public_ip_ids.go b/azurerm/data_source_arm_public_ip_ids.go new file mode 100644 index 000000000000..259aad30d7bd --- /dev/null +++ b/azurerm/data_source_arm_public_ip_ids.go @@ -0,0 +1,60 @@ +package azurerm + +import ( + "fmt" + "net/http" + "time" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceArmPublicIPIds() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmPublicIPIdsRead, + + Schema: map[string]*schema.Schema{ + "resource_group_name": { + Type: schema.TypeString, + Required: true, + }, + "minimum_count": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + "ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceArmPublicIPIdsRead(d *schema.ResourceData, meta interface{}) error { + publicIPClient := meta.(*ArmClient).publicIPClient + + resGroup := d.Get("resource_group_name").(string) + minimumCount, minimumCountOk := d.GetOk("minimum_count") + resp, err := publicIPClient.List(resGroup) + if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + } + return fmt.Errorf("Error making Read request on Azure resource group %s: %s", resGroup, err) + } + availableIds := make([]string, 0) + for _, element := range *resp.Value { + if element.IPConfiguration == nil { + availableIds = append(availableIds, *element.ID) + } + } + + if minimumCountOk && len(availableIds) < minimumCount.(int) { + return fmt.Errorf("Not enough unassigned public IP addresses in resource group %s", resGroup) + } + d.SetId(time.Now().UTC().String()) + d.Set("ids", availableIds) + + return nil +} diff --git a/azurerm/data_source_arm_public_ip_ids_test.go b/azurerm/data_source_arm_public_ip_ids_test.go new file mode 100644 index 000000000000..689c3fd74182 --- /dev/null +++ b/azurerm/data_source_arm_public_ip_ids_test.go @@ -0,0 +1,171 @@ +package azurerm + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAzureRMPublicIPIds_basic(t *testing.T) { + dataSourceName := "data.azurerm_public_ip_ids.test" + name, resourceGroupName := randNames() + + config := testAccDataSourceAzureRMPublicIPIdsBasic(name, resourceGroupName, testLocation(), 10, 0) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName), + resource.TestCheckResourceAttr(dataSourceName, "ids.#", "10"), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccDataSourceAzureRMPublicIPIds_mixed(t *testing.T) { + dataSourceName := "data.azurerm_public_ip_ids.test" + name, resourceGroupName := randNames() + + config := testAccDataSourceAzureRMPublicIPIdsBasic(name, resourceGroupName, testLocation(), 10, 6) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName), + resource.TestCheckResourceAttr(dataSourceName, "ids.#", "4"), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccDataSourceAzureRMPublicIPIds_count(t *testing.T) { + dataSourceName := "data.azurerm_public_ip_ids.test" + name, resourceGroupName := randNames() + + config := testAccDataSourceAzureRMPublicIPIdsCount(name, resourceGroupName, testLocation(), 10, 5) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName), + resource.TestCheckResourceAttr(dataSourceName, "ids.#", "10"), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccDataSourceAzureRMPublicIPIds_tooFew(t *testing.T) { + name, resourceGroupName := randNames() + + config := testAccDataSourceAzureRMPublicIPIdsCount(name, resourceGroupName, testLocation(), 10, 15) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(fmt.Sprintf("Not enough unassigned public IP addresses in resource group %s", resourceGroupName)), + }, + }, + }) +} + +func randNames() (string, string) { + ri := acctest.RandInt() + name := fmt.Sprintf("acctestpublicipids-%d", ri) + resourceGroupName := fmt.Sprintf("acctestRG-%d", ri) + return name, resourceGroupName +} + +func testAccDataSourceAzureRMPublicIPIdsBasic(name string, resourceGroupName string, location string, pipCount int, lbCount int) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "%s" + location = "%s" +} + +resource "azurerm_public_ip" "test" { + count = %d + name = "%s-${count.index}" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + public_ip_address_allocation = "static" + idle_timeout_in_minutes = 30 + + tags { + environment = "test" + } +} + +resource "azurerm_lb" "test" { + count = %d + name = "load-balancer-${count.index}" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + frontend_ip_configuration { + name = "frontend" + public_ip_address_id = "${element(azurerm_public_ip.test.*.id, count.index)}" + } +} + +data "azurerm_public_ip_ids" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + depends_on = ["azurerm_lb.test", "azurerm_public_ip.test"] +} +`, resourceGroupName, location, pipCount, name, lbCount) +} + +func testAccDataSourceAzureRMPublicIPIdsCount(name string, resourceGroupName string, location string, pipCount int, minCount int) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "%s" + location = "%s" +} + +resource "azurerm_public_ip" "test" { + count = %d + name = "%s-${count.index}" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + public_ip_address_allocation = "static" + idle_timeout_in_minutes = 30 + + tags { + environment = "test" + } +} + +data "azurerm_public_ip_ids" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + minimum_count = %d + depends_on = ["azurerm_public_ip.test"] +} +`, resourceGroupName, location, pipCount, name, minCount) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 623de61ed6eb..59645ca4b87f 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -90,6 +90,7 @@ func Provider() terraform.ResourceProvider { "azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(), "azurerm_platform_image": dataSourceArmPlatformImage(), "azurerm_public_ip": dataSourceArmPublicIP(), + "azurerm_public_ip_ids": dataSourceArmPublicIPIds(), "azurerm_resource_group": dataSourceArmResourceGroup(), "azurerm_role_definition": dataSourceArmRoleDefinition(), "azurerm_storage_account": dataSourceArmStorageAccount(), From e34250932f89b02a5b1eefb89dab10d4344274a9 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Mon, 4 Sep 2017 05:49:01 +0100 Subject: [PATCH 02/15] add docs --- website/docs/d/public_ip_ids.html.markdown | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 website/docs/d/public_ip_ids.html.markdown diff --git a/website/docs/d/public_ip_ids.html.markdown b/website/docs/d/public_ip_ids.html.markdown new file mode 100644 index 000000000000..9d891dcb6b97 --- /dev/null +++ b/website/docs/d/public_ip_ids.html.markdown @@ -0,0 +1,44 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_public_ip_ids" +sidebar_current: "docs-azurerm-datasource-public-ip-ids" +description: |- + Provides a list of unassociated public IP address IDs. +--- + +# azurerm\_public\_ip\_ids + +Use this data source to get a list of unassociated public IP address IDs +in a resource group, optionally specifying a minimum required number. + +## Example Usage + +```hcl +data "azurerm_public_ip_ids" "datasourceips" { + resource_group_name = "pipRG" + minimum_count = 2 +} + +resource "azurerm_lb" "load_balancer" { + count = 2 + name = "load_balancer-${count.index}" + location = "northeurope" + resource_group_name = "acctestRG" + + frontend_ip_configuration { + name = "frontend" + public_ip_address_id = "${data.azurerm_public_ip_ids.datasourceips.ids[count.index]}" + } +} +``` + +## Argument Reference + +* `resource_group_name` - (Required) Specifies the name of the resource group. +* `minimum_count` - (Optional) Specifies the minimum number of IP addresses that +must be available, otherwise an error will be raised. + + +## Attributes Reference + +* `ids` - A list of public IP address resource IDs. \ No newline at end of file From 7d774fe7700aa789137f347e34bf64bd638e099f Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Sun, 8 Oct 2017 12:03:17 +0100 Subject: [PATCH 03/15] rename ds --- azurerm/data_source_arm_public_ip_ids.go | 60 ---------- azurerm/data_source_arm_public_ips.go | 106 ++++++++++++++++++ ....go => data_source_arm_public_ips_test.go} | 30 ++--- azurerm/provider.go | 2 +- website/docs/d/public_ip_ids.html.markdown | 6 +- 5 files changed, 125 insertions(+), 79 deletions(-) delete mode 100644 azurerm/data_source_arm_public_ip_ids.go create mode 100644 azurerm/data_source_arm_public_ips.go rename azurerm/{data_source_arm_public_ip_ids_test.go => data_source_arm_public_ips_test.go} (78%) diff --git a/azurerm/data_source_arm_public_ip_ids.go b/azurerm/data_source_arm_public_ip_ids.go deleted file mode 100644 index 259aad30d7bd..000000000000 --- a/azurerm/data_source_arm_public_ip_ids.go +++ /dev/null @@ -1,60 +0,0 @@ -package azurerm - -import ( - "fmt" - "net/http" - "time" - - "github.com/hashicorp/terraform/helper/schema" -) - -func dataSourceArmPublicIPIds() *schema.Resource { - return &schema.Resource{ - Read: dataSourceArmPublicIPIdsRead, - - Schema: map[string]*schema.Schema{ - "resource_group_name": { - Type: schema.TypeString, - Required: true, - }, - "minimum_count": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - }, - "ids": &schema.Schema{ - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - }, - } -} - -func dataSourceArmPublicIPIdsRead(d *schema.ResourceData, meta interface{}) error { - publicIPClient := meta.(*ArmClient).publicIPClient - - resGroup := d.Get("resource_group_name").(string) - minimumCount, minimumCountOk := d.GetOk("minimum_count") - resp, err := publicIPClient.List(resGroup) - if err != nil { - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - } - return fmt.Errorf("Error making Read request on Azure resource group %s: %s", resGroup, err) - } - availableIds := make([]string, 0) - for _, element := range *resp.Value { - if element.IPConfiguration == nil { - availableIds = append(availableIds, *element.ID) - } - } - - if minimumCountOk && len(availableIds) < minimumCount.(int) { - return fmt.Errorf("Not enough unassigned public IP addresses in resource group %s", resGroup) - } - d.SetId(time.Now().UTC().String()) - d.Set("ids", availableIds) - - return nil -} diff --git a/azurerm/data_source_arm_public_ips.go b/azurerm/data_source_arm_public_ips.go new file mode 100644 index 000000000000..9728f02130b0 --- /dev/null +++ b/azurerm/data_source_arm_public_ips.go @@ -0,0 +1,106 @@ +package azurerm + +import ( + "fmt" + "net/http" + "time" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/Azure/azure-sdk-for-go/arm/network" +) + +func dataSourceArmPublicIPs() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmPublicIPsRead, + + Schema: map[string]*schema.Schema{ + "resource_group_name": { + Type: schema.TypeString, + Required: true, + }, + "minimum_count": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + "attached": { + Type: schema.TypeBool, + Required: true, + ForceNew: true, + }, + "ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "fqdns": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "ip_addresses": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "domain_name_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error { + publicIPClient := meta.(*ArmClient).publicIPClient + + resGroup := d.Get("resource_group_name").(string) + minimumCount, minimumCountOk := d.GetOk("minimum_count") + attachedOnly := d.Get("attached").(bool) + resp, err := publicIPClient.List(resGroup) + if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + } + return fmt.Errorf("Error making Read request on Azure resource group %s: %s", resGroup, err) + } + var filteredIps []network.PublicIPAddress + for _, element := range *resp.Value { + if (element.IPConfiguration != nil) == attachedOnly { + filteredIps = append(filteredIps, element) + } + } + + var ids, names, fqdns, ip_addresses, domain_name_labels []string + for _, element := range filteredIps { + ids = append(ids, *element.ID) + names = append(names, *element.Name) + if attachedOnly { + fqdns = append(fqdns, *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn) + ip_addresses = append(ip_addresses, *element.PublicIPAddressPropertiesFormat.IPAddress) + domain_name_labels = append(domain_name_labels, *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel) + } else { + fqdns = append(fqdns, "") + ip_addresses = append(ip_addresses, "") + domain_name_labels = append(domain_name_labels, "") + } + } + + if minimumCountOk && len(ids) < minimumCount.(int) { + return fmt.Errorf("Not enough unassigned public IP addresses in resource group %s", resGroup) + } + d.SetId(time.Now().UTC().String()) + d.Set("ids", ids) + d.Set("names", names) + d.Set("fqdns", fqdns) + d.Set("ip_addresses", ip_addresses) + d.Set("domain_name_labels", domain_name_labels) + + return nil +} \ No newline at end of file diff --git a/azurerm/data_source_arm_public_ip_ids_test.go b/azurerm/data_source_arm_public_ips_test.go similarity index 78% rename from azurerm/data_source_arm_public_ip_ids_test.go rename to azurerm/data_source_arm_public_ips_test.go index 689c3fd74182..ea1ae3a3d3ef 100644 --- a/azurerm/data_source_arm_public_ip_ids_test.go +++ b/azurerm/data_source_arm_public_ips_test.go @@ -9,11 +9,11 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccDataSourceAzureRMPublicIPIds_basic(t *testing.T) { - dataSourceName := "data.azurerm_public_ip_ids.test" +func TestAccDataSourceAzureRMPublicIPs_basic(t *testing.T) { + dataSourceName := "data.azurerm_public_ips.test" name, resourceGroupName := randNames() - config := testAccDataSourceAzureRMPublicIPIdsBasic(name, resourceGroupName, testLocation(), 10, 0) + config := testAccDataSourceAzureRMPublicIPsBasic(name, resourceGroupName, testLocation(), 10, 0) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -32,11 +32,11 @@ func TestAccDataSourceAzureRMPublicIPIds_basic(t *testing.T) { }) } -func TestAccDataSourceAzureRMPublicIPIds_mixed(t *testing.T) { - dataSourceName := "data.azurerm_public_ip_ids.test" +func TestAccDataSourceAzureRMPublicIPs_mixed(t *testing.T) { + dataSourceName := "data.azurerm_public_ips.test" name, resourceGroupName := randNames() - config := testAccDataSourceAzureRMPublicIPIdsBasic(name, resourceGroupName, testLocation(), 10, 6) + config := testAccDataSourceAzureRMPublicIPsBasic(name, resourceGroupName, testLocation(), 10, 6) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -55,11 +55,11 @@ func TestAccDataSourceAzureRMPublicIPIds_mixed(t *testing.T) { }) } -func TestAccDataSourceAzureRMPublicIPIds_count(t *testing.T) { - dataSourceName := "data.azurerm_public_ip_ids.test" +func TestAccDataSourceAzureRMPublicIPs_count(t *testing.T) { + dataSourceName := "data.azurerm_public_ips.test" name, resourceGroupName := randNames() - config := testAccDataSourceAzureRMPublicIPIdsCount(name, resourceGroupName, testLocation(), 10, 5) + config := testAccDataSourceAzureRMPublicIPsCount(name, resourceGroupName, testLocation(), 10, 5) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -78,10 +78,10 @@ func TestAccDataSourceAzureRMPublicIPIds_count(t *testing.T) { }) } -func TestAccDataSourceAzureRMPublicIPIds_tooFew(t *testing.T) { +func TestAccDataSourceAzureRMPublicIPs_tooFew(t *testing.T) { name, resourceGroupName := randNames() - config := testAccDataSourceAzureRMPublicIPIdsCount(name, resourceGroupName, testLocation(), 10, 15) + config := testAccDataSourceAzureRMPublicIPsCount(name, resourceGroupName, testLocation(), 10, 15) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -103,7 +103,7 @@ func randNames() (string, string) { return name, resourceGroupName } -func testAccDataSourceAzureRMPublicIPIdsBasic(name string, resourceGroupName string, location string, pipCount int, lbCount int) string { +func testAccDataSourceAzureRMPublicIPsBasic(name string, resourceGroupName string, location string, pipCount int, lbCount int) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "%s" @@ -135,14 +135,14 @@ resource "azurerm_lb" "test" { } } -data "azurerm_public_ip_ids" "test" { +data "azurerm_public_ips" "test" { resource_group_name = "${azurerm_resource_group.test.name}" depends_on = ["azurerm_lb.test", "azurerm_public_ip.test"] } `, resourceGroupName, location, pipCount, name, lbCount) } -func testAccDataSourceAzureRMPublicIPIdsCount(name string, resourceGroupName string, location string, pipCount int, minCount int) string { +func testAccDataSourceAzureRMPublicIPsCount(name string, resourceGroupName string, location string, pipCount int, minCount int) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "%s" @@ -162,7 +162,7 @@ resource "azurerm_public_ip" "test" { } } -data "azurerm_public_ip_ids" "test" { +data "azurerm_public_ips" "test" { resource_group_name = "${azurerm_resource_group.test.name}" minimum_count = %d depends_on = ["azurerm_public_ip.test"] diff --git a/azurerm/provider.go b/azurerm/provider.go index 59645ca4b87f..c54d964a13a9 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -90,7 +90,7 @@ func Provider() terraform.ResourceProvider { "azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(), "azurerm_platform_image": dataSourceArmPlatformImage(), "azurerm_public_ip": dataSourceArmPublicIP(), - "azurerm_public_ip_ids": dataSourceArmPublicIPIds(), + "azurerm_public_ips": dataSourceArmPublicIPs(), "azurerm_resource_group": dataSourceArmResourceGroup(), "azurerm_role_definition": dataSourceArmRoleDefinition(), "azurerm_storage_account": dataSourceArmStorageAccount(), diff --git a/website/docs/d/public_ip_ids.html.markdown b/website/docs/d/public_ip_ids.html.markdown index 9d891dcb6b97..1156f7e6dc1c 100644 --- a/website/docs/d/public_ip_ids.html.markdown +++ b/website/docs/d/public_ip_ids.html.markdown @@ -1,6 +1,6 @@ --- layout: "azurerm" -page_title: "Azure Resource Manager: azurerm_public_ip_ids" +page_title: "Azure Resource Manager: azurerm_public_ips" sidebar_current: "docs-azurerm-datasource-public-ip-ids" description: |- Provides a list of unassociated public IP address IDs. @@ -14,7 +14,7 @@ in a resource group, optionally specifying a minimum required number. ## Example Usage ```hcl -data "azurerm_public_ip_ids" "datasourceips" { +data "azurerm_public_ips" "datasourceips" { resource_group_name = "pipRG" minimum_count = 2 } @@ -27,7 +27,7 @@ resource "azurerm_lb" "load_balancer" { frontend_ip_configuration { name = "frontend" - public_ip_address_id = "${data.azurerm_public_ip_ids.datasourceips.ids[count.index]}" + public_ip_address_id = "${data.azurerm_public_ips.datasourceips.ids[count.index]}" } } ``` From 694de65e829114b62e5299bd822284b89aebd3d7 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Sun, 8 Oct 2017 13:49:12 +0100 Subject: [PATCH 04/15] return list of maps with pip details --- azurerm/data_source_arm_public_ips.go | 77 +++++++++++---------------- 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/azurerm/data_source_arm_public_ips.go b/azurerm/data_source_arm_public_ips.go index 9728f02130b0..df0c7e68c652 100644 --- a/azurerm/data_source_arm_public_ips.go +++ b/azurerm/data_source_arm_public_ips.go @@ -5,8 +5,8 @@ import ( "net/http" "time" - "github.com/hashicorp/terraform/helper/schema" "github.com/Azure/azure-sdk-for-go/arm/network" + "github.com/hashicorp/terraform/helper/schema" ) func dataSourceArmPublicIPs() *schema.Resource { @@ -14,10 +14,7 @@ func dataSourceArmPublicIPs() *schema.Resource { Read: dataSourceArmPublicIPsRead, Schema: map[string]*schema.Schema{ - "resource_group_name": { - Type: schema.TypeString, - Required: true, - }, + "resource_group_name": resourceGroupNameForDataSourceSchema(), "minimum_count": { Type: schema.TypeInt, Optional: true, @@ -28,30 +25,10 @@ func dataSourceArmPublicIPs() *schema.Resource { Required: true, ForceNew: true, }, - "ids": &schema.Schema{ - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "names": &schema.Schema{ - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "fqdns": &schema.Schema{ - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "ip_addresses": &schema.Schema{ - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "domain_name_labels": &schema.Schema{ + "public_ips": { Type: schema.TypeList, Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, + Elem: &schema.Schema{Type: schema.TypeMap}, }, }, } @@ -76,31 +53,37 @@ func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error filteredIps = append(filteredIps, element) } } - - var ids, names, fqdns, ip_addresses, domain_name_labels []string + if minimumCountOk && len(filteredIps) < minimumCount.(int) { + return fmt.Errorf("Not enough unassigned public IP addresses in resource group %s", resGroup) + } + var results []map[string]string for _, element := range filteredIps { - ids = append(ids, *element.ID) - names = append(names, *element.Name) - if attachedOnly { - fqdns = append(fqdns, *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn) - ip_addresses = append(ip_addresses, *element.PublicIPAddressPropertiesFormat.IPAddress) - domain_name_labels = append(domain_name_labels, *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel) + m := make(map[string]string) + m["public_ip_address_id"] = *element.ID + m["name"] = *element.Name + if element.PublicIPAddressPropertiesFormat.DNSSettings != nil { + if element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != nil && *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != "" { + m["fqdn"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn + } else { + m["fqdn"] = "" + } + if element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != nil && *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != "" { + m["domain_name_label"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel + } else { + m["domain_name_label"] = "" + } + } + if element.PublicIPAddressPropertiesFormat.IPAddress != nil && *element.PublicIPAddressPropertiesFormat.IPAddress != "" { + m["ip_address"] = *element.PublicIPAddressPropertiesFormat.IPAddress } else { - fqdns = append(fqdns, "") - ip_addresses = append(ip_addresses, "") - domain_name_labels = append(domain_name_labels, "") + m["ip_address"] = "" } - } - if minimumCountOk && len(ids) < minimumCount.(int) { - return fmt.Errorf("Not enough unassigned public IP addresses in resource group %s", resGroup) + results = append(results, m) } + d.SetId(time.Now().UTC().String()) - d.Set("ids", ids) - d.Set("names", names) - d.Set("fqdns", fqdns) - d.Set("ip_addresses", ip_addresses) - d.Set("domain_name_labels", domain_name_labels) + d.Set("public_ips", results) return nil -} \ No newline at end of file +} From 2ce427326d8281618f56cabe8f64019ff3955e60 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Sun, 8 Oct 2017 21:24:25 +0100 Subject: [PATCH 05/15] update tests --- azurerm/data_source_arm_public_ips_test.go | 42 ++++++++++++++-------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/azurerm/data_source_arm_public_ips_test.go b/azurerm/data_source_arm_public_ips_test.go index ea1ae3a3d3ef..fca8ef79e745 100644 --- a/azurerm/data_source_arm_public_ips_test.go +++ b/azurerm/data_source_arm_public_ips_test.go @@ -10,7 +10,8 @@ import ( ) func TestAccDataSourceAzureRMPublicIPs_basic(t *testing.T) { - dataSourceName := "data.azurerm_public_ips.test" + dataSourceNameUsed := "data.azurerm_public_ips.test_used" + dataSourceNameUnused := "data.azurerm_public_ips.test_unused" name, resourceGroupName := randNames() config := testAccDataSourceAzureRMPublicIPsBasic(name, resourceGroupName, testLocation(), 10, 0) @@ -23,8 +24,10 @@ func TestAccDataSourceAzureRMPublicIPs_basic(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName), - resource.TestCheckResourceAttr(dataSourceName, "ids.#", "10"), + resource.TestCheckResourceAttr(dataSourceNameUsed, "resource_group_name", resourceGroupName), + resource.TestCheckResourceAttr(dataSourceNameUsed, "public_ips.#", "0"), + resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.#", "10"), + resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.0.name", fmt.Sprintf("%s-0", name)), ), ExpectNonEmptyPlan: true, }, @@ -33,7 +36,8 @@ func TestAccDataSourceAzureRMPublicIPs_basic(t *testing.T) { } func TestAccDataSourceAzureRMPublicIPs_mixed(t *testing.T) { - dataSourceName := "data.azurerm_public_ips.test" + dataSourceNameUsed := "data.azurerm_public_ips.test_used" + dataSourceNameUnused := "data.azurerm_public_ips.test_unused" name, resourceGroupName := randNames() config := testAccDataSourceAzureRMPublicIPsBasic(name, resourceGroupName, testLocation(), 10, 6) @@ -46,8 +50,11 @@ func TestAccDataSourceAzureRMPublicIPs_mixed(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName), - resource.TestCheckResourceAttr(dataSourceName, "ids.#", "4"), + resource.TestCheckResourceAttr(dataSourceNameUsed, "resource_group_name", resourceGroupName), + resource.TestCheckResourceAttr(dataSourceNameUsed, "public_ips.#", "6"), + resource.TestCheckResourceAttr(dataSourceNameUsed, "public_ips.0.name", fmt.Sprintf("%s-0", name)), + resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.#", "4"), + resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.0.name", fmt.Sprintf("%s-6", name)), ), ExpectNonEmptyPlan: true, }, @@ -56,7 +63,7 @@ func TestAccDataSourceAzureRMPublicIPs_mixed(t *testing.T) { } func TestAccDataSourceAzureRMPublicIPs_count(t *testing.T) { - dataSourceName := "data.azurerm_public_ips.test" + dataSourceNameUnused := "data.azurerm_public_ips.test_unused" name, resourceGroupName := randNames() config := testAccDataSourceAzureRMPublicIPsCount(name, resourceGroupName, testLocation(), 10, 5) @@ -69,8 +76,8 @@ func TestAccDataSourceAzureRMPublicIPs_count(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName), - resource.TestCheckResourceAttr(dataSourceName, "ids.#", "10"), + resource.TestCheckResourceAttr(dataSourceNameUnused, "resource_group_name", resourceGroupName), + resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.#", "10"), ), ExpectNonEmptyPlan: true, }, @@ -98,7 +105,7 @@ func TestAccDataSourceAzureRMPublicIPs_tooFew(t *testing.T) { func randNames() (string, string) { ri := acctest.RandInt() - name := fmt.Sprintf("acctestpublicipids-%d", ri) + name := fmt.Sprintf("acctestpublicippublic_ips-%d", ri) resourceGroupName := fmt.Sprintf("acctestRG-%d", ri) return name, resourceGroupName } @@ -135,8 +142,14 @@ resource "azurerm_lb" "test" { } } -data "azurerm_public_ips" "test" { - resource_group_name = "${azurerm_resource_group.test.name}" +data "azurerm_public_ips" "test_unused" { + resource_group_name = "${azurerm_resource_group.test.name}" + attached = false + depends_on = ["azurerm_lb.test", "azurerm_public_ip.test"] +} +data "azurerm_public_ips" "test_used" { + resource_group_name = "${azurerm_resource_group.test.name}" + attached = true depends_on = ["azurerm_lb.test", "azurerm_public_ip.test"] } `, resourceGroupName, location, pipCount, name, lbCount) @@ -162,8 +175,9 @@ resource "azurerm_public_ip" "test" { } } -data "azurerm_public_ips" "test" { - resource_group_name = "${azurerm_resource_group.test.name}" +data "azurerm_public_ips" "test_unused" { + resource_group_name = "${azurerm_resource_group.test.name}" + attached = false minimum_count = %d depends_on = ["azurerm_public_ip.test"] } From 4f80fccca8090074504dfb3be4a1c3c232de278e Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Sun, 8 Oct 2017 21:38:35 +0100 Subject: [PATCH 06/15] update docs --- ...html.markdown => public_ips.html.markdown} | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) rename website/docs/d/{public_ip_ids.html.markdown => public_ips.html.markdown} (50%) diff --git a/website/docs/d/public_ip_ids.html.markdown b/website/docs/d/public_ips.html.markdown similarity index 50% rename from website/docs/d/public_ip_ids.html.markdown rename to website/docs/d/public_ips.html.markdown index 1156f7e6dc1c..5c5a839f5ea1 100644 --- a/website/docs/d/public_ip_ids.html.markdown +++ b/website/docs/d/public_ips.html.markdown @@ -1,21 +1,22 @@ --- layout: "azurerm" page_title: "Azure Resource Manager: azurerm_public_ips" -sidebar_current: "docs-azurerm-datasource-public-ip-ids" +sidebar_current: "docs-azurerm-datasource-public-ips" description: |- - Provides a list of unassociated public IP address IDs. + Provides a list of public IP addresses. --- -# azurerm\_public\_ip\_ids +# azurerm\_public\_ips -Use this data source to get a list of unassociated public IP address IDs +Use this data source to get a list of associated or unassociated public IP addresses in a resource group, optionally specifying a minimum required number. ## Example Usage ```hcl -data "azurerm_public_ips" "datasourceips" { - resource_group_name = "pipRG" +data "azurerm_public_ips" "test" { + resource_group_name = "pip-test" + attached = false minimum_count = 2 } @@ -27,7 +28,7 @@ resource "azurerm_lb" "load_balancer" { frontend_ip_configuration { name = "frontend" - public_ip_address_id = "${data.azurerm_public_ips.datasourceips.ids[count.index]}" + public_ip_address_id = "${lookup(data.azurerm_public_ips.test.public_ips[count.index], "public_ip_address_id")}" } } ``` @@ -35,10 +36,13 @@ resource "azurerm_lb" "load_balancer" { ## Argument Reference * `resource_group_name` - (Required) Specifies the name of the resource group. +* `attached` - (Required) Whether to return public IPs that are attached or not. * `minimum_count` - (Optional) Specifies the minimum number of IP addresses that must be available, otherwise an error will be raised. ## Attributes Reference -* `ids` - A list of public IP address resource IDs. \ No newline at end of file +* `public_ips` - A list of public IP addresses. Each public IP is represented by a +map containing the following keys; public_ip_address_id, name, fqdn, domain_name_label, +ip_address. Note that if the public IP is unassigned then some values may be empty. \ No newline at end of file From 056c6dfb5df739f2a9d703dbb4aceea21ed98a5e Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Thu, 1 Feb 2018 10:19:44 +0000 Subject: [PATCH 07/15] pr review updates --- azurerm/data_source_arm_public_ips.go | 67 ++++++++++++++-------- azurerm/data_source_arm_public_ips_test.go | 26 +-------- website/docs/d/public_ips.html.markdown | 5 +- 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/azurerm/data_source_arm_public_ips.go b/azurerm/data_source_arm_public_ips.go index df0c7e68c652..5fd20b71b4bf 100644 --- a/azurerm/data_source_arm_public_ips.go +++ b/azurerm/data_source_arm_public_ips.go @@ -2,11 +2,11 @@ package azurerm import ( "fmt" - "net/http" "time" - "github.com/Azure/azure-sdk-for-go/arm/network" + "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network" "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) func dataSourceArmPublicIPs() *schema.Resource { @@ -15,11 +15,6 @@ func dataSourceArmPublicIPs() *schema.Resource { Schema: map[string]*schema.Schema{ "resource_group_name": resourceGroupNameForDataSourceSchema(), - "minimum_count": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - }, "attached": { Type: schema.TypeBool, Required: true, @@ -28,7 +23,35 @@ func dataSourceArmPublicIPs() *schema.Resource { "public_ips": { Type: schema.TypeList, Computed: true, - Elem: &schema.Schema{Type: schema.TypeMap}, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "fqdn": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "domain_name_label": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "ip_address": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + }, }, }, } @@ -36,47 +59,43 @@ func dataSourceArmPublicIPs() *schema.Resource { func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error { publicIPClient := meta.(*ArmClient).publicIPClient + ctx := meta.(*ArmClient).StopContext resGroup := d.Get("resource_group_name").(string) - minimumCount, minimumCountOk := d.GetOk("minimum_count") attachedOnly := d.Get("attached").(bool) - resp, err := publicIPClient.List(resGroup) + resp, err := publicIPClient.List(ctx, resGroup) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if utils.ResponseWasNotFound(resp.Response().Response) { d.SetId("") + return nil } - return fmt.Errorf("Error making Read request on Azure resource group %s: %s", resGroup, err) + return fmt.Errorf("Error making Read request on Azure resource group %q: %v", resGroup, err) } var filteredIps []network.PublicIPAddress - for _, element := range *resp.Value { + for _, element := range resp.Values() { if (element.IPConfiguration != nil) == attachedOnly { filteredIps = append(filteredIps, element) } } - if minimumCountOk && len(filteredIps) < minimumCount.(int) { - return fmt.Errorf("Not enough unassigned public IP addresses in resource group %s", resGroup) - } var results []map[string]string for _, element := range filteredIps { m := make(map[string]string) - m["public_ip_address_id"] = *element.ID - m["name"] = *element.Name + if element.ID != nil && *element.ID != "" { + m["id"] = *element.ID + } + if element.Name != nil && *element.Name != "" { + m["name"] = *element.Name + } if element.PublicIPAddressPropertiesFormat.DNSSettings != nil { if element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != nil && *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != "" { m["fqdn"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn - } else { - m["fqdn"] = "" } if element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != nil && *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != "" { m["domain_name_label"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel - } else { - m["domain_name_label"] = "" } } if element.PublicIPAddressPropertiesFormat.IPAddress != nil && *element.PublicIPAddressPropertiesFormat.IPAddress != "" { m["ip_address"] = *element.PublicIPAddressPropertiesFormat.IPAddress - } else { - m["ip_address"] = "" } results = append(results, m) diff --git a/azurerm/data_source_arm_public_ips_test.go b/azurerm/data_source_arm_public_ips_test.go index fca8ef79e745..e0bc9a04ebc0 100644 --- a/azurerm/data_source_arm_public_ips_test.go +++ b/azurerm/data_source_arm_public_ips_test.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -66,7 +65,7 @@ func TestAccDataSourceAzureRMPublicIPs_count(t *testing.T) { dataSourceNameUnused := "data.azurerm_public_ips.test_unused" name, resourceGroupName := randNames() - config := testAccDataSourceAzureRMPublicIPsCount(name, resourceGroupName, testLocation(), 10, 5) + config := testAccDataSourceAzureRMPublicIPsCount(name, resourceGroupName, testLocation(), 10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -85,24 +84,6 @@ func TestAccDataSourceAzureRMPublicIPs_count(t *testing.T) { }) } -func TestAccDataSourceAzureRMPublicIPs_tooFew(t *testing.T) { - name, resourceGroupName := randNames() - - config := testAccDataSourceAzureRMPublicIPsCount(name, resourceGroupName, testLocation(), 10, 15) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMPublicIpDestroy, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile(fmt.Sprintf("Not enough unassigned public IP addresses in resource group %s", resourceGroupName)), - }, - }, - }) -} - func randNames() (string, string) { ri := acctest.RandInt() name := fmt.Sprintf("acctestpublicippublic_ips-%d", ri) @@ -155,7 +136,7 @@ data "azurerm_public_ips" "test_used" { `, resourceGroupName, location, pipCount, name, lbCount) } -func testAccDataSourceAzureRMPublicIPsCount(name string, resourceGroupName string, location string, pipCount int, minCount int) string { +func testAccDataSourceAzureRMPublicIPsCount(name string, resourceGroupName string, location string, pipCount int) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "%s" @@ -178,8 +159,7 @@ resource "azurerm_public_ip" "test" { data "azurerm_public_ips" "test_unused" { resource_group_name = "${azurerm_resource_group.test.name}" attached = false - minimum_count = %d depends_on = ["azurerm_public_ip.test"] } -`, resourceGroupName, location, pipCount, name, minCount) +`, resourceGroupName, location, pipCount, name) } diff --git a/website/docs/d/public_ips.html.markdown b/website/docs/d/public_ips.html.markdown index 5c5a839f5ea1..de2b6b34ac71 100644 --- a/website/docs/d/public_ips.html.markdown +++ b/website/docs/d/public_ips.html.markdown @@ -17,7 +17,6 @@ in a resource group, optionally specifying a minimum required number. data "azurerm_public_ips" "test" { resource_group_name = "pip-test" attached = false - minimum_count = 2 } resource "azurerm_lb" "load_balancer" { @@ -28,7 +27,7 @@ resource "azurerm_lb" "load_balancer" { frontend_ip_configuration { name = "frontend" - public_ip_address_id = "${lookup(data.azurerm_public_ips.test.public_ips[count.index], "public_ip_address_id")}" + public_ip_address_id = "${lookup(data.azurerm_public_ips.test.public_ips[count.index], "id")}" } } ``` @@ -37,8 +36,6 @@ resource "azurerm_lb" "load_balancer" { * `resource_group_name` - (Required) Specifies the name of the resource group. * `attached` - (Required) Whether to return public IPs that are attached or not. -* `minimum_count` - (Optional) Specifies the minimum number of IP addresses that -must be available, otherwise an error will be raised. ## Attributes Reference From dc4ceafbcd5278251ce2afd40ce271a42bd9f21b Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 13:03:05 -0700 Subject: [PATCH 08/15] Refactoring the code --- azurerm/data_source_arm_public_ips.go | 70 ++++++++++++++++----------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/azurerm/data_source_arm_public_ips.go b/azurerm/data_source_arm_public_ips.go index 5fd20b71b4bf..705db87631b0 100644 --- a/azurerm/data_source_arm_public_ips.go +++ b/azurerm/data_source_arm_public_ips.go @@ -15,11 +15,12 @@ func dataSourceArmPublicIPs() *schema.Resource { Schema: map[string]*schema.Schema{ "resource_group_name": resourceGroupNameForDataSourceSchema(), + "attached": { Type: schema.TypeBool, Required: true, - ForceNew: true, }, + "public_ips": { Type: schema.TypeList, Computed: true, @@ -27,27 +28,22 @@ func dataSourceArmPublicIPs() *schema.Resource { Schema: map[string]*schema.Schema{ "id": { Type: schema.TypeString, - Optional: true, Computed: true, }, "name": { Type: schema.TypeString, - Optional: true, Computed: true, }, "fqdn": { Type: schema.TypeString, - Optional: true, Computed: true, }, "domain_name_label": { Type: schema.TypeString, - Optional: true, Computed: true, }, "ip_address": { Type: schema.TypeString, - Optional: true, Computed: true, }, }, @@ -58,12 +54,12 @@ func dataSourceArmPublicIPs() *schema.Resource { } func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error { - publicIPClient := meta.(*ArmClient).publicIPClient + client := meta.(*ArmClient).publicIPClient ctx := meta.(*ArmClient).StopContext resGroup := d.Get("resource_group_name").(string) attachedOnly := d.Get("attached").(bool) - resp, err := publicIPClient.List(ctx, resGroup) + resp, err := client.List(ctx, resGroup) if err != nil { if utils.ResponseWasNotFound(resp.Response().Response) { d.SetId("") @@ -71,34 +67,20 @@ func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error } return fmt.Errorf("Error making Read request on Azure resource group %q: %v", resGroup, err) } + var filteredIps []network.PublicIPAddress for _, element := range resp.Values() { - if (element.IPConfiguration != nil) == attachedOnly { + nicIsAttached := element.IPConfiguration != nil + + if attachedOnly && nicIsAttached { filteredIps = append(filteredIps, element) } } + var results []map[string]string for _, element := range filteredIps { - m := make(map[string]string) - if element.ID != nil && *element.ID != "" { - m["id"] = *element.ID - } - if element.Name != nil && *element.Name != "" { - m["name"] = *element.Name - } - if element.PublicIPAddressPropertiesFormat.DNSSettings != nil { - if element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != nil && *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != "" { - m["fqdn"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn - } - if element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != nil && *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != "" { - m["domain_name_label"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel - } - } - if element.PublicIPAddressPropertiesFormat.IPAddress != nil && *element.PublicIPAddressPropertiesFormat.IPAddress != "" { - m["ip_address"] = *element.PublicIPAddressPropertiesFormat.IPAddress - } - - results = append(results, m) + flattenedIPAddress := flattenDataSourcePublicIP(element) + results = append(results, flattenedIPAddress) } d.SetId(time.Now().UTC().String()) @@ -106,3 +88,33 @@ func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error return nil } + +func flattenDataSourcePublicIP(input network.PublicIPAddress) map[string]string { + output := make(map[string]string, 0) + + if input.ID != nil { + output["id"] = *input.ID + } + + if input.Name != nil { + output["name"] = *input.Name + } + + if props := input.PublicIPAddressPropertiesFormat; props != nil { + if dns := props.DNSSettings; dns != nil { + if fqdn := dns.Fqdn; fqdn != nil { + output["fqdn"] = *fqdn + } + + if label := dns.DomainNameLabel; label != nil { + output["domain_name_label"] = *label + } + } + + if ip := props.IPAddress; ip != nil { + output["ip_address"] = *ip + } + } + + return output +} From 9307161a739649a4e9fde874202fe053210178ac Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 13:03:24 -0700 Subject: [PATCH 09/15] Ditching the `arm` prefix --- .../{data_source_arm_public_ips.go => data_source_public_ips.go} | 0 ...urce_arm_public_ips_test.go => data_source_public_ips_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename azurerm/{data_source_arm_public_ips.go => data_source_public_ips.go} (100%) rename azurerm/{data_source_arm_public_ips_test.go => data_source_public_ips_test.go} (100%) diff --git a/azurerm/data_source_arm_public_ips.go b/azurerm/data_source_public_ips.go similarity index 100% rename from azurerm/data_source_arm_public_ips.go rename to azurerm/data_source_public_ips.go diff --git a/azurerm/data_source_arm_public_ips_test.go b/azurerm/data_source_public_ips_test.go similarity index 100% rename from azurerm/data_source_arm_public_ips_test.go rename to azurerm/data_source_public_ips_test.go From 8e1eafee1dc1c1327b76fd7f40d893e941e78797 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 13:32:41 -0700 Subject: [PATCH 10/15] Fixing up the tests: ``` $ acctests azurerm TestAccDataSourceAzureRMPublicIPs_ === RUN TestAccDataSourceAzureRMPublicIPs_basic --- PASS: TestAccDataSourceAzureRMPublicIPs_basic (85.74s) === RUN TestAccDataSourceAzureRMPublicIPs_mixed --- PASS: TestAccDataSourceAzureRMPublicIPs_mixed (82.73s) === RUN TestAccDataSourceAzureRMPublicIPs_count --- PASS: TestAccDataSourceAzureRMPublicIPs_count (79.26s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 247.767s ``` --- azurerm/data_source_public_ips.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/azurerm/data_source_public_ips.go b/azurerm/data_source_public_ips.go index 705db87631b0..b86f5a00e8db 100644 --- a/azurerm/data_source_public_ips.go +++ b/azurerm/data_source_public_ips.go @@ -57,36 +57,45 @@ func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error client := meta.(*ArmClient).publicIPClient ctx := meta.(*ArmClient).StopContext - resGroup := d.Get("resource_group_name").(string) + resourceGroup := d.Get("resource_group_name").(string) attachedOnly := d.Get("attached").(bool) - resp, err := client.List(ctx, resGroup) + resp, err := client.List(ctx, resourceGroup) if err != nil { if utils.ResponseWasNotFound(resp.Response().Response) { d.SetId("") return nil } - return fmt.Errorf("Error making Read request on Azure resource group %q: %v", resGroup, err) + + return fmt.Errorf("Error listing Public IP Addresses in the Resource Group %q: %v", resourceGroup, err) } var filteredIps []network.PublicIPAddress for _, element := range resp.Values() { nicIsAttached := element.IPConfiguration != nil - if attachedOnly && nicIsAttached { + if attachedOnly == nicIsAttached { filteredIps = append(filteredIps, element) } } - var results []map[string]string - for _, element := range filteredIps { + d.SetId(time.Now().UTC().String()) + results := flattenDataSourcePublicIPs(filteredIps) + if err := d.Set("public_ips", results); err != nil { + return fmt.Errorf("Error setting `public_ips`: %+v", err) + } + + return nil +} + +func flattenDataSourcePublicIPs(input []network.PublicIPAddress) []interface{} { + results := make([]map[string]string, 0) + + for _, element := range input { flattenedIPAddress := flattenDataSourcePublicIP(element) results = append(results, flattenedIPAddress) } - d.SetId(time.Now().UTC().String()) - d.Set("public_ips", results) - - return nil + return results } func flattenDataSourcePublicIP(input network.PublicIPAddress) map[string]string { From d496463138b94dc0b2ef20854f75a4e912a4af06 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 15:13:19 -0700 Subject: [PATCH 11/15] Support for filtering by name prefix and ip allocation method --- azurerm/data_source_public_ips.go | 59 ++++++++- azurerm/data_source_public_ips_test.go | 171 ++++++++++++++++--------- 2 files changed, 164 insertions(+), 66 deletions(-) diff --git a/azurerm/data_source_public_ips.go b/azurerm/data_source_public_ips.go index b86f5a00e8db..3b236d276500 100644 --- a/azurerm/data_source_public_ips.go +++ b/azurerm/data_source_public_ips.go @@ -2,10 +2,13 @@ package azurerm import ( "fmt" + "log" + "strings" "time" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -16,9 +19,23 @@ func dataSourceArmPublicIPs() *schema.Resource { Schema: map[string]*schema.Schema{ "resource_group_name": resourceGroupNameForDataSourceSchema(), + "name_prefix": { + Type: schema.TypeString, + Optional: true, + }, + "attached": { Type: schema.TypeBool, - Required: true, + Optional: true, + }, + + "allocation_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(network.Dynamic), + string(network.Static), + }, false), }, "public_ips": { @@ -58,7 +75,8 @@ func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error ctx := meta.(*ArmClient).StopContext resourceGroup := d.Get("resource_group_name").(string) - attachedOnly := d.Get("attached").(bool) + + log.Printf("[DEBUG] Reading Public IP's in Resource Group %q", resourceGroup) resp, err := client.List(ctx, resourceGroup) if err != nil { if utils.ResponseWasNotFound(resp.Response().Response) { @@ -69,17 +87,44 @@ func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Error listing Public IP Addresses in the Resource Group %q: %v", resourceGroup, err) } - var filteredIps []network.PublicIPAddress + filteredIPAddresses := make([]network.PublicIPAddress, 0) for _, element := range resp.Values() { nicIsAttached := element.IPConfiguration != nil + shouldInclude := true + + if v, ok := d.GetOkExists("name_prefix"); ok { + if prefix := v.(string); prefix != "" { + if !strings.HasPrefix(*element.Name, prefix) { + shouldInclude = false + } + } + } - if attachedOnly == nicIsAttached { - filteredIps = append(filteredIps, element) + if v, ok := d.GetOkExists("attached"); ok { + attachedOnly := v.(bool) + + if attachedOnly != nicIsAttached { + shouldInclude = false + } + } + + if v, ok := d.GetOkExists("allocation_type"); ok { + if allocationType := v.(string); allocationType != "" { + allocation := network.IPAllocationMethod(allocationType) + if element.PublicIPAllocationMethod != allocation { + shouldInclude = false + } + } + } + + if shouldInclude { + filteredIPAddresses = append(filteredIPAddresses, element) } } d.SetId(time.Now().UTC().String()) - results := flattenDataSourcePublicIPs(filteredIps) + + results := flattenDataSourcePublicIPs(filteredIPAddresses) if err := d.Set("public_ips", results); err != nil { return fmt.Errorf("Error setting `public_ips`: %+v", err) } @@ -88,7 +133,7 @@ func dataSourceArmPublicIPsRead(d *schema.ResourceData, meta interface{}) error } func flattenDataSourcePublicIPs(input []network.PublicIPAddress) []interface{} { - results := make([]map[string]string, 0) + results := make([]interface{}, 0) for _, element := range input { flattenedIPAddress := flattenDataSourcePublicIP(element) diff --git a/azurerm/data_source_public_ips_test.go b/azurerm/data_source_public_ips_test.go index e0bc9a04ebc0..ed5d6f4faf51 100644 --- a/azurerm/data_source_public_ips_test.go +++ b/azurerm/data_source_public_ips_test.go @@ -8,12 +8,12 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccDataSourceAzureRMPublicIPs_basic(t *testing.T) { - dataSourceNameUsed := "data.azurerm_public_ips.test_used" - dataSourceNameUnused := "data.azurerm_public_ips.test_unused" - name, resourceGroupName := randNames() +func TestAccDataSourceAzureRMPublicIPs_namePrefix(t *testing.T) { + dataSourceName := "data.azurerm_public_ips.test" + ri := acctest.RandInt() + rs := acctest.RandString(5) - config := testAccDataSourceAzureRMPublicIPsBasic(name, resourceGroupName, testLocation(), 10, 0) + config := testAccDataSourceAzureRMPublicIPs_prefix(ri, rs, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -23,23 +23,21 @@ func TestAccDataSourceAzureRMPublicIPs_basic(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceNameUsed, "resource_group_name", resourceGroupName), - resource.TestCheckResourceAttr(dataSourceNameUsed, "public_ips.#", "0"), - resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.#", "10"), - resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.0.name", fmt.Sprintf("%s-0", name)), + resource.TestCheckResourceAttr(dataSourceName, "public_ips.#", "2"), + resource.TestCheckResourceAttr(dataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpipa%s-0", rs)), ), - ExpectNonEmptyPlan: true, }, }, }) } -func TestAccDataSourceAzureRMPublicIPs_mixed(t *testing.T) { - dataSourceNameUsed := "data.azurerm_public_ips.test_used" - dataSourceNameUnused := "data.azurerm_public_ips.test_unused" - name, resourceGroupName := randNames() +func TestAccDataSourceAzureRMPublicIPs_assigned(t *testing.T) { + attachedDataSourceName := "data.azurerm_public_ips.attached" + unattachedDataSourceName := "data.azurerm_public_ips.unattached" + ri := acctest.RandInt() + rs := acctest.RandString(5) - config := testAccDataSourceAzureRMPublicIPsBasic(name, resourceGroupName, testLocation(), 10, 6) + config := testAccDataSourceAzureRMPublicIPs_attached(ri, rs, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -49,23 +47,23 @@ func TestAccDataSourceAzureRMPublicIPs_mixed(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceNameUsed, "resource_group_name", resourceGroupName), - resource.TestCheckResourceAttr(dataSourceNameUsed, "public_ips.#", "6"), - resource.TestCheckResourceAttr(dataSourceNameUsed, "public_ips.0.name", fmt.Sprintf("%s-0", name)), - resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.#", "4"), - resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.0.name", fmt.Sprintf("%s-6", name)), + resource.TestCheckResourceAttr(attachedDataSourceName, "public_ips.#", "3"), + resource.TestCheckResourceAttr(attachedDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpip%s-0", rs)), + resource.TestCheckResourceAttr(unattachedDataSourceName, "public_ips.#", "4"), + resource.TestCheckResourceAttr(unattachedDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpip%s-3", rs)), ), - ExpectNonEmptyPlan: true, }, }, }) } -func TestAccDataSourceAzureRMPublicIPs_count(t *testing.T) { - dataSourceNameUnused := "data.azurerm_public_ips.test_unused" - name, resourceGroupName := randNames() +func TestAccDataSourceAzureRMPublicIPs_allocationType(t *testing.T) { + staticDataSourceName := "data.azurerm_public_ips.static" + dynamicDataSourceName := "data.azurerm_public_ips.dynamic" + ri := acctest.RandInt() + rs := acctest.RandString(5) - config := testAccDataSourceAzureRMPublicIPsCount(name, resourceGroupName, testLocation(), 10) + config := testAccDataSourceAzureRMPublicIPs_allocationType(ri, rs, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -75,32 +73,26 @@ func TestAccDataSourceAzureRMPublicIPs_count(t *testing.T) { { Config: config, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceNameUnused, "resource_group_name", resourceGroupName), - resource.TestCheckResourceAttr(dataSourceNameUnused, "public_ips.#", "10"), + resource.TestCheckResourceAttr(staticDataSourceName, "public_ips.#", "3"), + resource.TestCheckResourceAttr(staticDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpips%s-0", rs)), + resource.TestCheckResourceAttr(dynamicDataSourceName, "public_ips.#", "4"), + resource.TestCheckResourceAttr(dynamicDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpipd%s-0", rs)), ), - ExpectNonEmptyPlan: true, }, }, }) } -func randNames() (string, string) { - ri := acctest.RandInt() - name := fmt.Sprintf("acctestpublicippublic_ips-%d", ri) - resourceGroupName := fmt.Sprintf("acctestRG-%d", ri) - return name, resourceGroupName -} - -func testAccDataSourceAzureRMPublicIPsBasic(name string, resourceGroupName string, location string, pipCount int, lbCount int) string { +func testAccDataSourceAzureRMPublicIPs_attached(rInt int, rString string, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "%s" + name = "acctestrg-%d" location = "%s" } resource "azurerm_public_ip" "test" { - count = %d - name = "%s-${count.index}" + count = 7 + name = "acctestpip%s-${count.index}" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" public_ip_address_allocation = "static" @@ -112,8 +104,8 @@ resource "azurerm_public_ip" "test" { } resource "azurerm_lb" "test" { - count = %d - name = "load-balancer-${count.index}" + count = 3 + name = "acctestlb-${count.index}" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" @@ -123,29 +115,43 @@ resource "azurerm_lb" "test" { } } -data "azurerm_public_ips" "test_unused" { - resource_group_name = "${azurerm_resource_group.test.name}" - attached = false - depends_on = ["azurerm_lb.test", "azurerm_public_ip.test"] +data "azurerm_public_ips" "unattached" { + resource_group_name = "${azurerm_resource_group.test.name}" + attached = false + depends_on = ["azurerm_lb.test"] } -data "azurerm_public_ips" "test_used" { - resource_group_name = "${azurerm_resource_group.test.name}" - attached = true - depends_on = ["azurerm_lb.test", "azurerm_public_ip.test"] + +data "azurerm_public_ips" "attached" { + resource_group_name = "${azurerm_resource_group.test.name}" + attached = true + depends_on = ["azurerm_lb.test"] } -`, resourceGroupName, location, pipCount, name, lbCount) +`, rInt, location, rString) } -func testAccDataSourceAzureRMPublicIPsCount(name string, resourceGroupName string, location string, pipCount int) string { +func testAccDataSourceAzureRMPublicIPs_prefix(rInt int, rString string, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "%s" + name = "acctestrg-%d" location = "%s" } resource "azurerm_public_ip" "test" { - count = %d - name = "%s-${count.index}" + count = 2 + name = "acctestpipb%s-${count.index}" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + public_ip_address_allocation = "static" + idle_timeout_in_minutes = 30 + + tags { + environment = "test" + } +} + +resource "azurerm_public_ip" "test2" { + count = 2 + name = "acctestpipa%s-${count.index}" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" public_ip_address_allocation = "static" @@ -156,10 +162,57 @@ resource "azurerm_public_ip" "test" { } } -data "azurerm_public_ips" "test_unused" { - resource_group_name = "${azurerm_resource_group.test.name}" - attached = false - depends_on = ["azurerm_public_ip.test"] +data "azurerm_public_ips" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name_prefix = "acctestpipa" + depends_on = ["azurerm_public_ip.test", "azurerm_public_ip.test2"] +} +`, rInt, location, rString, rString) +} + +func testAccDataSourceAzureRMPublicIPs_allocationType(rInt int, rString string, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" +} + +resource "azurerm_public_ip" "dynamic" { + count = 4 + name = "acctestpipd%s-${count.index}" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + public_ip_address_allocation = "dynamic" + idle_timeout_in_minutes = 30 + + tags { + environment = "test" + } +} + +resource "azurerm_public_ip" "static" { + count = 3 + name = "acctestpips%s-${count.index}" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + public_ip_address_allocation = "static" + idle_timeout_in_minutes = 30 + + tags { + environment = "test" + } +} + +data "azurerm_public_ips" "dynamic" { + resource_group_name = "${azurerm_resource_group.test.name}" + allocation_type = "Dynamic" + depends_on = ["azurerm_public_ip.dynamic"] +} + +data "azurerm_public_ips" "static" { + resource_group_name = "${azurerm_resource_group.test.name}" + allocation_type = "Static" + depends_on = ["azurerm_public_ip.static"] } -`, resourceGroupName, location, pipCount, name) +`, rInt, location, rString, rString) } From 480819915bfc1f05d4df88867e23f0dfb0305c07 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 15:39:06 -0700 Subject: [PATCH 12/15] Fixing the tests Tests pass: ``` $ acctests azurerm TestAccDataSourceAzureRMPublicIPs_ === RUN TestAccDataSourceAzureRMPublicIPs_namePrefix --- PASS: TestAccDataSourceAzureRMPublicIPs_namePrefix (86.67s) === RUN TestAccDataSourceAzureRMPublicIPs_assigned --- PASS: TestAccDataSourceAzureRMPublicIPs_assigned (90.43s) === RUN TestAccDataSourceAzureRMPublicIPs_allocationType --- PASS: TestAccDataSourceAzureRMPublicIPs_allocationType (82.07s) ``` --- azurerm/data_source_public_ips_test.go | 62 ++++++++++++++++++++------ 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/azurerm/data_source_public_ips_test.go b/azurerm/data_source_public_ips_test.go index ed5d6f4faf51..bd4111353b95 100644 --- a/azurerm/data_source_public_ips_test.go +++ b/azurerm/data_source_public_ips_test.go @@ -12,8 +12,10 @@ func TestAccDataSourceAzureRMPublicIPs_namePrefix(t *testing.T) { dataSourceName := "data.azurerm_public_ips.test" ri := acctest.RandInt() rs := acctest.RandString(5) + location := testLocation() - config := testAccDataSourceAzureRMPublicIPs_prefix(ri, rs, testLocation()) + resourceConfig := testAccDataSourceAzureRMPublicIPs_prefix(ri, rs, location) + dataSourceConfig := testAccDataSourceAzureRMPublicIPs_prefixDataSource(ri, rs, location) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -21,7 +23,11 @@ func TestAccDataSourceAzureRMPublicIPs_namePrefix(t *testing.T) { CheckDestroy: testCheckAzureRMPublicIpDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: resourceConfig, + Check: resource.ComposeTestCheckFunc(), + }, + { + Config: dataSourceConfig, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(dataSourceName, "public_ips.#", "2"), resource.TestCheckResourceAttr(dataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpipa%s-0", rs)), @@ -36,8 +42,10 @@ func TestAccDataSourceAzureRMPublicIPs_assigned(t *testing.T) { unattachedDataSourceName := "data.azurerm_public_ips.unattached" ri := acctest.RandInt() rs := acctest.RandString(5) + location := testLocation() - config := testAccDataSourceAzureRMPublicIPs_attached(ri, rs, testLocation()) + resourceConfig := testAccDataSourceAzureRMPublicIPs_attached(ri, rs, location) + dataSourceConfig := testAccDataSourceAzureRMPublicIPs_attachedDataSource(ri, rs, location) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -45,7 +53,11 @@ func TestAccDataSourceAzureRMPublicIPs_assigned(t *testing.T) { CheckDestroy: testCheckAzureRMPublicIpDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: resourceConfig, + Check: resource.ComposeTestCheckFunc(), + }, + { + Config: dataSourceConfig, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(attachedDataSourceName, "public_ips.#", "3"), resource.TestCheckResourceAttr(attachedDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpip%s-0", rs)), @@ -62,8 +74,10 @@ func TestAccDataSourceAzureRMPublicIPs_allocationType(t *testing.T) { dynamicDataSourceName := "data.azurerm_public_ips.dynamic" ri := acctest.RandInt() rs := acctest.RandString(5) + location := testLocation() - config := testAccDataSourceAzureRMPublicIPs_allocationType(ri, rs, testLocation()) + resourceConfig := testAccDataSourceAzureRMPublicIPs_allocationType(ri, rs, location) + dataSourceConfig := testAccDataSourceAzureRMPublicIPs_allocationTypeDataSources(ri, rs, location) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -71,7 +85,11 @@ func TestAccDataSourceAzureRMPublicIPs_allocationType(t *testing.T) { CheckDestroy: testCheckAzureRMPublicIpDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: resourceConfig, + Check: resource.ComposeTestCheckFunc(), + }, + { + Config: dataSourceConfig, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(staticDataSourceName, "public_ips.#", "3"), resource.TestCheckResourceAttr(staticDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpips%s-0", rs)), @@ -114,19 +132,24 @@ resource "azurerm_lb" "test" { public_ip_address_id = "${element(azurerm_public_ip.test.*.id, count.index)}" } } +`, rInt, location, rString) +} + +func testAccDataSourceAzureRMPublicIPs_attachedDataSource(rInt int, rString string, location string) string { + resources := testAccDataSourceAzureRMPublicIPs_attached(rInt, rString, location) + return fmt.Sprintf(` +%s data "azurerm_public_ips" "unattached" { resource_group_name = "${azurerm_resource_group.test.name}" attached = false - depends_on = ["azurerm_lb.test"] } data "azurerm_public_ips" "attached" { resource_group_name = "${azurerm_resource_group.test.name}" attached = true - depends_on = ["azurerm_lb.test"] } -`, rInt, location, rString) +`, resources) } func testAccDataSourceAzureRMPublicIPs_prefix(rInt int, rString string, location string) string { @@ -161,13 +184,19 @@ resource "azurerm_public_ip" "test2" { environment = "test" } } +`, rInt, location, rString, rString) +} + +func testAccDataSourceAzureRMPublicIPs_prefixDataSource(rInt int, rString string, location string) string { + prefixed := testAccDataSourceAzureRMPublicIPs_prefix(rInt, rString, location) + return fmt.Sprintf(` +%s data "azurerm_public_ips" "test" { resource_group_name = "${azurerm_resource_group.test.name}" name_prefix = "acctestpipa" - depends_on = ["azurerm_public_ip.test", "azurerm_public_ip.test2"] } -`, rInt, location, rString, rString) +`, prefixed) } func testAccDataSourceAzureRMPublicIPs_allocationType(rInt int, rString string, location string) string { @@ -202,17 +231,22 @@ resource "azurerm_public_ip" "static" { environment = "test" } } +`, rInt, location, rString, rString) +} + +func testAccDataSourceAzureRMPublicIPs_allocationTypeDataSources(rInt int, rString string, location string) string { + allocationType := testAccDataSourceAzureRMPublicIPs_allocationType(rInt, rString, location) + return fmt.Sprintf(` +%s data "azurerm_public_ips" "dynamic" { resource_group_name = "${azurerm_resource_group.test.name}" allocation_type = "Dynamic" - depends_on = ["azurerm_public_ip.dynamic"] } data "azurerm_public_ips" "static" { resource_group_name = "${azurerm_resource_group.test.name}" allocation_type = "Static" - depends_on = ["azurerm_public_ip.static"] } -`, rInt, location, rString, rString) +`, allocationType) } From c180c9e5409191058d0cfa837922c56a2e420e2e Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 15:47:45 -0700 Subject: [PATCH 13/15] Documenting the new fields --- website/docs/d/public_ips.html.markdown | 33 ++++++++++--------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/website/docs/d/public_ips.html.markdown b/website/docs/d/public_ips.html.markdown index de2b6b34ac71..e4f91108029d 100644 --- a/website/docs/d/public_ips.html.markdown +++ b/website/docs/d/public_ips.html.markdown @@ -6,10 +6,9 @@ description: |- Provides a list of public IP addresses. --- -# azurerm\_public\_ips +# azurerm_public_ips -Use this data source to get a list of associated or unassociated public IP addresses -in a resource group, optionally specifying a minimum required number. +Use this data source to access a filtered list of Public IP Addresses ## Example Usage @@ -18,28 +17,22 @@ data "azurerm_public_ips" "test" { resource_group_name = "pip-test" attached = false } - -resource "azurerm_lb" "load_balancer" { - count = 2 - name = "load_balancer-${count.index}" - location = "northeurope" - resource_group_name = "acctestRG" - - frontend_ip_configuration { - name = "frontend" - public_ip_address_id = "${lookup(data.azurerm_public_ips.test.public_ips[count.index], "id")}" - } -} ``` ## Argument Reference * `resource_group_name` - (Required) Specifies the name of the resource group. -* `attached` - (Required) Whether to return public IPs that are attached or not. - +* `attached` - (Optional) Should we only return IP Addresses which are attached to a device (VM/LB) etc? +* `name_prefix` - (Optional) A prefix match used for the IP Addresses `name` field, case sensitive. +* `allocation_type` - (Optional) The Allocation Type for the Public IP Address. Possible values include `Static` or `Dynamic`. ## Attributes Reference -* `public_ips` - A list of public IP addresses. Each public IP is represented by a -map containing the following keys; public_ip_address_id, name, fqdn, domain_name_label, -ip_address. Note that if the public IP is unassigned then some values may be empty. \ No newline at end of file +* `public_ips` - A List of `public_ips` blocks as defined below filtered by the criteria above. + +A `public_ips` block contains: + +* `id` - The ID of the Public IP Address +* `domain_name_label` - The Domain Name Label of the Public IP Address +* `fqdn` - The FQDN of the Public IP Address +* `name` - The Name of the Public IP Address From c238706f8d0d53ab65de6f128b9d1746ea43a75e Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 15:48:42 -0700 Subject: [PATCH 14/15] Adding a sidebar link --- website/azurerm.erb | 6 +++++- website/docs/d/public_ip.html.markdown | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/website/azurerm.erb b/website/azurerm.erb index d79d4923e665..0c478d300db1 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -75,10 +75,14 @@ azurerm_platform_image - > + > azurerm_public_ip + > + azurerm_public_ips + + > azurerm_resource_group diff --git a/website/docs/d/public_ip.html.markdown b/website/docs/d/public_ip.html.markdown index 89d228d72c54..14df94a888d2 100644 --- a/website/docs/d/public_ip.html.markdown +++ b/website/docs/d/public_ip.html.markdown @@ -1,7 +1,7 @@ --- layout: "azurerm" page_title: "Azure Resource Manager: azurerm_public_ip" -sidebar_current: "docs-azurerm-datasource-public-ip" +sidebar_current: "docs-azurerm-datasource-public-ip-x" description: |- Retrieves information about the specified public IP address. From 6a91d1967d92f41ea5e7b069f5afc71518c9ce05 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 6 Mar 2018 16:08:01 -0700 Subject: [PATCH 15/15] Review feedback --- website/docs/d/public_ips.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/public_ips.html.markdown b/website/docs/d/public_ips.html.markdown index e4f91108029d..09051f11cc99 100644 --- a/website/docs/d/public_ips.html.markdown +++ b/website/docs/d/public_ips.html.markdown @@ -22,7 +22,7 @@ data "azurerm_public_ips" "test" { ## Argument Reference * `resource_group_name` - (Required) Specifies the name of the resource group. -* `attached` - (Optional) Should we only return IP Addresses which are attached to a device (VM/LB) etc? +* `attached` - (Optional) Filter to include IP Addresses which are attached to a device, such as a VM/LB (`true`) or unattached (`false`). * `name_prefix` - (Optional) A prefix match used for the IP Addresses `name` field, case sensitive. * `allocation_type` - (Optional) The Allocation Type for the Public IP Address. Possible values include `Static` or `Dynamic`.