From e4bd6857f022e4339404e5d7a89b252cbb6c632d Mon Sep 17 00:00:00 2001 From: midacts Date: Mon, 16 Sep 2019 21:19:05 -0400 Subject: [PATCH 1/3] Adding data source 'azurerm_public_ip_prefix' --- azurerm/data_source_public_ip_prefix.go | 65 ++++++++++++++++++ azurerm/data_source_public_ip_prefix_test.go | 66 +++++++++++++++++++ azurerm/provider.go | 1 + website/docs/d/public_ip_prefix.html.markdown | 39 +++++++++++ 4 files changed, 171 insertions(+) create mode 100644 azurerm/data_source_public_ip_prefix.go create mode 100644 azurerm/data_source_public_ip_prefix_test.go create mode 100644 website/docs/d/public_ip_prefix.html.markdown diff --git a/azurerm/data_source_public_ip_prefix.go b/azurerm/data_source_public_ip_prefix.go new file mode 100644 index 000000000000..4111d09eef3d --- /dev/null +++ b/azurerm/data_source_public_ip_prefix.go @@ -0,0 +1,65 @@ +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.SchemaSingleZone(), + + "tags": tags.Schema(), + }, + } +} + +func dataSourceArmPublicIpPrefixRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).network.PublicIPPrefixesClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + resGroup := d.Get("resource_group_name").(string) + resp, err := client.Get(ctx, resGroup, name, "") + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Public IP prefix %q was not found", name) + } + return err + } + + d.SetId(*resp.ID) + + return resourceArmPublicIpPrefixRead(d, meta) +} diff --git a/azurerm/data_source_public_ip_prefix_test.go b/azurerm/data_source_public_ip_prefix_test.go new file mode 100644 index 000000000000..d4413e03dd4e --- /dev/null +++ b/azurerm/data_source_public_ip_prefix_test.go @@ -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) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 7013fc867319..bde3242474ae 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -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(), diff --git a/website/docs/d/public_ip_prefix.html.markdown b/website/docs/d/public_ip_prefix.html.markdown new file mode 100644 index 000000000000..c8ef62006d58 --- /dev/null +++ b/website/docs/d/public_ip_prefix.html.markdown @@ -0,0 +1,39 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_public_ip_prefix" +sidebar_current: "docs-azurerm-datasource-public-ip-prefix-x" +description: |- + Gets information about an existing Public IP Prefix. + +--- + +# Data Source: azurerm_public_ip_prefix + +Use this data source to access information about an existing Public IP Prefix. + +## Example Usage (reference an existing) + +```hcl +data "azurerm_public_ip_prefix" "test" { + name = "name_of_public_ip" + resource_group_name = "name_of_resource_group" +} + +output "public_ip_prefix" { + value = "${data.azurerm_public_ip_prefix.test.ip_prefix}" +} +``` + +## Argument Reference + +* `name` - (Required) Specifies the name of the public IP prefix. +* `resource_group_name` - (Required) Specifies the name of the resource group. + +## Attributes Reference + +* `name` - The name of the Public IP prefix resource. +* `resource_group_name` - The name of the resource group in which to create the public IP. +* `location` - The supported Azure location where the resource exists. +* `sku` - The SKU of the Public IP Prefix. +* `prefix_length` - The number of bits of the prefix. +* `tags` - A mapping of tags to assigned to the resource. From 5f8606401a0f2c3b733defbb6271aedf64eb3c50 Mon Sep 17 00:00:00 2001 From: midacts Date: Fri, 20 Sep 2019 01:51:03 +0000 Subject: [PATCH 2/3] Updating PR to resolve recommendations --- azurerm/data_source_public_ip_prefix.go | 26 +++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/azurerm/data_source_public_ip_prefix.go b/azurerm/data_source_public_ip_prefix.go index 4111d09eef3d..09b1dade93aa 100644 --- a/azurerm/data_source_public_ip_prefix.go +++ b/azurerm/data_source_public_ip_prefix.go @@ -38,9 +38,9 @@ func dataSourceArmPublicIpPrefix() *schema.Resource { Computed: true, }, - "zones": azure.SchemaSingleZone(), + "zones": azure.SchemaZonesComputed(), - "tags": tags.Schema(), + "tags": tags.SchemaDataSource(), }, } } @@ -50,16 +50,26 @@ func dataSourceArmPublicIpPrefixRead(d *schema.ResourceData, meta interface{}) e ctx := meta.(*ArmClient).StopContext name := d.Get("name").(string) - resGroup := d.Get("resource_group_name").(string) - resp, err := client.Get(ctx, resGroup, name, "") + 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", name) + return fmt.Errorf("Error: Public IP prefix %q was not found in Resource Group %q", name, resourceGroup) } - return err + return fmt.Errorf("Error retrieving Public IP Prefix %q (Resource Group %q): %+v", name, resourceGroup, err) } d.SetId(*resp.ID) - - return resourceArmPublicIpPrefixRead(d, meta) + 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) } From 2563889ac60b12e644d71d90a701964f3f7c5bc1 Mon Sep 17 00:00:00 2001 From: midacts Date: Sat, 21 Sep 2019 14:53:37 +0000 Subject: [PATCH 3/3] Adding the public_ip_prefix to the website sidebar --- website/azurerm.erb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/azurerm.erb b/website/azurerm.erb index 36a93fc9bbf5..63734054baeb 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -274,6 +274,10 @@ azurerm_policy_definition +
  • + azurerm_public_ip_prefix +
  • +
  • azurerm_public_ip